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

IndexError: array index out of range



  • File "strategA.py", line 127, in next
    if (self.data.close[2] < self.sma and self.data.close[2] < self.ema) and (self.status != 1) and (self.status != 2):
    File "/home/yelnar/anaconda3/envs/py27/lib/python2.7/site-packages/backtrader/linebuffer.py", line 163, in getitem
    return self.array[self.idx + ago]

            if self.status == 1:
            self.cash_aapl = round((self.data0.close[0] - self.init_close1) * self.qty1, 2)
            self.cash_msft = round((self.init_close2 - self.data1.close[0]) * self.qty2, 2)
            self.profit = round((self.cash_aapl + self.cash_msft), 2)
    
            print('Profit stock1: {}'.format(self.cash_aapl))
            print('Profit stock2: {}'.format(self.cash_msft))
            print('Total profit: {}'.format(self.profit))
    
        if (self.data.close[2] < self.sma and self.data.close[2] < self.ema) and (self.status != 1) and (self.status != 2):
    
            # Calculating the number of shares for each stock
            value = self.portfolio_value_basket1  # Divide the cash equally
            x = int(value / (self.data0.close))  # Find the number of shares for Stock1
            y = int(value / (self.data1.close))  # Find the number of shares for Stock2
    
            print('portfolio_value_basket1: {}'.format(self.portfolio_value_basket1))
    
            # Place the order
            self.log('BUY CREATE %s, price = %.2f, qty = %d' % ("EUR.JPY", self.data0.close[0], x))
            self.buy(data=self.data0, size=(x))
    
            self.log('SELL CREATE %s, price = %.2f, qty = %d' % ("USD.JPY", self.data1.close[0], y))
            self.sell(data=self.data1, size=(y))
    
            self.qty1 = x  # The new open position quantity for Stock1 is x shares
            self.qty2 = y  # The new open position quantity for Stock2 is y shares
            self.init_close1 = self.data0.close[0]
            self.init_close2 = self.data1.close[0]
            self.status = 1  # The current status is "long the spread"


  • If you need some advice, you may want to share full script. Not just arbitrary piece of it.


  • administrators

    @ab_trader said in IndexError: array index out of range:

    If you need some advice, you may want to share full script. Not just arbitrary piece of it.

    Indeed ... although this time

    @YELNAr said in IndexError: array index out of range:

    if (self.data.close[2] < self.sma and self.data.close[2]
    

    It should be clear that looking into the future is still a very difficult feat.


Log in to reply
 

});