For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

more data more problems



  • presume I add 4 sets of data, exactly as the following, just different names:

    start_date = datetime.date(2017, 1, 1)
    end_date = datetime.date(2018, 4, 1)
    
    AAPL = bt.feeds.GenericCSVData(
        plot=False,
        dataname='./backtrader/data/minute/AAPL-2M.csv',
    
        fromdate=start_date,
        todate=end_date,
        timeframe=bt.TimeFrame.Minutes,
        compression=80,
    
        dtformat=('%Y-%m-%d %H:%M:%S'),
        sessionstart=datetime.time(9, 30),
        sessionend=datetime.time(16, ),
    
        datetime=0,
        high=1,
        low=2,
        open=3,
        close=4,
        volume=5,
        openinterest=6,
    )
    

    then I add the data and a SINGLE strategy:

    #     cerebro.adddata(AAPL)
    #     cerebro.adddata(MSFT)
    cerebro.adddata(GOOG)
    cerebro.adddata(C)
    
    cerebro.addstrategy(TestStrategy, ticker_0='GOOG',ticker_1='C')
    

    the output for the strategy might be :
    Starting Portfolio Value: 1000000.00
    Final Portfolio Value: 1001815.16

    now if I uncomment those two cerebro.adddata lines with out changing anything else, and my data in strategy is defined by the dname:

        self.dataset_0 = eval(self.p.ticker_0)
        self.dataset_1 = eval(self.p.ticker_1)
    

    my output for the strategy changes to the following:
    Starting Portfolio Value: 1000000.00
    Final Portfolio Value: 1001554.27

    ***How does adding data that is never called seem to effect my live strategies ? ***

    data sample is as followed:

    2017-01-03 11:30:00,28.3400,27.9901,28.2500,28.0600,216786,188503
    2017-01-03 12:50:00,28.1100,27.9800,28.0500,28.0400,316431,79297
    2017-01-03 14:10:00,28.0400,27.8900,28.0400,27.9100,411203,78593
    2017-01-03 15:30:00,27.9500,27.8600,27.9150,27.8900,548365,114589
    2017-01-03 16:00:00,28.0700,27.8800,27.8900,28.0600,701523,130827

    strategy logic for placing orders is as followed:

        if self.days > 1:
            # If we're not in the market...
            if not self.invested:
                if et < -sqrt:
                    # Long Entry
                    self.log('LONG')
                    self.cur_hedge_qty = 500
                    self.buy(data=self.dataset_1, size=self.qty)
                    self.sell(data=self.dataset_0, size=self.cur_hedge_qty)
                    
                    self.invested = "long"
    
                elif et > sqrt:
                    # # Short Entry
                    self.log('SHORT')
                    self.cur_hedge_qty =500
                    self.sell(data=self.dataset_1, size=self.qty)
                    self.buy(data=self.dataset_0, size=self.cur_hedge_qty)
                    
                    self.invested = "short"
    
                
    
            # If we are in the market...
            if self.invested:
                if self.invested == "long" and et > -sqrt:
                    self.log('CLOSING LONG')
                    self.order = self.close(data=self.dataset_1)
                    self.order = self.close(data=self.dataset_0)
                    self.invested = None
                    
                elif self.invested == "short" and et < sqrt:
                    self.log('CLOSING SHORT')
                    self.close(data=self.dataset_1)
                    self.close(data=self.dataset_0)
                    self.invested = None


  • @backtrader
    this problem seems to be related to different files not having matching bars, however, they are the same time frame.

    when filter out in the CSV file the pre-market and after market data i have no issues with this problem at all.

    I guess to properly fix this I would need to before I import a file to check the length and then import the largest to smallest in terms of length of bars first?



  • That could happen if your data doesn't have the same start/end dates or is not properly aligned. What I do to deal with this is load a dummy reference data that goes from the start to the end (I don't use the data in the code, just the date).

    Adding the ticker in your strategy as below seems a bit dodgy:

    cerebro.addstrategy(TestStrategy, ticker_0='GOOG',ticker_1='C')
    

    Instead what I do is something like:

    cerebro.adddata(GOOG, name='GOOG')
    

    And then when you go through your data, you can discard any serie which you do not want to trade. For dealing with data with different start/end dates, you need to add prenext(), there are plenty of posts on this, you should go through the forum.

    Looks like this:

        def prenext(self):
            self.next()
    
        def next(self):
            if self.order:
                return
            # Initialize Data
            fdata = [(i, dta) for i, dta in enumerate(self.datas[1:]) if len(dta)]
            if not fdata:
                return
            # Debugging
            series = ['(Index: {} - Date: {})'.format(fdata[i][0], fdata[i][1].datetime.date(0)) for i, dta in enumerate(fdata)]
            self.debug('Reference: {} - {}'.format(self.date(0), series))
    

    You can get the datanames like below:

            self.names = self.getdatanames()
            del self.names[0]  # Remove Reference
            self.debug('Symbols: {}'.format(self.names))
    

    PS: This is an extract from my code so you might have to adapt it a bit to get it to work for you.
    PS2: See https://community.backtrader.com/topic/201/next-is-skipping-data


  • administrators

    @laurent-michelizza said in more data more problems:

    That could happen if your data doesn't have the same start/end dates or is not properly aligned

    Only the 1st. The system won't enter the next phase of the strategy until all data feeds (and also the associated indicators) have covered the warm-up period (aka as minperiod)

    The reason is clear: in next all elements are guaranteed to have a value available at index [0]. Hence the need to await the minimum period of all elements to be consumed.

    If you need to access data before, use prenext, but each element will have to be checked for actual data.

    See: