DEV Community

Currency Pig
Currency Pig

Posted on

Introduction to Technical Analysis with Python

Technical analysis is a popular method used by traders and investors to make predictions about future price movements in financial markets. By analysing historical price and volume data, individuals can gain valuable insights into potential market trends. In this article, we'll walk through a simple example of performing technical analysis in Python, using the Pandas library for data manipulation and the TA-Lib library for calculating technical indicators.

Coding

Setting Up the Environment

Before diving into the code, it's essential to set up your Python environment. You'll need to install the Pandas and TA-Lib libraries using pip:

pip install pandas
pip install TA-Lib
Enter fullscreen mode Exit fullscreen mode

Fake Data

Create a sample dataset representing daily closing prices over a 5-day period.

data = {
    'Date': ['2023-01-01', '2023-01-02', '2023-01-03', '2023-01-04', '2023-01-05'],
    'Close': [100, 102, 105, 108, 107]
}
Enter fullscreen mode Exit fullscreen mode

Dataframe

The data is organized in a Pandas DataFrame, and we convert the 'Date' column to a datetime format for better manipulation.

# Create a DataFrame from the sample data
df = pd.DataFrame(data)
df['Date'] = pd.to_datetime(df['Date'])
Enter fullscreen mode Exit fullscreen mode

Calculate SMA

We calculate a 3-day Simple Moving Average (SMA) using Pandas' rolling function.

period = 3 # In days
df['SMA'] = df['Close'].rolling(window=period).mean()
Enter fullscreen mode Exit fullscreen mode

Calculate other technical indicators: such as RSI and MACD

We calculate two additional technical indicators: the Relative Strength Index (RSI) and the Moving Average Convergence Divergence (MACD) using TA-Lib functions.

df['RSI'] = talib.RSI(df['Close'], timeperiod=14)
df['MACD'], df['Signal'] = talib.MACD(df['Close'], fastperiod=12, slowperiod=26, signalperiod=9)
Enter fullscreen mode Exit fullscreen mode

Display the data

The resulting DataFrame displays the original data along with the calculated technical indicators.

print(df)
Enter fullscreen mode Exit fullscreen mode

Code all together

import pandas as pd
import talib

data = {
    'Date': ['2023-01-01', '2023-01-02', '2023-01-03', '2023-01-04', '2023-01-05'],
    'Close': [100, 102, 105, 108, 107]
}

df = pd.DataFrame(data)
df['Date'] = pd.to_datetime(df['Date'])

period = 3
df['SMA'] = df['Close'].rolling(window=period).mean()

df['RSI'] = talib.RSI(df['Close'], timeperiod=14)
df['MACD'], df['Signal'] = talib.MACD(df['Close'], fastperiod=12, slowperiod=26, signalperiod=9)

print(df)
Enter fullscreen mode Exit fullscreen mode

Further Analysis and Visualization

This is just a simple example of technical analysis. Traders and analysts can use more sophisticated indicators, optimize parameters, and visualize the results using libraries like Matplotlib. Technical analysis can be a powerful tool when combined with other forms of analysis and research to make informed trading decisions.

Conclusion:

Python, with its rich ecosystem of libraries, makes it accessible for anyone to perform technical analysis and gain insights into market trends. By leveraging the power of data analysis, you can enhance your ability to make well-informed investment decisions in the world of finance.

Top comments (3)

Collapse
 
anitaolsen profile image
Anita Olsen

This is awesome! Thank you so much for sharing this. I am educated within business and economics so this will come in handy! I am currely learning Python as well.

Collapse
 
currencypig profile image
Currency Pig

You're welcome, I'm glad to hear that this was helpful.
If you have any questions or need further assistance, feel free to ask.
Best of luck with your learning process.

Collapse
 
anitaolsen profile image
Anita Olsen

Thank you so much! Highly appreciated!