Plotly combined with pandas_ta is a great tool for visualizing technical indicators and Plotly python library comes with better customization in creating various chart visualization types. Plotly brings a powerful library for creating interactive charts and visually appealing plots.
Whereas, pandas_ta brings 130+ classical technical indicators like supertrend, moving averages, macd, rsi, atr, and various oscillators. It provides an effortless way to compute and calculate technical indicators.
Installing the Library
pip install yfinance
pip install pandas_ta
pip install plotly
Downloading the Stock data from Yahoo Finance and Compute Technical Indicators like RSI, ATR and Supertrend (3,7)
import yfinance as yf
import plotly.subplots as sp
import plotly.graph_objects as go
import pandas_ta as ta
import numpy as np
# Fetch stock data
symbol = 'TATAMOTORS.NS'
start_date = '2022-01-01'
end_date = '2023-01-01'
stock_data = yf.download(symbol, start=start_date, end=end_date)
# Calculate technical indicators
stock_data.ta.atr(length=14, append=True)
stock_data.ta.rsi(length=14, append=True)
stock_data.ta.supertrend(append=True, atr_length=7, multiplier=3)
stock_data
Output
[*********************100%***********************] 1 of 1 completed
Open High Low Close Adj Close Volume ATRr_14 RSI_14 SUPERT_7_3.0 SUPERTd_7_3.0 SUPERTl_7_3.0 SUPERTs_7_3.0
Date
2022-06-01 445.100006 446.000000 439.600006 444.600006 443.218628 11930669 NaN NaN 0.000000 1 NaN NaN
2022-06-02 445.000000 445.600006 435.649994 439.149994 437.785553 12742438 NaN NaN NaN 1 NaN NaN
2022-06-03 444.899994 445.049988 430.500000 431.899994 430.558075 12780122 NaN NaN NaN 1 NaN NaN
2022-06-06 427.899994 433.950012 424.600006 432.350006 431.006683 11896105 NaN NaN NaN 1 NaN NaN
2022-06-07 432.500000 438.200012 429.100006 435.850006 434.495819 11153869 NaN NaN NaN 1 NaN NaN
... ... ... ... ... ... ... ... ... ... ... ... ...
2023-12-04 716.000000 717.000000 704.799988 705.599976 705.599976 9348036 13.684554 69.059318 669.040471 1 669.040471 NaN
2023-12-05 705.900024 714.900024 705.000000 708.950012 708.950012 9601509 13.414231 70.206895 669.827554 1 669.827554 NaN
2023-12-06 709.049988 724.799988 709.000000 722.450012 722.450012 12148825 13.588212 74.337551 675.716468 1 675.716468 NaN
2023-12-07 725.000000 727.500000 716.000000 721.950012 721.950012 10451801 13.439054 73.928726 681.521264 1 681.521264 NaN
2023-12-08 722.049988 727.599976 707.500000 714.549988 714.549988 8554990 13.914834 67.970747 681.521264 1 681.521264 NaN
378 rows × 12 columns
Plotting the Technical Indicators
# Create subplots in a separate plane with different row heights
fig = sp.make_subplots(rows=3, cols=1, shared_xaxes=True,
subplot_titles=[f'{symbol} Stock Prices', 'Technical Indicators'],
vertical_spacing=0.1,
row_heights=[0.7, 0.15, 0.15])
# Add Stock Prices subplot
fig.add_trace(go.Candlestick(x=stock_data.index,
open=stock_data['Open'],
high=stock_data['High'],
low=stock_data['Low'],
close=stock_data['Close'],
name='Stock Prices'), row=1, col=1)
# Add Supertrend subplot with dynamic colors and smaller marker dots
fig.add_trace(go.Scatter(x=stock_data.index, y=stock_data['SUPERT_7_3.0'],
mode='lines+markers',
marker=dict(color=np.where(stock_data['Close'] > stock_data['SUPERT_7_3.0'], 'green', 'red'),
size=3), # Set the size of the marker dots
line=dict(color='rgba(0,0,0,0)'), # This line is added to hide the line connecting markers
name='Supertrend'), row=1, col=1)
# Add ATR subplot
fig.add_trace(go.Scatter(x=stock_data.index, y=stock_data['ATRr_14'],
mode='lines',
line=dict(color='blue'), # Set the color to blue
name='ATR'), row=2, col=1)
# Add RSI subplot
fig.add_trace(go.Scatter(x=stock_data.index, y=stock_data['RSI_14'],
mode='lines',
name='RSI'), row=3, col=1)
# Update layout to set figure size
fig.update_layout(xaxis_rangeslider_visible=False,
template='plotly_dark',
title_text=f'{symbol} Stock Analysis',
xaxis=dict(type='category'), # Set x-axis type to category which removes the sat and sun gap
xaxis_title_text='Date',
yaxis_title_text='Price',
yaxis2_title_text='ATR', # Add y-axis title for ATR subplot
yaxis3_title_text='RSI', # Add y-axis title for RSI subplot
height=800, # Set the height of the figure
width=1200) # Set the width of the figure
# Show the figure
fig.show()
Here the sp.make_subplots – function is from the plotly.subplots module and it is used to create a subplot grid. The Subplots are divided into 3 rows – the first row is used to plot candlestick charts with supertrend indicators and second row to plot ATR levels and the third row to plot RSI levels. The row_heights=[0.7, 0.15, 0.15] implies that the first row consumes 70% of the canvas so that the candlestick plot will have a larger space to plot, and the 2nd and 3rd row utilizes 15% of each of the total canvas.
In Plotly, the add_trace function is used to add one or more traces to a plot. A trace represents a single set of data in a plot, such as a line, scatter plot, bar chart, etc. The add_trace
function is part of the Figure
class in Plotly, and it allows you to build up complex plots by adding multiple traces to a single figure. A total of 4 traces are added to plot the candlestick charts, supertrend, ATR, and RSI indicator.
The Supertrend markers are added in such a way as to plot dynamic colors according to buy/sell mode. The supertrend is plotted with green color if the close of the candle is above supertrend else a red color is plotted to the trace.
The update_layout method in Plotly to customize the layout and appearance of the plot. For a dark theme, the template parameter is set to plotly_dark. And to remove the Saturday and Sunday or any holiday gap xaxis=dict(type=’category’) is added. The height and weight of the canvas are set as 800 and 1200 respectively.
Finally fig.show() is used to display the final figure with all the computed technical indicators in one figure.