How to get OHLC only once

rrjain
Hello,

I'm using following code which is running in infinite loop, I need data only 1 time to read the OHLC and want to perform further action. What needs to be modified to close the loop without getting error after getting 1 set of data..

inst_token = [3050241]
def on_connect(ws, response):
ws.subscribe(inst_token)
ws.set_mode(ws.MODE_QUOTE, inst_token)
kws.on_ticks = on_ticks
kws.on_connect = on_connect
kws.connect()
Thanks and Regards,
Ritesh
  • rakeshr
    @rrjain
    You might unsubscribe token in real-time. Go through pykiteconnect FAQ for example code.
  • rrjain
    rrjain edited June 2020
    Hello,

    @rakeshr

    Can you please guide where I'm wrong, as the data is still coming..


    def on_connect(ws, response):
    ws.subscribe(inst_token)
    ws.set_mode(ws.MODE_QUOTE, inst_token)

    def unsubscribe(self, inst_token):
    """
    Unsubscribe the given list of instrument_tokens.
    - `instrument_tokens` is list of instrument_tokens to unsubscribe.
    """
    try:
    self.ws.sendMessage(
    six.b(json.dumps({"a": self._message_unsubscribe, "v": inst_token}))
    )
    for token in inst_token:
    try:
    del(self.subscribed_tokens[token])
    except KeyError:
    pass
    return True
    except Exception as e:
    self._close(reason="Error while unsubscribe: {}".format(str(e)))
    raise

    kws.on_ticks = on_ticks
    kws.on_connect = on_connect
    kws.unsubscribe = unsubscribe

    kws.connect()

    Regards,
    Ritesh Jain
  • rakeshr
    @rrjain
    def unsubscribe(self, inst_token):
    """
    Unsubscribe the given list of instrument_tokens.
    - `instrument_tokens` is list of instrument_tokens to unsubscribe.
    """
    try:
    self.ws.sendMessage(
    six.b(json.dumps({"a": self._message_unsubscribe, "v": inst_token}))
    )
    for token in inst_token:
    try:
    del(self.subscribed_tokens[token])
    except KeyError:
    pass
    return True
    except Exception as e:
    self._close(reason="Error while unsubscribe: {}".format(str(e)))
    raise
    You don't have to write above unsubscribe source function at your end. You can directly inherit it from ws class.
    Eg.
    ws.unsubscribe(token_list)
    You can go through this thread. We have explained, how to subsribe/unsubscribe token in real time.
  • rrjain
    Thanks @rakeshr

    It worked fine...

    One more query, I know this is not the platform to get coding support, but would be a great help if you can guide....

    In Python after executing all logic, I am getting output saved in variable named "SP"

    Say the Value in SP = BANKNIFTY20JUN21800PE, how can I pull ticker data for this saved in SP..

    Tried this, but not working

    tickerlist = [SP]

    Regards,
    Ritesh Jain
  • rakeshr
    @rrjain
    Say the Value in SP = BANKNIFTY20JUN21800PE, how can I pull ticker data for this saved in SP
    You need to use an instrument token to fetch ticker data not trading symbol. You might write helper function at your end, which accepts trading symbol and returns its instrument token, you can use this file for mapping.
  • rrjain
    @rakeshr

    Yes I have code that convert it to instrument token, I have tested the complete script and the same is perfectly fine...

    What I need your guidance is i don't want to manually enter name, and need to pull from SP

    where SP = BANKNIFTY20JUN21800PE

    tickerlist = ["BANKNIFTY20JUN21800PE"]
    NSELTPformat=['NSE:{}'.format(i) for i in tickerlist]
    instead of this, want something like

    tickerlist = [SP]
    NSELTPformat=['NSE:{}'.format(i) for i in tickerlist]
    Thanks and Regards,
    Ritesh
  • rrjain
    Thanks @rakeshr for your guidance, my problem is resolved...

    You can please close this thread.
This discussion has been closed.