如何绘制外部mplfinance图? For more details about trend line extrapolation, and the issues that relate to whether or not you trend includes non-trading days, click here.

问题描述

func containsSearchWithnspredicate(attribute: String,text: String) {
    let request: NSFetchRequest<Dinner> = Dinner.fetchRequest()
    let predicate = nspredicate(format: "\(attribute) CONTAINS[cd] %@",text)
    request.predicate = predicate
    do { items = try context.fetch(request) }
    catch let error { print("Error: \(error)") }
}

我收到错误消息:

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,28.72),(datetime(2021,3,26),23.49)]
mpf.plot(df_history,type='candle',alines=two_points)

有没有办法在范围外绘图?

解决方法

解决方法是设置 show_nontrading=True

我用你的代码进行了测试,修改为:

mpf.plot(df_history,type='candle',alines=two_points,show_nontrading=True)

并得到

enter image description here

这不适用于 show_nontrading=False(默认值,如果未指定,值)的原因是因为在这种情况下,x 轴值实际上只是表示数据框中行号的整数(尽管 x-轴标签格式为日期),因此您传递给 aline kwarg 的日期必须转换为行号(如果日期介于两行之间,则必须转换为行号)。该算法使用现有数据。会考虑是否可以轻松修改以进行外推(如果是这样,可能不完全是线性的)。 The code can be seen here。我愿意接受建议。与此同时,show_nontrading=True 应该可以正常工作。


另一种解决方法是在数据框的开头和结尾多放一行,其中包含趋势线的日期。

您需要开始结束的原因是两者您的趋势线日期超出了您的数据范围。

这对于 datetime(2020,8,30) 可能并不明显,因为对 get_data_yahoo() 的调用使用相同的日期,但是API 返回 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,30),end=datetime(2021,2,23))

dfa = df_history.iloc[0:1].copy()     # copy first row
dfa.index = [datetime(2020,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,28.72),(datetime(2021,26),23.49)]
mpf.plot(df_history,alines=two_points)

结果:

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.

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...