Skip to content

Instantly share code, notes, and snippets.

@vividvilla
Last active March 9, 2022 17:54
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save vividvilla/db62e69af44831a6c4126ed193ea5214 to your computer and use it in GitHub Desktop.
Save vividvilla/db62e69af44831a6c4126ed193ea5214 to your computer and use it in GitHub Desktop.
Test Kite Connect data streaming with threaded Raw
import time
import logging
from kiteconnect import WebSocket
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# Initialise.
kws = WebSocket("api_key", "public_token", "zerodha_user_id")
# RELIANCE BSE and RELIANCE NSE
tokens = [128083204, 738561]
print("Tokens length", len(tokens))
# Callback for tick reception.
def on_tick(tick, ws):
print(tick)
# Callback for successful connection.
def on_connect(ws):
ws.subscribe(tokens)
ws.set_mode(ws.MODE_FULL, tokens)
def on_close():
print("closed")
def on_error():
print("error")
# Assign the callbacks.
kws.on_tick = on_tick
kws.on_connect = on_connect
kws.on_close = on_close
kws.on_error = on_error
# 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)
# kws.connect(disable_ssl_verification=True) # for ubuntu
count = 0
while True:
count += 1
print("This is main thread. Will change webosocket mode every 5 seconds.")
if count % 2 == 0:
if kws.is_connected():
print("Set mode to LTP")
kws.set_mode(kws.MODE_LTP, tokens)
else:
if kws.is_connected():
print("Set mode to quote")
kws.set_mode(kws.MODE_QUOTE, tokens)
time.sleep(5)
@sinu
Copy link

sinu commented Dec 10, 2019

@vividvilla can we use proxy with connect ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment