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/

    Passing historical trades to strategy

    Indicators/Strategies/Analyzers
    3
    4
    251
    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.
    • Noman Akhtar
      Noman Akhtar last edited by

      Hi,

      New to backtrader so still in the process of learning. I want to pass historical trades as a separate dataframe to my strategy along with ohlcv dataframe. Could you link me to an article that describes how I may be able to do that and then reference the columns in the strategy class?

      I do,

          data = bt.feeds.PandasData(dataname=trades)
          # trades is a pandas dataframe with ['timestamp', 'side', 'price', 'amount'] as the columns.
          cerebro.adddata(data)
      

      My Strategy Class is as follows:

      class StrategyOne(bt.Strategy):
      
          def __init__(self):
              self.dataprice = self.datas[0].price
      
          def log(self, text, datetime=None):
              datetime = datetime or self.datas[0].datetime(0)
              print('{}, {}'.format(datetime, text))
      
          def next(self):
              self.log('{} Close: {}'.format(len(self), self.dataprice[0]))
      

      When I run the strategy, it says
      AttributeError: 'Lines_LineSeries_DataSeries_OHLC_OHLCDateTime_Abst' object has no attribute 'price'

      What would be the correct way to approach this?

      Many thanks

      1 Reply Last reply Reply Quote 0
      • vladisld
        vladisld last edited by

        Please take a look at the following blog:
        https://www.backtrader.com/blog/posts/2017-07-05-order-history/order-history/

        Noman Akhtar 1 Reply Last reply Reply Quote 0
        • Noman Akhtar
          Noman Akhtar @vladisld last edited by

          @vladisld aah thanks so much. Let me try it out. Just to clarify (and I should have mentioned this previously) by historical trades, I meant the market trades and not just my trades. So, the 2 dataframes will have different time indexing. One way, is that i group the trades by ohlcv time intervals so they have the same time indexing as ohlcv data and json dump all the trades for the particular time intervals as another column into ohlcv but it will be nice to have the flexibility to pass two dataframe containing different information independent of each others time indexing if that makes sense

          1 Reply Last reply Reply Quote 0
          • run-out
            run-out last edited by

            For adding in an extra line you can find information here.

            Of course with different time frames, you will need to resample your date, information for which can be found here.

            If you still have trouble let us know and give us sample data. But give the above a try first.

            RunBacktest.com

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