Skip to content
Advertisement

how to draw outside mplfinance plot?

JavaScript

I get the error message:

JavaScript

Is there a way to plot outside the range?

Advertisement

Answer

The workaround for this is to set show_nontrading=True

I tested with your code, modifying to:

JavaScript

and get

enter image description here

The reason this does not work with show_nontrading=False (the default, if unspecified, value) is because in that case the x-axis values are actually just integers representing the row number in your dataframe (although the x-axis labels are formatted as dates), so the dates you pass in to the aline kwarg have to be converting to a row number (or fraction thereof if the date falls between two rows). The algorithm uses the existing data. Will think about whether can be easily modified to extrapolate (may not be entirely linear if so). The code can be seen here. Am open to suggestions. In the meantime show_nontrading=True should work fine.


Another workaround is to place an extra row at both the beginning and the end of your dataframe, with the dates of your trend line.

The reason you need both the beginning and end is because both your trend line dates are out of the range of your data.

This may not be obvious for datetime(2020, 8, 30), since the call to get_data_yahoo() uses the same date, however the API returns 8.31.2020 as the first date, since 8.0 is a Sunday and a non-trading day.

Thus:

JavaScript

The result:

enter image description here

For more details about trend line extrapolation, and the issues that relate to whether or not you trend includes non-trading days, click here.

Advertisement