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/

    Multiple data feeds with duplicate output

    General Code/Help
    2
    3
    93
    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.
    • V
      Vypy1 last edited by

      I don't know what I am doing wrong, what I want is to be able to use one strategy only multiple datas of different stocks. I referred to this post and to this post.

      I tried to write a script similar to it just to make sure that feeds are getting read in as expected. While I do end up getting close prices for both my feeds, I get a duplicate output.

      Provided the duplicate output below my code. One date is getting printed twice. How do I address this?

      My code:

      class maCross(bt.Strategy):
      
          def __init__(self):
                  
      #         for n in range(len(self.datas)):
              self.hopen = self.datas[0].open
              self.hclose = self.datas[0].close
              self.mopen = self.datas[1].open
              self.mclose = self.datas[1].close
      
      
          def next(self):
      
      #         for i in range(len(self.datas)):
              print(f'Date: {self.datas[0].datetime.date()}, HDFC Close: {self.hclose[0]} Maruti Close: {self.mclose[0]}')
          
      
      cerebro = bt.Cerebro()
      datalist = [hdfc, maruti]
      
      for i in range(len(datalist)):
          data = pandas_data_ext(dataname=datalist[i][0], datetime=None, open=-1, high=-1, low=-1, close=-1, volume=-1, Peaks=-1, Trough=-1)
          cerebro.adddata(data)
      cerebro.addstrategy(maCross)
      cerebro.run()
      

      Output:

      Date: 2017-02-10, HDFC Close: 1388.55 Maruti Close: 6112.25
      Date: 2017-02-10, HDFC Close: 1388.55 Maruti Close: 6022.0
      Date: 2017-02-13, HDFC Close: 1392.05 Maruti Close: 6022.0
      Date: 2017-02-13, HDFC Close: 1392.05 Maruti Close: 5939.85
      Date: 2017-02-14, HDFC Close: 1398.15 Maruti Close: 5939.85
      Date: 2017-02-14, HDFC Close: 1398.15 Maruti Close: 5862.7
      Date: 2017-02-15, HDFC Close: 1399.15 Maruti Close: 5862.7
      Date: 2017-02-15, HDFC Close: 1399.15 Maruti Close: 6024.55
      Date: 2017-02-16, HDFC Close: 1393.55 Maruti Close: 6024.55
      Date: 2017-02-16, HDFC Close: 1393.55 Maruti Close: 5989.75
      Date: 2017-02-17, HDFC Close: 1402.5 Maruti Close: 5989.75
      Date: 2017-02-17, HDFC Close: 1402.5 Maruti Close: 6093.15
      Date: 2017-02-20, HDFC Close: 1393.3 Maruti Close: 6093.15
      Date: 2017-02-20, HDFC Close: 1393.3 Maruti Close: 6063.15
      Date: 2017-02-21, HDFC Close: 1412.45 Maruti Close: 6063.15
      Date: 2017-02-21, HDFC Close: 1412.45 Maruti Close: 6079.65
      Date: 2017-02-22, HDFC Close: 1394.1 Maruti Close: 6079.65
      Date: 2017-02-22, HDFC Close: 1394.1 Maruti Close: 6031.0
      Date: 2017-02-23, HDFC Close: 1385.7 Maruti Close: 6031.0
      Date: 2017-02-23, HDFC Close: 1385.7 Maruti Close: 5951.5
      Date: 2017-02-27, HDFC Close: 1375.7 Maruti Close: 5951.5
      Date: 2017-02-27, HDFC Close: 1375.7 Maruti Close: 5922.5
      Date: 2017-02-28, HDFC Close: 1370.05 Maruti Close: 5922.5
      
      R 1 Reply Last reply Reply Quote 0
      • R
        rajanprabu @Vypy1 last edited by

        @vypy1

        By default BT assumes that you have timeframe Day. If its not daily timeframe give timeframe. That may fix your issue.. just a guess with what you posted.

        V 1 Reply Last reply Reply Quote 0
        • V
          Vypy1 @rajanprabu last edited by

          @rajanprabu I noticed the other df was timezone aware, I made it tz naive and that solved the issue and I am not getting duplicate outputs anymore. But out of curiosity, do you know why having a tz aware prints duplicate outputs?

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