Issue while trying to get_ydata of an EMA from a fig or from the axes
Branden720 opened this issue · 3 comments
Hello,i was trying to get_ydata of an EMA from the plotted fig, my line of code was as below
Ema_plot_ydata= axlist[0].get_lines[0].get_ydata()
The compilation response was,.. Valueerror= x and y values must be the same size…please assist,the documentation wasn’t sufficent
@Branden720 talk about "wasn't sufficient" ... it's virtually impossible to debug code from a single line of code. Please show the entire code, and provide a data example, so that others can reproduce the problem.
@DanielGoldfarb CODE IS AS BELOW,what i was trying to accomplish with the code here is i want to calculate when the current EMA is above the previous EMA for a buy signal and vice versa using the ydata from the already plotted figure instead of using ema value. So i keep getting errors as i mentioned above
period = 1260
exp= pdf1 ['open' ].ewm(span=period, adjust=False) .mean ()
apds = [mpf.make_addplot (exp, color='blue')]
s1 = mpf.make_mpf_style (base_mpf_style='yahoo', y_on_right=False)
fig, axlist = mpf.plot(pdf1, type='candle', addplot=apds, style=s1, returnfig=True)
ema_plot_ydata = apds[0].plot[0].lines[0].get_ydata (). <<<KEY
buy_signals = []
sell_signals = []
for i in range (1, len (ema_plot_ydata)):
if ema_plot_ydata[i] > ema_plot_ydatali - 1]:
buy_signals.append(i) # Buy signal elif ema_plot_ydata[i] < ema_plot_ydata[i - 1]:
sell_signals.append(i) # Sell signal
add_buy_signals = mpf.make_addplot(buy_signals, type='scatter', markersize=50,
marker=‘^’, color='green')
add_sell_signals = mpf.make_addplot (sell_signals, type='scatter', markersize=50,
marker='v', color='red')
fig, axlist = mpf.plot (pdf1, type=‘candle’,
style=s1, addplot=[apds,
add_buy_signals, add_sell_signals], returnfig=True)
mpf.show ()
@Branden720
First of all, your code won't run as written. Perhaps you have some typos in it.
Second, and more importantly, what you are trying to do makes no sense. You already have you exponential moving average here:
exp = pdf1['open'].ewm(span=period,adjust=False).mean()
Why plot it and then try to extract it from the plot?
That's just wasteful. Instead, use the data that you have already:
for i in range (1, len(exp)):
if exp[i] > exp[i - 1]:
...
I won't go into everything else that's wrong with the code as you have written it above. It clearly won't work. That said, if you wanted to correctly extract the data from the plot, the appropriate line of code would be:
data = axlist[0].get_lines()[0].get_ydata()
If you were to then compare data
to exp
you would find they are identical!