Skip to content

Instantly share code, notes, and snippets.

@joeirimpan
Created April 16, 2018 08:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save joeirimpan/ac72922b5206c4ad076344b187d71562 to your computer and use it in GitHub Desktop.
Save joeirimpan/ac72922b5206c4ad076344b187d71562 to your computer and use it in GitHub Desktop.
import logging
from kiteconnect import KiteTicker
logging.basicConfig(level=logging.DEBUG)
# Initialise
kws = KiteTicker("<API-KEY>", "<ACCESS-TOKEN>")
def on_ticks(ws, ticks): # noqa
# Callback to receive ticks.
logging.info("Ticks: {}".format(ticks))
# Closing websocket connection on fetching a tick
ws.close()
def on_connect(ws, response): # noqa
# 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])
def on_close(ws, code, reason):
ws.stop()
# Assign the callbacks.
kws.on_ticks = on_ticks
kws.on_connect = on_connect
kws.on_close = on_close
# Infinite loop on the main thread. Nothing after this will run.
# You have to use the pre-defined callbacks to manage subscriptions.
kws.connect()
@joeirimpan
Copy link
Author

Console log

INFO:root:Ticks: [{'timestamp': datetime.datetime(2018, 4, 16, 14, 0, 21), 'last_price': 933.1, 'oi_day_low': 0, 'volume': 3012431, 'sell_quantity': 385075, 'last_quantity': 4, 'change': -0.6124514033125632, 'oi': 0, 'average_price': 931.65, 'ohlc': {'high': 938.25, 'close': 938.85, 'open': 934.45, 'low': 928.5}, 'tradable': True, 'depth': {'sell': [{'price': 933.1, 'orders': 9, 'quantity': 866}, {'price': 933.15, 'orders': 8, 'quantity': 344}, {'price': 933.2, 'orders': 1, 'quantity': 25}, {'price': 933.25, 'orders': 1, 'quantity': 100}, {'price': 933.3, 'orders': 2, 'quantity': 51}], 'buy': [{'price': 933.05, 'orders': 10, 'quantity': 758}, {'price': 933.0, 'orders': 7, 'quantity': 265}, {'price': 932.9, 'orders': 3, 'quantity': 62}, {'price': 932.8, 'orders': 2, 'quantity': 225}, {'price': 932.75, 'orders': 3, 'quantity': 103}]}, 'mode': 'full', 'last_trade_time': datetime.datetime(2018, 4, 16, 14, 0, 20), 'buy_quantity': 290794, 'oi_day_high': 0, 'instrument_token': 738561}, {'last_price': 1563.75, 'volume': 157561, 'sell_quantity': 64828, 'last_quantity': 2, 'change': 1.7768231963291974, 'average_price': 1553.36, 'ohlc': {'high': 1567.55, 'close': 1536.45, 'open': 1531.7, 'low': 1530.0}, 'tradable': True, 'mode': 'quote', 'buy_quantity': 84039, 'instrument_token': 5633}]
ERROR:kiteconnect.ticker:Connection closed: None - None

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