It looks like you're new here. If you want to get involved, click one of these buttons!
#create KiteTicker object
kws = KiteTicker(key_secret[0], kite.access_token)
tokens = tokenLookup(instrument_df, tickers)
def on_ticks(ws,ticks):
#renkoOperation(ticks)
#stopLoss(ticks)
print(ticks)
def on_connect(ws,response):
ws.subscribe(tokens)
ws.set_mode(ws.MODE_LTP,tokens)
def on_close(ws, code, reason):
# On connection close stop the main loop
# Reconnection will not happen after executing `ws.stop()`
ws.stop()
# Assign the callbacks.
kws.on_ticks = on_ticks
kws.on_connect = on_connect
kws.on_close = on_close
# 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]
kws.subscribe(tokens)