Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    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)

    General Code/Help
    1
    1
    42
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • 蔡昇剛
      蔡昇剛 last edited by

      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:
      Screenshot 2022-04-11 073042.jpg
      Thanks in advance!

      1 Reply Last reply Reply Quote 0
      • 1 / 1
      • First post
        Last post
      Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors