Skip to content

Instantly share code, notes, and snippets.

@vividvilla
Last active October 16, 2022 12:32
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vividvilla/0d9fcaec1c02d65c4a429e43d55ae667 to your computer and use it in GitHub Desktop.
Save vividvilla/0d9fcaec1c02d65c4a429e43d55ae667 to your computer and use it in GitHub Desktop.
Kite Connect Python client websocket streaming example - https://github.com/rainmattertech/pykiteconnect
import logging
from kiteconnect import WebSocket
logging.basicConfig(filename='ticks.log', level=logging.DEBUG, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# Initialise.
kws = WebSocket("api_key", "public_token", "user_id")
tokens = [5215745, 633601, 1195009, 779521, 758529, 1256193, 194561, 1837825, 952577, 1723649, 3930881, 4451329, 593665, 3431425, 2905857, 3771393, 3789569, 3463169, 381697, 54273, 415745, 2933761, 3580417, 49409, 3060993, 4464129, 3375873, 4574465, 636673, 3721473, 2796801]
print("Tokens length", len(tokens))
# Callback for tick reception.
def on_tick(tick, ws):
logging.debug(tick)
# Callback for successful connection.
def on_connect(ws):
ws.subscribe(tokens)
ws.set_mode(ws.MODE_FULL, tokens)
# 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()
# kws.connect(disable_ssl_verification=True) # for ubuntu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment