Subscribing to multiple tokens at run time and response of the Subscribe method

sagaranilganu
@sujith , @rakeshr, @Vivek

My strategy in python is ready which needs to subscribe to multiple tokens in the staged manner ::
1. Subscribe to Nifty FUT in "Full mode" taking the Instrument Token from instrument dump only once at the start before my Thread begins
2. Ahead in the code based on the outcome of the strategy; "Subscribe" to another Instrument Token in "Full mode".
3. As per the requirements in the code "Subscribe" to another Instrument Token so on and so forth as the need arises.

Q1] Since the callback method => def on_connect(ws, response) contains these two methods => ws.subscribe([738561, 5633]) and ws.set_mode(ws.MODE_FULL, [738561])
How the subscription can be achieved at "Run time"
V V Imp :: (Can these two methods be called elsewhere in the thread? In that event my "on_connect" definition would be empty !!!???)

Q2] What "response" does these two methods give and do they fire exception so that a
try-catch can be written for them to handle the exception and the program logic.

Please suggest accordingly!!

**************************************************************
My code something looks like this ::=>

import logging
from kiteconnect import KiteConnect
from kiteconnect import kiteTicker
........

******Login_KiteConnect*****

****Macros****
****Variables***

# *****Queue object created here*****

****My Function Definitions *****

try-catch for => Inst_Lst = kite.instruments()
****Instruments token finder***
"NIFTY FUT Instrument Token in a list"

try-catch for => Margin = kite.margins()

My Thread class definition ()
{
global variables

Infinite while loop
"Here are the code locations where I will get new "Instrument Tokens" and they
are needed to be subscribed!!"

}


*******Thread object created *********
*******Thread started*******

def on_ticks(ws, ticks):
# Callback to receive ticks.
q.put(ticks)
logging.debug("Ticks: {}".format(ticks))


def on_connect(ws, response):
# Callback on successful connect.
# Subscribe to a list of instrument_tokens (RELIANCE and ACC here).
ws.subscribe(instrument_tokens_Lst)

# Set RELIANCE to tick in `full` mode.
ws.set_mode(ws.MODE_FULL, instrument_tokens_Lst)


def on_close(ws, code, reason):
# On connection close stop the main loop
# Reconnection will not happen after executing `ws.stop()`
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()

**************************************************************
  • sagaranilganu
    Am am stuck with this last part of "Websocket apis" . Your helping hand stands crucial!!
    Request help.
  • rakeshr
    @sagaranilganu
    You can go through this thread, we have given example to subscribe/unsubscribe during Websocket run time.
  • sagaranilganu
    sagaranilganu edited May 2020
    @rakeshr
    I went through your suggested all threads but frankly I am now confused!

    1] I understood ===> the kws.connect() should be kws.connect(threaded=True) and outside any while loop

    2] Did not understand ===> Can there be multiple same websocket definitions?
    One just defined outside and one in the while loop?

    My interpretation ===>
    Since I am using the Thread for whole of my logic to whirl in an endless while loop; are you suggesting that all the websockets should be inside that Thread while loop something like this ====>

    def my_strategy():
    {
    global variables

    while True
    def on_ticks(ws, ticks):
    q.put(ticks)

    def on_connect(ws, response):
    ws.subscribe(instru_tokens_Lst)
    ws.set_mode(ws.MODE_FULL, instru_tokens_Lst)

    def on_close(ws, code, reason):
    ws.stop()


    ******************
    my_ticks = q.get()
    instru_tokens_Lst.append(Instru_Token)
    Rest of my logic
    ******************


    # Assign the callbacks.
    kws.on_ticks = on_ticks
    kws.on_connect = on_connect
    kws.on_close = on_close

    kws.connect(threaded=True)

    }

    -------------- My strategy thread creation and run method calling -----------
    ML = Thread(target=my_strategy, daemon=True)

    # Threads of classes are created the moment respective class
    # "run" method is called!!!!
    ML.start()

    -------------------------------END OF SOFTWARE---------------------------------

    VVImp note ==>
    My code current facts ::
    As I am using queue; I am not requiring to call any other method to retrieve the ticks. I have designed a Server-Client program as a simulator and through simple socket I receive data continuously per 1sec and in the "my_strategy thread" my code logic works well!!

    My interpretation ::
    Now referring the suggested threads :: It seems like my threading logic is useless as the websockets are to be written in the while loop itself only once.
    Thus just by saying => kws.connect(threaded=True) it would thread all the websockets on the while loop .
    So instead of my threaded while loop it should simply be in the main code.

    Request guidance!!
  • sagaranilganu
    sagaranilganu edited May 2020
    @renjitharajan84
    Hi,
    Went through your posts in :: "kiteconnect.ticker:Connection error: 1006 - connection was closed uncleanly (None). " - regarding "Web sockets and subscription issues"
    Seems it has worked for you.
    Can you put more light on it as discussed in this thread. (Created new to avoid hijacking of thread!!)
    Request help.

    Regards,
    Sagar
  • sagaranilganu
    @renjitharajan84
    P.S : For your reference please refer to your tag that created by ::
    mnabhishek April 7 in Python client Flag
Sign In or Register to comment.