Unable to place a BUY order with upstox Api

Hi @Pradeep_Jaiswar ,

I am unable place a BUY order using upstox Api
Please find below code
def place_order(self,quantity,instrument_token,tran_type) :
url =f’https://api-v2.upstox.com/order/place

    headers ={
        'accept': 'application/json',
        'Api-Version': '2.0' ,
        'Content-Type': 'application/json',
        'Authorization': f'Bearer {self.access_token}'
        }
    data = {
            "quantity": quantity,
            "product": self.PRODUCT_D,
            "validity":self.VALIDITY_DAY,
            "price": 0.0,
            "tag": self.TAG,
            "instrument_token": instrument_token, #"NSE_EQ|INE848E01016"
            "order_type": self.ORDER_TYPE_MARKET,
            "transaction_type":tran_type ,
            "disclosed_quantity": 0,
            "trigger_price": 0.0,
            "is_amo": self.IS_AMO
            }
    response = requests.post(url,headers=headers,data=data)
    # js_reponse = response.json()["data"]["order_id"]
    print(response.status_code)
    print(response.url)
    print(response.text)
    print(data)

Status code:-
400

End Point:-
https://api-v2.upstox.com/order/place

Please find below dictionary as data:-
{‘quantity’: 15, ‘product’: ‘D’, ‘validity’: ‘DAY’, ‘price’: 0.0, ‘tag’: ‘NA’, ‘instrument_token’: ‘NSE_FO|47344’, ‘order_type’: ‘MARKET’, ‘transaction_type’: ‘BUY’, ‘disclosed_quantity’: 0, ‘trigger_price’: 0.0, ‘is_amo’: False}

Error:-
{“status”:“error”,“errors”:[{“errorCode”:“UDAPI100038”,“message”:“Invalid input passed to the API”,“propertyPath”:null,“invalidValue”:null,“error_code”:“UDAPI100038”,“property_path”:null,“invalid_value”:null}]}

Please have look and fix the issue

Thank You!!

@nagarjuna Kindly rectify the specified line within your code:

response = requests.post(url, headers=headers, json=data)

Ensure that you include json=data as indicated.

For your reference

Feel free to explore our Python SDK available at GitHub - upstox/upstox-python: Official Python SDK for accessing Upstox API, which handles the initial setup for a swift start. Give it a go and inform us about your experience - we’re here to assist you.

Sure Pradeep, I will check and let you know.
Thank you!!

1 Like

Hi @Pradeep_Jaiswar ,

I am able to place order using API. Thank You!!

1 Like

I have used postman for testing the calls but that’s not working

curl --location ‘https://api-v2.upstox.com/order/place
–header ‘accept: application/json’
–header ‘Api-Version: 2.0’
–header ‘Content-Type: application/json’
–header ‘Authorization: Bearer {token added here}’
–data '{
“quantity”: 1,
“product”: “I”,

“validity”: “DAY”,
“price”: 0,
“tag”: “string”,
“instrument_token”: “NSE_INDEX|Nifty Bank”,
“order_type”: “MARKET”,
“transaction_type”: “BUY”,
“disclosed_quantity”: 0,
“trigger_price”: 0,
“is_amo”: false
}’

Response from the call:
{

"status": "error",
"errors": [
    {
        "errorCode": "UDAPI10001",
        "message": "Something went wrong",
        "propertyPath": null,
        "invalidValue": null,
        "error_code": "UDAPI10001",
        "property_path": null,
        "invalid_value": null
    }
]

}

Kindly be aware that trading on the index is prohibited. Please utilize some of the available symbols and attempt trading. Inform us if you encounter any problems with the permissible symbols and we will incorporate clear rejection reasons in our future updates.

@Pradeep_Jaiswar, Can you please provide me the available symbols for trading using API and can you also clarify the following questions.
1.If I place a buy order, what will be the order type(call or put) and the same goes for sell order.
2.If I place a order using Market as the option then in which price will it place the order.

You can locate the accessible trading symbols within the Instruments BOD data on this page: Upstox Developer API - Documentation of investment and trading platform developer API

I recommend gaining a fundamental comprehension of call and put options. You can read an article about call options at this link: What Is Call Option - Definition & Examples, How to Use It

Market orders are fulfilled at the most favorable market price available at the moment the order is placed.

I am unable to place the buy order using python upstox-sdk. I am fetching positions from the portfolio and squaring it of based on my strategy I am using the below code:-

portfolio_api_response = portfolio_api_instance.get_positions(api_version)

for position in portfolio_api_response.data:
data[position.instrument_token] = {
“quantity”: position.quantity,
“product”:position.product,
}

order_api_instance = upstox_client.OrderApi(upstox_client.ApiClient(configuration))
body = upstox_client.PlaceOrderRequest(
quantity=abs(position.quantity),
product=position.product,
validity=‘DAY’,
price=0,
instrument_token=position.instrument_token,
order_type=‘MARKET’,
transaction_type=‘BUY’,
disclosed_quantity=0,
trigger_price=0,
is_amo=False
) # PlaceOrderRequest |
order_api_response = order_api_instance.place_order(body, api_version)
print(order_api_response)

the above code is not giving error but also it is not sending the orders to upstox

kindly help please

I’m planning to place an order, for example: NIFTY247NOV24400CE with an entry price of 100, stoploss at 80, and target at 105. The current market price is 60. Since Upstox API doesn’t support GTT (Good Till Triggered) orders, the solution isn’t straightforward.

Here’s what I intend to do:

  1. Place a Buy Order with a I have to place but order in Regular with SL Lmt. price 100, Trigger price 98 .
  2. Use some kind of webhook or socket connection to monitor the market and check when the price reaches 100. or Webhook trigger (I’m not sure upstox if offering that) once the order has been placed.
  3. Once the price reaches 100, check my portfolio to confirm whether the order was placed and if I have the instrument in my holdings.
  4. If the order is successfully placed, I would then need to place a Sell Regular Limit order at 80 for the stoploss, and another Sell SL Limit order at 105 for the target.

However, I am encountering the following error when subscribing to market data:

“Upstox enabled. Subscribing to Upstox market data…
Name of the token NIFTY07NOV2452400PE
Invalid instrument format: NIFTY07NOV2452400PE
Cannot subscribe. Invalid instrument token: NIFTY07NOV2452400PE.”

my code

async function placeBuyOrder(instrumentToken, quantity, price) {
    try {
        const orderPayload = {
            exchange: 'NSE',
            tradingsymbol: instrumentToken, // NIFTY07NOV2452400PE
            quantity: quantity,
            price: price,
            transaction_type: 'BUY',
            order_type: 'LIMIT',
            product: 'CNC', // Change to 'MIS' for intraday
        };

        const api = new UpstoxClient.OrderApi();
        const response = await api.placeOrder(orderPayload);
        console.log('Buy order placed successfully:', response);
    } catch (error) {
        console.error('Error placing buy order:', error);
    }
}