How to correctly trade on data1 and data2?



  • 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


  • administrators

    @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.


  • administrators

    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.



  • @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.


  • administrators

    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)



  • @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.



  • @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



  • @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.


  • administrators

    @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.


Log in to reply
 

Looks like your connection to Backtrader Community was lost, please wait while we try to reconnect.