SDK : Placing SL order simulatenously with a new Purchase order

Hi Team,

I am using the SDK v2 version in my code. I would like to place the SL order programmatically when I place the new purchase order.

I was reading through the documentation (upstox-python/docs/PlaceOrderRequest.md at master · upstox/upstox-python · GitHub) and it says that :

**order_type**      Type of order. It can be one of the following MARKET refers to market order LIMIT refers to Limit Order SL refers to Stop Loss Limit SL-M refers to Stop Loss Market

How do I use it?

As of now, when I place the order, I pass “MARKET” value for Order_Type.

  • Do I need to again call the same method with now Ordertype == SL as the field for the same script?
  • How will system know this SL is for which order?
  • If I have two orders for script one as CNC and one has MIS, how will system know this SL is for which?

Thanks in advance.

Regards,
Rahul Mittal

FYI : @Pradeep_Jaiswar , @AbhishekRathore

Thank you for contacting us. Here’s the response to your queries:

  • There’s no need for a separate method invocation. When submitting an SL MARKET order Just add the trigger_price in the same request, as detailed in our documentation. For an SL LIMIT order, though, you need to specify the price as well
  • The SL is associated with the specific stock and order_type, and the system monitors this internally.
  • CNC and MIS represent two distinct order categories. SL order designated for CNC applies only to CNC, while SL set for MIS is exclusively relevant to MIS orders. They are not interchangeable.

I hope the information above is helpful.

1 Like

Thanks for the update @Pradeep_Jaiswar . Sorry but I couldn’t follow it well in terms of how to implement it.

Right now, I execute the trades all as “MARKET”. Below is the example of one of the trades:

{'quantity': 32, 'product': 'D', 'validity': 'DAY', 'price': 0, 'tag': 'UPSTOX', 'instrument_token': 'NSE_EQ|INE749A01030', 'order_type': 'MARKET', 'transaction_type': 'BUY', 'disclosed_quantity': 0, 'trigger_price': 0, 'is_amo': 'false'}

Could you please explain me with the above example on how I can place the stoploss order too while placing the purchase order?

Also, I would like to do following:

  • Trail the stoploss i.e. keep on modifying the stoploss programmatically as and when the LTP goes in my desired direction…
  • Have these SLs in place always. i.e. either they stay active until they are cancelled / expired. Or, I can create a new SL daily if that is not possible.

Hi @Pradeep_Jaiswar, Were you able to review my query above?

@RahulMittal87 Please find the correct example below for your use case.

I’ve made a few modifications: the ‘order_type’ has been updated to ‘SL-M’, and a ‘trigger_price’ has been included for the stop loss feature.

{
  "quantity": 32,
  "product": "D",
  "validity": "DAY",
  "price": 0,
  "tag": "UPSTOX",
  "instrument_token": "NSE_EQ|INE749A01030",
  "order_type": "SL-M",
  "transaction_type": "BUY",
  "disclosed_quantity": 0,
  "trigger_price": 700,
  "is_amo": false
}

If you want a Stop Loss limit order, set the order_type to “SL” and specify the price field as the limit price.

Currently, our system doesn’t support trailing stop loss, but we’re actively working on this feature, which will be available soon. Please note that Stop Loss orders will remain active for one day and won’t continue until the next day. However, you can place a limit order on the following day. To address this limitation, we plan to enable GTT (Good Till Triggered) orders through the API, allowing for orders that remain valid until a specified condition is met or until you cancel them.

I hope the above information helps.

1 Like

Thanks a lot for the details @Pradeep_Jaiswar. Ah understood now, sorry for my lack to knowledge on these types of orders. I have till now being doing direct Market buy and sell based on my code trail…

So, to summarize my understanding (please do correct me if I am wrong).

  1. Instead of doing the “MARKET” order, I should be using SL/SL-M orders as these orders also support the BUY+StopLoss together.
  2. If I am using SL-M, then “price” field is set to 0 so that the order is placed at the Market price
  3. If I am using SL, then “price” field should contain the purchase price for the stock.
  4. In both the cases, the “trigger_price” field is the Stop-loss price i.e. when the price drops/reaches to this price, the trade will be exited by the system.
  5. The StopLoss order expires on the same the day. In this case, how do I place the stoploss order again? Do I need to use the Modify order in the next day and define the trigger_price? Any example will be really helpful.

Thanks again for all the guidance on this @Pradeep_Jaiswar

Hi @Pradeep_Jaiswar and @RahulMittal87

was just scrolling the post… but is this query related to placing the BUY and SL order together in a single order?

let me know if this is possible. cause currently I am placing 1 BUY LIMIT order and 1 separate SL-L order

Also I am trying to trade in Index Options. So please let me know if in my case I can do the same thing and how.

Thanks

Hi @CE5464 / Ajinkya,

Great to hear that my queries coincides with others too :slight_smile:

