Websocket some token in the subscription list unsubscribe on its own

Rishant
Hi,

I am facing following problem. I have a list of equities in my subscription list. Sometimes, I see that it has stopped subscribing to certain equities in the list .

Do you know why it happens and can you suggest a way out of it ?
  • Rishant
    Rishant edited December 2017
    my code
    import cPickle as pickle
    from kiteconnect import WebSocket
    import time
    from datetime import datetime
    from Queue import Queue,LifoQueue
    from threading import Thread
    import pandas as pd
    import MySQLdb
    df = pd.DataFrame()

    q =LifoQueue(maxsize=10)
    #max size is to ensure that the program is not running even after the WebSocket has broken for long
    #tickData = 0

    # Initialise.
    kws = WebSocket("*****", "******", "*****", reconnect=True)

    # Callback for tick reception.
    def on_tick(tick, ws):
    tick.append(datetime.now())
    q.put(tick)
    #tickdata = tick
    #time.sleep(10)

    # Callback for successful connection.
    def on_connect(ws):
    # Subscribe to a list of instrument_tokens (RELIANCE and ACC here).
    listSubscription = [134657, 1629185, 895745, 3924993, 348929, 758529, 359937, 784129, 3001089, 415745, 2796801, 356865, 140033]
    ws.subscribe(listSubscription)

    # Set RELIANCE to tick in `full` mode.
    ws.set_mode(ws.MODE_QUOTE, listSubscription)

    def on_close(ws):
    print("closed")

    def on_error(error,ws):
    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

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

    kws.enable_reconnect(reconnect_interval=5, reconnect_tries=50)

    # 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)
    test = {}
    data_instrument_token = pd.DataFrame(columns = ['last_price','timeStamp','ratiobuysell'] )
    #data = {738561:{'last_price':[],'timeStamp':[]}}
    #data_instrument_token.set_index('timeStamp')
    ltpDict = {134657:0, 1629185:0, 895745:0, 3924993:0, 348929:0, 758529:0, 359937:0, 784129:0, 3001089:0, 415745:0, 2796801:0, 356865:0, 140033:0}
    test['universe'] ={134657: 'BPCL', 1629185: 'NATIONALUM', 895745: 'TATASTEEL', 3924993: 'NMDC', 348929: 'HINDALCO', 758529: 'SAIL', 359937: 'HINDPETRO', 784129: 'VEDL', 3001089: 'JSWSTEEL', 415745: 'IOC', 2796801: 'GODREJIND', 356865: 'HINDUNILVR', 140033: 'BRITANNIA'}
    1,1 ratiobuysell = 0
    i=0
    #A Pandas Thread
    while True:

    a = q.get()

    for j in range(0,len(a)-1):

    #data_instrument_token.set_value(i,'last_price',a[j]['last_price'])
    #data_instrument_token.set_value(i,'instrument_token',a[j]['instrument_token'])
    #data_instrument_token.set_value(i,'timeStamp',a[len(a)-1])
    if a[j]['sell_quantity'] > 0:
    ratiobuysell = int(float(a[j]['buy_quantity'])*100/a[j]['sell_quantity'])
    print a[len(a)-1]
    args = (a[j]['instrument_token'],a[j]['last_price'],a[len(a)-1], ratiobuysell)
    sql = "INSERT INTO tickData(token,close,date,ratiobuysell)"" VALUES(%s,%s,%s,%s)"
    conn = MySQLdb.connect(host="****", user="****", passwd="****", db="****")
    cursor = conn.cursor()
    dataDb = cursor.execute(sql,args)
    ltpDict[a[j]['instrument_token']]=a[j]['last_price']
    conn.commit()

    query = """ UPDATE stocksData
    SET BPCL = %s,NATIONALUM = %s,TATASTEEL = %s,NMDC = %s,HINDALCO = %s,SAIL = %s,HINDPETRO = %s,VEDL = %s,JSWSTEEL = %s,IOC = %s, GODREJIND = %s,HINDUNILVR = %s,BRITANNIA = %s,date = %s
    WHERE fieldInfo = "LastTradedPrice"; """
    dataLTP = (ltpDict[134657],ltpDict[1629185],ltpDict[895745],ltpDict[3924993],ltpDict[348929],ltpDict[758529],ltpDict[359937],ltpDict[784129],ltpDict[3001089],ltpDict[415745],ltpDict[2796801],ltpDict[356865],ltpDict[140033],a[len(a)-1])
    dataDb = cursor.execute(query,dataLTP)
    conn.commit()
    conn.close()
    i = i + 1
  • Rishant
    Rishant edited December 2017
    Is it because of the Queue I am storing data into? Is it that I miss a lot of ticks and hence the update doesnt happen ? as I can clearly see that my system lags the original movement in the market.
  • sujith
    Check out this example for usage of Kite ticker data.
  • Rishant
    ok, thanks. I would check this
Sign In or Register to comment.