Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mpf.plot - extending line joining two points #278

Open
sukioral opened this issue Oct 20, 2020 · 12 comments
Open

mpf.plot - extending line joining two points #278

sukioral opened this issue Oct 20, 2020 · 12 comments
Labels
question Further information is requested

Comments

@sukioral
Copy link

sukioral commented Oct 20, 2020

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 sukioral added the question Further information is requested label Oct 20, 2020
@sukioral
Copy link
Author

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.

@DanielGoldfarb
Copy link
Collaborator

Try tight_layout=True.

@DanielGoldfarb
Copy link
Collaborator

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

@sukioral
Copy link
Author

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?

@DanielGoldfarb
Copy link
Collaborator

DanielGoldfarb commented Oct 21, 2020

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

@sukioral
Copy link
Author

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

@DanielGoldfarb
Copy link
Collaborator

DanielGoldfarb commented Oct 21, 2020

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
Copy link
Collaborator

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.

@sukioral
Copy link
Author

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

@sukioral
Copy link
Author

I have not thought of using slope to extrapolate.

@DanielGoldfarb
Copy link
Collaborator

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

@DanielGoldfarb
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants