Nobody using this feature? no one facing this issue?
Latest posts made by Ravi Vidap
-
RE: Replay data not working at all with Interactive brokers
-
Replay data not working at all with Interactive brokers
Replay data looping through only Higher TimeFrame completely ignoring lower TimeFrame.
I am ready to pay to solve this issue. I have posted this issue so many times now. Please help
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.Minutes, compression=15, 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.Days,compression=1) cerebro.addstrategy(IntraTrendStrategy) cerebro.run()
Output
2020-09-01T23:59:59.999989, ---next len 1 - counter 1
2020-09-02T23:59:59.999989, ---next len 2 - counter 2
2020-09-03T23:59:59.999989, ---next len 3 - counter 3
2020-09-04T23:59:59.999989, ---next len 4 - counter 4
2020-09-07T23:59:59.999989, ---next len 5 - counter 5
2020-09-08T23:59:59.999989, ---next len 6 - counter 6
2020-09-09T23:59:59.999989, ---next len 7 - counter 7
2020-09-10T23:59:59.999989, ---next len 8 - counter 8
2020-09-11T23:59:59.999989, ---next len 9 - counter 9 -
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 btfeedsclass 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 -
ReplayData not working
cerebro.adddata(data) is working in below code, but when I replace that call with cerebro.replaydata(data,timeframe=bt.TimeFrame.Days,compression=1)
Its not working, no errors also.
Code Snippet:
stockkwargs = dict(
timeframe=bt.TimeFrame.Minutes,
compression=5,
historical=True, # only historical download
fromdate=datetime.datetime(2020, 9, 1), # get data from..
todate=datetime.datetime(2020, 9, 3) # get data from..
)cerebro = bt.Cerebro(stdstats=False)
store = bt.stores.IBStore(port=7496)
data = store.getdata(dataname='TCS-STK-NSE-INR',**stockkwargs)
cerebro.replaydata(data,timeframe=bt.TimeFrame.Days,compression=1)
cerebro.addstrategy(IntraTrendStrategy)
cerebro.run() -
Exception has occurred: ValueError day is out of range for month File "F:\IBBackTradeLiveDataEx.py", line 165, in <module> cerebro.run()
Getting above error. Trying to replay data, higherTF=Daily, LowerTimeFrame=5 min
Code below:from future import absolute_import, division, print_function, unicode_literals
import backtrader as bt
from backtrader import Order
import matplotlib as plt
import datetime
import pytz
import tacerebro = bt.Cerebro()
store = bt.stores.IBStore(host="127.0.0.1", port=7496, clientId= 4)
cerebro.addstrategy(IntraTrendStrategy)
stockkwargs = dict(
timeframe=bt.TimeFrame.Minutes,
compression=5,
rtbar=False, # use RealTime 5 seconds bars
historical=True, # only historical download
qcheck=0.5, # timeout in seconds (float) to check for events
fromdate=datetime.datetime(2020, 1, 1), # get data from..
todate=datetime.datetime(2020, 9, 20), # get data from..
latethrough=False, # let late samples through
tradename=None, # use a different asset as order target
tz="Asia/Kolkata"
)data0 = store.getdata(dataname="TCS-STK-NSE-INR", **stockkwargs)
cerebro.replaydata(data0, timeframe=bt.TimeFrame.Days, compression=1)
cerebro.broker.setcash(100000.0)
cerebro.broker.setcommission(commission=0.001)
print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())cerebro.run()
print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
-
RE: Lowest close of n bars throws error
Thanks for reply! Link was useful and I could solve my problem.
-
RE: Exception has occurred: ZeroDivisionError float division by zero
Please ignore I was able to solve it
-
Lowest close of n bars throws error
I am trying to find out if current bar close is lowest of say previous 15 bars and below is my code :
self.dataclose[0] == self.dataclose[-15:].min()
But I am getting following error:TypeError: unsupported operand type(s) for +: 'int' and 'slice' -
Exception has occurred: ZeroDivisionError float division by zero
Hi, When I try to add rsi to my intraday strategy , I am getting below error:
Exception has occurred: ZeroDivisionError float division by zero
File "F:\Learn\NR7\IBBackTradeLiveDataEx.py", line 160, in <module> cerebro.run()
What might be going wrong?#!/usr/bin/env python
-- coding: utf-8; py-indent-offset:4 --e
from future import absolute_import, division, print_function, unicode_literals
import backtrader as bt
from backtrader import Order
import matplotlib as plt
import datetime
import pytzclass 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): # Keep a reference to the "close" line in the data[0] dataseries self.dataclose = self.datas[0].close self.dataopen = self.datas[0].open self.datahigh = self.datas[0].high self.datalow = self.datas[0].low self.rsi = bt.indicators.RSI_SMA(self.dataclose, period=5) # To keep track of pending orders and buy price/commission self.order = None self.buyprice = None self.sellprice = None self.buycomm = None self.daylow = None self.dayhigh = None self.long = None def notify_order(self, order): if order.status in [order.Submitted, order.Accepted]: # Buy/Sell order submitted/accepted to/by broker - Nothing to do return # 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(): self.log( 'BUY EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f' % (order.executed.price, order.executed.value, order.executed.comm)) self.buyprice = order.executed.price self.buycomm = order.executed.comm else: # Sell self.log('SELL EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f' % (order.executed.price, order.executed.value, order.executed.comm)) self.sellprice = order.executed.price self.bar_executed = len(self) elif order.status in [order.Canceled, order.Margin, order.Rejected]: self.log('Order Canceled/Margin/Rejected') self.order = None def notify_trade(self, trade): if not trade.isclosed: return self.log('OPERATION PROFIT, GROSS %.2f, NET %.2f' % (trade.pnl, trade.pnlcomm)) def next(self): # Simply log the closing price of the series from the reference self.log('%d Close, %.2f' % (len(self), self.dataclose[0])) if(self.data.datetime.time(0) == datetime.time(9,20)): self.daylow = self.datalow[0] self.dayhigh = self.datahigh[0] # Check if an order is pending ... if yes, we cannot send a 2nd one if self.order: if(self.data.datetime.time(0) == datetime.time(3,25)): self.order = None return # Check if we are in the market if not self.position: #add another condition as candle should be bullish if (self.data.datetime.time(0) == datetime.time(9,25) and self.rsi[0] < 50 and self.datalow[0] > self.dataopen[-1]): self.log('BUY CREATE, %.2f' % self.dataclose[0]) # Keep track of the created order to avoid a 2nd order #self.order = self.buy(exectype=Order.StopLimit, price=self.datahigh[0], valid=self.datas[0].datetime.date(0) + datetime.timedelta(days=3)) self.order = self.buy(size=1) self.long = True if (self.data.datetime.time(0) == datetime.time(9,25) and self.datahigh[0] < self.dataopen[-1]): self.log('SELL CREATE, %.2f' % self.dataclose[0]) # Keep track of the created order to avoid a 2nd order #self.order = self.buy(exectype=Order.StopLimit, price=self.datahigh[0], valid=self.datas[0].datetime.date(0) + datetime.timedelta(days=3)) self.order = self.sell(size=1) self.long = False else: # Already in the market ... we might sell if(self.long): target = self.buyprice + ((self.buyprice - self.daylow) * 2) if (self.data.datetime.time(0) == datetime.time(15,25) or self.dataclose[0]>=target or self.datalow[0] < self.daylow): # SELL, SELL, SELL!!! (with all possible default parameters) self.log('SELL CREATE, %.2f' % self.dataclose[0]) # Keep track of the created order to avoid a 2nd order self.order = self.sell(size=1) else: target = self.sellprice - ((self.dayhigh-self.sellprice) * 2) if (self.data.datetime.time(0) == datetime.time(15,25) or self.dataclose[0]<=target or self.datahigh[0] > self.dayhigh): # SELL, SELL, SELL!!! (with all possible default parameters) self.log('BUY CREATE, %.2f' % self.dataclose[0]) # Keep track of the created order to avoid a 2nd order self.order = self.buy(size=1)
Create a cerebro entity
cerebro = bt.Cerebro()
store = bt.stores.IBStore(host="127.0.0.1", port=7496, clientId= 4)
Add a strategy
cerebro.addstrategy(IntraTrendStrategy)
stockkwargs = dict(
timeframe=bt.TimeFrame.Minutes,
rtbar=False, # use RealTime 5 seconds bars
historical=True, # only historical download
qcheck=0.5, # timeout in seconds (float) to check for events
fromdate=datetime.datetime(2020, 1, 1), # get data from..
todate=datetime.datetime(2020, 9, 20), # get data from..
latethrough=False, # let late samples through
tradename=None, # use a different asset as order target
tz="Asia/Kolkata"
)data0 = store.getdata(dataname="TCS-STK-NSE-INR", **stockkwargs)
cerebro.resampledata(data0, timeframe=bt.TimeFrame.Minutes, compression=5)Set our desired cash start
cerebro.broker.setcash(100000.0)
Set the commission - 0.1% ... divide by 100 to remove the %
cerebro.broker.setcommission(commission=0.001)
Print out the starting conditions
print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
Run over everything
cerebro.run()
Print out the final result
print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
#cerebro.plot(style='candlestick')