Skip to content

Add feature to draw vertical line delineate between pre-market and after-hour trading #54

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

Closed
eric573 opened this issue Mar 15, 2020 · 11 comments
Assignees
Labels
enhancement New feature or request released code merged into repo AND released to Pypi

Comments

@eric573
Copy link

eric573 commented Mar 15, 2020

Hi, is there anyway to draw vertical lines to delineate between pre-market and after-hour trading?

@eric573 eric573 added the question Further information is requested label Mar 15, 2020
@DanielGoldfarb
Copy link
Collaborator

At present there is no way to do this. You can sort of fake it out with mpf.make_addplot() by passing in a list of data (the same length as your dataframe) that is all nan except for two points (where you want the vertical line). If your data series has a lot of points, it will look like a vertical line. (But if your data series is relatively short, then it will be obvious that the line is on a slight angle ;-)


There is a plan to add two kwargs to mpf.plot(), vline= and hline=

For example, for a vertical line you could specify vline=<someTimeStamp> and you would get a vertical line at that timestamp, or for a horizontal line hline=<somePriceValue> and you would get a horizontal line at that value. Alternatively you could specify a list of timestamps for vline, and/or a list of values for hline, in order to plot more than one vertical or horizontal line (one line for each value in the list).

If you have a different idea as to how the interface should be, that you think may have an advantage over the vline and hline idea, please feel free to describe it here.

If you are interested in coding this vline and hline enhancement, I will be happy to guide you through the code. (Otherwise, I am hoping to code it myself maybe sometime in the next 4 to 8 weeks).


@coffincw
Copy link
Collaborator

I'd like to vouch for this enhancement, currently, with my project, I'm using the workaround @DanielGoldfarb describes, but the line doesn't look quite straight sometimes.

@free-soellingeraj
Copy link
Contributor

@DanielGoldfarb Let me know if I can help test or implement something on this.

@DanielGoldfarb
Copy link
Collaborator

@free-soellingeraj
Aaron,
Thank you very much for offering to contribute. This is almost done. I have vlines (vertical), hlines (horizontal), and alines (arbitrary) working. I am presently developing tlines (trend), which I expect to finish some time today. If you would like to help test before I enter a PR, you can pick up the code from here. ... and you can see how it works by looking at the following three scratch notebooks:

If you'd rather wait to test in a day (or two, I have some other cleanup to do too) for the PR to be in place, no worries. Up to you. In terms of implementation, if you would like to implement regression tests for this that would also be helpful. Take a look at this test folder and create test_lines.py (or perhaps there should be a separate file for each: test_vlines.py, test_hlines.py, etc. so that the files are more reasonable in size). You can follow the general pattern of what's being done there for other features. Please let me know if any of this is of interest and if you have the time to move forward on any of it.

All the best. --Daniel

@free-soellingeraj
Copy link
Contributor

Sure, I will take a look. I had a need specifically for several of these features. I will let you know.

@Brechard
Copy link

Brechard commented May 3, 2020

I've tried this and works perfectly thanks! Can we get it in make_addplot function too so it can be used for indicators in the lower pannel?

@Brechard
Copy link

Brechard commented May 3, 2020

also being able to choose colors could be interesting

@DanielGoldfarb
Copy link
Collaborator

@Brechard
Rodrigo,

Thanks for the idea regarding make_addplot(). I've been thinking that ideally mpf.make_addplot() should support all the same kwargs that mpf.plot() does. At this point I am not sure if we can support all of them, but certainly some of them should be do-able.

Regarding choosing colors, this is already supported for vlines, hlines, etc.
See the documentation here. You can control colors, linestyle, linewidths, and alpha.

All the best. --Daniel

@DanielGoldfarb DanielGoldfarb added released code merged into repo AND released to Pypi and removed merged / awaiting release to pypi code merged into repo, but not yet released to Pypi labels May 6, 2020
@gambitov
Copy link

gambitov commented Nov 13, 2020

Dear @DanielGoldfarb, congratulation for your work, it is very helpful.
I am trying to use horizontal lines with different linestyles, I organize data in a list with price (niveles) and style (estilo).

mpf.plot(TA_data, type='candle', axtitle=(symbol), style='yahoo', ax=ax1, volume=ax2,
                xrotation=0, addplot=apds, hlines=dict(hlines=niveles, linewidths=1, colors='k', linestyle=estilo))

but I get next error:

TypeError: kwarg "linestyle" validator returned False for value: "['--', '--', '--', '--', '--', '--', '--']"
    'Validator'   : lambda value: value is None or value in valid_linestyles },

may you help me? please.

@DanielGoldfarb
Copy link
Collaborator

@gambitov
mplfinance [hvta]lines kwargs accept only a single value for linestyle.

However, it does appear from the code that multiple values (list,tuple) are possible for colors and for linewidths. So you may consider using colors= to differentiate your various hlines.

Alternatively, as a work-around, you may draw each horizontal line using mpf.make_addplot() which will allow you to assign a different linestyle to each horizontal line. For example, suppose you want a horizontal lines at values of 185.0 195.0 and 210.0, then, in order to have a different line-type for each line, do the following:

apds = [ 
         mpf.make_addplot( [185.0]*len(TA_data), linestyle='-' ),
         mpf.make_addplot( [195.0]*len(TA_data), linestyle='--' ),
         mpf.make_addplot( [210.0]*len(TA_data), linestyle='-.' )
       ]

Given that colors and linewidths appear to accept multiple values, it seems a reasonable enahncement request for linestyle also to accept multiple values. I will have to investigate: It is certainly reasonable to accept a list or tuple for linestyle for hlines and vlines (assuming the list is the same length as the number of lines requested), but it may raise complications for tlines and alines (if so, then a list or tuple will not be supported for those types of lines).

HTH. All the best. --Daniel

@fxhuhn
Copy link
Contributor

fxhuhn commented Nov 16, 2020

Hi @DanielGoldfarb ,
it seems that it fits to my hlines proposal mentioned in #219 a dict of dicts ;-)
hlines = [dict_support_lines, dict_resistence_lines]

Regards, Markus

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request released code merged into repo AND released to Pypi
Projects
None yet
Development

No branches or pull requests

7 participants