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

Order execution price



  • Hi,

    I'm seeing a thing I can't explain when backtesting a simple strategy - the order.executed.value does not seem to match with the order.executed.price and order.executed.size.

    See in the output from my strategy elow, the order cost (=value) is the same for two subsequent orders, even though the execution price is different.
    2017-08-17T06:00:00, BUY EXECUTED, Size: 0.10, Price: 4330.29, Cost: 433.03, Comm 1.08, order ref: 1
    2017-08-17T16:00:00, SELL EXECUTED, Size: -0.10, Price: 4352.33, Cost: 433.03, Comm 1.09, order ref: 2
    2017-08-18T02:00:00, BUY EXECUTED, Size: 0.10, Price: 4251.95, Cost: 425.19, Comm 1.06, order ref: 3
    2017-08-18T12:00:00, SELL EXECUTED, Size: -0.10, Price: 4320.52, Cost: 425.19, Comm 1.08, order ref: 4
    2017-08-18T22:00:00, BUY EXECUTED, Size: 0.10, Price: 3996.92, Cost: 399.69, Comm 1.00, order ref: 5
    2017-08-19T08:00:00, SELL EXECUTED, Size: -0.10, Price: 4033.47, Cost: 399.69, Comm 1.01, order ref: 6
    2017-08-19T18:00:00, BUY EXECUTED, Size: 0.10, Price: 4027.37, Cost: 402.74, Comm 1.01, order ref: 7
    2017-08-20T04:00:00, SELL EXECUTED, Size: -0.10, Price: 4094.62, Cost: 402.74, Comm 1.02, order ref: 8
    2017-08-20T14:00:00, BUY EXECUTED, Size: 0.10, Price: 4063.35, Cost: 406.34, Comm 1.02, order ref: 9
    2017-08-21T00:00:00, SELL EXECUTED, Size: -0.10, Price: 4069.13, Cost: 406.34, Comm 1.02, order ref: 10

    The trade pnl is computed correctly though. Anybody can explain this to me?

    Here's my simple sample strategy, it simply buys every 20 bars and sells the same stake 10 bars later:

    `class StupidStrategy(bt.Strategy):
    def init(self):
    super(StupidStrategy, self).init()

    def next(self):
    	super(StupidStrategy, self).next()
    	if len(self.datas[0]) % 20 == 1:
    		self.buy(exectype=bt.Order.Market, size=0.1)
    	elif len(self.datas[0]) % 20 == 11:
    		self.sell(exectype=bt.Order.Market, size=0.1)
    
    def notify_order(self, order):
    	super(StupidStrategy, self).next()
    	if order.status in [order.Submitted, order.Accepted, order.Partial]:
    		# Buy/Sell order submitted/accepted to/by broker/partially filled - Nothing to do
    		return
    
    	# Check if an order has been completed
    	if order.status in [order.Completed]:
    		if order.isbuy():
    			self.log('BUY EXECUTED, Size: %.2f, Price: %.2f, Cost: %.2f, Comm %.2f, order ref: %d' %(order.executed.size, order.executed.price, order.executed.value, order.executed.comm, order.ref))
    		elif order.issell():
    			if order.exectype in (bt.Order.Stop, bt.Order.StopLimit, bt.Order.StopTrail, bt.Order.StopTrailLimit):
    				self.log('STOPLOSS EXECUTED, Size: %.2f, Price: %.2f, Cost: %.2f, Comm %.2f, order ref: %d' %(order.executed.size, order.executed.price, order.executed.value, order.executed.comm, order.ref))
    			else:
    				self.log('SELL EXECUTED, Size: %.2f, Price: %.2f, Cost: %.2f, Comm %.2f, order ref: %d' %(order.executed.size, order.executed.price, order.executed.value, order.executed.comm, order.ref))
    
    	elif order.status in [order.Canceled, order.Margin, order.Rejected]:
    		self.log('Order of type %s Canceled/Margin/Rejected'%(order.getordername()))
    

    `

    Thanks!
    T.



  • @tomasrollo I'm not super sure but according to the docs it seems to be the expected behavior:

    price: execution price Note: if no price is given and no pricelimite is given, the closing price at the time or order creation will be used as reference

    what you might want to do is to use the notify_trade() method and check trade.value.



  • The execution price is correctly reflecting the current bar's close, that's fine, the problem is in the execution value.

    E.g. for the 2nd order:
    executed.value = executed.price * executed.size = 4352.33 * 0.1 = 435.23
    but the order.executed.value reported is 433.03


  • administrators

    Because value is telling you the value that you are recovering from the existing position. The profit and loss (which can increase or decrease the value) and the commission (which unless you are the broker, will decrease the value) have to be added.

    You are probably better off checking the Trade



  • OK, @backtrader , I understand it now, I'll try to work with the trade instead, thanks!

    T.