简体   繁体   中英

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?
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:

在此处输入图像描述

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM