Forked from vividvilla/pykiteconnect_streaming_threaded.py
Created
February 27, 2021 15:11
-
-
Save raaghulr/a2fe50f80f9bf14251884b7617aecd47 to your computer and use it in GitHub Desktop.
Test Kite Connect data streaming with threaded Raw
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment