kiteTicker error

SharadKothari
Sometimes I get following error, after which kiteTicker stop functioning (not more ticks). Please help.

Unhandled Error
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\twisted\internet\base.py", line 1261, in run
self.mainLoop()
File "C:\ProgramData\Anaconda3\lib\site-packages\twisted\internet\base.py", line 1273, in mainLoop
self.doIteration(t)
File "C:\ProgramData\Anaconda3\lib\site-packages\twisted\internet\selectreactor.py", line 143, in doSelect
_logrun(selectable, _drdw, selectable, method)
--- ---
File "C:\ProgramData\Anaconda3\lib\site-packages\twisted\python\log.py", line 103, in callWithLogger
return callWithContext({"system": lp}, func, *args, **kw)
File "C:\ProgramData\Anaconda3\lib\site-packages\twisted\python\log.py", line 86, in callWithContext
return context.call({ILogContext: newCtx}, func, *args, **kw)
File "C:\ProgramData\Anaconda3\lib\site-packages\twisted\python\context.py", line 122, in callWithContext
return self.currentContext().callWithContext(ctx, func, *args, **kw)
File "C:\ProgramData\Anaconda3\lib\site-packages\twisted\python\context.py", line 85, in callWithContext
return func(*args,**kw)
File "C:\ProgramData\Anaconda3\lib\site-packages\twisted\internet\selectreactor.py", line 154, in _doReadOrWrite
self._disconnectSelectable(selectable, why, method=="doRead")
File "C:\ProgramData\Anaconda3\lib\site-packages\twisted\internet\posixbase.py", line 258, in _disconnectSelectable
selectable.connectionLost(failure.Failure(why))
File "C:\ProgramData\Anaconda3\lib\site-packages\twisted\internet\tcp.py", line 519, in connectionLost
self._commonConnection.connectionLost(self, reason)
File "C:\ProgramData\Anaconda3\lib\site-packages\twisted\internet\tcp.py", line 327, in connectionLost
protocol.connectionLost(reason)
File "C:\ProgramData\Anaconda3\lib\site-packages\twisted\protocols\tls.py", line 398, in connectionLost
self._flushReceiveBIO()
File "C:\ProgramData\Anaconda3\lib\site-packages\twisted\protocols\tls.py", line 295, in _flushReceiveBIO
ProtocolWrapper.dataReceived(self, bytes)
File "C:\ProgramData\Anaconda3\lib\site-packages\twisted\protocols\policies.py", line 120, in dataReceived
self.wrappedProtocol.dataReceived(data)
File "C:\ProgramData\Anaconda3\lib\site-packages\autobahn\twisted\websocket.py", line 291, in dataReceived
self._dataReceived(data)
File "C:\ProgramData\Anaconda3\lib\site-packages\autobahn\websocket\protocol.py", line 1213, in _dataReceived
self.consumeData()
File "C:\ProgramData\Anaconda3\lib\site-packages\autobahn\websocket\protocol.py", line 1225, in consumeData
while self.processData() and self.state != WebSocketProtocol.STATE_CLOSED:
File "C:\ProgramData\Anaconda3\lib\site-packages\autobahn\websocket\protocol.py", line 1583, in processData
fr = self.onFrameData(payload)
File "C:\ProgramData\Anaconda3\lib\site-packages\autobahn\websocket\protocol.py", line 1678, in onFrameData
self._onMessageFrameData(payload)
File "C:\ProgramData\Anaconda3\lib\site-packages\autobahn\twisted\websocket.py", line 310, in _onMessageFrameData
self.onMessageFrameData(payload)
File "C:\ProgramData\Anaconda3\lib\site-packages\autobahn\websocket\protocol.py", line 608, in onMessageFrameData
self.frame_data.append(payload)
builtins.AttributeError: 'NoneType' object has no attribute 'append'

  • rakeshr
    @SharadKothari
    It seems to be happening because of the blocking of the main ticker thread. You can go through this thread to more about it.
  • SAPANKHUNTIA
    Hii @rakeshr.
    I used the thread you reffered here. and the structure of my code is similar to as you have advised. but still it is showing similar error.
    import logging
    from kiteconnect import KiteTicker

    logging.basicConfig(level=logging.DEBUG)

    kws = KiteTicker("your_api_key", "your_access_token")

    def on_ticks(ws, ticks):
    logging.debug("Ticks: {}".format(ticks))

    def on_connect(ws, response):
    ws.subscribe([738561, 5633])
    ws.set_mode(ws.MODE_FULL, [738561])

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

    def place_an_order(name, price, Transac_type, no_of_shares):
    ------
    ------

    # kws.on_ticks = on_ticks
    kws.on_connect = on_connect
    # kws.on_close = on_close
    kws.connect(threaded=True)

    start_time=--:--
    end_time= --:--
    time_now = datetime.datetime.now()

    while True:
    #Perform required data operation using tick data
    while start_time < dt_now < end_time:
    def on_ticks(ws, ticks):
    ..........
    helper_method_1(ticks)
    .........

    def helper_method_1(ticks):
    .........
    Perform computation here
    ........
    #Assign callback
    time_now = datetime.datetime.now()
    kws.on_ticks=on_ticks
    while dt_now> end_time:
    def on_ticks(ws, ticks):
    ..........
    helper_method_2(ticks)
    .........

    def helper_method_2(ticks):
    .........
    Perform computation here
    place orders
    request open positions list
    ........
    #Assign callback
    time_now = datetime.datetime.now()
    kws.on_ticks=on_ticks
  • SAPANKHUNTIA
    And this is the error. Though it is not throwing error always, but the error is un predictable and worrysome if I am taking large volumes.

    and here is the error

    HINDCOPPER, -0.3000000000001819, 12.4689
    Unhandled Error
    Traceback (most recent call last):
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/twisted/python/log.py", line 103, in callWithLogger
    return callWithContext({"system": lp}, func, *args, **kw)
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/twisted/python/log.py", line 86, in callWithContext
    return context.call({ILogContext: newCtx}, func, *args, **kw)
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/twisted/python/context.py", line 122, in callWithContext
    return self.currentContext().callWithContext(ctx, func, *args, **kw)
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/twisted/python/context.py", line 85, in callWithContext
    return func(*args,**kw)
    --- ---
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/twisted/internet/selectreactor.py", line 149, in _doReadOrWrite
    why = getattr(selectable, method)()
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/twisted/internet/tcp.py", line 243, in doRead
    return self._dataReceived(data)
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/twisted/internet/tcp.py", line 249, in _dataReceived
    rval = self.protocol.dataReceived(data)
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/twisted/protocols/tls.py", line 330, in dataReceived
    self._flushReceiveBIO()
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/twisted/protocols/tls.py", line 295, in _flushReceiveBIO
    ProtocolWrapper.dataReceived(self, bytes)
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/twisted/protocols/policies.py", line 120, in dataReceived
    self.wrappedProtocol.dataReceived(data)
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/autobahn/twisted/websocket.py", line 291, in dataReceived
    self._dataReceived(data)
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/autobahn/websocket/protocol.py", line 1213, in _dataReceived
    self.consumeData()
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/autobahn/websocket/protocol.py", line 1225, in consumeData
    while self.processData() and self.state != WebSocketProtocol.STATE_CLOSED:
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/autobahn/websocket/protocol.py", line 1591, in processData
    fr = self.onFrameEnd()
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/autobahn/websocket/protocol.py", line 1713, in onFrameEnd
    self._onMessageEnd()
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/autobahn/twisted/websocket.py", line 319, in _onMessageEnd
    self.onMessageEnd()
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/autobahn/websocket/protocol.py", line 634, in onMessageEnd
    self._onMessage(payload, self.message_is_binary)
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/autobahn/twisted/websocket.py", line 322, in _onMessage
    self.onMessage(payload, isBinary)
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/kiteconnect/ticker.py", line 71, in onMessage
    self.factory.on_message(self, payload, is_binary)
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/kiteconnect/ticker.py", line 670, in _on_message
    self.on_ticks(self, self._parse_binary(payload))
    File "/Users/sapan/Desktop/Sapan/coding/python/python_learning/ideas_with_kite_api-7.py", line 177, in on_ticks
    helper_method_2(ticks= ticks)
    File "/Users/sapan/Desktop/Sapan/coding/python/python_learning/ideas_with_kite_api-7.py", line 184, in helper_method_2
    open_position = kite.positions()["net"]
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/kiteconnect/connect.py", line 441, in positions
    return self._get("portfolio.positions")
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/kiteconnect/connect.py", line 826, in _get
    return self._request(route, "GET", url_args=url_args, params=params, is_json=is_json)
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/kiteconnect/connect.py", line 877, in _request
    raise e
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/kiteconnect/connect.py", line 865, in _request
    r = self.reqsession.request(method,
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/requests/api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/requests/sessions.py", line 542, in request
    resp = self.send(prep, **send_kwargs)
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/requests/sessions.py", line 655, in send
    r = adapter.send(request, **kwargs)
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/requests/adapters.py", line 529, in send
    raise ReadTimeout(e, request=request)
    requests.exceptions.ReadTimeout: HTTPSConnectionPool(host='api.kite.trade', port=443): Read timed out. (read timeout=7)

  • SAPANKHUNTIA
    And this is the next part of the error.

    Unhandled Error
    Traceback (most recent call last):
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/threading.py", line 892, in run
    self._target(*self._args, **self._kwargs)
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/twisted/internet/base.py", line 1283, in run
    self.mainLoop()
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/twisted/internet/base.py", line 1295, in mainLoop
    self.doIteration(t)
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/twisted/internet/selectreactor.py", line 143, in doSelect
    _logrun(selectable, _drdw, selectable, method)
    --- ---
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/twisted/python/log.py", line 103, in callWithLogger
    return callWithContext({"system": lp}, func, *args, **kw)
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/twisted/python/log.py", line 86, in callWithContext
    return context.call({ILogContext: newCtx}, func, *args, **kw)
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/twisted/python/context.py", line 122, in callWithContext
    return self.currentContext().callWithContext(ctx, func, *args, **kw)
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/twisted/python/context.py", line 85, in callWithContext
    return func(*args,**kw)
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/twisted/internet/selectreactor.py", line 154, in _doReadOrWrite
    self._disconnectSelectable(selectable, why, method=="doRead")
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/twisted/internet/posixbase.py", line 258, in _disconnectSelectable
    selectable.connectionLost(failure.Failure(why))
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/twisted/internet/tcp.py", line 519, in connectionLost
    self._commonConnection.connectionLost(self, reason)
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/twisted/internet/tcp.py", line 327, in connectionLost
    protocol.connectionLost(reason)
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/twisted/protocols/tls.py", line 398, in connectionLost
    self._flushReceiveBIO()
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/twisted/protocols/tls.py", line 295, in _flushReceiveBIO
    ProtocolWrapper.dataReceived(self, bytes)
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/twisted/protocols/policies.py", line 120, in dataReceived
    self.wrappedProtocol.dataReceived(data)
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/autobahn/twisted/websocket.py", line 291, in dataReceived
    self._dataReceived(data)
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/autobahn/websocket/protocol.py", line 1213, in _dataReceived
    self.consumeData()
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/autobahn/websocket/protocol.py", line 1225, in consumeData
    while self.processData() and self.state != WebSocketProtocol.STATE_CLOSED:
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/autobahn/websocket/protocol.py", line 1583, in processData
    fr = self.onFrameData(payload)
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/autobahn/websocket/protocol.py", line 1678, in onFrameData
    self._onMessageFrameData(payload)
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/autobahn/twisted/websocket.py", line 310, in _onMessageFrameData
    self.onMessageFrameData(payload)
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/autobahn/websocket/protocol.py", line 608, in onMessageFrameData
    self.frame_data.append(payload)
    builtins.AttributeError: 'NoneType' object has no attribute 'append'

    Kindly tell me the reason and if there is any way to solve this.
    Thank you.
  • rakeshr
    builtins.AttributeError: 'NoneType' object has no attribute 'append'
    The above error occurs when handling another initiating error in your codebase.
    helper_method_2
    open_position = kite.positions()["net"]
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/kiteconnect/connect.py", line 441, in positions
    return self._get("portfolio.positions")
    requests.exceptions.ReadTimeout: HTTPSConnectionPool(host='api.kite.trade', port=443): Read timed out. (read timeout=7)
    It's because of time-out happening while continously polling positions with every tick. Which is not recommended at all. Go through this article to know more.
  • SAPANKHUNTIA
    Hii @rakeshr and team.
    Thank you for your reply.
    Can it be sorted out by applying a delay after i request kite.positions()["net"].

    Else can i delay the callback from the on_ticks ? Because if we can, our system can get some time to process the position request.

    because if I'M putting a market order and my code is designed for high frequency trade. then i need to call the position and fetch the buy and sell value from there only.

    if i should not call it in every tick, what should be the frequency ?

    Is it an internet issue ?

    Kindly help me resolve this one.
    Thank you.
  • sujith
    @SAPANKHUNTIA,
    Kite Connect is not suitable for latency-based or high-frequency trading. For HFT, you need to set up a colocation system at the exchange. It will cost above 18lakhs per annum.

    Basically, you shouldn't block the thread that is receiving ticks. All your computations, DB dumps and data fetches should be moved to a secondary thread.
  • SAPANKHUNTIA
    okay @sujith. Thank you.
    The above said package is not affordable for a retail trader.

    What is the frequency of receiving ticks from websocket ?

    the same code is running fine in mid-day. But in the beginning of a trading day it is showing error.


    Regarding this, will you please provide some hint how it can be achieved ?

    Thank you.
  • SAPANKHUNTIA
    Hi @sujith, @rakeshr and team,

    I tried something like this.

    def helper_method_2(ticks):
    for company in ticks:
    Perform computation here
    place orders
    request open positions list
    while True:
    def on_ticks(ws, ticks):
    for tick in ticks:
    print(ticks)
    new_thread = threading.Thread(target=helper_method_2(ticks = ticks))
    new_thread.start()
    #Assign callback
    kws.on_ticks=on_ticks
    But i think my purpose won't be served here as the main thread will be blocked untill the new_thread is processed. Kindly share your words on this.

    Again I am trying to do something like this
    ticks_data = {}
    def helper_method_2(ticks):
    for company in ticks:
    Perform computation here
    place orders
    request open positions list

    def on_ticks(ws, ticks):
    global ticks_data
    ticks_data= ticks

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

    kws.on_ticks = on_ticks
    kws.on_connect = on_connect
    # kws.on_close = on_close

    kws.connect(threaded=True)

    while True:
    t1 = threading.Thread(target=on_ticks(ws = ws, ticks = ticks))
    new_thread = threading.Thread(target=helper_method_2(ticks = ticks_data))

    t1.start()
    new_thread.start()

    t1.join()
    new_thread.join()

    kws.on_ticks=on_ticks
    But again here I am stuck as I am confused what to assign to ws and ticks in the on_ticks(ws,ticks) function. and the same confusion arises here that the main thread will be blocked until the new_thread joins the flow.

    Being a beginner I am finding it tough. Any suggestion will be very helpful.
    Thank you.
  • RED
    Hi @SAPANKHUNTIA ,
    Your code looks ok, but instead of joining threads create a daemon thread.

    You can't expect your helper thread to be ready every time a tick is received, so use queue to store ticks and pop ticks from queue to helper thread for smooth flow.

    Just scroll through older threads with similar questions, you will get these answered along with the code required.
  • SAPANKHUNTIA
    hi @RED
    Thank you.

    Seems likee you are talking about the second method.
    But it is throwing an error ticks and ws is not defined. so what to assign these two?


    And again if i remove the thread.join() then will it be fine ?
  • RED
    RED edited May 2021
    Hi @SAPANKHUNTIA

    Threading structure:
    t1 = threading.Thread(target = func_name, args=(arg1,arg2,....))

    Use t1.start() only if ticks are present, so you wont start a thread with empty tick array, resulting in fatal error.

    Code Example:
    ticker = [] #queue or collections.queue

    def tick_func():
    a = ticker.pop(0)
    # run your code

    while True:
    if ticks:
    ticker.append(ticks)

    if not t1:
    t1 = threading.Thread(target=tick_func, name=t1, daemon = True)
    t1.start()
    elif not t1.is_alive():
    t1 = threading.Thread(target=tick_func, name=t1, daemon = True)
    t1.start()
    Above is a sample code, you can modify it and improve its efficiency.
  • SAPANKHUNTIA
    Thank you @RED.
    I have tried as you have mentioned and i was running the callback and calculations in two different threads. After 2:30 Hrs of smooth running it threw a similar error
    requests.exceptions.ReadTimeout: HTTPSConnectionPool(host='api.kite.trade', port=443): Read timed out. (read timeout=7)
    and the code stopped completely.
    def helper_method_2(ticks):
    request open positions list
    for company in ticks:
    Perform computation here
    place orders
    while True:
    def on_ticks(ws, ticks):
    global ticks_data
    ticks_data = ticks
    if not new_thread.is_alive():
    new_thread = threading.Thread(target=helper_method_2(ticks = ticks_data), daemon= True)
    new_thread.start()
    dt_now = datetime.datetime.now().time()
    kws.on_ticks=on_ticks
    here is the complete error stack.
    Traceback (most recent call last):
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/urllib3/connectionpool.py", line 445, in _make_request
    six.raise_from(e, None)
    File "", line 3, in raise_from
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/urllib3/connectionpool.py", line 440, in _make_request
    httplib_response = conn.getresponse()
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py", line 1347, in getresponse
    response.begin()
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py", line 307, in begin
    version, status, reason = self._read_status()
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py", line 268, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/socket.py", line 704, in readinto
    return self._sock.recv_into(b)
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ssl.py", line 1241, in recv_into
    return self.read(nbytes, buffer)
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ssl.py", line 1099, in read
    return self._sslobj.read(len, buffer)
    socket.timeout: The read operation timed out

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/requests/adapters.py", line 439, in send
    resp = conn.urlopen(
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/urllib3/connectionpool.py", line 755, in urlopen
    retries = retries.increment(
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/urllib3/util/retry.py", line 531, in increment
    raise six.reraise(type(error), error, _stacktrace)
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/urllib3/packages/six.py", line 735, in reraise
    raise value
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/urllib3/connectionpool.py", line 699, in urlopen
    httplib_response = self._make_request(
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/urllib3/connectionpool.py", line 447, in _make_request
    self._raise_timeout(err=e, url=url, timeout_value=read_timeout)
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/urllib3/connectionpool.py", line 336, in _raise_timeout
    raise ReadTimeoutError(
    urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='api.kite.trade', port=443): Read timed out. (read timeout=7)

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
    File "/Users/sapan/Desktop/Sapan/coding/python/python_learning/ideas_with_kite_api-7_1.py", line 294, in
    new_thread = threading.Thread(target=helper_method_2(ticks = ticks_data), daemon= True)
    File "/Users/sapan/Desktop/Sapan/coding/python/python_learning/ideas_with_kite_api-7_1.py", line 121, in helper_method_2
    open_position = kite.positions()["net"]
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/kiteconnect/connect.py", line 441, in positions
    return self._get("portfolio.positions")
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/kiteconnect/connect.py", line 826, in _get
    return self._request(route, "GET", url_args=url_args, params=params, is_json=is_json)
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/kiteconnect/connect.py", line 877, in _request
    raise e
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/kiteconnect/connect.py", line 865, in _request
    r = self.reqsession.request(method,
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/requests/api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/requests/sessions.py", line 542, in request
    resp = self.send(prep, **send_kwargs)
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/requests/sessions.py", line 655, in send
    r = adapter.send(request, **kwargs)
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/requests/adapters.py", line 529, in send
    raise ReadTimeout(e, request=request)
    requests.exceptions.ReadTimeout: HTTPSConnectionPool(host='api.kite.trade', port=443): Read timed out. (read timeout=7)

    seems like again I am getting something wrong in polling positions. as i"m again stuck at the same issue.
    File "/Users/sapan/Desktop/Sapan/coding/python/python_learning/ideas_with_kite_api-7_1.py", line 294, in
    new_thread = threading.Thread(target=helper_method_2(ticks = ticks_data), daemon= True)
    File "/Users/sapan/Desktop/Sapan/coding/python/python_learning/ideas_with_kite_api-7_1.py", line 121, in helper_method_2
    open_position = kite.positions()["net"]
    Is there any way to reconnect if this error arises?

    Kindly help me with this.
    Thank you.
  • SAPANKHUNTIA
    I am sorry @RED and team.

    I have been coding it wrong.
    new_thread = threading.Thread(target=helper_method_2(ticks = ticks_data), daemon= True)
    new_thread.start()
    I had been calling the function during creating the new_thread. so it was blocking the main thread i guess.
    Let me try it once more in the right way.

    Thank you.

  • RED
    Hi @SAPANKHUNTIA,

    You are calling the thread wrong, it doesn't spawn a new worker thread instead it runs on main.

    Code:
    new_thread = threading.Thread(target=helper_method_2, args= (ticks_data ), daemon= True)

    Pass args seperately.
  • SAPANKHUNTIA
    Thank you @RED .
    I followed your idea. now i can fetch my positions without blocking the main thread.
    sometimes the new_thread shows Readtimeout error. but then another new_thread is created without hampering the flow.

    Thank you again.
  • darkrai
    darkrai edited January 2022
    Hi red , im tried to run this but im getting t1 not defined error ..... is there a way to define it outside the if condition .
  • darkrai
    ill try with to initialize
    t1=None
    before the start of the of the while loop
Sign In or Register to comment.