For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
Extending a Datafeed (Lines_LineSeries_DataSeries_OHLC_OHLCDateTime_Abst' object has no attribut)e)
-
Hello, I'm trying to add extending data to my data feed, but when I output, It's showed " AttributeError: 'Lines_LineSeries_DataSeries_OHLC_OHLCDateTime_Abst' object has no attribute 'longshort_ratio' "
I have read Extending a Datafeed, How to Feed Backtrader Alternative Data, and still don't know what went wrong,
here is my code:import datetime import backtrader as bt import pandas as pd from OI_Data import * class ExtraPandasDirectData(bt.feeds.PandasDirectData): lines = ('longshort_ratio', ) params = (('longshort_ratio', -1), ) datafields = bt.feeds.PandasData.datafields + (['longshort_ratio']) class MyStrategy(bt.Strategy): def __init__(self): bt.indicators.SMA(self.data.openinterest, period=1, subplot=True, plotname='Open_Interest') print(*self.data.longshort_ratio) cerebro = bt.Cerebro() Kline = pd.read_csv('Data/data.csv') Kline['datetime'] = pd.to_datetime(Kline['datetime']) Kline.set_index('datetime', inplace=True) feed = bt.feeds.PandasDirectData( dataname=Kline, fromdate=datetime.datetime(2022, 3, 11, 0, 0, 0), todate=datetime.datetime(2022, 4, 10, 15, 0, 0), timeframe=bt.TimeFrame.Minutes, compression=60) cerebro.adddata(feed) cerebro.addstrategy(MyStrategy) cerebro.run()
Here is my data:
Thanks in advance!