HOW TO REDUCE AWS LATENCY?

JATIN7838
Hello everyone. I have an algo running on AWS Mumbai and the order placement latency is 100-130 ms . Although it is not bad but sometimes, a few of the limit orders are not completed. We have tried to take lower sellers from market depth to reduce this issue but it is not that effective. Any idea how to reduce this latency?
Tagged:
  • rakeshr
    a few of the limit orders are not completed
    Didn't get you on this?
    Do you mean, orders were not completed within the prescribed latency range or it was not completed at all with some reason?
  • JATIN7838
    No they were not completed at all
  • JATIN7838
    JATIN7838 edited July 25
    It is due to volatility of market not any issue from api but I want to reduce the latency so there is more
    chance of order execution
  • sujith
    Kite Connect isn't meant for time bound or low latency strategies. We suggest you go for colocation setup at the exchange for these kind of strategies.
  • JATIN7838
    Its not HFT. My only concern is limit orders to minimize slippages and reduce latency
  • sujith
    Kite Connect APIs are meant for the retail use and it is not meant for low latency strategies.
  • JATIN7838
    I went through some threads and saw people were having lower latency from aws mumbai than mine thats why I asked. I just want my limit order to be completed.
  • KamalChhirang
    Please, do let me know, If you find any way to reduce latency without colocation setup.
  • JATIN7838
    Currently, my AWS Mumbai server is taking 130ms for order completion. I have seen in other threads, people are having lower latency than mine. Hope there is a solution other than colo.
  • JATIN7838
    I tried changing my instance type and taking more expensive one with 25gigabit network performance but even that has same output. There is something wrong here can anyone help?


  • DD1365
    By Latency of 100-130 ms you must be referring to the time taken for API call completion, right? For example see below log:

    2022-02-07 04:15:00,244 Starting new HTTPS connection (1): api.kite.trade:443
    2022-02-07 04:15:00,321 https://api.kite.trade:443 "POST /orders/regular HTTP/1.1" 200 None

    In above log, 321ms - 244ms = 77ms is the time taken (lag) by KiteConnect Python API to execute the call

    2022-02-07 05:36:15,669 Starting new HTTPS connection (1): api.kite.trade:443
    2022-02-07 05:36:15,771 https://api.kite.trade:443 "PUT /orders/regular/220207100646694 HTTP/1.1" 200 None

    In above log, 771ms - 669ms = 98ms is the time taken (lag) by KiteConnect Python API to execute the call

    So if you are referring to these lags then it's normal to face lags between 50-200ms if you are using Python API. Problem is not with AWS or KiteConnect, it's just that KiteConnect Python API is a wrapper over the raw HTTP API (curl), and Python is not as fast as languages like .Net (when we are talking about milliseconds) that's why lag is a bit higher.
  • JATIN7838
    So which language/method should I use for least lag?
  • JATIN7838
    JATIN7838 edited July 29

    I think there is something wrong with websocket code. Should there be some multithreading or its fine.
    ss.png 20.5K
  • rakeshr
    Should there be some multithreading or its fine.
    Depends on your computation extensiveness. You can create a new thread for tick computation, but it won't be recommended for the higher number of instrument subscriptions.
    While statement to keep the main thread alive looks fine to me.
    Go through websocket FAQs here.
  • Guhan
    @rakeshr, can you suggest what is higher number of instrument subscriptions. Because currently I am subscribing upto 50 instruments, but I believe in the near future it may increase to 100. Is 100 instruments high?
  • rakeshr
    Is 100 instruments high?
    There is no fixed number to this. Depends on system config and other local factors. You will have to discover this by trial and error.
  • Guhan
    @rakeshr, why I asked is
    I am working on new sock connection algo, I am occasionally getting error,
    " kws.subscribe(z_subs_list)
    File "/usr/local/lib/python3.8/dist-packages/kiteconnect/ticker.py", line 569, in subscribe
    self.ws.sendMessage(
    AttributeError: 'NoneType' object has no attribute 'sendMessage' "

    The error occurs when kws.subscribe(z_subs_list) line is executed
    The issue is, I am unable to reproduce the error again. It happens occasionally.

    below is the skeleton of the code:

    ...........
    ...........
    def on_ticks(ws, ticks):
    ##ticks to DB
    def on_connect(ws, response):
    ##pass
    ...........

    if __name__ == '__main__':
    p1 = multiprocessing.Process(target=func_name,args=(arg1,))
    p1.start()

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

    while True:
    ##z_subs_list = read from file
    kws.subscribe(z_subs_list)
  • SRIJAN
    SRIJAN edited August 8
    @Guhan ,

    The subscribe method uses 'ws' object which is the current initialised websocket object.

    You can't directly call subscribe/unsubscribe.
    You have to call it under on_ticks if you want to change subscriptions dynamically.


    Otherwise,the 'ws' object is not global,so it's NoneType and it has no attributes.

    So,you can directly transfer your subscribe inside on_ticks.
  • SRIJAN
    SRIJAN edited August 8
    I don't know why , the site is not letting me add the GitHub link in the comment,but you can go to line number 573 of ticker.py on the official Python Client documentation on GitHub.

    @sujith Sir, @rakeshr Sir,

    Why is this happening??
  • rakeshr
    @Guhan
    AttributeError: 'NoneType' object has no attribute 'sendMessage' "
    The subscribe method uses 'ws' object which is the current initialized websocket object.
    Correct. As, you can refer to here, subscribe/un-subscribe method uses websocket object(ws) to send subscription/un-subscription request to the server. And ws is initialized when the connection is established successfully i.e in on_connect, refer here. So, you are trying to subscribe here, before the successful websocket connection.
    the site is not letting me add the GitHub link in the comment
    @SRIJAN This CSS link, loads editor toolbar, which might be getting blocked in your network/browser. Maybe you can inspect your browser dev console to see the blocking reason.
  • SRIJAN
    SRIJAN edited August 8
    @rakeshr Sir,

    It was accepted initially.
    But then I edited something in my comment,then it showed:

    'Your comment will be displayed after it is approved '.

    However,the draft was saved.
    So,I commented the same thing I wanted to last time when the site blocked the comment, although this time I removed the GitHub link,then it let me.

    Because,it was approved initially,and I don't face this problem generally, I guess ,the site thought I was trying to spam links.
    Earlier also,I had the same problem,when posting some links, accepted initially,but when I edit something else in the comment,it asks me to wait till the comment is approved.
  • Guhan
    @SRIJAN @rakeshr, yes you are right, I am aware of that too, that's why I have called kws.connect before kws.subscribe part.
    I am using kws.subscribe to dynamically subscribe tokens when required.

    Today I faced error around 09:23, that too after successfully subscribing 2 new tokens. Will adding sleep after kws.connect solve the issue. I am quite perplexed by this issue.
  • SRIJAN
    SRIJAN edited August 8
    Yes,if you use sleep ,you wouldn't encounter this issue.

    Actually,it takes around a second to connect to websocket,but this subscribe call is getting executed before the connection,that is before the 'ws' is initialised as told by Rakesh Sir.

    So,add one second sleep,and then you won't get this error.
Sign In or Register to comment.