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

Trying to set up a simple ichimoku strategy: array index out of range error



  • Hey all,

    Very new to backtrader and object oriented programming in general so some guidance would be nice!
    I am trying to debug this error:

    99         # Long Short ichimoku logic
    --> 100         self.long = (self.data.close[0] > self.ichi.senkou_span_a[0])        & (self.data.close[0] > self.ichi.senkou_span_b[0])        & (self.tkcross == 1)
        101 
        102         self.short = (self.data.close[0] < self.ichi.senkou_span_a[0])        & (self.data.close[0] < self.ichi.senkou_span_b[0])        & (self.tkcross == -1)
    
    ~/anaconda3/envs/py35/lib/python3.5/site-packages/backtrader/linebuffer.py in __getitem__(self, ago)
        161 
        162     def __getitem__(self, ago):
    --> 163         return self.array[self.idx + ago]
        164 
        165     def get(self, ago=0, size=1):
    
    IndexError: array index out of range
    

    And this is the entire strategy code snippet :

    
    class TheStrategy(bt.Strategy):
        params = (
            ('atrperiod', 14),  # ATR Period (standard)
            ('atrdist_x', 1.5),   # ATR distance for stop price
            ('atrdist_y', 1.35),   # ATR distance for take profit price
            ('tenkan', 9),
            ('kijun', 26),
            ('senkou', 52),
            ('senkou_lead', 26),  # forward push
            ('chikou', 26),  # backwards push
        )
    
        def notify_order(self, order):
            if order.status == order.Completed:
                pass
    
            if not order.alive():
                self.order = None  # indicate no order is pending
    
        def __init__(self):
            self.ichi = bt.indicators.Ichimoku(self.datas[0],
                                               tenkan=self.params.tenkan,
                                               kijun=self.params.kijun,
                                               senkou=self.params.senkou,
                                               senkou_lead=self.params.senkou_lead,
                                               chikou=self.params.chikou)
    
            # Cross of tenkan and kijun -
            #1.0 if the 1st data crosses the 2nd data upwards - long 
            #-1.0 if the 1st data crosses the 2nd data downwards - short
    
            self.tkcross = bt.indicators.CrossOver(self.ichi.tenkan_sen, self.ichi.kijun_sen)
    
            # To set the stop price
            self.atr = bt.indicators.ATR(self.data, period=self.p.atrperiod)
    
            # Long Short ichimoku logic
            self.long = (self.data.close[0] > self.ichi.senkou_span_a[0])\
            & (self.data.close[0] > self.ichi.senkou_span_b[0])\
            & (self.tkcross == 1)
            
            self.short = (self.data.close[0] < self.ichi.senkou_span_a[0])\
            & (self.data.close[0] < self.ichi.senkou_span_b[0])\
            & (self.tkcross == -1)
    
        def start(self):
            self.order = None  # sentinel to avoid operrations on pending order
    
        def next(self):
            if self.order:
                return  # pending order execution
    
            if not self.position:  # not in the market
                if self.short:
                    self.order = self.sell()
                    ldist = self.atr[0] * self.p.atrdist_x
                    self.lstop = self.data.close[0] + ldist
                    pdist = self.atr[0] * self.p.atrdist_y
                    self.take_profit = self.data.close[0] - pdist
                if self.long:
                    self.order = self.buy()
                    ldist = self.atr[0] * self.p.atrdist_x
                    self.lstop = self.data.close[0] - ldist
                    pdist = self.atr[0] * self.p.atrdist_y
                    self.take_profit = self.data.close[0] + pdist
    
            else:  # in the market
                pclose = self.data.close[0]
                pstop = self.pstop
    
                if  ((pstop<pclose<self.take_profit)|(pstop>pclose>self.take_profit)):
                    self.close()  # Close position
    
    


  • Changed the logic according the doc pages:

    # Long Short ichimoku logic
            self.long = bt.And((self.data.close[0] > self.ichi.senkou_span_a(0)),
            (self.data.close[0] > self.ichi.senkou_span_b(0)),
            (self.tkcross == 1))
            
            self.short = bt.And((self.data.close[0] < self.ichi.senkou_span_a(0)),
            (self.data.close[0] < self.ichi.senkou_span_b(0)),
            (self.tkcross == -1))
    
    

    Seems to have resolved that.


Log in to reply
 

});