DEV Community

Cover image for Effortlessly Calculate 100+ Technical Indicators in Python – A Quick Guide
Shridhar G Vatharkar
Shridhar G Vatharkar

Posted on

Effortlessly Calculate 100+ Technical Indicators in Python – A Quick Guide

Technical indicators are essential for detailed market analysis and data-driven decision-making. This tutorial will walk you through retrieving historical forex data using the TraderMade API and computing key technical indicators with the Python TA-Lib library. By the end, you will have the necessary tools to integrate these indicators into your applications, platforms, and trading systems.

Our API provides real-time data on a wide range of tradable assets, including over 32 US stocks, 10 significant indices, precious metals, and access to more than 100 currencies (explore currencies & CFDs).

Before proceeding, it is essential to install the TA-Lib library. Since pip often fails to build this library on specific systems, mainly Windows, we will demonstrate an alternative method using a wheel file.

Linux users should refer to the official TA-Lib library. Windows users should visit the TA-Lib Windows wheel page, download the appropriate wheel file based on their Python version and system architecture (32-bit or 64-bit), and ensure that pip is executed from the exact location as the downloaded file.

In the example below, we have downloaded a wheel file compatible with 64-bit computers running Python 3.10. Thus, the following download method is recommended:

pip install requests pandas 
pip install TA_Lib-0.4.32-cp310-cp310-win_amd64.whl

Enter fullscreen mode Exit fullscreen mode

Image description

After installing the library, register with TraderMade to get your free Forex API key. Next, install TraderMade for seamless integration.

pip install tradermade
Enter fullscreen mode Exit fullscreen mode

Detailed Guide

Step 1: Import Required Libraries

Start by importing the essential data retrieval, processing, and analysis libraries.

import talib
import tradermade as tm
Enter fullscreen mode Exit fullscreen mode

Step 2: Configure Your API Key

Insert your TraderMade API key to enable authentication.

# Replace Your TraderMade API key
tm.set_rest_api_key("api_key")
Enter fullscreen mode Exit fullscreen mode

Step 3: Define a Function to Retrieve Market Data

Develop a function to fetch historical forex data for a selected currency pair over a specified date range.

tm.timeseries(currency='EURUSD', start="2024-08-05",end="2024-11-04",interval="daily",fields=["open", "high", "low","close"]) 
Enter fullscreen mode Exit fullscreen mode

Step 4: Compute Essential Technical Indicators

TA-Lib offers numerous indicators, including candlestick pattern recognition. Below are some key technical indicators widely used by traders:

  • Simple Moving Average (SMA)
    Smooths price data over a defined period (e.g., 20 days) to identify trends.

  • Relative Strength Index (RSI)
    A momentum oscillator that measures the speed and change of price movements, helping to detect overbought or oversold conditions.

  • Moving Average Convergence Divergence (MACD)
    Analyzes the relationship between two moving averages of a financial instrument, aiding in spotting potential market momentum shifts and generating buy/sell signals.

# Calculate technical indicators using TA-Lib
df['SMA_20'] = talib.SMA(df['close'], timeperiod=20) # 20-period Simple Moving Average

df['RSI_14'] = talib.RSI(df['close'], timeperiod=14) # 14-period Relative Strength Index
df['MACD'], df['MACD_signal'], df['MACD_hist'] = talib.MACD(df['close'],
                                                                  fastperiod=12,
                                                                  slowperiod=26,
                                                                  signalperiod=9)
Enter fullscreen mode Exit fullscreen mode

Step 5: View the Results

Finally, the last few rows of the DataFrame will be displayed to examine the OHLC data alongside the computed indicators.

df = df.set_index("date")
df = df.dropna()
print(df.head())
Enter fullscreen mode Exit fullscreen mode
              open     high      low    close    SMA_20     RSI_14      MACD  MACD_signal  MACD_hist

date
2024-09-19  1.11172  1.11790  1.10686  1.11621  1.109625  62.318384  0.003357     0.003439  -0.000083
2024-09-20  1.11626  1.11820  1.11362  1.11631  1.109472  62.410940  0.003576     0.003467   0.000109
2024-09-23  1.11646  1.11674  1.10834  1.11114  1.109223  54.902623  0.003295     0.003432  -0.000138
2024-09-24  1.11114  1.11811  1.11036  1.11797  1.109199  61.493338  0.003582     0.003462   0.000119
2024-09-25  1.11799  1.12141  1.11217  1.11329  1.109263  55.507279  0.003392     0.003448  -0.000056
Enter fullscreen mode Exit fullscreen mode

Complete Code

Below is the full implementation—review it carefully.

# Display the data with indicators

import talib
import tradermade as tm

tm.set_rest_api_key("api_key")

df = tm.timeseries(currency='EURUSD', start="2024-08-05",end="2024-11-04",interval="daily",fields=["open", "high", "low","close"])

df['SMA_20'] = talib.SMA(df['close'], timeperiod=20) # 20-period Simple Moving Average

df['RSI_14'] = talib.RSI(df['close'], timeperiod=14) # 14-period Relative Strength Index
df['MACD'], df['MACD_signal'], df['MACD_hist'] = talib.MACD(df['close'],
                                                                  fastperiod=12,
                                                                  slowperiod=26,
                                                                  signalperiod=9)

df = df.set_index("date")
df = df.dropna()
print(df.head())
Enter fullscreen mode Exit fullscreen mode

The Bottom Line

This tutorial demonstrated how to retrieve historical forex data using the TraderMade SDK and compute key technical indicators such as SMA, RSI, and MACD with the TA-Lib library. However, this is just the beginning—TA-Lib offers over 100 indicators, including candlestick patterns, applicable to various markets.

If you found this guide helpful, share our tutorials or reference them in your blogs. Don't hesitate to reach out for any questions or assistance with TA-Lib installation. Happy coding!

Also, please review the tutorial initially published on our website: 100+ Technical Indicators in Python.
Here is a tutorial video: Technical Indicators in Python Video Tutorial.

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay