[URGENT] Stop loss order did not get place after 6 mins of sending the order

Hi team

I have been using a logic/script which has been working fine since one month but today it failed.

This is what I do:

  1. Send a market order.
  2. SEND a SL-L order

this code has been working fine till yesterday but today it got some issue and caused some losses.

Today. my first market order, I send a order at 9:31 and it only got placed at 9:33:26 . Its quite weired beacuse I never send the market order at 26th second, I always send the order at 17th second. Exchange Order id is “1400000012579186”. Order ID = “250212010053357”. Please look into this.

After that I always send the stop loss order after market order which only got placed after 6 mins at 09:37:48. Order ID “250212010063722”

Can you please look into these orders and let me know these two things:

  1. Why market order was placed at 9:33:26 instead of 9:31:17.
  2. Why stop loss order did not got placed immediately and why it only got placed after 6 min if market order palcement.

Please not that my script did not throw an error it was just running and got successfully run in 6 mins. Its Its usual run time is only 4 seconds.

Thank you very much in advance.

Happened with one more oder now:

Market Order sent at 2025-02-12 10:40:15 but got placed at 10:42:27 AM
{‘data’: {‘order_id’: ‘250212010184813’}, ‘status’: ‘success’}

@Ketan Can you please help me here.

one more order got delay in market order and stop loss order, Please see the time difefernce. Not sure whats happening today

2025-02-12 10:47:16,181 INFO
NSE_FO|51183 82.95
placing Market order
MKT ORDER
{‘data’: {‘order_id’: ‘250212010204240’}, ‘status’: ‘success’}

placing SL-L order
{‘data’: {‘order_id’: ‘250212010204248’}, ‘status’: ‘success’}
2025-02-12 10:53:49,956

Hi @amit44,

We have reviewed the logs for all your order IDs and found the following:

  1. 250212010053357 → This order reached our server at 09:33:26.
  2. 250212010063722 → This order reached our servers at 09:37:48.
  3. 250212010184813 → This order reached our servers at 10:42:27.

Additionally, we did not observe any increase in latency for order placement. We recommend checking your local network setup and code for any potential issues.

You may also consider using the v3 Order API, which provides latency details in the order response payload. This can help assess the time taken by the server to process orders at your end:
Upstox Developer API - Place Order

Let us know if you need any further assistance.

Thank you!

Why this suddenly happened today, it was working fine. Let say there is some internet issue from my end, dont you think that the script will just throw an error? why it will wait for the order to get placed, if connection between my side and upstox is broken, ideally it will throw some error and not just keep running and wait till the order get filled.

Not sure but let me know if thats the possibility. Let say that my internet connection speed gets really slow while placing the order but that should also impact on the data recieving part. I am getting the data from web socket continuously. Do you think that intersnet speed only impacts the order placement and not the data fetching part. Let me know if that can be the case I can look into.

Are you hosting code at your own setup or in AWS? recommend you try hosting in AWS Mumbai and see if the issue happens there too.

Hi @shrini, I am using my own local server not the AWS.

I will try to improve few things as suggested above:

  1. Look for the internet connetions issue
  2. Move to V3 api

After doing these two things, if problem still persists, I will let team know on this thread.

Thanks.

1 Like

@shrini I also faced the same issue. api-hft.upstox.com endpoint is the culprit.

Issue goes away when I use https://api.upstox.com/v2/order/place instead of https://api-hft.upstox.com/v2/order/place

Hi Kislay, Thanks for sharing this.
were you able to solve this issue by any chance? How frequently you have faced this issue?

I am using python sdk ( upstox python library) for order placement. So I am not sure which rest api link in being used under the hood. Did you make any changes to solve this issue.

Yes I am using https://api.upstox.com/v2/order/place to place orders. You can directly place orders using this endpoint, from python code, bypassing the SDK. Refer to the documentation for python example.

1 Like

Thanks for sharing this workaround. I will try this.

@shrini @Ketan, I faced the same issue today also. market order was running for 2 min and then I had to abort the running code.

As per your suggestion, I tried to check my internet speed and it’s working fine. Basically, I am sshing to my server without any interruption,

I could not try the v3 version of api since I could not find the python sdk part for that.

As other people suggested that this error is random. Yesterday after first two or three orders, rest of them were placed without any delay. And today I placed only one order and it got stuck (kept running)

