Issue
I want to plot a candlestick graph with additional indicators where one of indicator can be all nan
. I'm using python matplotlib utilities called mplfinance for this. Mplfinance takes one parameter as main data for building candlesticks and second parameter is an array with additional indicators values. When I try to implement a custom indicator I first add an empty column filled with nan
to the array with indicators and then fill it in a loop with a condition. It may happen that the whole column can stay all nan
after the loop so I get an error and can't plot the graph.
df = pd.DataFrame.from_dict(pd.json_normalize(newBars), orient='columns')
idf = df.copy()
idf = idf.iloc[:,[0]]
idf.columns = ['col0']
idf.assign(col1=float('nan')) # Now we add column #1
for i in range(len(idf)-1):
if a > b: # Some condition I use to calculate Col1
idf.iat[i, 1] = float_value
indicators = [
mpf.make_addplot(idf['Col0'],color='grey',width=1,panel=0),
mpf.make_addplot(idf['Col1'],color='g',type='scatter',markersize=50,marker='^',panel=0),
]
mpf.plot(df, type='candle', style='yahoo', volume=True, addplot=indicators,
figscale=1.1,figratio=(8,5), panel_ratios=(2,1))
From the code there is a chance that Col1 can be all nan
and in this case I get the following error:
ValueError: zero-size array to reduction operation maximum which has no identity
How can I avoid this error and just plot the graph without nan
columns even if such column exists in the array?
Solution
Mplfinance is designed this way on purpose. if you pass all NaN data to mpf.make_addplot()
you are effect saying plot nothing. You can easily test if you have any data before adding the make_addplot()
to you list of addplot
indicators.
Yes, it may make your code simpler if you can just pass indicators without having to check if your model actually "indicated" anything, however (1) this will make the mplfinance code have to check, increasing (albeit very slightly) the cost of maintaining the mplfinance library, and (2) it could be that you passed all NaN values by mistake, in which case if mplfinance simply ignores the data you may spend a lot of time debugging to determine why your indicator is not showing up on the plot.
For further discussion, see: https://github.com/matplotlib/mplfinance/issues/259#issuecomment-688429294
Answered By - Daniel Goldfarb
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.