mplfinance icon indicating copy to clipboard operation
mplfinance copied to clipboard

mpf.plot - extending line joining two points

Open sukioral opened this issue 4 years ago • 12 comments

I am using the follow code to join the lower points of two bars together (the blue bold line):

mpf.plot(data,hlines=dict(hlines=hlines_carrier,colors=['g','r'],linestyle='-.'),type='candle',alines=two_points)

mplfinance_extend_line

However, I would like to the line to extend onto the right and touch the y-axis. What codes I should change in order to do that?

Thank you.

sukioral avatar Oct 20 '20 02:10 sukioral

Does anyone know how to extend the blue bold line? Please teach me. I have done quite some research in the documents but I could not figure out a way.

sukioral avatar Oct 20 '20 09:10 sukioral

Try tight_layout=True.

DanielGoldfarb avatar Oct 20 '20 10:10 DanielGoldfarb

Alternatively you can also use the xlim kwarg, but tight_layout is simpler.

DanielGoldfarb avatar Oct 20 '20 10:10 DanielGoldfarb

I tried to set tight_layout=True in the mpf.plot() but the blue bold line is still just connecting the 2 points. Did I wrongly do?

sukioral avatar Oct 21 '20 01:10 sukioral

I tried to set tight_layout=True in the mpf.plot() but the blue bold line is still just connecting the 2 points. Did I wrongly do?

I apologize. I misinterpreted your original question. I mistakenly thought you were looking to get rid of the space on the left and right between the data and the vertical (y) axis.

Would it be correct to say that you want the plot to look like this?

issue278

DanielGoldfarb avatar Oct 21 '20 02:10 DanielGoldfarb

Oh yes!!! This is exactly what I wanted to plot! May I know how?

sukioral avatar Oct 21 '20 03:10 sukioral

There is no feature of mplfinance that will automatically extrapolate alines. (I made the above picture using Windows Paint). However you can extrapolate the line yourself using your two_points data, and then use that extrapolated value to pass into the alines kwarg.

I assume that two_points looks something like this: [ (dateA,priceA),(dateB,priceB) ]

What you want to do is extrapolate that line to find the price at the far right, let's call it priceC which would be the price at date data.index[-1].

One way to do it would be to find the slope: (priceB-priceA)/(dateB-dateA), and then use that slope to calculate the price at date data.index[-1] The problem with this approach is that the x-axis is not enitrely linear with respect to dates (because you are not displaying non-trading days). However this approach may provide a good approximation as is. (And, in theory, it should be exact if you set show_nontrading=True).

If you want to continue to allow show_nontrading to remain its default, unspecified value of False, then you will have to calculate the slope using the index value of dateA and dateB. That is, the integer row number that corresponds to the location of dateA and dateB. You then use that slope to calculate the price at the final row (the final row has a value of len(data.index)-1).

If dateA and dateB both exactly match dates in data.index, this is relatively easy. Let's say ixA is the index location (row number) of dateA, and ixB is the index location (row number) of dateB. Then the slope you want is (priceB-priceA)/(ixB-ixA). Then use that slope to calculate the price at row number len(data.index)-1.

Let me know if this is enough information to help. (If not, then I can be more specific about coding the extrapolation, and setting alines, but I will need you to provide your exact data, and exact value you have set for the variable two_points).

DanielGoldfarb avatar Oct 21 '20 03:10 DanielGoldfarb

Also, fyi, once you have ixA and ixB, a simple way to find the linear equation for extrapolating is to use numpy polyfit setting deg = 1.

DanielGoldfarb avatar Oct 21 '20 04:10 DanielGoldfarb

This is very informative! Thank you very much for the advice! I will try out this method later.

sukioral avatar Oct 22 '20 00:10 sukioral

I have not thought of using slope to extrapolate.

sukioral avatar Oct 22 '20 00:10 sukioral

@sukioral , Please let me know if it works well for you, or if you have any other questions. Thank you. --Daniel

DanielGoldfarb avatar Oct 22 '20 02:10 DanielGoldfarb

@sukioral -- were you able to get this to work??

DanielGoldfarb avatar Nov 04 '20 00:11 DanielGoldfarb