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

Retrieving Executed Prices



  • Hello,
    I am running into an issue and was hoping if anyone could help. I have a bracket order running which makes the long/short orders per the condition. When I reference my object in notify_order as self.exec_price = order.executed.price, and print the executed prices it returns the executed prices of the stop loss or take profit orders not the price where it was originally bought/sold from. So, what way could I retrieve the first executed price?
    Code below:

    def notify_order(self, order):
    		date = self.data.datetime.datetime().date()
    		ep = order.executed.price
    		es = order.executed.size
    		tv = ep * es
    		leverage = self.broker.getcommissioninfo(self.data).get_leverage()
    		self.margin = abs((ep * es) / leverage)
    
    		if order.status == order.Accepted:
    			print('-' * 32, ' NOTIFY ORDER ', '-' * 32)
    			print('{} Order Accepted'.format(order.info['name']))
    			print('{}, Status {}: Ref: {}, Size: {}, Price: {}'.format(
    				date,
    				order.status,
    				order.ref,
    				round(order.size, 2),
    				'NA' if not order.price else round(order.price,5)
    			))
    
    		if order.status == order.Completed:
    			self.exec_price = round(order.executed.price,5)
    			print('-' * 32, ' NOTIFY ORDER ', '-' * 32)
    			print('{} Order Completed'.format(order.info['name']))
    			print('{}, Status {}: Ref: {}, Size: {}, Price: {}'.format(
    				date,
    				order.status,
    				order.ref,
    				round(order.size, 2),
    				round(order.executed.price, 5)
    			))
    			print('-' * 80)

  • administrators

    An order in backtrader and in any broker will only give the execution price of that order and not the price at which you bought/sold something. Because you can buy 100 and then 50 (at different prices) and then sell 125. The action with 125 is fully independent of the previous actions.

    You have to

    • Record the execution price of the main order of the bracket


  • On an another note, I observed that my accepted orders (stop loss and take profit) and executed orders are the same in some cases but in other cases the difference is pretty big. I am testing the TestStrategy on Forex data and the difference in pips is what concerns me. I am familiar with the fact that backtrader tries to fill in the best price, but is there anyway to optimize it so it doesn't make that huge of a difference in the prices.

    def next(self):
    
          if self.order:
          	None
          
          	if not self.position:
                    #stop order: round(self.atr[0] * 1.5,4)
          			
          		long_sl = self.dataclose - stop_order
          		long_tp = self.dataclose + profit_order
          		short_sl = self.dataclose + stop_order
          		short_tp = self.dataclose - profit_order
          
          		if self.trade_sig > 0:
          			buy_ord = self.buy(size = units, exectype = bt.Order.Market, transmit = False)
    
          			buy_ord.addinfo(name = 'BUY')
    
          			buy_ord_sl = self.sell(price = long_sl, size = buy_ord.size, exectype = bt.Order.Stop, transmit = False, parent = buy_ord)
    
          			buy_ord_sl.addinfo(name = 'BUY SL')
    
          			buy_ord_tp = self.sell(price = long_tp, size = buy_ord.size, exectype = bt.Order.Limit, transmit = True, parent = buy_ord)
    
          			buy_ord_tp.addinfo(name = 'BUY TP')
          
          				
          
          		elif self.trade_sig < 0:
          			sell_ord = self.sell(size = units, exectype = bt.Order.Market, transmit = False)
    
          			sell_ord.addinfo(name = 'SELL')
    
          			sell_ord_sl = self.buy(price = short_sl, size = sell_ord.size, exectype = bt.Order.Stop, transmit = False, parent = sell_ord)
    
          			sell_ord_sl.addinfo(name = 'SELL SL')
    
          			sell_ord_tp = self.buy(price = short_tp, size = sell_ord.size, exectype = bt.Order.Stop, transmit = True, parent = sell_ord)
    
          			sell_ord_tp.addinfo(name = 'SELL TP')
    
    
    #Result---------------------
    SELL Order Accepted
    2017-03-30, Status 2: Ref: 4, Size: -254760.76, Price: NA
    --------------------------------  NOTIFY ORDER  --------------------------------
    SELL SL Order Accepted
    2017-03-30, Status 2: Ref: 5, Size: 254760.76, Price: 1.07902
    --------------------------------  NOTIFY ORDER  --------------------------------
    SELL TP Order Accepted
    2017-03-30, Status 2: Ref: 6, Size: 254760.76, Price: 1.05602 
    --------------------------------  NOTIFY ORDER  --------------------------------
    SELL Order Completed
    2017-03-30, Status 4: Ref: 4, Size: -254760.76, Price: 1.06699
    --------------------------------------------------------------------------------
    --------------------------------  NOTIFY ORDER  --------------------------------
    SELL TP Order Completed
    2017-04-02, Status 4: Ref: 6, Size: 254760.76, Price: 1.06719 
    #difference in the set TP: 109.7 pips
    --------------------------------------------------------------------------------
    --------------------------------  NOTIFY ORDER  --------------------------------
    SELL SL Order Cancelled
    2017-04-02, Status 5: Ref: 5, Size: 254760.76022372086, Price: 1.07902
    --------------------------------------------------------------------------------
    --------------------------------  NOTIFY TRADE  --------------------------------
    2017-04-02 00:00:00, Close Price: 1.06699, Profit, Gross -52.76, Net -98.82

  • administrators

    @robin-dhillon said in Retrieving Executed Prices:

    I observed that my accepted orders (stop loss and take profit) and executed orders are the same in some cases but in other cases the difference is pretty big

    And what is your understanding of pretty big? Where are the logs, the associated data? Why should the matched price be any other and not the one which is returned?

    @robin-dhillon said in Retrieving Executed Prices:

    I am familiar with the fact that backtrader tries to fill in the best price

    You are not. backtrader tries it best to give you a realistic price, with the constraints inherent to the nature of the data (ohlc format, timeframe). It's not the best price, it is the price which it can at best be delivered.

    @robin-dhillon said in Retrieving Executed Prices:

    but is there anyway to optimize it so it doesn't make that huge of a difference in the prices.

    If you want to cheat yourself, you can, because that's the translation of optimize. You may try Slippage, but that will affect all orders and the ones you like know will be worse than they are.


Log in to reply
 

});