Jocelyn AL
Jocelyn AL

Reputation: 67

Adding signals on the candle chart

I would like to plot signals on my chart is there is a way to do it on candle stick? I did the following and got stuck :(

!pip install yfinance
!pip install mplfinance
import yfinance as yf
import mplfinance as mpf
import numpy as np 
import pandas as pd 

df=yf.download('BTC-USD',start='2008-01-04',end='2021-06-3',interval='1d')

buy=np.where((df['Close'] > df['Open']) & (df['Close'].shift(1) < df['Open'].shift(1),1,0)

fig = plt.figure(figsize = (20,10))
mpf.plot(df,figsize=(20,12),type ='candle',volume=True);

# any idea how to add the signal?

Upvotes: 2

Views: 4019

Answers (2)

Daniel Goldfarb
Daniel Goldfarb

Reputation: 7714

You place signals on the plot using the "make additional plot" api: mpf.make_addplot(data,**kwargs). The data that you pass in to make_addplot must be the same length as your original candlestick dataframe (so that mplfinance can line it up appropriately with the candlesticks). If you do not want to plot a signal at every location you simply fill the data with nan values except where you do want to plot a signal.

The return value from ap = mpf.make_addplot() is then passed into mpf.plot(df,addplot=ap) using the addplot kwarg.

You can see many examples in this tutorial on adding your own technical studies to plots.

Take the time (maybe 10 minutes or so) to go carefully through the entire tutorial. It will be time well spent.

Upvotes: 0

Marcelo Wizenberg
Marcelo Wizenberg

Reputation: 121

import yfinance as yf
import mplfinance as mpf
import numpy as np 

df = yf.download('BTC-USD', start='2008-01-04', end='2021-06-3', interval='1d').tail(50)

buy = np.where((df['Close'] > df['Open']) & (df['Close'].shift(1) < df['Open'].shift(1)), 1, np.nan) * 0.95 * df['Low']

apd = [mpf.make_addplot(buy, scatter=True, markersize=100, marker=r'$\Uparrow$', color='green')]

mpf.plot(df, type='candle', volume=True, addplot=apd)

I just added .tail() for better visualization.

Output:

enter image description here

Upvotes: 8

Related Questions