Any advice would be helpful here.

This is odd. Will have our devs see if they can analyze. The difference between both endpoints is that one routes via CloudFlare and the other is via AWS. Both are highly reliable providers so it’s odd.

Could you run your code on debug/verbose mode to see which part it gets stuck on?

I’m guessing that the DNS resolution suddenly takes longer than normal on one vs the other intermittently.

Sure. Below is the code I am using to send the market order. Today it got stuck on this part only.

Again to re-iterate on the internet issue, I am using upstox live feed since past six month, not a single time it got stuck or had any issue. If connection would have been an issue, it should impact data feed also (thats just my guess).

Also, I remember this happening in Dec’24 also when I was just testing the api.
When I was sending the order, It was getting stuck few times but it did not cause any trouble after that. So since mid-dec to yesterday, It did not throw any error. Only yesterday and today, I faced this issue.

Code for market order I am using. I will report more detail on this if this happens tomorrow also (only if I place a order)

def send_buy_mkt_order(ins_key, qty):
import upstox_client
from upstox_client.rest import ApiException

configuration = upstox_client.Configuration()
configuration.access_token = access_token

api_instance = upstox_client.OrderApi(upstox_client.ApiClient(configuration))

body = upstox_client.PlaceOrderRequest(qty, "I", "DAY", 0.0, "cm", ins_key, "MARKET", "BUY", 0, 0.0, False)

api_version = '2.0'
try:
    api_response = api_instance.place_order(body, api_version)
    print("MKT ORDER")
    print(api_response)
    order_id = api_response.data.order_id
except ApiException as e:
    print("Exception when calling OrderApi->place_order: %s\n" % e)
    api_response = None
    order_id = "ERROR while placing order"

return order_id
1 Like

One thing you can try right away - change the DNS on your machine to 8.8.8.8 (Google) or 1.1.1.1 (Cloudflare) instead of using the DNS given to you from your ISP and see if it helps

Thanks for this suggestion. Let me try this tomorrow and see if this solves the issue.

1 Like

Hi team,

I tried to change the DNS also and now i can confidently say that the issue is just random. Let me share some details for the order I placed today using api.

The most recent order, I placed kept on running for 6 mins and 34 seconds.
Please see the log below.

My order was supposed to be placed at 10:49:17 as usual. But it kept on running for 6 mins 34 second and finally my market order for filled. But one more weird this is that, from my order id it was supposed to fetch buy price to calculate the stop loss. But it is brining it as 0 so my stop loss order also throwing an error since It can not take 0 as price.

PLEAE NOTE that, I placed two orders using the same code before this order and they got filled successfully, stop loss was placed success fully.

AND before that, my first order at 9:24 also faced the same issue. Getting stuck.

so basically this is what happened:

1st order at 9:24 - Got stuck
2nd and 3rd order - Got placed using api-
4th order - the below log file. Got stuck.

