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/

    How to correctly trade on data1 and data2?

    General Code/Help
    3
    9
    3071
    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.
    • A
      asuralm last edited by

      Hi:

      I add two data source to the cerebro and I want to trade on the data1(the default is data0). The two datas may not have the same history time length.

      Here is my code:
      In the strategy next(), I wrote code like:

          def next(self):
              # Check if an order is pending ... if yes, we cannot send a 2nd one
              if self.order:
                  return
      
              # Check if we are in the market
              if not self.getposition(data=self.datas[1]):
                  if self.datas[1].datetime.date(0).weekday() == 0:# and self.dataclose > self.sma  :
                      # BUY, BUY, BUY!!! (with all possible default parameters)
                      self.log('Monday BUY CREATE, %.2f' % self.dataclose[0])                
                      # Keep track of the created order to avoid a 2nd order
                      self.order = self.buy(data=self.datas[1])
              else:                        
                  # 3 days exit
                  if len(self) >= (self.bar_executed + self.params.para2):
                      # SELL, SELL, SELL!!! (with all possible default parameters)
                      self.log('SELL CREATE, %.2f' % self.dataclose[0])
                      self.order = self.close(data=self.datas[1])
      

      In the cerebro settings, my code looks like:

              fromdate = ....
              todate = ....
      
              # setup data
              data = bt.feeds.GenericCSVData(dataname='SourceFiles/CU.csv', ...)
      
      
              data2 = bt.feeds.GenericCSVData(dataname='SourceFiles/A#.csv',...)
              self.cerebro.adddata(data, name='data1])
              self.cerebro.adddata(data2, name="data2")
      
              # setup analyzers
              self.cerebro.addanalyzer(bt.analyzers.DrawDown)
              self.cerebro.addanalyzer(bt.analyzers.TradeAnalyzer)
              self.cerebro.addanalyzer(bt.analyzers.SQN)
              self.cerebro.addanalyzer(bt.analyzers.AnnualReturn)
      
              # setup obversers
              self.cerebro.addobservermulti(bt.observers.DrawDown)
      
              self.cerebro.addobservermulti(bt.observers.Trade)
              self.cerebro.addobservermulti(bt.observers.BuySell)
      
      
              self.cerebro.addstrategy(SampleStrategy,....)
      
              self.result = self.cerebro.run()
      

      But when I run the strategy, it looks like:
      0_1494000013546_upload-7c9dfc20-9585-473c-adfb-4fcf5a7cde1d

      The order on data2 are not correctly opened and closed at all. And it is not showing buy and sell correctly on data 2.

      Any help please?
      Thanks

      B 1 Reply Last reply Reply Quote 0
      • B
        backtrader administrators @asuralm last edited by

        @asuralm said in How to correctly trade on data1 and data2?:

        The order on data2 are not correctly opened and closed at all

        Unless you have some other information, it seems like they are being correctly opened and closed (x days after entering). There is a corresponding sell (and therefore a closed trade) for each buy

        And it is not showing buy and sell correctly on data 2.

        The buy/sell markers are surprisingly displaced to the right. The pattern they form correspond to the actual prices which are located (vertically) where the traces are. It really seems to be just an optical/presentation issue.

        Understanding which time dis-alignment the data feeds have, could help.

        1 Reply Last reply Reply Quote 1
        • B
          backtrader administrators last edited by

          There is something you could try in your own code:

          import backtrader as bt
          
          bt.observers.BuySell._stclock = True
          

          what will force the observer to align itself with the strategy rather than with the data feed which is looking at. It may be worth a try.

          1 Reply Last reply Reply Quote 1
          • A
            asuralm last edited by

            @backtrader said in How to correctly trade on data1 and data2?:

            There is something you could try in your own code:

            import backtrader as bt
            
            bt.observers.BuySell._stclock = True
            

            what will force the observer to align itself with the strategy rather than with the data feed which is looking at. It may be worth a try.

            I tried it like:

            # setup analyzers
                    self.cerebro.addanalyzer(bt.analyzers.DrawDown)
            
                    # setup obversers
                    self.cerebro.addobserver(bt.observers.DrawDown)
            
                    bt.observers.BuySell._stclock = True
            
                    self.cerebro.addstrategy(...)
            
                    self.result = self.cerebro.run()
            

            It gives me
            0_1494051829376_upload-890e3af8-6c23-4bd2-9a4f-62d3cb30018e

            if I don't include bt.overservers.BuySell._stclock=True. the result looks like:
            0_1494051896752_upload-9d747919-d750-4e73-ac4d-cc0cc528030a

            It erase all the buysell. It seems that everytime I add two datafeed, it gives me the wrong displacement of the buysell.

            1 Reply Last reply Reply Quote 0
            • B
              backtrader administrators last edited by

              One important thing to understand if something fails is test against something consistent. Your test runs are obviously done with different data sets and different logic. The latest example clearly buys data0, whereas the former bought data1. The former had two 1-Day data feeds and the latter has a 15-Minutes data feed and then a 1-Day (looks like the resampled version) data feed (many other differences are present)

              A 1 Reply Last reply Reply Quote 1
              • A
                asuralm @backtrader last edited by

                @backtrader

                Yes they are indeed different data feeds but it seems the plotting is not correct when multi data feed the testing engineer no matter what.

                Am I using the bt.observers.BuySell._stclock=True correctly?
                I trace to the observers class but didn't find such attribute _stclock though.

                H B 2 Replies Last reply Reply Quote 0
                • H
                  holicst @asuralm last edited by

                  @asuralm Hi,

                  I tried to reproduce your impressive results with Forex, but I had no luck. May I ask what your datas are, what are you trading?

                  Cheers,
                  Tamás

                  A 1 Reply Last reply Reply Quote 0
                  • A
                    asuralm @holicst last edited by

                    @holicst

                    It is commodity and I just use it to test the plotting. To trade on Monday just for the simplicity reason. not a strategy really.

                    1 Reply Last reply Reply Quote 0
                    • B
                      backtrader administrators @asuralm last edited by

                      @asuralm said in How to correctly trade on data1 and data2?:

                      Am I using the bt.observers.BuySell._stclock=True correctly?

                      There is nothing right or wrong about it. It was a hack.

                      @asuralm said in How to correctly trade on data1 and data2?:

                      Yes they are indeed different data feeds but it seems the plotting is not correct when multi data feed the testing engineer no matter what.

                      If each example you present has different feeds, different logic (summary: everything is different) there is no way to discern where a/the problem may be.

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