For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
Cheat_on_open true: how to execute the order on another price than the next open price
-
For now my code looks like this:
class TestStrategy(bt.Strategy): params = ( ('maperiod', 60), ) def log(self, txt, dt=None): ''' Logging function for this strategy''' dt = dt or self.datas[0].datetime.date(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.data_cpa = self.datas[0].high self.data_cpb = self.datas[0].low self.order = None self.buyprice = None self.buycomm = None self.sma = bt.indicators.SimpleMovingAverage( self.datas[0], period=self.params.maperiod) 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, %.8f' % order.executed.price) elif order.issell(): self.log('SELL EXECUTED, %.8f' % order.executed.price) self.bar_executed = 0 elif order.status in [order.Canceled, order.Margin, order.Rejected]: self.log('Order Canceled/Margin/Rejected') # Write down: no pending order self.order = None def notify_trade(self, trade): if not trade.isclosed: return self.log('OPERATION PROFIT, GROSS %.8f, NET %.f' % (trade.pnl, trade.pnlcomm)) def next(self): # Simply log the closing price of the series from the reference self.log('Close, %.8f' % self.dataclose[0]) if self.order: return # Check if we are in the market if not self.position: if self.dataclose[0] > self.sma[0]: # BUY, BUY, BUY!!! (with all possible default parameters) self.log('BUY CREATE, %.8f' % self.dataclose[0]) # Keep track of the created order to avoid a 2nd order self.order = self.buy(price= self.dataclose[0]) else: if self.dataclose[0] < self.sma[0]: # SELL, SELL, SELL!!! (with all possible default parameters) self.log('SELL CREATE, %.8f' % self.dataclose[0]) # Keep track of the created order to avoid a 2nd order self.order = self.sell(price= self.dataclose[0]) if __name__ == '__main__': # Create a cerebro entity cerebro = bt.Cerebro(cheat_on_open=True) # Add a strategy cerebro.addstrategy(TestStrategy) # retrieve the csv file modpath = os.path.dirname(os.path.abspath(sys.argv[0])) datapath = os.path.join(modpath, './datas/zrxeth_sample.txt') # Create a Data Feed data = bt.feeds.GenericCSVData( dataname = datapath, fromdate=datetime.datetime(2018, 9, 28), todate=datetime.datetime(2018, 12, 3), nullvalue=0.0, dtformat=('%Y-%m-%d %H:%M:%S'), datetime=0, high=1, low=2, open=3, close=4, volume=5, openinterest=-1 ) # Add the Data Feed to Cerebro cerebro.adddata(data) # Set our desired cash start cerebro.broker.setcash(100.0) # Print out the starting conditions print('Starting Portfolio Value: %.8f' % cerebro.broker.getvalue()) # Run over everything cerebro.run() # Print out the final result print('Final Portfolio Value: %.8f' % cerebro.broker.getvalue())
From my understanding of the library, when you use the parameter cheat_on_open=True, you can authorize the order the order to be executed directly and not the day after. However, the executed order takes as price the next open price, how is it possible to choose which price the order will apply? Thanks!
-