Not getting tick data

gkannan1965
Hi,
I tried this program today and got no data. I have seen previous post where it is mentioned that during non-market hours, the last cache data will be streamed. I am not able to get that. The code I use is:
from kiteconnect import WebSocket
# Initialise.
kws = WebSocket("vvv", "xxxxx", "DU0139")
print kws

kws.connect()

# Callback for tick reception.
def on_tick(tick, ws):
print tick

# Callback for successful connection.
def on_connect(ws):
# Subscribe to a list of instrument_tokens
print ws.subscribe([2561])

# Set ABAN to tick in `quote` mode.
print ws.set_mode(ws.MODE_QUOTE, [2561])
#print ws

# Assign the callbacks.
kws.on_tick = on_tick
kws.on_connect = on_connect

if kws.is_connected():
print kws.is_connected()
kws.close()
kws object is created and reports as

I plan to run a cron job every 10 mins to fetch quote for say 100 scrips.

Please help.
  • Vivek
    Vivek edited June 2016
    @gkannan1965 Please assign the callback functions first and then connect. In your case move kws.connect() after assigning callbacks.
  • gkannan1965
    Can you help me with a sample code to get ACC tick data every 5mins using Websockets?
  • pybull
    I am also in the same boat. I have assigned all the callbacks. The only callback invoked is however the on_message callback, which probably corresponds to the heartbeat. The on_tick callback never gets invoked. I have set the mode to ws.MODE_LTP and tried with only one stock (4345345) which corresponds to REIAGROLTD. Is there any other prerequisite?
  • pybull
    pybull edited June 2016
    Following is my code. Maybe I am missing something.

    def setWebSocket(self, ws):
    def on_tick(tick, ws):
    self.onWSTick(tick, ws)

    def on_connect(ws):
    self.onWSConnect(ws)

    def on_message(message, ws):
    self.onWSMessage(message, ws)

    def on_error(error, ws):
    self.onWSError(error, ws)

    def on_close(ws):
    self.onWSClose(ws)

    ws.on_tick = on_tick
    ws.on_connect = on_connect
    ws.on_message = on_message
    ws.on_error = on_error
    ws.on_close = on_close

    ws.connect()
    As said earlier, can see the on_message callback getting invoked every few seconds (I am assuming it is the heartbeat), but the on_tick callback is never invoked.
  • Vivek
    @pybull Have you tried subscribing to any symbol? Ideally subscription should be done in on_connect callback. You might be getting heartbeat data in on_message callback but we ignore it in on_tick callback.
  • pybull
    @vivek, thanks for the quick response.

    Yup, I have tried subscribing to REIAGROLTD and ASHOKLEY.

    Following is the source around that area. The symbol is hardcoded in debug mode, which is currently enabled

    def __init__(self):
    if cherrypy.config['debug']:
    self.symbolTable = {'54273' : 'ASHOKLEY'}

    def onWSConnect(self, ws):
    print "====== Websocket connected ======="
    ws.subscribe(self.symbolTable.keys())
    ws.set_mode(ws.MODE_LTP, self.symbolTable.keys())
  • pybull
    Just saw a stupid mistake on my part. The instrument ID is an integer and not a string. Fixed that, but now, the market is closed :(

    Guess I will be able to test it tomorrow!
  • gkannan1965
    pybull - Did it work for you?
Sign In or Register to comment.