2025-02-14 10:49:16,504 INFO Sys path: [‘my/local/path***/Build_NIFTY_FNO_BUY_ORDER__NP__2025-02-14T05-19-01.736/compute_NIFTY_FNO_BUY_ORDER_NP/python-recipe/pyout0qCvY0k2w0LT’, ‘DATA_DIR/lib/python’, ‘/my/local/path***/python’, ‘/usr/lib/python37.zip’, ‘/usr/lib/python3.7’, ‘/usr/lib/python3.7/lib-dynload’, ‘/home/DATA_DIR/code-envs/python/code_env_1/lib/python3.7/site-packages’, ‘my/local/path***/DATA_DIR/jobs/QUANT_PROD/Build_NIFTY_FNO_BUY_ORDER__NP__2025-02-14T05-19-01.736/compute_NIFTY_FNO_BUY_ORDER_NP/python-recipe/pyout0qCvY0k2w0LT/project-python-libs/QUANT_PROD/python’]
2025-02-14 10:49:16,505 INFO Script file: my/local/pathDATA_DIR/jobs/QUANT_PROD/Build_NIFTY_FNO_BUY_ORDER__NP__2025-02-14T05-19-01.736/compute_NIFTY_FNO_BUY_ORDER_NP/python-recipe/pyout0qCvY0k2w0LT/script.py
my/local/path
/DATA_DIR/code-envs/python/code_env_1/lib/python3.7/site-packages/pkg_resources/init.py:121: DeprecationWarning: pkg_resources is deprecated as an API
warnings.warn(“pkg_resources is deprecated as an API”, DeprecationWarning)
my/local/path***/DATA_DIR/code-envs/python/code_env_1/lib/python3.7/site-packages/pkg_resources/init.py:2870: DeprecationWarning: Deprecated call to pkg_resources.declare_namespace('mpl_toolkits').
Implementing implicit namespace packages (as specified in PEP 420) is preferred to pkg_resources.declare_namespace. See Keywords - setuptools 75.8.0.post20250203 documentation
declare_namespace(pkg)
close_price: 22887.6
22900
NSE_FO|63522 197.35
placing Market order
MKT ORDER
{‘data’: {‘order_id’: ‘250214010157651’}, ‘status’: ‘success’}
buy price : 0.0
0.0
0.0
placing SL-L order
/home/DATA_DIR/code-envs/python/code_env_1/lib/python3.7/site-packages/upstox_client/rest.py:45: DeprecationWarning: HTTPResponse.getheaders() is deprecated and will be removed in urllib3 v2.1.0. Instead access HTTPResponse.headers directly.
return self.urllib3_response.getheaders()
Exception when calling OrderApi->place_order: (400)
Reason: Bad Request
HTTP response headers: HTTPHeaderDict({‘Date’: ‘Fri, 14 Feb 2025 05:25:50 GMT’, ‘Content-Type’: ‘application/json’, ‘Transfer-Encoding’: ‘chunked’, ‘Connection’: ‘keep-alive’, ‘Vary’: ‘Origin, Access-Control-Request-Method, Access-Control-Request-Headers’, ‘message’: ‘request failed’, ‘requestId’: ‘ae47d80e-682d-4783-a9fa-6f3d8861b718’, ‘X-Content-Type-Options’: ‘nosniff’, ‘X-XSS-Protection’: ‘1; mode=block’, ‘Cache-Control’: ‘no-cache, no-store, max-age=0, must-revalidate’, ‘Pragma’: ‘no-cache’, ‘Expires’: ‘0’, ‘Strict-Transport-Security’: ‘max-age=31536000 ; includeSubDomains’, ‘X-Frame-Options’: ‘DENY’})
HTTP response body: b’{“status”:“error”,“errors”:[{“errorCode”:“UDAPI1052”,“message”:“The order ''quantity'' cannot be zero”,“propertyPath”:“quantity”,“invalidValue”:0,“error_code”:“UDAPI1052”,“property_path”:“quantity”,“invalid_value”:0},{“errorCode”:“UDAPI1041”,“message”:“The ''price'' and ''trigger_price'' both are required”,“propertyPath”:“price, trigger_price”,“invalidValue”:[0.0,0.0],“error_code”:“UDAPI1041”,“property_path”:“price, trigger_price”,“invalid_value”:[0.0,0.0]},{“errorCode”:“UDAPI1038”,“message”:“Trigger price should be greater than limit price”,“propertyPath”:“price, trigger_price”,“invalidValue”:[0.0,0.0],“error_code”:“UDAPI1038”,“property_path”:“price, trigger_price”,“invalid_value”:[0.0,0.0]}]}’
2025-02-14 10:55:50,445 INFO Initializing dataset writer for dataset QUANT_PROD.NIFTY_FNO_BUY_ORDER

one quick update - recent order at 11:08 passed successfully. So without changing anything orders are not being placed.

By the way I am using python 3.7 in case something changed for this version in upstox python sdk.

Thanks a lot for all the help.

@amit44

Thanks for sharing the details! Since the issue appears to be intermittent and related to order execution delays, I’d recommend using the V3 place order functions in the Upstox API.

The V3 order placement functions provide latency information from Upstox’s side, which can help identify whether the delay is happening at the API level or elsewhere. This should help narrow down the root cause more effectively.

Also, there haven’t been any version-specific changes that would affect Python 3.7, so that shouldn’t be a factor in this issue.

Thanks!

Thanks everyone for your suggestions. Now orders are being placed without any delay.
I used what @Kislay suggested.

Using https://api.upstox.com/v3/order/place to place the orders is working fine.

2 Likes