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/

    Plotting not working when loading using replay data for multiple assets

    General Code/Help
    1
    2
    27
    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.
    • kiskoapeles
      kiskoapeles last edited by

      Plotting for the exponential moving averages for multiple assets work on cerebro.resampledata but not on cerebro.replaydata.

      How can I still use replaydata to more accurately simulate my trading strategy.

      Here is my code:

      %matplotlib inline
      
      import backtrader as bt
      import backtrader.feeds as btfeeds
      import backtrader.indicators as btind
      import datetime
      from tqdm.notebook import tqdm
      
      working_crypto_list = ['ETC', 'XRP', 'ADA']
      print(working_crypto_list)
      
      start_date=datetime.datetime(2018, 1, 1)
      end_date=datetime.datetime(2021, 12, 30)
      
      input_data_timeframe = bt.TimeFrame.Minutes
      input_data_compression = 60
      
      starting_cash = 100_000.0
      timeframe = bt.TimeFrame.Days
      ma_window_0 = 9
      ma_window_1 = 20
      ma_window_2 = 50
      
      class TestStrategy(bt.Strategy):
          params = (
          ('data_list', working_crypto_list),
      )
      
          def __init__(self):
              self.closed_trades = 0
              
              self.close_price_array = {}
              self.high_price_array = {}
              self.low_price_array = {}
      
              self.ema_0_array = {}
              self.ema_1_array = {}
              self.ema_2_array = {}
      
              self.macd_array = {}
                  
                  
              #For every data feed in data_list
              for asset_index in  tqdm(range(len(self.params.data_list))):
      
                  # Keep a reference to the "close" line in the data[0] dataseries
                  self.close_price_array[asset_index] = self.datas[asset_index].close
      
                  self.high_price_array[asset_index] = self.datas[asset_index].high
                  self.low_price_array[asset_index] = self.datas[asset_index].low
      
                  self.ema_0_array[asset_index] = btind.ExponentialMovingAverage(self.datas[asset_index].close, period=ma_window_0)
                  self.ema_1_array[asset_index] = btind.ExponentialMovingAverage(self.datas[asset_index].close, period=ma_window_1)
                  self.ema_2_array[asset_index] = btind.ExponentialMovingAverage(self.datas[asset_index].close, period=ma_window_2)
      
                  self.macd_array[asset_index] = btind.MACD(self.datas[asset_index].close)
                  
          def next(self):
              return
      
      cerebro = bt.Cerebro()
      cerebro.addstrategy(TestStrategy, data_list=working_crypto_list)
      
      
      for crypto in working_crypto_list:
          file = f'{crypto}USDT_1h_1514736000000_1640880000000.csv'
          data = btfeeds.GenericCSVData(
          dtformat=('%Y-%m-%d %H:%M:%S'),
          dataname=file,
          headers=True,
          datetime=0,
          open=1,
          high=2,
          low=3,
          close=4,
          volume=5,
          openinterest=-1,
          fromdate=start_date,
              todate=end_date,
              timeframe=input_data_timeframe, 
              compression=input_data_compression,
          )
          
          #PROBLEMATIC PART
          cerebro.replaydata(data, timeframe=bt.TimeFrame.Days) #NOT WORKING
          #cerebro.resampledata(data, timeframe=bt.TimeFrame.Days) #WORKING
      
      cerebro.broker.setcash(starting_cash)
      cerebro.run()
      cerebro.plot(iplot=True)
      
      kiskoapeles 1 Reply Last reply Reply Quote 0
      • kiskoapeles
        kiskoapeles @kiskoapeles last edited by

        Here are images of the results

        using cerebro.replaydata:
        Exponential Moving Averages are off for the other assets

        using cerebro.resampledata:
        No problem

        Here are the three csv files I used

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