Hi,
I have a longshort strategy. It decides whether to long/short based on today's close price and decide the order size based on tomorrow's open price. Sizer is AllInSizer.
I tried to mimic the cheat-on-open.py sample https://www.backtrader.com/docu/cerebro/cheat-on-open/cheat-on-open.html but still, my order sizes are decided by previous close, not open price and I got insufficient margin.
Here is code:
'''
def next(self):
print('{} next, open {} close {}'.format(
self.data.datetime.date(),
self.data.open[0], self.data.close[0])
)
if self.cheating: #look at next_open()
return
self.operate()
def next_open(self):
if not self.cheating: #look at next()
return
#in cheating mode
self.operate()
def operate(self):
# enter into positions
if self.buysell[0]>0:
if self.position.size<0: #close existing short position
self.close()
self.log('Close short, close price {}, size {}'.format(self.dataclose[0],self.order.created.size))
if self.position.size<=0:# Enter into long.
self.order=self.buy()
self.log('BUY CREATE, close price {}, size {}'.format(self.dataclose[0], self.order.created.size))
if self.buysell[0]<0:
if self.position.size >= 0:
self.close()
self.log('Close long, close price {}, size {}'.format(self.dataclose[0],self.order.created.size))
if self.position.size >= 0: # Enter into short.
self.order = self.sell()
self.log('SELL CREATE, close price {}, size {}'.format(self.dataclose[0], self.order.created.size))
'''
Below is part of the main program:
'''
cerebro = bt.Cerebro(stdstats= True,cheat_on_open=True)
cerebro.addstrategy(teststrat,fastn = 5, slown=30)
cerebro.adddata(data)
cerebro.broker.setcash(1000000.0)
cerebro.addsizer(bt.sizers.AllInSizer)
cerebro.broker.setcommission(commission=1/10000)
cerebro.broker.set_slippage_fixed(5)
cerebro.run()
'''
Below are some of the output:
'''
2010-02-12 next, open 41257.33 close 41702.98
2010-02-22 next, open 42979.95 close 42654.28
2010-02-23 next, open 42302.9 close 43031.37
2010-02-24, BUY CREATE, close price 43099.94, size 23.2018884481
2010-02-24, Order Margin
2010-02-24 next, open 42628.57 close 43099.94
2010-02-25 next, open 43117.08 close 42302.9
2010-02-26 next, open 42500.02 close 42568.58
2010-03-01 next, open 43638.31 close 42449.72
2010-03-02 next, open 42212.0 close 40913.04
2010-03-03 next, open 41218.68 close 41227.17
2010-03-04 next, open 41430.93 close 40284.78
2010-03-05 next, open 40395.15 close 40785.69
2010-03-08 next, open 41176.23 close 42110.12
2010-03-09, Close long, close price 41651.66, size 23.2018884481
2010-03-09, SELL CREATE, close price 41651.66, size -24.0086469543
2010-03-09 Sell Executed at price 41858.91
2010-03-09 next, open 41863.91 close 41651.66
2010-03-10, Close short, close price 41694.11, size -24.0086469543
2010-03-10, BUY CREATE, close price 41694.11, size 24.0086469543
2010-03-10 Buy Executed at price 41283.11
2010-03-10 Buy Executed at price 41283.11
2010-03-10, OPERATION PROFIT, GROSS 13824.18, NET 13624.57
2010-03-10 next, open 41278.11 close 41694.11
2010-03-11 next, open 41745.05 close 40904.55
2010-03-12 next, open 41108.31 close 40293.27
2010-03-15 next, open 40293.27 close 38866.96
2010-03-16, Close long, close price 39580.12, size 24.0086469543
2010-03-16, SELL CREATE, close price 39580.12, size -24.0086469543
2010-03-16 Sell Executed at price 38437.47
2010-03-16 Sell Executed at price 38437.47
2010-03-16, OPERATION PROFIT, GROSS -68319.97, NET -68511.36
2010-03-16 next, open 38442.47 close 39580.12
2010-03-17 next, open 39919.72 close 40547.97
2010-03-18 next, open 40259.31 close 40174.41
2010-03-19 next, open 40242.33 close 41091.33
2010-03-22 next, open 40768.71 close 40760.22
2010-03-23 next, open 40853.61 close 40004.62
2010-03-24 next, open 40140.45 close 40327.23
2010-03-25 next, open 40446.09 close 41897.87
2010-03-26 next, open 42279.92 close 42203.51
2010-03-29, Close short, close price 42373.31, size -24.0086469543
2010-03-29, BUY CREATE, close price 42373.31, size 24.0086469543
2010-03-29, Order Margin
2010-03-29 Buy Executed at price 42590.56
2010-03-29, OPERATION PROFIT, GROSS -99710.07, NET -99904.61
'''
My problem is twofold:
-
according to "2010-02-24, BUY CREATE, close price 43099.94, size 23.2018884481
2010-02-24, Order Margin
2010-02-24 next, open 42628.57 close 43099.94" obviously, the program still uses that days' closed price to decide order size, even though I used cheap-on-open option.
-
After a while the order sizes all became fixed at 24.0086469543, even though AllInSizer is used.
I guess it's probably related to there are always 2 orders together, one is close the previous long/short, another one is open new short/long. Then size = available cash/price does not give me the right size. But I don't know exact the reason and solution.
-
lastly, how to round size to an integer without define my own sizer function?
Many thanks for any help.