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

Replay data behaving weird?



  • I want to generate singals based on daily timeframe but enter based on 30 min timeframe.
    Below is code and output. It is not as expected or specified in documents.
    Prenext funtion is never called. What i am doing wrong.

    import backtrader as bt
    from backtrader import Order
    import matplotlib as plt
    import datetime
    import pytz
    import ta
    import backtrader.feeds as btfeeds

    class IntraTrendStrategy(bt.Strategy):

    def log(self, txt, dt=None):
        ''' Logging function fot this strategy'''
        dt = dt or self.datas[0].datetime.datetime(0)
        print('%s, %s' % (dt.isoformat(), txt))
    
    def __init__(self):
        pass
    
    def start(self):
        self.counter = 0
    
    def prenext(self):
        self.counter += 1
        self.log('prenext len %d - counter %d' % (len(self), self.counter))
    
    def next(self):
        self.counter += 1
        self.log('---next len %d - counter %d' % (len(self), self.counter))
    

    stockkwargs = dict(
    timeframe=bt.TimeFrame.Days,
    compression=1,
    historical=True, # only historical download
    fromdate=datetime.datetime(2020, 9, 1), # get data from..
    todate=datetime.datetime(2020, 9, 13) # get data from..
    )

    cerebro = bt.Cerebro(stdstats=False)
    store = bt.stores.IBStore(port=7496,clientId=5)
    data = store.getdata(dataname='TCS-STK-NSE-INR',**stockkwargs)
    cerebro.replaydata(data,timeframe=bt.TimeFrame.Minutes,compression=30)
    cerebro.addstrategy(IntraTrendStrategy)
    cerebro.run()

    ----------------------------- Output------------------------:
    2020-09-01T03:45:00, ---next len 1 - counter 1
    2020-09-01T04:00:00, ---next len 1 - counter 2
    2020-09-01T04:30:00, ---next len 2 - counter 3
    2020-09-01T05:00:00, ---next len 3 - counter 4
    2020-09-01T05:30:00, ---next len 4 - counter 5
    2020-09-01T06:00:00, ---next len 5 - counter 6
    2020-09-01T06:30:00, ---next len 6 - counter 7
    2020-09-01T07:00:00, ---next len 7 - counter 8
    2020-09-01T07:30:00, ---next len 8 - counter 9
    2020-09-01T08:00:00, ---next len 9 - counter 10
    2020-09-01T08:30:00, ---next len 10 - counter 11
    2020-09-01T09:00:00, ---next len 11 - counter 12
    2020-09-01T09:30:00, ---next len 12 - counter 13
    2020-09-02T03:45:00, ---next len 13 - counter 14
    2020-09-02T04:00:00, ---next len 13 - counter 15
    2020-09-02T04:30:00, ---next len 14 - counter 16
    2020-09-02T05:00:00, ---next len 15 - counter 17
    2020-09-02T05:30:00, ---next len 16 - counter 18
    2020-09-02T06:00:00, ---next len 17 - counter 19
    2020-09-02T06:30:00, ---next len 18 - counter 20
    2020-09-02T07:00:00, ---next len 19 - counter 21
    2020-09-02T07:30:00, ---next len 20 - counter 22
    2020-09-02T08:00:00, ---next len 21 - counter 23
    2020-09-02T08:30:00, ---next len 22 - counter 24
    2020-09-02T09:00:00, ---next len 23 - counter 25
    2020-09-02T09:30:00, ---next len 24 - counter 26
    2020-09-03T03:45:00, ---next len 25 - counter 27
    2020-09-03T04:00:00, ---next len 25 - counter 28
    2020-09-03T04:30:00, ---next len 26 - counter 29
    2020-09-03T05:00:00, ---next len 27 - counter 30
    2020-09-03T05:30:00, ---next len 28 - counter 31
    2020-09-03T06:00:00, ---next len 29 - counter 32
    2020-09-03T06:30:00, ---next len 30 - counter 33
    2020-09-03T07:00:00, ---next len 31 - counter 34
    2020-09-03T07:30:00, ---next len 32 - counter 35
    2020-09-03T08:00:00, ---next len 33 - counter 36
    2020-09-03T08:30:00, ---next len 34 - counter 37
    2020-09-03T09:00:00, ---next len 35 - counter 38
    2020-09-03T09:30:00, ---next len 36 - counter 39
    2020-09-04T03:45:00, ---next len 37 - counter 40
    2020-09-04T04:00:00, ---next len 37 - counter 41
    2020-09-04T04:30:00, ---next len 38 - counter 42
    2020-09-04T05:00:00, ---next len 39 - counter 43
    2020-09-04T05:30:00, ---next len 40 - counter 44
    2020-09-04T06:00:00, ---next len 41 - counter 45
    2020-09-04T06:30:00, ---next len 42 - counter 46
    2020-09-04T07:00:00, ---next len 43 - counter 47
    2020-09-04T07:30:00, ---next len 44 - counter 48
    2020-09-04T08:00:00, ---next len 45 - counter 49
    2020-09-04T08:30:00, ---next len 46 - counter 50
    2020-09-04T09:00:00, ---next len 47 - counter 51
    2020-09-04T09:30:00, ---next len 48 - counter 52
    2020-09-07T03:45:00, ---next len 49 - counter 53
    2020-09-07T04:00:00, ---next len 49 - counter 54
    2020-09-07T04:30:00, ---next len 50 - counter 55
    2020-09-07T05:00:00, ---next len 51 - counter 56
    2020-09-07T05:30:00, ---next len 52 - counter 57
    2020-09-07T06:00:00, ---next len 53 - counter 58
    2020-09-07T06:30:00, ---next len 54 - counter 59
    2020-09-07T07:00:00, ---next len 55 - counter 60
    2020-09-07T07:30:00, ---next len 56 - counter 61
    2020-09-07T08:00:00, ---next len 57 - counter 62
    2020-09-07T08:30:00, ---next len 58 - counter 63
    2020-09-07T09:00:00, ---next len 59 - counter 64
    2020-09-07T09:30:00, ---next len 60 - counter 65
    2020-09-08T03:45:00, ---next len 61 - counter 66
    2020-09-08T04:00:00, ---next len 61 - counter 67
    2020-09-08T04:30:00, ---next len 62 - counter 68
    2020-09-08T05:00:00, ---next len 63 - counter 69
    2020-09-08T05:30:00, ---next len 64 - counter 70
    2020-09-08T06:00:00, ---next len 65 - counter 71
    2020-09-08T06:30:00, ---next len 66 - counter 72
    2020-09-08T07:00:00, ---next len 67 - counter 73
    2020-09-08T07:30:00, ---next len 68 - counter 74
    2020-09-08T08:00:00, ---next len 69 - counter 75
    2020-09-08T08:30:00, ---next len 70 - counter 76
    2020-09-08T09:00:00, ---next len 71 - counter 77
    2020-09-08T09:30:00, ---next len 72 - counter 78
    2020-09-09T03:45:00, ---next len 73 - counter 79
    2020-09-09T04:00:00, ---next len 73 - counter 80
    2020-09-09T04:30:00, ---next len 74 - counter 81
    2020-09-09T05:00:00, ---next len 75 - counter 82
    2020-09-09T05:30:00, ---next len 76 - counter 83
    2020-09-09T06:00:00, ---next len 77 - counter 84
    2020-09-09T06:30:00, ---next len 78 - counter 85
    2020-09-09T07:00:00, ---next len 79 - counter 86
    2020-09-09T07:30:00, ---next len 80 - counter 87
    2020-09-09T08:00:00, ---next len 81 - counter 88
    2020-09-09T08:30:00, ---next len 82 - counter 89
    2020-09-09T09:00:00, ---next len 83 - counter 90
    2020-09-09T09:30:00, ---next len 84 - counter 91
    2020-09-10T03:45:00, ---next len 85 - counter 92
    2020-09-10T04:00:00, ---next len 85 - counter 93
    2020-09-10T04:30:00, ---next len 86 - counter 94
    2020-09-10T05:00:00, ---next len 87 - counter 95
    2020-09-10T05:30:00, ---next len 88 - counter 96
    2020-09-10T06:00:00, ---next len 89 - counter 97
    2020-09-10T06:30:00, ---next len 90 - counter 98
    2020-09-10T07:00:00, ---next len 91 - counter 99
    2020-09-10T07:30:00, ---next len 92 - counter 100
    2020-09-10T08:00:00, ---next len 93 - counter 101
    2020-09-10T08:30:00, ---next len 94 - counter 102
    2020-09-10T09:00:00, ---next len 95 - counter 103
    2020-09-10T09:30:00, ---next len 96 - counter 104
    2020-09-11T03:45:00, ---next len 97 - counter 105
    2020-09-11T04:00:00, ---next len 97 - counter 106
    2020-09-11T04:30:00, ---next len 98 - counter 107
    2020-09-11T05:00:00, ---next len 99 - counter 108
    2020-09-11T05:30:00, ---next len 100 - counter 109
    2020-09-11T06:00:00, ---next len 101 - counter 110
    2020-09-11T06:30:00, ---next len 102 - counter 111
    2020-09-11T07:00:00, ---next len 103 - counter 112
    2020-09-11T07:30:00, ---next len 104 - counter 113
    2020-09-11T08:00:00, ---next len 105 - counter 114
    2020-09-11T08:30:00, ---next len 106 - counter 115
    2020-09-11T09:00:00, ---next len 107 - counter 116
    2020-09-11T09:30:00, ---next len 108 - counter 117


Log in to reply
 

});