python - AttributeError:“Lines_LineSeries_DataSeries_OHLC_OHLCDateTime_Abst”对象没有属性“SRNE”
问题描述
我有一个数据框
timestamp SRNE CRSR GME ... ASO TH DTE ATH
0 2021-04-06 00:00:00 7.86 34.66 184.50 ... 31.14 3.230 135.13 51.10
1 2021-04-07 00:00:00 7.58 34.58 177.97 ... 30.78 3.640 135.70 50.47
2 2021-04-08 00:00:00 7.74 34.43 170.26 ... 29.05 3.360 134.95 49.87
3 2021-04-09 00:00:00 7.51 33.52 158.36 ... 30.84 3.125 135.46 50.45
4 2021-04-12 00:00:00 6.94 33.67 141.09 ... 32.29 3.420 135.63 50.80
我正在创建一个回测器,我想访问特定的列。这是我尝试过的
class TestStrategy(bt.Strategy):
def log(self, txt, dt=None):
''' Logging function fot this strategy'''
dt = dt or self.datas[0].datetime.date(0)
print('%s, %s' % (dt.isoformat(), txt))
def __init__(self):
# Keep a reference to the "close" line in the data[0] dataseries
self.dataclose = self.datas[0].SRNE
但它给出了一个错误 AttributeError: 'Lines_LineSeries_DataSeries_OHLC_OHLCDateTime_Abst' 对象没有属性 'SRNE' 感谢您的帮助
解决方案
您需要扩展 Datafeed - https://backtrader.com/docu/extending-a-datafeed/
简而言之,您必须自定义正在使用的数据加载器。如果你使用 bt.feeds.PandasData 确保首先添加行。
它可能看起来像这样:
class PandasData(bt.feeds.PandasData):
lines = ('SRNE', 'CRSR', 'GME', ... ,'ASO', 'TH', 'DTE', 'ATH')
params = (('SRNE', -1), ('CRSR', -1), ('GME', -1), ('ASO', -1) etc...)
datafields = PandasData.datafields + (['SRNE', 'CRSR', 'GME', ... ,'ASO', 'TH', 'DTE', 'ATH')])
data = PandasData(df)
然后你继续
cerebro.adddata(data)
让我知道这是否有帮助。:)
推荐阅读
- java - 有没有办法在 IntelliJ 中获取数据库选项卡?
- .net - 尝试创建实例时没有合适的构造函数
- swift - SwiftUI Initialzier 要求 String 符合 Identifiable
- python - Python 排除行正则表达式字符串等(python 日志记录/Ookla 服务器
- flutter - 如何转换列表
列出 ? - sql - PLSQL - 帮助我的正则表达式电话号码和区号
- python - 如何在词干和删除标点符号后按新行拆分正则表达式结果列表?
- javascript - 无法使用 puppeteer 单击按钮
- javascript - Vue.JS 的通用排序方法存在问题
- python - python 函数中的变量需要“self”吗?