如何绘制外部 mplfinance 图?
how to draw outside mplfinance plot?
from datetime import datetime, timedelta, date
import pandas_datareader as pdr
import mplfinance as mpf
df_history = pdr.get_data_yahoo(symbols='GOLD',
start=datetime(2020, 8, 30),
end=datetime(2021, 2, 23))
# Today is: 24.02.2021
two_points = [(datetime(2020, 8, 30),28.72),(datetime(2021, 3, 26),23.49)]
mpf.plot(df_history, type='candle', alines=two_points)
我收到错误消息:
ValueError: User specified line date "2020-08-30 00:00:00" is before (less than) range of plotted data (2020-08-31 00:00:00 to 2021-02-23 00:00:00)
有没有办法在范围外绘图?
解决方法是设置 show_nontrading=True
我测试了你的代码,修改为:
mpf.plot(df_history, type='candle', alines=two_points, show_nontrading=True)
并得到
这不适用于 show_nontrading=False
的原因(默认值,如果未指定,值)是因为在这种情况下,x 轴值实际上只是表示数据框中行号的整数(尽管x 轴标签被格式化为日期),因此您传递给 aline
kwarg 的日期必须转换为行号(如果日期介于两行之间,则为行号的分数)。该算法使用现有数据。会考虑是否可以轻松修改以推断(如果是这样可能不是完全线性的)。 The code can be seen here。我愿意接受建议。同时 show_nontrading=True
应该可以正常工作。
另一种解决方法是在数据框的开头和结尾放置一个额外的行,其中包含趋势线的日期。
你需要开头 和 结尾的原因是因为 both 你的趋势线日期超出了您的数据范围。
这对于 datetime(2020, 8, 30)
可能不是很明显,因为对 get_data_yahoo()
的调用使用相同的日期,然而 API returns 8.31.2020 作为第一个日期,因为 8.0 是星期日和非交易日。
因此:
from datetime import datetime, timedelta, date
import pandas_datareader as pdr
import mplfinance as mpf
df_history = pdr.get_data_yahoo(symbols='GOLD',
start=datetime(2020, 8, 30),
end=datetime(2021, 2, 23))
dfa = df_history.iloc[0:1].copy() # copy first row
dfa.index = [datetime(2020, 8, 30)] # switch the date
dfb = df_history.iloc[-2:-1].copy() # copy last row
dfb.index = [datetime(2021, 3, 26)] # switch the date
# Concatenate the three DataFrames
df_history = pd.concat([dfa,df_history,dfb])
# Now we the trend line dates are withing the range of data in df_history:
two_points = [(datetime(2020, 8, 30),28.72),(datetime(2021, 3, 26),23.49)]
mpf.plot(df_history, type='candle', alines=two_points)
结果:
For more details about trend line extrapolation, and the issues that relate to whether or not you trend includes non-trading days, click here.
from datetime import datetime, timedelta, date
import pandas_datareader as pdr
import mplfinance as mpf
df_history = pdr.get_data_yahoo(symbols='GOLD',
start=datetime(2020, 8, 30),
end=datetime(2021, 2, 23))
# Today is: 24.02.2021
two_points = [(datetime(2020, 8, 30),28.72),(datetime(2021, 3, 26),23.49)]
mpf.plot(df_history, type='candle', alines=two_points)
我收到错误消息:
ValueError: User specified line date "2020-08-30 00:00:00" is before (less than) range of plotted data (2020-08-31 00:00:00 to 2021-02-23 00:00:00)
有没有办法在范围外绘图?
解决方法是设置 show_nontrading=True
我测试了你的代码,修改为:
mpf.plot(df_history, type='candle', alines=two_points, show_nontrading=True)
并得到
这不适用于 show_nontrading=False
的原因(默认值,如果未指定,值)是因为在这种情况下,x 轴值实际上只是表示数据框中行号的整数(尽管x 轴标签被格式化为日期),因此您传递给 aline
kwarg 的日期必须转换为行号(如果日期介于两行之间,则为行号的分数)。该算法使用现有数据。会考虑是否可以轻松修改以推断(如果是这样可能不是完全线性的)。 The code can be seen here。我愿意接受建议。同时 show_nontrading=True
应该可以正常工作。
另一种解决方法是在数据框的开头和结尾放置一个额外的行,其中包含趋势线的日期。
你需要开头 和 结尾的原因是因为 both 你的趋势线日期超出了您的数据范围。
这对于 datetime(2020, 8, 30)
可能不是很明显,因为对 get_data_yahoo()
的调用使用相同的日期,然而 API returns 8.31.2020 作为第一个日期,因为 8.0 是星期日和非交易日。
因此:
from datetime import datetime, timedelta, date
import pandas_datareader as pdr
import mplfinance as mpf
df_history = pdr.get_data_yahoo(symbols='GOLD',
start=datetime(2020, 8, 30),
end=datetime(2021, 2, 23))
dfa = df_history.iloc[0:1].copy() # copy first row
dfa.index = [datetime(2020, 8, 30)] # switch the date
dfb = df_history.iloc[-2:-1].copy() # copy last row
dfb.index = [datetime(2021, 3, 26)] # switch the date
# Concatenate the three DataFrames
df_history = pd.concat([dfa,df_history,dfb])
# Now we the trend line dates are withing the range of data in df_history:
two_points = [(datetime(2020, 8, 30),28.72),(datetime(2021, 3, 26),23.49)]
mpf.plot(df_history, type='candle', alines=two_points)
结果: