@@ -147,7 +147,7 @@ def __init__(self, username, session_id, master_contracts_to_download = None):
147147 self .__exchange_messages = []
148148 # Initialize Depth data
149149 self .__depth_data = {}
150- self .__ltp = {}
150+ self .__tick_data = {}
151151
152152 try :
153153 self .get_profile ()
@@ -249,57 +249,92 @@ def __extract_tick_data(self, data):
249249 data ["instrument" ] = self .get_instrument_by_token (data .pop ("e" ), int (data .pop ("tk" )))
250250 if ("ts" in data ): # Symbol
251251 data .pop ("ts" )
252- if (data ["instrument" ].symbol not in self .__ltp ):
253- self .__ltp [data ["instrument" ].symbol ] = 0
252+ if (data ["instrument" ].symbol not in self .__tick_data ):
253+ self .__tick_data [data ["instrument" ].symbol ] = {}
254+ self .__tick_data [data ["instrument" ].symbol ]["ltp" ] = 0
255+ self .__tick_data [data ["instrument" ].symbol ]["percent_change" ] = 0
256+ self .__tick_data [data ["instrument" ].symbol ]["change_value" ] = 0
257+ self .__tick_data [data ["instrument" ].symbol ]["volume" ] = 0
258+ self .__tick_data [data ["instrument" ].symbol ]["open" ] = 0
259+ self .__tick_data [data ["instrument" ].symbol ]["high" ] = 0
260+ self .__tick_data [data ["instrument" ].symbol ]["low" ] = 0
261+ self .__tick_data [data ["instrument" ].symbol ]["close" ] = 0
262+ self .__tick_data [data ["instrument" ].symbol ]["exchange_time_stamp" ] = None
263+ self .__tick_data [data ["instrument" ].symbol ]["atp" ] = 0
264+ self .__tick_data [data ["instrument" ].symbol ]["tick_increment" ] = 0
265+ self .__tick_data [data ["instrument" ].symbol ]["lot_size" ] = 0
266+ self .__tick_data [data ["instrument" ].symbol ]["price_precision" ] = 0
267+ self .__tick_data [data ["instrument" ].symbol ]["total_open_interest" ] = 0
254268 if ("lp" in data ): # Last Traded Price
255- self .__ltp [data ["instrument" ].symbol ] = float (data .pop ("lp" ))
256- data ["ltp" ] = self .__ltp [data ["instrument" ].symbol ]
269+ self .__tick_data [data ["instrument" ].symbol ]["ltp" ] = float (data .pop ("lp" ))
257270 if ("pc" in data ): # percentage change
258- data ["percent_change" ] = float (data .pop ("pc" ))
271+ self . __tick_data [ data [ "instrument" ]. symbol ] ["percent_change" ] = float (data .pop ("pc" ))
259272 if ("cv" in data ): # change value (absolute change in price)
260- data ["change_value" ] = float (data .pop ("cv" ))
261- if ("v" in data ): # Volume
262- data ["volume" ] = int (data .pop ("v" ))
263- if ("o" in data ): # Open
264- data ["open" ] = float (data .pop ("o" ))
265- if ("h" in data ): # High
266- data ["high" ] = float (data .pop ("h" ))
267- if ("l" in data ): # Low
268- data ["low" ] = float (data .pop ("l" ))
269- if ("c" in data ): # Close
270- data ["close" ] = float (data .pop ("c" ))
273+ self . __tick_data [ data [ "instrument" ]. symbol ] ["change_value" ] = float (data .pop ("cv" ))
274+ if ("v" in data ): # Volume
275+ self . __tick_data [ data [ "instrument" ]. symbol ] ["volume" ] = int (data .pop ("v" ))
276+ if ("o" in data ): # Open
277+ self . __tick_data [ data [ "instrument" ]. symbol ] ["open" ] = float (data .pop ("o" ))
278+ if ("h" in data ): # High
279+ self . __tick_data [ data [ "instrument" ]. symbol ] ["high" ] = float (data .pop ("h" ))
280+ if ("l" in data ): # Low
281+ self . __tick_data [ data [ "instrument" ]. symbol ] ["low" ] = float (data .pop ("l" ))
282+ if ("c" in data ): # Close
283+ self . __tick_data [ data [ "instrument" ]. symbol ] ["close" ] = float (data .pop ("c" ))
271284 if ("ft" in data ): # Feed Time
272- data ["exchange_time_stamp" ] = datetime .datetime .fromtimestamp (int (data .pop ("ft" )))
285+ self . __tick_data [ data [ "instrument" ]. symbol ] ["exchange_time_stamp" ] = datetime .datetime .fromtimestamp (int (data .pop ("ft" )))
273286 if ("ap" in data ): # Average Price
274- data ["atp" ] = float (data .pop ("ap" ))
287+ self . __tick_data [ data [ "instrument" ]. symbol ] ["atp" ] = float (data .pop ("ap" ))
275288 if ("ti" in data ): # Tick Increment
276- data ["tick_increment" ] = float (data .pop ("ti" ))
289+ self . __tick_data [ data [ "instrument" ]. symbol ] ["tick_increment" ] = float (data .pop ("ti" ))
277290 if ("ls" in data ): # Lot Size
278- data ["lot_size" ] = int (data .pop ("ls" ))
291+ self . __tick_data [ data [ "instrument" ]. symbol ] ["lot_size" ] = int (data .pop ("ls" ))
279292 if (data ["instrument" ].symbol not in self .__depth_data ): # Initialize depth data
280293 self .__depth_data [data ["instrument" ].symbol ] = {}
281- self .__depth_data [data ["instrument" ].symbol ]["bid_prices" ] = [None , None , None , None , None ]
282- self .__depth_data [data ["instrument" ].symbol ]["ask_prices" ] = [None , None , None , None , None ]
283- self .__depth_data [data ["instrument" ].symbol ]["bid_quantities" ] = [None , None , None , None , None ]
284- self .__depth_data [data ["instrument" ].symbol ]["ask_quantities" ] = [None , None , None , None , None ]
285- self .__depth_data [data ["instrument" ].symbol ]["buy_orders" ] = [None , None , None , None , None ]
286- self .__depth_data [data ["instrument" ].symbol ]["sell_orders" ] = [None , None , None , None , None ]
294+ self .__depth_data [data ["instrument" ].symbol ]["bid_prices" ] = [None , None , None , None , None ]
295+ self .__depth_data [data ["instrument" ].symbol ]["ask_prices" ] = [None , None , None , None , None ]
296+ self .__depth_data [data ["instrument" ].symbol ]["bid_quantities" ] = [None , None , None , None , None ]
297+ self .__depth_data [data ["instrument" ].symbol ]["ask_quantities" ] = [None , None , None , None , None ]
298+ self .__depth_data [data ["instrument" ].symbol ]["buy_orders" ] = [None , None , None , None , None ]
299+ self .__depth_data [data ["instrument" ].symbol ]["sell_orders" ] = [None , None , None , None , None ]
300+ self .__depth_data [data ["instrument" ].symbol ]["open_interest" ] = 0
301+ self .__depth_data [data ["instrument" ].symbol ]["last_traded_quantity" ]= 0
302+ self .__depth_data [data ["instrument" ].symbol ]["last_traded_time" ] = None
303+ self .__depth_data [data ["instrument" ].symbol ]["total_buy_quantity" ] = 0
304+ self .__depth_data [data ["instrument" ].symbol ]["total_sell_quantity" ] = 0
305+ self .__depth_data [data ["instrument" ].symbol ]["upper_circuit" ] = 0
306+ self .__depth_data [data ["instrument" ].symbol ]["lower_circuit" ] = 0
307+
287308 if ("bp1" in data ): # Best Bid
288- data ["best_bid_price" ] = float (data .pop ("bp1" ))
289- self .__depth_data [data ["instrument" ].symbol ]["bid_prices" ][0 ] = data ["best_bid_price" ]
309+ self .__depth_data [data ["instrument" ].symbol ]["bid_prices" ][0 ] = float (data .pop ("bp1" ))
290310 if ("sp1" in data ): # Best Ask
291- data ["best_ask_price" ] = float (data .pop ("sp1" ))
292- self .__depth_data [data ["instrument" ].symbol ]["ask_prices" ][0 ] = data ["best_ask_price" ]
311+ self .__depth_data [data ["instrument" ].symbol ]["ask_prices" ][0 ] = float (data .pop ("sp1" ))
293312 if ("bq1" in data ): # Best Bid Quantity
294- data ["best_bid_quantity" ] = int (data .pop ("bq1" ))
295- self .__depth_data [data ["instrument" ].symbol ]["bid_quantities" ][0 ] = data ["best_bid_quantity" ]
313+ self .__depth_data [data ["instrument" ].symbol ]["bid_quantities" ][0 ] = int (data .pop ("bq1" ))
296314 if ("sq1" in data ): # Best Ask Quantity
297- data ["best_ask_quantity" ] = int (data .pop ("sq1" ))
298- self .__depth_data [data ["instrument" ].symbol ]["ask_quantities" ][0 ] = data ["best_ask_quantity" ]
315+ self .__depth_data [data ["instrument" ].symbol ]["ask_quantities" ][0 ] = int (data .pop ("sq1" ))
299316 if ("pp" in data ): # Price Precision
300- data ["price_precision" ] = int (data .pop ("pp" ))
317+ self . __tick_data [ data [ "instrument" ]. symbol ] ["price_precision" ] = int (data .pop ("pp" ))
301318 if ("toi" in data ): # Total Open Interest
302- data ["total_open_interest" ] = int (data .pop ("toi" ))
319+ self .__tick_data [data ["instrument" ].symbol ]["total_open_interest" ] = int (data .pop ("toi" ))
320+ data ["ltp" ] = self .__tick_data [data ["instrument" ].symbol ]["ltp" ]
321+ data ["percent_change" ] = self .__tick_data [data ["instrument" ].symbol ]["percent_change" ]
322+ data ["change_value" ] = self .__tick_data [data ["instrument" ].symbol ]["change_value" ]
323+ data ["volume" ] = self .__tick_data [data ["instrument" ].symbol ]["volume" ]
324+ data ["open" ] = self .__tick_data [data ["instrument" ].symbol ]["open" ]
325+ data ["high" ] = self .__tick_data [data ["instrument" ].symbol ]["high" ]
326+ data ["low" ] = self .__tick_data [data ["instrument" ].symbol ]["low" ]
327+ data ["close" ] = self .__tick_data [data ["instrument" ].symbol ]["close" ]
328+ data ["exchange_time_stamp" ] = self .__tick_data [data ["instrument" ].symbol ]["exchange_time_stamp" ]
329+ data ["atp" ] = self .__tick_data [data ["instrument" ].symbol ]["atp" ]
330+ data ["tick_increment" ] = self .__tick_data [data ["instrument" ].symbol ]["tick_increment" ]
331+ data ["lot_size" ] = self .__tick_data [data ["instrument" ].symbol ]["lot_size" ]
332+ data ["best_bid_price" ] = self .__depth_data [data ["instrument" ].symbol ]["bid_prices" ][0 ]
333+ data ["best_ask_price" ] = self .__depth_data [data ["instrument" ].symbol ]["ask_prices" ][0 ]
334+ data ["best_bid_quantity" ] = self .__depth_data [data ["instrument" ].symbol ]["bid_quantities" ][0 ]
335+ data ["best_ask_quantity" ] = self .__depth_data [data ["instrument" ].symbol ]["ask_quantities" ][0 ]
336+ data ["price_precision" ] = self .__tick_data [data ["instrument" ].symbol ]["price_precision" ]
337+ data ["total_open_interest" ] = self .__tick_data [data ["instrument" ].symbol ]["total_open_interest" ]
303338 return data
304339
305340 def __extract_depth_data (self , data ):
@@ -381,19 +416,27 @@ def __extract_depth_data(self, data):
381416 data ["sell_orders" ] = self .__depth_data [data ["instrument" ].symbol ]["sell_orders" ].copy ()
382417
383418 if ("oi" in data ): # Open Interest
384- data ["open_interest" ] = int (data .pop ("oi" ))
419+ self . __depth_data [ data [ "instrument" ]. symbol ] ["open_interest" ] = int (data .pop ("oi" ))
385420 if ("ltq" in data ): # Last Traded Quantity
386- data ["last_traded_quantity" ] = int (data .pop ("ltq" ))
421+ self . __depth_data [ data [ "instrument" ]. symbol ] ["last_traded_quantity" ] = int (data .pop ("ltq" ))
387422 if ("ltt" in data ): # Last Traded Time
388- data ["last_traded_time" ] = datetime .datetime .strptime (data .pop ("ltt" ), "%H:%M:%S" ).time ()
423+ self . __depth_data [ data [ "instrument" ]. symbol ] ["last_traded_time" ] = datetime .datetime .strptime (data .pop ("ltt" ), "%H:%M:%S" ).time ()
389424 if ("tbq" in data ): # Total Buy Quantity
390- data ["total_buy_quantity" ] = int (data .pop ("tbq" ))
425+ self . __depth_data [ data [ "instrument" ]. symbol ] ["total_buy_quantity" ] = int (data .pop ("tbq" ))
391426 if ("tsq" in data ): # Total Sell Quantity
392- data ["total_sell_quantity" ] = int (data .pop ("tsq" ))
427+ self . __depth_data [ data [ "instrument" ]. symbol ] ["total_sell_quantity" ] = int (data .pop ("tsq" ))
393428 if ("uc" in data ): # Upper Circuit
394- data ["upper_circuit" ] = float (data .pop ("uc" ))
429+ self . __depth_data [ data [ "instrument" ]. symbol ] ["upper_circuit" ] = float (data .pop ("uc" ))
395430 if ("lc" in data ): # Lower Circuit
396- data ["lower_circuit" ] = float (data .pop ("lc" ))
431+ self .__depth_data [data ["instrument" ].symbol ]["lower_circuit" ] = float (data .pop ("lc" ))
432+
433+ data ["open_interest" ] = self .__depth_data [data ["instrument" ].symbol ]["open_interest" ]
434+ data ["last_traded_quantity" ] = self .__depth_data [data ["instrument" ].symbol ]["last_traded_quantity" ]
435+ data ["last_traded_time" ] = self .__depth_data [data ["instrument" ].symbol ]["last_traded_time" ]
436+ data ["total_buy_quantity" ] = self .__depth_data [data ["instrument" ].symbol ]["total_buy_quantity" ]
437+ data ["total_sell_quantity" ] = self .__depth_data [data ["instrument" ].symbol ]["total_sell_quantity" ]
438+ data ["upper_circuit" ] = self .__depth_data [data ["instrument" ].symbol ]["upper_circuit" ]
439+ data ["lower_circuit" ] = self .__depth_data [data ["instrument" ].symbol ]["lower_circuit" ]
397440 return data
398441
399442 def __on_data_callback (self , ws = None , message = None , data_type = None , continue_flag = None ):
@@ -674,7 +717,6 @@ def modify_order(self, transaction_type, instrument, product_type, order_id, ord
674717 "transtype" : transaction_type .value ,
675718 "prctyp" : order_type .value ,
676719 "qty" : quantity ,
677- "trading_symbol" : instrument .symbol ,
678720 "price" : price ,
679721 "trigPrice" : trigger_price ,
680722 "pCode" : prod_type
0 commit comments