instrument list needs to be hardcoded in on_connect?

ncoder
Hi, Do we need to hardcode the list of instruments in on_connect callback?
I tried following two versions. One where, I'm hardcoding the instruments inside on_connect callback, is working. Other one is not.

###############################hardcoded version - status : working
def on_connect(ws, response):
# Callback on successful connect.
# Subscribe to a list of instrument_tokens (RELIANCE and ACC here).
tokens = [1270529, 3771393, 3001089]
ws.subscribe(tokens)
ws.set_mode(ws.MODE_QUOTE,tokens)

############################### NOT hardcoded version - status : NOT working
tokens = [1270529, 3771393, 3001089]

def on_connect(ws, response):
# Callback on successful connect.
# Subscribe to a list of instrument_tokens (RELIANCE and ACC here).
global tokens
ws.subscribe(tokens)
ws.set_mode(ws.MODE_QUOTE,tokens)
  • rakeshr
    tokens = [1270529, 3771393, 3001089]

    def on_connect(ws, response):
    # Callback on successful connect.
    # Subscribe to a list of instrument_tokens (RELIANCE and ACC here).
    global tokens
    ws.subscribe(tokens)
    ws.set_mode(ws.MODE_QUOTE,tokens)
    The code looks fine. Is this throwing any error?
    Also, you can debug more on this by printing token value within the on_connect method.
  • ncoder
    ncoder edited March 23
    It's throwing an error, when the instruments are not hardcoded. Error message -->
    connection closed uncleanly: I dropped the WebSocket TCP connection: close reason without close code.

    Now, I know, it usually occurs when other methods are executed in on_tick callback. But , here, on_tick is working fine and as expected when the instrument values are hardcoded. I did debug and token values are appearing fine in on_connect.
    Somehow, it's happening only when instruments are being passed on , from outside of methods. Even, when I'm passing the values as a part of class , to it's own method. But hardcoding is working in every scenario and not hardcoding is throwing error in every scenario.

    Is it possible to show an example of 'NOT hardcoded' scenario, that's working?
  • rakeshr
    Now, I know, it usually occurs when other methods are executed in on_tick callback
    This can be the culprit. Avoid blocking the main ticker thread. You can go through this thread to know more.
  • ncoder
    ncoder edited March 24
    I don't think , that is the case here. Because, when I'm disabling all other code in on_tick and only printing the ticks for debugging, it's still not working. For example , I have included the following code as a part of a class and tokens are passed to the class as an argument and initiated in the __init__. When I'm passing it to on_connect, it's not working. Hardcoding it, works though.

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


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


    def on_close(self,ws, code, reason):
    # On connection close stop the event loop.
    # Reconnection will not happen after executing `ws.stop()`

    ws.stop()
Sign In or Register to comment.