# Infinite loop on the main thread. Nothing after this will run. # You have to use the pre-defined callbacks to manage subscriptions. kws.connect(threaded=True) #multithreaded application time.sleep(3)
positions_data = {} subscribed_tokens = [] # Initialize with an empty list to track subscribed tokens
positions = kite.positions()["net"] updated_positions = {} for position in positions: symbol = position["tradingsymbol"] updated_positions[symbol] = { "instrument_token": position["instrument_token"], "quantity": position["quantity"], "average_price": position["average_price"], "pnl": 0, # Will be updated dynamically }
# Update global positions_data positions_data = updated_positions logging.info(f"Updated positions: {positions_data}")
# Ensure required tokens remain subscribed required_tokens = [instrumentLookup(instrument_df, "NIFTY 50")] # Add required persistent tokens here new_tokens = [position["instrument_token"] for position in positions_data.values()] tokens_to_subscribe = list(set(required_tokens + new_tokens) - set(subscribed_tokens)) tokens = tokens_to_subscribe tokens = [str(token) for token in tokens]