Error: HTTPSConnectionPool(host='api.kite.trade', port=443): Read timed out. (read timeout=7)

AAAAAAAAAA
AAAAAAAAAA edited January 2022 in Python client
Is there any way around this? If anyone has figured out, I'll be very grateful.

Only kite.positions() throws this error. I have it under a try except loop like so

I have this method called multiple times through out the code and everywhere it is placed inside this try-except structure. In case of the error I also have a time.sleep(0.2) for time out.

I get this error multiple times a day and sometimes continuously for hours! This is a pain. Because of this the code misses several entry and exit points. As you can imagine, this is quite bad. I will be eternally grateful to anyone out there to help get rid of this error.

Currently I run the code on my mac, would it help if I out it in AWS Mumbai server?

Thanks
  • sujith
    You shouldn't be polling positions API in the first place. You can fetch and cache the values.
    The only time it changes is when there is a trade or new order placed.
    We would suggest you to use postbacks or order updates via websockets as events to fetch positions API.

    If you are polling positions for P&L then you can check out this thread.
  • AAAAAAAAAA
    AAAAAAAAAA edited January 2022
    I use kite.ltp() to see the P&L positions periodically. Is that ok or does that need some changing too?

    > We would suggest you to use postbacks or order updates via websockets as events to fetch positions API.
    Could you please link an API doc or a thread here that explains this? Are you saying to only use kite.positions() whenever a new order has been placed and filled?

    Essentially, are you suggesting to make my own positions tracker?

    Thanks @sujith
  • SRIJAN
    Postbacks: https://kite.trade/docs/connect/v3/postbacks/
    Websocket on_order_update:
    https://kite.trade/forum/discussion/5980/on-order-update-not-working

    Also,this read timeout is due to your connection error. Is it happening only with positions api or other APIs too?? I suugest you to get a good stable network.
  • AAAAAAAAAA
    Just positions API. How would I go about knowing whether I have a good stable network or not? My average upload and download speed is 120 Mbps, so speed is not an issue. How do I determine the stability?
  • SRIJAN
    SRIJAN edited January 2022
    I don't much about network issues. Just read a few articles on this forum. If this error would be happening with every api,then it would be connection error. But if it's only with positions,then i don't know what's causing the. problem. You can try postback or on_order_update as Sujith Sir advised. Or ,you can save the order_id's and fetch order history of the order_id,or the whole orderbook to know the status of your positions.
  • AAAAAAAAAA
    @sujith @SRIJAN I have made some changes. I will see the performance for a few days and let you know how it goes. If theres an issue, should I continue on this thread itself? Kindly advice Sujith. Thanks.
  • AAAAAAAAAA
    @sujith I made it so, that kite.positions() is called once per tick and in some special circumstances and I havent seen that error popped again.
  • sujith
    Why would you need positions upstream data every second? You need to fetch and store data and use that. You shouldn't keep polling positions.
  • AAAAAAAAAA
    @sujith Is that possible using on_order_updates? Could you please point me to an example of its application? Thanks
  • rakeshr
    Yes, you can make use of on_order_update to fetch live order update. Go through python example for the same here.
  • AAAAAAAAAA
    Thank you @rakeshr . I will check it out and revert if I have any queries.

    I also checked out the thread that Mr Sujith provided in his initial response about the P&L. The formula there is > pnl = (sellValue - buyValue) + (netQuantity * lastPrice * multiplier);

    In order to receive the updated P&L with each updated tick,
    1. am I not gonna have to use the API kite.ltp() per tick anyway for anything that I have in position?
    2. Or is it that I would have to subscribe the contracts I might trade before hand in the on_connect method and get the last price from there?

    Is one method more preferable over the other? Thank you!

  • SRIJAN
    The data in on_order_update doesn't have ltp. So, you can go with 2. or use kite.ltp().
  • AAAAAAAAAA
    I see. Using kite.ltp() is definitely preferable because thats what I am already doing. But it also gets called with every tick. I am wondering if that will also become an issue like kite.positions() did.
  • AAAAAAAAAA
    AAAAAAAAAA edited March 2022
    Hello @sujith, I hope its ok to use this old thread as my question relates.

    I have changed the code and now it only calls on kite.positions() when there is a kite.place_order() above it, meaning, only after the algo has ordered something, I call positions to update my own datasheet about my current positions and I use that sheet for the rest of my algo.

    Ever since then the error in question above has not come up, and has worked successfully. Till now that is. Today, the same error was shown multiple times resulting in me missing the entry and exit times and basically making it dysfunctional.

    As per your advise I have stopped polling kite.positions(), and it had largely worked, but I still got this situation back today. Kindly guide me what should I do now?
  • rakeshr
    Was position API timing out? Can you paste the exact log with the request timestamp?
    So, even single position API requests were failing?
    Kindly guide me what should I do now?
    You might have to add some time delay between subsequent requests.
  • AAAAAAAAAA
    Hello @rakeshr I have a delay of sleep(0.2) incase there's the error comes up. What amount of delay do you recommend?

    1. In your view is it possible to keep track of positions live, without ever calling kite.positions()?
    2. I know there's on_order_update, but that only works sometimes. I find the data it returns sparingly in my logs. Could I be doing it wrong?

    Much thanks!
  • rakeshr
    I have a delay of sleep(0.2) incase there's the error comes up. What amount of delay do you recommend?
    0.2 seconds is too less, maybe you can increase it to 1-second or something similar.
    In your view is it possible to keep track of positions live, without ever calling kite.positions()?
    Yes, you can follow this thread. You need to fetch complete positions at the day start and store them on your end. Then just subscribes to WebSocket or ltp quote for live P&L update. And only re-fetch the position, if there is new orders placed i.e new position created.
    I know there's on_order_update, but that only works sometimes. I find the data it returns sparingly in my logs. Could I be doing it wrong?
    on_order_update gets triggered only when there is an order update. So, unless there is any new order update, you won't receive any response on the same.
  • AAAAAAAAAA
    And only re-fetch the position, if there is new orders placed i.e new position created.
    This is what I am doing right now @rakeshr . I call kite.positions() at the very start and have it stored in a variable. I then only call kite.positions() when it is confirmed that there has been a change in position. Even after all this, sometimes, I am getting the error. And those times are enough to cause me a loss worth several days gain due to missed exits.

    Thats why I asked whether it was possible to keep track of the live positions without calling kite.positions() even a single time, except at the very start.
  • SRIJAN
    SRIJAN edited April 2022
    @AAAAAAAAAA ,
    This really looks like your local error . If there was any problem with the positions api,the forum would have been flooded with queries about the api.
    There are only 2 ways to solve your problem:
    1. Use try-except block to handle the exception.
    2. Use on_order_update,and update positions accordingly.

    I don't think anybody can help you more than this.
  • AAAAAAAAAA
    AAAAAAAAAA edited April 2022
    @SRIJAN There's literally a try-except block at the start of this thread. @rakeshr @sujith Is what Srijan said true? The issue is on my end here? Because I think its not. As Sujith has asked me multiple times before to not poll positions, since that was a major issue, I have stopped doing that and it has improved things a lot. However, even after using positions only after the order update, it still gives me that error, although much sparingly. Just asking if I can the same functionality without ever relying on kite.positions.

    Thanks
  • sujith
    This could be connection pool issue, you can create a custom python connection pool and pass it to pykiteconnect
    You can checkout documentation here.

    Sample will be something like this,
    from kiteconnect import KiteConnect
    pool = requests.Session()
    kite = KiteConnect(api_key="your_api_key", pool=pool)
Sign In or Register to comment.