mplfinance icon indicating copy to clipboard operation
mplfinance copied to clipboard

add scatter points to renko plot

Open Baappii opened this issue 2 years ago • 4 comments

@DanielGoldfarb ... thank you for your wonderful work.

Hello can someone help me to add scatter points to my renko chart. All i am trying to do is add scatter chart to existing chart(ie marker) "o" where volume is > 500 in my data.

sample Data structure - csv file:

time,Open,High,Low,Close,Volume,date
2020-06-16 09:33,2439.0,2439.0,2439.0,2439.0,340.0,2020-06-16 09:33:03.872999936-04:00
2020-06-16 10:25,2450.5,2450.5,2450.5,2450.5,521.0,2020-06-16 10:25:01.012000-04:00
2020-06-16 10:30,2440.0,2440.0,1440.0,2440.0,415.0,2020-06-16 10:30:13.260999936-04:00
2020-06-16 10:30,2429.5,2429.5,2429.5,2429.5,770.0,2020-06-16 10:30:20.295000064-04:00

Basic code:

df = []
df = pd.read_csv('test.csv',index_col=0,parse_dates=True)
df = df.iloc[:20000]
bucket_size = 0.00012 * max(df['Close'])
volprofile  = df['Volume'].groupby(df['Close'].apply(lambda x: bucket_size*round(x/bucket_size,0))).sum()
mc = mpf.make_marketcolors(base_mpf_style='yahoo')
s  = mpf.make_mpf_style(base_mpf_style='nightclouds',marketcolors=mc)
fig, axlist = mpf.plot(df,type='renko',renko_params=dict(brick_size=3),returnfig=True,style=s,tight_layout=True)
vpax = fig.add_axes(axlist[0].get_position())
vpax.set_axis_off()
vpax.set_xlim(right=1.2*max(volprofile.values))
mpf.show()

current sample image: image

Baappii avatar Sep 23 '21 00:09 Baappii

Adding data to a renko plot is very tricky. This is because renko calculations result in both (1) a non-linear time axis, and (2) fewer points on the time axis than there are rows in the dataframe.

One possible workaround may be to use the return_calculated_values kwarg. You must first create a variable that contains an empty dictionary, and then pass this variable in with the return_calculated_values kwarg:

cvals = {}
fig, axlist = mpf.plot(data,type=renko,...,return_calculated_values=cvals,returnfig=True)

As shown above, do this also with the returnfig=True kwarg.

You can then examine the values returned in the cvals dictionary to see how many renko bricks were ploted along the x-axis, and to see what the datetime values are for those bricks. Then, using the same datetime values, you can create a scatter plot on one of the axes objects in the returned axlist.

DanielGoldfarb avatar Sep 23 '21 19:09 DanielGoldfarb

@DanielGoldfarb thank you for you kind input. I was just running into wall, Your explanation makes easy for me to understand whole concept. My only question would be is there a sample code as ref on how to plot scatter plot using "axlist". can this axlist(scatter points) displayed in same panel as renko chart. As i was not able to addplot to renko previously. appreciate your time.

Baappii avatar Sep 23 '21 23:09 Baappii

@Baappii This page here (returnfig=True) explains how you can determine which axes object in the axlist that is returned by plot (in terms of which list item corresponds to the axes objects for each panel). So by choosing the appropriate axlist[item] you can put the scatter plot on whichever panel you want.

Then it is just a matter of calling matplotlib's Axes.scatter() (for example, as axlist[item].scatter() ).

The x-values passed into scatter() are tricky: for renko, the x-values will need to be the row number of the corresponding datetimes returned by return_calculated_values. (You can see how the renko code does this here). I am 99% sure that is the correct way to do it, but let me know if there is a problem.

Keep in mind, since you are using returnfig=True then you may need to call mpf.show() when you are done manipulating things and you are ready to display your plot. All the best. -Daniel

DanielGoldfarb avatar Sep 24 '21 14:09 DanielGoldfarb

@Baappii

Have you had any luck with this?

--Daniel

DanielGoldfarb avatar Dec 15 '21 23:12 DanielGoldfarb