So. The primary problem to approach this was we were seeing this as a two legged problem. Lets bifurcate that and make it single leg problem.
Leg[0] = “Execute BankNIFTY ATM CE at 9:20 with stop loss of 20% and exit at 14:50”
Leg[1] = “Execute BankNIFTY ATM PE at 9:20 with stop loss of 20% and exit at 14:50”
and so on…
We will maintain a dict of all Legs of each account in this format.
{
0: {start:{ 'average_price': 110.5,
'cancelled_quantity': 0,
'disclosed_quantity': 0,
'exchange': 'NFO',
'exchange_order_id': '1400000005892263',
'exchange_timestamp': '2020-07-17 09:32:31',
'exchange_update_timestamp': '2020-07-17 09:32:31',
'filled_quantity': 20,
'guid': '01Xe7ezwky56UFn',
'instrument_token': 11258882,
'market_protection': 0,
'meta': {},
'order_id': '200717000592173',
'order_timestamp': '2020-07-17 09:32:31',
'order_type': 'MARKET',
'parent_order_id': None,
'pending_quantity': 0,
'placed_by': 'QT1105',
'price': 0,
'product': 'NRML',
'quantity': 20,
'status': 'COMPLETE',
'status_message': None,
'status_message_raw': None,
'tag': None,
'tradingsymbol': 'BANKNIFTY2072320800PE',
'transaction_type': 'SELL',
'trigger_price': 0,
'validity': 'DAY',
'variety': 'regular'}
end:{ 'average_price': 110.5,
'cancelled_quantity': 0,
'disclosed_quantity': 0,
'exchange': 'NFO',
'exchange_order_id': '1400000005892263',
'exchange_timestamp': '2020-07-17 09:32:31',
'exchange_update_timestamp': '2020-07-17 09:32:31',
'filled_quantity': 20,
'guid': '01Xe7ezwky56UFn',
'instrument_token': 11258882,
'market_protection': 0,
'meta': {},
'order_id': '200717000592173',
'order_timestamp': '2020-07-17 09:32:31',
'order_type': 'MARKET',
'parent_order_id': None,
'pending_quantity': 0,
'placed_by': 'QT1105',
'price': 0,
'product': 'NRML',
'quantity': 20,
'status': 'COMPLETE',
'status_message': None,
'status_message_raw': None,
'tag': None,
'tradingsymbol': 'BANKNIFTY2072320800PE',
'transaction_type': 'SELL',
'trigger_price': 0,
'validity': 'DAY',
'variety': 'regular'}
data:{
log1: "some other info"
}
}
}
Execution:
On 9:20 - Send Order Execution of Leg [0]
Expecting Output:
{ 'average_price': 110.5,
'cancelled_quantity': 0,
'disclosed_quantity': 0,
'exchange': 'NFO',
'exchange_order_id': '1400000005892263',
'exchange_timestamp': '2020-07-17 09:32:31',
'exchange_update_timestamp': '2020-07-17 09:32:31',
'filled_quantity': 20,
'guid': '01Xe7ezwky56UFn',
'instrument_token': 11258882,
'market_protection': 0,
'meta': {},
'order_id': '200717000592173',
'order_timestamp': '2020-07-17 09:32:31',
'order_type': 'MARKET',
'parent_order_id': None,
'pending_quantity': 0,
'placed_by': 'QT1105',
'price': 0,
'product': 'NRML',
'quantity': 20,
'status': 'COMPLETE',
'status_message': None,
'status_message_raw': None,
'tag': None,
'tradingsymbol': 'BANKNIFTY2072320800PE',
'transaction_type': 'SELL',
'trigger_price': 0,
'validity': 'DAY',
'variety': 'regular'}
If the status is complete
, We will send order execution of Leg [0] and will expect similar output.
On 14:50
Send Order Modification of Leg[0] using ["end"]["order_id]
If the quantity ≠ filled quantity
i.e. SL not triggered
If No: Update the [“end”] with this output
If Yes:
Check (If status: “open”): Modify it to reverse market order
Check (If status: “cancelled”):
If(cancelled manual): Skip
If(cancelled by broker): Fire Fresh reverse Market Order