Skip to content

Instantly share code, notes, and snippets.

@vividvilla
Created August 11, 2016 10:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vividvilla/8b76dc0de59690fb27268da5ae9de11f to your computer and use it in GitHub Desktop.
Save vividvilla/8b76dc0de59690fb27268da5ae9de11f to your computer and use it in GitHub Desktop.
Test websocket streaming change mode
2016-08-11 14:55:26,222 - root - DEBUG - [{'last_price': 1011.45, 'volume': 2722657, 'sell_quantity': 284004, 'last_quantity': 1, 'change': 0.7771633537587873, 'average_price': 1013.38, 'ohlc': {'high': 1022.85, 'close': 1003.65, 'open': 1001.95, 'low': 999.6}, 'depth': {'sell': [], 'buy': []}, 'mode': 'quote', 'tradeable': True, 'buy_quantity': 191315, 'instrument_token': 738561}]
2016-08-11 14:55:26,222 - root - DEBUG - [{'last_price': 1011.45, 'volume': 2722657, 'sell_quantity': 284009, 'last_quantity': 1, 'change': 0.7771633537587873, 'average_price': 1013.38, 'ohlc': {'high': 1022.85, 'close': 1003.65, 'open': 1001.95, 'low': 999.6}, 'depth': {'sell': [], 'buy': []}, 'mode': 'quote', 'tradeable': True, 'buy_quantity': 191343, 'instrument_token': 738561}]
2016-08-11 14:55:26,222 - root - DEBUG - [{'last_price': 1011.5, 'volume': 2722764, 'sell_quantity': 284009, 'last_quantity': 107, 'change': 0.7821451701290313, 'average_price': 1013.38, 'ohlc': {'high': 1022.85, 'close': 1003.65, 'open': 1001.95, 'low': 999.6}, 'depth': {'sell': [], 'buy': []}, 'mode': 'quote', 'tradeable': True, 'buy_quantity': 191843, 'instrument_token': 738561}]
2016-08-11 14:55:26,222 - root - DEBUG - [{'last_price': 1011.8, 'volume': 2723064, 'sell_quantity': 283802, 'last_quantity': 15, 'change': 0.8120360683505183, 'average_price': 1013.38, 'ohlc': {'high': 1022.85, 'close': 1003.65, 'open': 1001.95, 'low': 999.6}, 'depth': {'sell': [], 'buy': []}, 'mode': 'quote', 'tradeable': True, 'buy_quantity': 189821, 'instrument_token': 738561}]
2016-08-11 14:55:26,222 - root - DEBUG - [{'last_price': 1011.85, 'volume': 2724458, 'sell_quantity': 284039, 'last_quantity': 153, 'change': 0.8170178847207737, 'average_price': 1013.38, 'ohlc': {'high': 1022.85, 'close': 1003.65, 'open': 1001.95, 'low': 999.6}, 'depth': {'sell': [], 'buy': []}, 'mode': 'quote', 'tradeable': True, 'buy_quantity': 189955, 'instrument_token': 738561}]
2016-08-11 14:55:31,669 - root - DEBUG - [{'instrument_token': 738561, 'last_price': 1011.75, 'mode': 'ltp', 'tradeable': True}]
2016-08-11 14:55:36,651 - root - DEBUG - [{'instrument_token': 738561, 'last_price': 1011.8, 'mode': 'ltp', 'tradeable': True}]
2016-08-11 14:55:38,663 - root - DEBUG - [{'instrument_token': 738561, 'last_price': 1011.75, 'mode': 'ltp', 'tradeable': True}]
2016-08-11 14:55:39,683 - root - DEBUG - [{'instrument_token': 738561, 'last_price': 1011.8, 'mode': 'ltp', 'tradeable': True}]
2016-08-11 14:55:40,668 - root - DEBUG - [{'instrument_token': 738561, 'last_price': 1011.75, 'mode': 'ltp', 'tradeable': True}]
2016-08-11 14:55:50,671 - root - DEBUG - [{'instrument_token': 738561, 'last_price': 1011.7, 'mode': 'ltp', 'tradeable': True}]
2016-08-11 14:55:53,654 - root - DEBUG - [{'instrument_token': 738561, 'last_price': 1011.75, 'mode': 'ltp', 'tradeable': True}]
2016-08-11 14:56:01,645 - root - DEBUG - [{'instrument_token': 738561, 'last_price': 1011.8, 'mode': 'ltp', 'tradeable': True}]
2016-08-11 14:56:04,679 - root - DEBUG - [{'instrument_token': 738561, 'last_price': 1011.7, 'mode': 'ltp', 'tradeable': True}]
2016-08-11 14:56:08,678 - root - DEBUG - [{'instrument_token': 738561, 'last_price': 1011.6, 'mode': 'ltp', 'tradeable': True}]
2016-08-11 14:56:11,675 - root - DEBUG - [{'instrument_token': 738561, 'last_price': 1011.65, 'mode': 'ltp', 'tradeable': True}]
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 = [738561]
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_QUOTE, tokens)
import time; time.sleep(5)
ws.set_mode(ws.MODE_LTP, 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