Assist to draw Simple Moving Average.
SergejsBure opened this issue · comments
I am a newbie in mplfinance.
When I use make_addplot for adding Simple Moving Average (SMA) period 12 something strange is hapening. There is two y-axis on the chart with different numbers.
I plan to add additional indicators, so I have started with SMA.
There is a MA 200 on the chart, which shows normally.
Code:
from datetime import datetime
import mplfinance as mpf
import yfinance as yf
from finta import TA
yticker = yf.Ticker("BTC-USD")
title = yticker.info["longName"] + ", " + str(round(yticker.fast_info["lastPrice"], 4)) + " " + yticker.fast_info["currency"]
ylabel = "Price, " + yticker.fast_info["currency"]
ydata = yticker.history(start='2016-01-01', end=datetime.now().strftime('%Y-%m-%d'))
ydata["SMA"] = TA.SMA(ydata, 12)
ydata.fillna(0, inplace=True)
plots = [
mpf.make_addplot(ydata["SMA"], width=0.5, panel=0)
]
mpf.plot(ydata.iloc[:, :7],
type="candle",
title=title,
volume=True,
ylabel=ylabel,
mav=200,
addplot=plots,
warn_too_much_data=10000,
panel_ratios=(5, 1),
ylim=(ydata['Close'].min() * .8, ydata['Close'].max() * 1.05),
tight_layout=False
)
Please, help to solve.
@SergejsBure
Sergey, Thank you for your interest in using mplfinance.
To answer your questions: First, regarding moving averages, I recommend that you not calculate them yourself, but rather use mpf.plot()
kwargs mav=
and ema=
for simple and exponential moving averages respectively.
Each of these can be set to either a single number, or a tuple or list.
For example mav=50
for a single 50 period moving average, or mav=(30,60,90)
for three simple moving averages of 30, 60 and 90 periods respectively.
Secondly, since you are planning to use make_addplot()
for additional indicators, the I strongly recommend that you carefully read the addplot tutorial and pay particular attention to the section titled "Plotting additional data with a secondary y-axis" which is about half way down the page.
Let me know if these suggestions help.
Thank you, @DanielGoldfarb !
The problem was solved, by adding secondary_y
.
mpf.make_addplot(ydata["SMA"], width=0.5, panel=0, secondary_y=False)
I will use indicators, calculated by my own, whats why kwargs mav=
and ema=
don't suit me.
Sincerely yours,
Sergey