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

Getting errors one after another

  • I wanted to make a range breakout strategy. Mkt opens at 9:15 IST. I want the algo to calculate the range of 1st 15min bar and buy or sell if the price is above high or low. I have a 1 min data.
    in this code i am getting an IndexError.
    Please help
    @backtrader @ab_trader @run-out

    from __future__ import (absolute_import, division, print_function,
    import backtrader as bt
    import datetime
    # from datetime import datetime
    import os
    import sys
    import backtrader.feeds as btfeed
    import pandas as pd
    class TestStrategy(bt.Strategy):
        def log(self, txt, dt=None):
            dt = dt or
            print('%s, %s' % (dt.isoformat(), txt))
        def __init__(self):
            self.datahigh = self.datas[0].high
            self.datalow = self.datas[0].low
            self.dataclose = self.datas[0].close
            self.order = None
            self.buyprice = None
            self.buycomm = None
            # self.starttime = datetime.replace(hour=9, minute=30, second=0)
            # self.t = self.datas[0].datetime.time(0)
        def notify_order(self, order):
            if order.status in [order.Submitted, order.Accepted]:
                # Buy/Sell order submitted/accepted to/by broker - Nothing to do
            # Check if an order has been completed
            # Attention: broker could reject order if not enough cash
            if order.status in [order.Completed]:
                if order.isbuy():
                        'BUY EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f' %
                    self.buyprice = order.executed.price
                    self.buycomm = order.executed.comm
                else:  # Sell
                    self.log('SELL EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f' %
            elif order.status in [order.Canceled, order.Margin, order.Rejected]:
                self.log('Order Canceled/Margin/Rejected')
            self.order = None
        def next(self):
            if self.order:
            if not self.position:
                if self.dataclose[0] > self.datahigh[-1]:
                    self.log('BUY CREATED, %.2f' % self.dataclose[0])
                    # Keep track of the created order to avoid a 2nd order
                    self.order =
                    if self.dataclose[0] < self.datalow[-1]:
                        self.log('SELL CREATED, %.2f' % self.dataclose[0])
                        # Keep track of the created order to avoid a 2nd order
                        self.order = self.sell()
        def stop(self):
            self.log(' Ending Value %.2f' % (
    if __name__ == '__main__':
        cerebro = bt.Cerebro()
        class DataFeed(btfeed.GenericCSVData):
            params = (
                ('dtformat', '%Y%m%d'),
                ('tmformat', '%H:%M:%S'),
                ('datetime', 0),
                ('time', 1),
                ('open', 2),
                ('high', 3),
                ('low', 4),
                ('close', 5),
                ('volume', 6),
                ('openinterest', 7)
        data = DataFeed(dataname='BANKNIFTYFEB18.csv', timeframe=bt.TimeFrame.Minutes, compression=15,
                        fromdate=datetime.datetime(2017, 2, 1),
                        todate=datetime.datetime(2017, 2, 28))

  • @Abhishek-Agrawal said in Getting errors one after another:

    if self.dataclose[0] > self.datahigh[-1]:

    Although I can't see your error since you didn't include it, I suspect the above line is the problem. You are trying to find currnet and previous bar. On the first bar there is no previous bar. This will throw an error.

    You need to manually skip the first bar in next with a self.counter of some sort.

    def __init__(self):
        self.counter = 0
    def next(self):
        if self.counter == 0:
            self.counter += 1

  • It still doesn't work. Can u connect over teamviewer for some help.

  • Can you include the errors you are getting here so that I or others can help you? Thanks.

Log in to reply