@Nikhil-Morye Ok solved it.
the isue was with the fixer comand of mine.
if any admin please close the tread
@Nikhil-Morye Ok solved it.
the isue was with the fixer comand of mine.
if any admin please close the tread
@Nikhil-Morye Ok solved it.
the isue was with the fixer comand of mine.
if any admin please close the tread
class TestStrategy(bt.Strategy):
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
mask = pd.date_range('2020/06/10 09:20:00', periods=30, freq='D')
z = mask[mask.dayofweek < 5]
def next(self):
if self.datas[0].datetime.datetime(0) in z:
if self.datahigh == self.dataopen:
mainside = self.buy(price=self.dataclose[0], exectype=bt.Order.Limit, transmit=False)
lowside = self.sell(price=self.dataclose[0]*0.95, size=mainside.size, exectype=bt.Order.Stop,transmit=False, parent=mainside)
highside = self.sell(price=self.dataclose[0]*1.15, size=mainside.size, exectype=bt.Order.Limit,transmit=True, parent=mainside)
print(f'On:{self.datas[0].datetime.datetime(0)},Buy at:{self.dataclose[0]},Sell at:{round(self.dataclose*0.95,2)},Stop Loss:{round(self.dataclose*1.05,2)}')
elif self.datalow == self.dataopen:
mainside = self.sell(price=self.dataclose[0], exectype=bt.Order.Limit, transmit=False)
lowside = self.close(price=self.dataclose[0]*1.05, size=mainside.size, exectype=bt.Order.Stop,transmit=False, parent=mainside)
highside = self.close(price=self.dataclose[0]*0.85, size=mainside.size, exectype=bt.Order.Limit,transmit=True, parent=mainside)
print(f'On:{self.datas[0].datetime.datetime(0)},Short at:{self.dataclose[0]},Sell at:{round(self.dataclose*1.05,2)},Stop Loss:{round(self.dataclose*0.95,2)}')
else:
pass
else:
pass
But the order is not being executed
Hey guys, I am trying to implement a strategy which is based on time.
Something like:
Is there some module in backtrader which could help me in it.
Currently, I m slicing the data frame based on time and then feeding it to bt, but I have around 30-40 such stock which I want to screen so it gets difficult.
Is there a workaround for this
Got my way around this peculiar problem with bracket order,
putting the code out if anyone needs it,
the code needs to tweaked around its too big for no reason but hopefully it helps who faced a similar issue like mine.
class TestStrategy(bt.Strategy):
params = dict (buyperiod=1.15,sellperiod=0.95)
def log(self, txt, dt=None):
''' Logging function fot this strategy'''
dt = dt or self.datas[0].datetime.date(0)
print('%s, %s' % (dt.isoformat(), txt))
def __init__(self):
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.marubozu = bt.talib.CDLMARUBOZU(self.dataopen,self.datahigh,self.datalow,self.dataclose)
self.order = None
self.buyprice = None
self.buycomm = None
self.sellprice = None
self.sellcomm = None
def notify_order(self, order):
if order.status in [order.Submitted, order.Accepted]:
return
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
elif order.issell():
#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.sellcomm = order.executed.comm
self.bar_executed = len(self)
elif order.status in [order.Canceled, order.Margin, order.Rejected]:
self.log('Order Canceled/Margin/Rejected')
pass
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('Close, %.2f' % self.dataclose[0])
# Check if an order is pending ... if yes, we cannot send a 2nd one
if self.order:
return
# Check if we are in the market
if not self.position:
if self.marubozu[0] == 100:
self.log('BUY CREATE, limitprice %.2f, price %.2f, stopprice %.2f' %(self.dataopen*1.35,self.dataclose[0],self.datalow[0]))
self.order = self.buy_bracket(limitprice=self.dataopen*self.p.buyperiod, price=self.dataclose, stopprice=self.datalow)
elif self.marubozu[0] == -100:
self.log('Sell CREATE, limitprice %.2f, price %.2f, stopprice %.2f' %(self.datalow*0.65,self.dataclose[0],self.datahigh[0]))
self.order = self.sell_bracket(limitprice=self.datalow*self.p.sellperiod, price=self.dataclose, stopprice=self.datahigh)
cerebro = bt.Cerebro()
cerebro.addstrategy(TestStrategy)
data = bt.feeds.YahooFinanceData(dataname='SBIN.BO',fromdate=datetime(2015, 1, 1),todate=datetime(2020, 6, 30),reversed=True)
cerebro.adddata(data)
cerebro.broker.setcash(1000.0)
cerebro.broker.setcommission(0.001)
print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
cerebro.run()
print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
#cerebro.plot();
Got it my way around it with bracket order. :)
@run-out yes its showing values in form of 0,100,-100
def init(self):
self.marubozu=bt.talib.CDLMORNINGSTAR(self.data.open,self.data.high,
self.data.low, self.data.close, penetration=0.3)
def next(self):
if self.marubozu[0] == 100:
self.buy()
elif self.marubozu[0] == -100:
self.sell()
I will be updating the code to add in stop loss and take profit limit, but that is not the issue.
orders are not being logged at close price with this.
Help will be appreciated.