def tick_init(api_key, public_token, id): kws = WebSocket(api_key, public_token, id) make_tick_df() #Remove below line before restarting in order to save data make_tick_file() num = int(input("Enter the number of stocks to subscribe\n")) set_subscription_list(num) # Assign the callbacks. kws.on_tick = on_tick kws.on_connect = on_connect # Infinite loop on the main thread. Nothing after this will run. # You have to use the pre-defined callbacks to manage subscriptions. kws.connect(True)
kite_api_key = "yw8yelu5bpfel8ib" kite_api_secret = "vaddqe1qb3lzorst3uolc1ptdo0l2cku" kws = WebSocket(kite_api_key, 'Kf879HRiMNS0DbOREhO1KyDqRl3J2bWu', "YK8879") def on_ticks(tick, ws): # Callback to receive ticks. #logging.debug("Ticks: {}".format(ticks)) with open("2_4_2018_1", "a+") as f: f.write(str(tick)) print(tick) def on_connect(ws): # Callback on successful connect. # Subscribe to a list of instrument_tokens (RELIANCE and ACC here). ws.subscribe([738561, 5633]) # Set RELIANCE to tick in `full` mode. ws.set_mode(ws.MODE_FULL, [738561, 5633]) # Assign the callbacks. kws.on_tick = on_ticks kws.on_connect = on_connect # Infinite loop on the main thread. Nothing after this will run. # You have to use the pre-defined callbacks to manage subscriptions. kws.connect()
# Subscribe to a list of instrument_tokens (RELIANCE and ACC # here). print(""" Connected to zerodha... """) print("Subscribing to ...{}".format(instruments)) print(type(instruments)) # ws.subscribe([int(ce['instrument_token']), int(pe['instrument_token'])]) ws.subscribe(instruments) # Set RELIANCE to tick in `full` mode. # ws.set_mode(ws.MODE_QUOTE, ce['instrument_token']) # ws.set_mode(ws.MODE_QUOTE, pe['instrument_token']) # Assign the callbacks. kws.on_tick = on_tick kws.on_connect = on_connect # To enable auto reconnect WebSocket connection in # case of network failure # - First param is interval between reconnection # attempts in seconds. # Callback `on_reconnect` is triggered on every # reconnection attempt. (Default interval is 5 # seconds) # - Second param is maximum number of retries before # the program exits triggering `on_noreconnect` # calback. (Defaults to 50 attempts) # Note that you can also enable auto reconnection # while initialising websocket. # Example `kws = WebSocket("your_api_key",