On SL in the same order:
Yeah, my understanding is till now same that in the same BUY order the SL can be done. However, I am waiting for confirmation from @Pradeep_Jaiswar on my previous message. I might give it a short in the live market tomorrow to see how it works.

On Seperate SL Order:
Since you mentioned that you are placing the seperate SL-L order, I wanted to understand how do you do that. I mean, how do you define that the SL order you are placing is for which previous script/order? Do you have any reference log something like below / or if you could modify it to help me understand.

Sample BUY Request 

{'quantity': 32, 'product': 'D', 'validity': 'DAY', 'price': 0, 'tag': 'UPSTOX', 'instrument_token': 'NSE_EQ|INE749A01030', 'order_type': 'MARKET', 'transaction_type': 'BUY', 'disclosed_quantity': 0, 'trigger_price': 0, 'is_amo': 'false'}

On F&O Trading:
I am also starting doing that using python. I think we should connect :slight_smile: . I’ll send you 1-1 message.

Regards,
Rahul

@Pradeep_Jaiswar : Could you please review the query and confirm my understanding.

Your understanding is correct regarding the “On SL in the same order.”

The “On Separate SL Order” refers to a distinct limit order for the opposite transaction. If the initial transaction were a buy, this would be a sell, and vice versa.

To maintain a stop loss (SL) for the coming days, you can use a Good-Til-Triggered (GTT) order if the price falls below or rises above a certain level. This feature is not yet accessible through the API (available on the web and mobile), but it is currently in development and will be available shortly. In the meantime, you’ll need to set a separate SL order each day as previously mentioned.

Hi @Pradeep_Jaiswar

Regarding :- “Your understanding is correct regarding the “On SL in the same order.””

Isnt this achievable by using CO/OCO orders.

but CO/OCO orders are not allowed in Index/Stock Options contract

Thats why we need to place 2 separate orders correct?
1 for Entry (BUY/SELL) and 1 vice-versa order of SL-Exit (SELL/BUY).

How can we achieve both Entry and SL-Exit criteria using single order without using CO/OCO?

Please let me know.

Thanks

Hi @RahulMittal87

I have 1 concern over your point no. 4.
“In both the cases, the “trigger_price” field is the Stop-loss price i.e. when the price drops/reaches to this price, the trade will be exited by the system.” → here the higlighted part should be entered and not exited

this again raise 1 concerns that how will we be able to set both entry and exit criteria in a single order

@Pradeep_Jaiswar let me know if I am missing anything here.

Thanks

Got it… Just to keep the understanding clear… I made these 3 scenarios

So, @CE5464 your point is that for Scenario #2 & #3, there is no way we define the “BUY” purchase as the order is at Market price?

@Pradeep_Jaiswar : Could you please check whether these field level details looks good along with my understanding of what will happen?

First Column:

  • Placing a buy order without any SL - This action will execute the stock purchase at the current market price.
  • Placing sell order separately - This action will complete the stock sale at the market price. However, it’s important to note a correction: You should set the order type to LIMIT and specify a price. This ensures that your sell order is carried out when the set price is reached instead of at the current market price.

Second Column:

  • Points 1 and 2 are accurate. Regarding the third point, change the order type to SL and define the price at which the stock should be purchased. In this case, the trigger price will be the price you intend to sell.

Third Column:

  • For guidance on this point, refer to the response provided in the second column for the third point.

I recommend familiarizing yourself with market orders, limit orders, and stop-loss orders, and then experimenting with them. By trying these out, you’ll gain a clearer understanding of how they work.

Thank you @Pradeep_Jaiswar for the very elaborative answer. I understood it overall now. I will do further research to understand with the concepts on these.

Hi @Pradeep_Jaiswar

I tried to test the Scenario #3

Gave exact parameters and got a response from the API as below:-

{'status': 'error', 'errors': [{'errorCode': 'UDAPI1037', 'message': 'Trigger price should be less than limit price', 'propertyPath': 'price, trigger_price', 'invalidValue': [699.0, 700.0], 'error_code': 'UDAPI1037', 'property_path': 'price, trigger_price', 'invalid_value': [699.0, 700.0]}]}

What I conlclude from this is we cannot use “SL” or “SL-M” to set a Stop Loss.

these order type are single leg order only…that means can only be used for entering the trade and cannot be used to set exit criteria.

Inorder to set Stop Loss we need to use CO/OCO order only.

So for
1] Scenario #2 :- The Stock will be bought at CMP only when the LTP crosses the trigger price .
2] Scenario #3 :- With the current params it will throw abpve error. but if we change the trigger price to 699 and price to 700 then below scenario will happen.

The Stock will be bought only when the LTP crosses the trigger price of 699 and the price at which the order will execute will be between the range of 699.00 to 700.00.
if there is any seller available between this range then at that price only the trade will happen.

Let me know if my understanding is correct.

Thanks