Order Placement and SL Order Issues via API (JUNE 2025)

Dear Dhan Support Team,

I am encountering issues with order placement and subsequent Stop-Loss (SL) orders while using your API. Specifically, I am facing two main challenges:

  1. Error with Stop-Loss Market (SL-M) Order:
    After placing a buy order, when I attempt to set a subsequent Stop-Loss Market (SL-M) order, I receive the following rejection error from the exchange:

    EXCH:16052: Function Not Available
    

    Here is the exact Python snippet I’m using to place the SL order:

def place_sl_order(sec_id:str, trig:float, qty:int)->str:
       h={"access-token":TOKEN_ID}
       c= f"sl_{random.randint(10000,99999)}"
       sl_pl= {
         "dhanClientId":CLIENT_CODE,"correlationId":c,
         "transactionType":"SELL","exchangeSegment":"NSE_FNO",
         "productType":"MARGIN","orderType":"STOP_LOSS_MARKET","validity":"DAY",
         "securityId":sec_id,"quantity":str(qty),
         "disclosedQuantity":"","price":"",
         "triggerPrice":f"{trig:.2f}","afterMarketOrder":False
       }
       try:
           r= requests.post(ORDERS_URL, json=sl_pl, headers=h, timeout=10)
           d= r.json()
           oid= d.get("orderId"); st= d.get("orderStatus","")
           if st in ("REJECTED","CANCELLED","EXPIRED"):
               err= d.get("omsErrorDescription","No error details")
               print(f"[SL REJECTED] => {err}")
               return None
           print(f"[SL PLACED] => trig={trig:.2f}, OID={oid}, st={st}")
           return oid
       except Exception as e:
           print(f"[SL EXC] => {e}")
           return None

  1. Issue with Order Slicing for Large Quantities:
    When attempting to place large orders (e.g., 1,000 lots or 75,000 quantities), the order slicing does not happen as expected. Below is the code snippet for placing the buy order:

    def place_buy_order(sec_id:str, qty:int)->str:
        h={"access-token":TOKEN_ID}
        cb= f"buy_{random.randint(10000,99999)}"
        buy_pl= {
          "dhanClientId":CLIENT_CODE,"correlationId":cb,
          "transactionType":"BUY","exchangeSegment":"NSE_FNO",
          "productType":"MARGIN","orderType":"MARKET","validity":"DAY",
          "securityId":sec_id,"quantity":str(qty),
          "disclosedQuantity":"","price":"","triggerPrice":"",
          "afterMarketOrder":False
        }
        try:
            r= requests.post(ORDERS_URL, json=buy_pl, headers=h, timeout=10)
            d= r.json()
            st= d.get("orderStatus",""); oid= d.get("orderId")
            if st=="REJECTED":
                err= d.get("omsErrorDescription","No error details")
                print(f"[BUY REJECTED] => {err}")
                return None
            print(f"[BUY PLACED] => OID={oid}, st={st}")
            return oid
        except Exception as e:
            print(f"[BUY EXC] => {e}")
            return None
    

Could you please review the provided snippets and share corrected and working Python snippets that I can directly replace in my script? Your guidance to resolve these issues quickly would be highly appreciated.

Thank you in advance for your prompt assistance.