首页 > 解决方案 > 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' 感谢您的帮助

标签: python

解决方案


您需要扩展 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)

让我知道这是否有帮助。:)


推荐阅读