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

    General Code/Help
    1
    1
    16
    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

      I'm trying to add extending data to my data feed, but when I output, It showed " AttributeError: 'Lines_LineSeries_DataSeries_OHLC_OHLCDateTime_Abst' object has no attribute 'longshort_ratio' " I have readExtending 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
      
      
      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):
              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()
      

      The output message:
      alt text
      Here is my data:
      alt text

      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