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

notify_trade not firing on subsequent trades when using tradeid?



  • Hi all,

    I've been experimenting with multiple parallel trades using tradeid and it seems that the strategy.notify_trade method is not firing for cases when there's a new order added to an existing trade?

    I'm using a sample strategy that simply buys and sells in waves of 10 buy/sell orders, which are distributed to 5 different trades, so at the end every trade consist of a BUY(1) BUY(2) SELL(1) SELL(2) sequence of orders. The sizer is the FixedSizer(stake=0.01).

    I have two issues:

    1. I'm not getting the notify_trade to fire during the BUY(2) and SELL(1) orders, it's only fired during the opening BUY(1) and closing SELL(2) orders
    2. after a while, the notify_trade does not even fire for the opening BUY(1) trade (see below at 2017-08-18T03:00:00 in the log)

    The strategy:

    import backtrader as bt
    
    class SampleStrategy(bt.Strategy):
    	def log(self, txt, dt=None):
    		''' Logging function for this strategy'''
    		dt = dt or self.datas[0].datetime.datetime(0)
    		print('%s, %s' % (dt.isoformat(), txt))
    
    	def __init__(self):
    		self.livetrades = {}
    		self.counter = 0
    
    	def notify_order(self, order):
    		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, Price: %.2f, Cost: %.2f, Comm %.2f, tradeID: %d' %(order.executed.price, order.executed.value, order.executed.comm, order.tradeid))
    			elif order.issell():
    				self.log('SELL EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f, tradeID: %d' %(order.executed.price, order.executed.value, order.executed.comm, order.tradeid))
    
    		elif order.status in [order.Canceled, order.Margin, order.Rejected]:
    			self.log('Order Canceled/Margin/Rejected')
    
    	def notify_trade(self, trade):
    		if trade.justopened:
    			self.log('New trade just opened with id %d'%(trade.tradeid))
    			self.livetrades[trade.tradeid] = trade
    		elif trade.isclosed:
    			self.log('A trade just closed with id %d'%(trade.tradeid))
    			self.livetrades.pop(trade.tradeid)
    		else:
    			self.log('Trade id %d updated'%(trade.tradeid))
    
    	def next(self):
    		self.log('______Starting NEXT()______')
    
    		if 0 <= self.counter % 20 < 10:
    			self.buy(tradeid=self.counter%5)
    		else:
    			self.sell(tradeid=self.counter%5)
    		self.counter += 1
    
    		self.log('Currently %d trades open with ids=%s'%(len(self.livetrades), ','.join([str(k) for k in self.livetrades.keys()])))
    

    Here's the log:

    Starting Portfolio Value: 100000.00
    2017-08-17T06:00:00, ______Starting NEXT()______
    2017-08-17T06:00:00, Currently 0 trades open with ids=
    2017-08-17T07:00:00, BUY EXECUTED, Price: 4330.29, Cost: 43.30, Comm 0.11, tradeID: 0
    2017-08-17T07:00:00, New trade just opened with id 0
    2017-08-17T07:00:00, ______Starting NEXT()______
    2017-08-17T07:00:00, Currently 1 trades open with ids=0
    2017-08-17T08:00:00, BUY EXECUTED, Price: 4324.35, Cost: 43.24, Comm 0.11, tradeID: 1
    2017-08-17T08:00:00, New trade just opened with id 1
    2017-08-17T08:00:00, ______Starting NEXT()______
    2017-08-17T08:00:00, Currently 2 trades open with ids=0,1
    2017-08-17T09:00:00, BUY EXECUTED, Price: 4333.32, Cost: 43.33, Comm 0.11, tradeID: 2
    2017-08-17T09:00:00, New trade just opened with id 2
    2017-08-17T09:00:00, ______Starting NEXT()______
    2017-08-17T09:00:00, Currently 3 trades open with ids=0,1,2
    2017-08-17T10:00:00, BUY EXECUTED, Price: 4360.69, Cost: 43.61, Comm 0.11, tradeID: 3
    2017-08-17T10:00:00, New trade just opened with id 3
    2017-08-17T10:00:00, ______Starting NEXT()______
    2017-08-17T10:00:00, Currently 4 trades open with ids=0,1,2,3
    2017-08-17T11:00:00, BUY EXECUTED, Price: 4441.10, Cost: 44.41, Comm 0.11, tradeID: 4
    2017-08-17T11:00:00, New trade just opened with id 4
    2017-08-17T11:00:00, ______Starting NEXT()______
    2017-08-17T11:00:00, Currently 5 trades open with ids=0,1,2,3,4
    2017-08-17T12:00:00, BUY EXECUTED, Price: 4460.00, Cost: 44.60, Comm 0.11, tradeID: 0
    2017-08-17T12:00:00, ______Starting NEXT()______
    2017-08-17T12:00:00, Currently 5 trades open with ids=0,1,2,3,4
    2017-08-17T13:00:00, BUY EXECUTED, Price: 4436.06, Cost: 44.36, Comm 0.11, tradeID: 1
    2017-08-17T13:00:00, ______Starting NEXT()______
    2017-08-17T13:00:00, Currently 5 trades open with ids=0,1,2,3,4
    2017-08-17T14:00:00, BUY EXECUTED, Price: 4411.00, Cost: 44.11, Comm 0.11, tradeID: 2
    2017-08-17T14:00:00, ______Starting NEXT()______
    2017-08-17T14:00:00, Currently 5 trades open with ids=0,1,2,3,4
    2017-08-17T15:00:00, BUY EXECUTED, Price: 4459.00, Cost: 44.59, Comm 0.11, tradeID: 3
    2017-08-17T15:00:00, ______Starting NEXT()______
    2017-08-17T15:00:00, Currently 5 trades open with ids=0,1,2,3,4
    2017-08-17T16:00:00, BUY EXECUTED, Price: 4470.82, Cost: 44.71, Comm 0.11, tradeID: 4
    2017-08-17T16:00:00, ______Starting NEXT()______
    2017-08-17T16:00:00, Currently 5 trades open with ids=0,1,2,3,4
    2017-08-17T17:00:00, SELL EXECUTED, Price: 4352.33, Cost: 44.03, Comm 0.11, tradeID: 0
    2017-08-17T17:00:00, ______Starting NEXT()______
    2017-08-17T17:00:00, Currently 5 trades open with ids=0,1,2,3,4
    2017-08-17T18:00:00, SELL EXECUTED, Price: 4354.18, Cost: 44.03, Comm 0.11, tradeID: 1
    2017-08-17T18:00:00, ______Starting NEXT()______
    2017-08-17T18:00:00, Currently 5 trades open with ids=0,1,2,3,4
    2017-08-17T19:00:00, SELL EXECUTED, Price: 4289.24, Cost: 44.03, Comm 0.11, tradeID: 2
    2017-08-17T19:00:00, ______Starting NEXT()______
    2017-08-17T19:00:00, Currently 5 trades open with ids=0,1,2,3,4
    2017-08-17T20:00:00, SELL EXECUTED, Price: 4241.91, Cost: 44.03, Comm 0.11, tradeID: 3
    2017-08-17T20:00:00, ______Starting NEXT()______
    2017-08-17T20:00:00, Currently 5 trades open with ids=0,1,2,3,4
    2017-08-17T21:00:00, SELL EXECUTED, Price: 4307.56, Cost: 44.03, Comm 0.11, tradeID: 4
    2017-08-17T21:00:00, ______Starting NEXT()______
    2017-08-17T21:00:00, Currently 5 trades open with ids=0,1,2,3,4
    2017-08-17T22:00:00, SELL EXECUTED, Price: 4346.74, Cost: 44.03, Comm 0.11, tradeID: 0
    2017-08-17T22:00:00, A trade just closed with id 0
    2017-08-17T22:00:00, ______Starting NEXT()______
    2017-08-17T22:00:00, Currently 4 trades open with ids=1,2,3,4
    2017-08-17T23:00:00, SELL EXECUTED, Price: 4333.55, Cost: 44.03, Comm 0.11, tradeID: 1
    2017-08-17T23:00:00, A trade just closed with id 1
    2017-08-17T23:00:00, ______Starting NEXT()______
    2017-08-17T23:00:00, Currently 3 trades open with ids=2,3,4
    2017-08-18T00:00:00, SELL EXECUTED, Price: 4313.39, Cost: 44.03, Comm 0.11, tradeID: 2
    2017-08-18T00:00:00, A trade just closed with id 2
    2017-08-18T00:00:00, ______Starting NEXT()______
    2017-08-18T00:00:00, Currently 2 trades open with ids=3,4
    2017-08-18T01:00:00, SELL EXECUTED, Price: 4285.08, Cost: 44.03, Comm 0.11, tradeID: 3
    2017-08-18T01:00:00, A trade just closed with id 3
    2017-08-18T01:00:00, ______Starting NEXT()______
    2017-08-18T01:00:00, Currently 1 trades open with ids=4
    2017-08-18T02:00:00, SELL EXECUTED, Price: 4269.36, Cost: 44.03, Comm 0.11, tradeID: 4
    2017-08-18T02:00:00, A trade just closed with id 4
    2017-08-18T02:00:00, ______Starting NEXT()______
    2017-08-18T02:00:00, Currently 0 trades open with ids=
    2017-08-18T03:00:00, BUY EXECUTED, Price: 4251.95, Cost: 42.52, Comm 0.11, tradeID: 0
    2017-08-18T03:00:00, ______Starting NEXT()______
    2017-08-18T03:00:00, Currently 0 trades open with ids=
    2017-08-18T04:00:00, BUY EXECUTED, Price: 4244.77, Cost: 42.45, Comm 0.11, tradeID: 1
    2017-08-18T04:00:00, New trade just opened with id 1
    2017-08-18T04:00:00, ______Starting NEXT()______
    2017-08-18T04:00:00, Currently 1 trades open with ids=1
    2017-08-18T05:00:00, BUY EXECUTED, Price: 4292.39, Cost: 42.92, Comm 0.11, tradeID: 2
    2017-08-18T05:00:00, New trade just opened with id 2
    2017-08-18T05:00:00, ______Starting NEXT()______
    2017-08-18T05:00:00, Currently 2 trades open with ids=1,2
    2017-08-18T06:00:00, BUY EXECUTED, Price: 4287.92, Cost: 42.88, Comm 0.11, tradeID: 3
    2017-08-18T06:00:00, New trade just opened with id 3
    2017-08-18T06:00:00, ______Starting NEXT()______
    2017-08-18T06:00:00, Currently 3 trades open with ids=1,2,3
    2017-08-18T07:00:00, BUY EXECUTED, Price: 4313.56, Cost: 43.14, Comm 0.11, tradeID: 4
    2017-08-18T07:00:00, New trade just opened with id 4
    2017-08-18T07:00:00, ______Starting NEXT()______
    2017-08-18T07:00:00, Currently 4 trades open with ids=1,2,3,4
    2017-08-18T08:00:00, BUY EXECUTED, Price: 4279.46, Cost: 42.79, Comm 0.11, tradeID: 0
    2017-08-18T08:00:00, ______Starting NEXT()______
    2017-08-18T08:00:00, Currently 4 trades open with ids=1,2,3,4
    2017-08-18T09:00:00, BUY EXECUTED, Price: 4285.00, Cost: 42.85, Comm 0.11, tradeID: 1
    2017-08-18T09:00:00, ______Starting NEXT()______
    2017-08-18T09:00:00, Currently 4 trades open with ids=1,2,3,4
    2017-08-18T10:00:00, BUY EXECUTED, Price: 4265.33, Cost: 42.65, Comm 0.11, tradeID: 2
    2017-08-18T10:00:00, ______Starting NEXT()______
    2017-08-18T10:00:00, Currently 4 trades open with ids=1,2,3,4
    2017-08-18T11:00:00, BUY EXECUTED, Price: 4304.15, Cost: 43.04, Comm 0.11, tradeID: 3
    2017-08-18T11:00:00, ______Starting NEXT()______
    2017-08-18T11:00:00, Currently 4 trades open with ids=1,2,3,4
    2017-08-18T12:00:00, BUY EXECUTED, Price: 4356.31, Cost: 43.56, Comm 0.11, tradeID: 4
    2017-08-18T12:00:00, ______Starting NEXT()______
    2017-08-18T12:00:00, Currently 4 trades open with ids=1,2,3,4
    2017-08-18T13:00:00, SELL EXECUTED, Price: 4320.52, Cost: 42.88, Comm 0.11, tradeID: 0
    2017-08-18T13:00:00, ______Starting NEXT()______
    2017-08-18T13:00:00, Currently 4 trades open with ids=1,2,3,4
    2017-08-18T14:00:00, SELL EXECUTED, Price: 4302.97, Cost: 42.88, Comm 0.11, tradeID: 1
    2017-08-18T14:00:00, ______Starting NEXT()______
    2017-08-18T14:00:00, Currently 4 trades open with ids=1,2,3,4
    2017-08-18T15:00:00, SELL EXECUTED, Price: 4293.09, Cost: 42.88, Comm 0.11, tradeID: 2
    2017-08-18T15:00:00, ______Starting NEXT()______
    2017-08-18T15:00:00, Currently 4 trades open with ids=1,2,3,4
    2017-08-18T16:00:00, SELL EXECUTED, Price: 4259.40, Cost: 42.88, Comm 0.11, tradeID: 3
    2017-08-18T16:00:00, ______Starting NEXT()______
    2017-08-18T16:00:00, Currently 4 trades open with ids=1,2,3,4
    2017-08-18T17:00:00, SELL EXECUTED, Price: 4234.54, Cost: 42.88, Comm 0.11, tradeID: 4
    2017-08-18T17:00:00, ______Starting NEXT()______
    2017-08-18T17:00:00, Currently 4 trades open with ids=1,2,3,4
    2017-08-18T18:00:00, SELL EXECUTED, Price: 4250.34, Cost: 42.88, Comm 0.11, tradeID: 0
    2017-08-18T18:00:00, A trade just closed with id 0
    

    Anyone knows why?

    Thanks!
    T.


  • administrators

    Trades are only notified when opened and closed. All intermediate states are not notified (the trade is updated though)



  • @backtrader OK, it seemed like that but I wasn't sure. Now the remaining question is why didn't the notify_trade fire for the opening BUY(1) at 2017-08-18T03:00:00, I'll try to look into it more
    Thanks
    T.