Lots of horizontal lines in mplfinance

79 views Asked by At

I ask for help, because I can not understand what is wrong. There is a chart, you need to draw 2 horizontal lines on it, but for some reason I encounter a validator error Code:

    df.set_index("Time", inplace=True)
    df.index = pd.DatetimeIndex(df.index)

    mpf.plot(df, type='candle', style='yahoo', title="Stock Price", ylabel='Price',
             alines=dict(alines=[line_1, line_2], colors="r", linestyle="-"))
    mpf.show()

Error:

TypeError: kwarg "alines" validator returned False for value: "{'alines': [[('189', 0.06682), ('999', 0.06682)], [('191', 0.06954), ('999' , 0.06954)]], 'colors': 'r', 'linestyle': '-'}"
    'Validator' : lambda value: _alines_validator(value) },

The experience is not the first one already, and earlier the norms were created, the ** mplfinance ** library is used, but now for some reason it does not skip. Maybe my eyes are blurred. Please help. Thank you in advance! Have a nice day and clean working code everyone!

as a result, I need to get a graph like this, only yellow lines, but I can't do it: enter image description here

2

There are 2 answers

0
Daniel Goldfarb On BEST ANSWER

I believe the problem is that your "date/price" pairs do not contain dates. In place of the dates, you seem to be entering strings that represent integers.

For example, you have

[
 [('189', 0.06682), ('999', 0.06682)], 
 [('191', 0.06954), ('999' , 0.06954)]
]

Perhaps this should be something more like:

[
 [('2023-08-01', 0.06682), ('2023-09-01', 0.06682)], 
 [('2023-08-05', 0.06954), ('2023-09-01' , 0.06954)]
]
1
Xiaomin Wu On

according the doc: https://github.com/matplotlib/mplfinance

your df must have columns named: Open, High, Low, Close

In https://github.com/matplotlib/mplfinance/blob/master/examples/using_lines.ipynb

the alines must be

a sequence of two or more date/price pairs, or
a sequence of sequences of two or more date/price pairs

while in your case, it is a dict not sequence,refer to alines (arbitrary lines) in https://github.com/matplotlib/mplfinance/blob/master/examples/using_lines.ipynb for more details.