Using SDK :: Buying a "Delivery" and then selling it using

Hi All,

I am new to Upstox SDK / API and trying to see how I can place a Delivery Order and then Square it off both programmatically.

I am confused whether I need to use the same method using which I placed the order or I need to use some other method by passing the OrderId to signify which holdings I need to sell.

Method I am using to place order:

upstox_client.PlaceOrderRequest(TradeQuantity,"D","DAY",0,instrument_token,"MARKET","BUY",0,0,"false")

Documentation : Upstox Developer API - Documentation of investment and trading platform developer API
SDK : GitHub - upstox/upstox-python: Official Python SDK for accessing Upstox API

Thanks in advance.

There is no particular method for executing a square-off order. To square off an order, you must utilize the same method (PlaceOrderRequest) that was originally used for placing the order.

To perform the square-off, you need to modify the transaction_type parameter based on the type of your initial leg. The square off transaction_type will be the opposite of the order type that was initially placed.

Thanks Pradeep for the reply.

So, technically, if I place a “SELL” order request, then initially the system checks whether there is an already open order/holding for the same instrument & Holding-period (i.e. Intraday or Day) or not.

If an holding exists for that instrument,then next order is executed on the existing order. If not, then a new is created.

Also, example:

Placing BUY Order:

upstox_client.PlaceOrderRequest(10,"D","DAY",0,instrument_token,"MARKET","BUY",0,0,"false")

Placing SELL Order:

upstox_client.PlaceOrderRequest(10,"D","DAY",0,instrument_token,"MARKET","SELL",0,0,"false")

You are logically right. However, the order matching takes place within the exchange system, and ultimately, your trades are settled (credit & debit)

2 Likes

Thank you Pradeep. Very helpful.

1 Like

Hi Pradeep, I am hitting an error while squaring off an existing holding. What am I missing?

Error:

*** AttributeError: 'numpy.int64' object has no attribute 'swagger_types'

Code

ipdb> body = upstox_client.PlaceOrderRequest(quantity=quantity,  product=product, validity=validity,price=price,instrument_token=instrument_token,order_type=order_type,transaction_type=transaction_type,disclosed_quantity=disclosed_quantity,trigger_price= 
trigger_price,is_amo=is_amo )^M

ipdb> api_version = self.api_version
ipdb> api_response =  self.api_instance_OrderApi.place_order(body, api_version)
*** AttributeError: 'numpy.int64' object has no attribute 'swagger_types'


ipdb> body
{'disclosed_quantity': 0,
 'instrument_token': 'NSE_EQ|INE665J01013',
 'is_amo': 'false',
 'order_type': 'MARKET',
 'price': 0,
 'product': 'D',
 'quantity': 3,
 'tag': None,
 'transaction_type': 'SELL',
 'trigger_price': 0,
 'validity': 'DAY'}

@RahulMittal87

Please refer to the following Gist for the sample execution.

Try executing this Gist as a standalone code to isolate the error.

Let us know if you still face issues.

Hi @shanmu
Thanks for the quick response. For now, I squared off the position manually so that my trade doesn’t enter in loss :slight_smile: … Let me try for next trade and revert.

@shanmu : I found the issue. It was the datatype that was leading to issue for “Quantity”

ipdb> type(quantity)
<class 'numpy.int64'>

I made this change and now it looks fine:

        body = {
            "quantity"          :       int(quantity),
            "product"           :       product,
            "validity"          :       validity,
            "price"             :       price,
            "tag"               :       "UPSTOX",
            "instrument_token"  :       instrument_token,
            "order_type"        :       order_type,
            "transaction_type"  :       transaction_type,
            "disclosed_quantity":       disclosed_quantity,
            "trigger_price"     :       trigger_price,
            "is_amo"            :       False
        }

Glad to hear that the reply contributed to the resolution of your problem.

Your support is greatly appreciated.

Thanks for the great support I have received from you guys @shanmu & @Pradeep_Jaiswar . It has been really helpful.

I got one more connected question though, when it comes to selling off a “Holding”, we usually enter that CDSL eDIS Transactions OTP once in a day so that selling can happen on the holdings… How does that work in the SDK case?

  • Do I need to do that operation using the APP so that selling is enabled for the entire day. Are there general steps I can use to do so using the app?
  • Or, there is a method that I can call to overcome this.
  • Or, this is not needed in the SDK ?

In order to enable the sale of your holdings throughout the entirety of the day, and also when selling holdings via SDK, it becomes necessary to follow CDSL steps via either the mobile app or the web app.

However, there exists a viable approach to circumvent this situation. To completely bypass the procedural requirements of CDSL, you can opt-in for DDPI. For additional details, you can refer to this link: How do I activate DDPI (POA) on Upstox? - Upstox Help Center

1 Like

Wonderful information @Pradeep_Jaiswar … Thanks a lot!

Hi @Pradeep_Jaiswar
I was trying to place CO on index options but it was getting rejected, saying its “not allowed”.
So I was just curious, if I place 2 seprate orders , i.e 1 for BUY order and other as a Stop Loss order using the same method
then will the 2nd order be treated as a square off order or a proper Stop Loss order by the system?

hi @CE5464,

Cover orders are not available on index options on Upstox pro app and on cover orders on index options via API is automatically rejected via risk management team.

For second query, the second order will be treated as Stop Loss order by the system.