Python Order Place not working despite following documented example

I encountered the following error when I try and buy a Nifty Option


File “C:\Python312\Lib\site-packages\requests\models.py”, line 1024, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://api-hft.upstox.com/v2/order/place


For the json data, I had used the structure provided in API examples. Please see my place_order function below:


def place_order(self, instrument_key, quantity, transaction_type, order_type="MARKET"):
    """Place an order for given symbol and quantity"""
    logger.info(f"Placing {transaction_type} order for {instrument_key}...")
    
    order_data = json.dumps({
        'quantity': quantity,
        'product': 'D',
        'validity': 'DAY',
        'price': 0,
        'tag': 'string',
        'instrument_token': instrument_key,
        'order_type': 'MARKET',
        'transaction_type': 'BUY',
        'disclosed_quantity': 0,
        'trigger_price': 0,
        'is_amo': True,
    })

    logger.info(order_data)

    try:
        response = requests.post(
            f"{self.order_url}/place",
            headers=self.get_auth_headers(),
            json=order_data
        )
        print("Call Issued...")
        response.raise_for_status()
        print("Response Status...")
        data = response.json()
        print("Got Data...")
        df_order = pd.json_normalize(data, record_path='data')
        print("Normalized Data...")
        order_id = df_order['order_id']
        print("Got Order Id...")
        logger.info(f"Order placed successfully. Order ID: {order_id}")
        return order_id
    except Exception as e:
        logger.error(f"Failed to place order: {str(e)}")
        raise

Please see the the json dump below that shows all values formats match the format in developer api example.

2025-03-21 22:14:32,869 - INFO - OTM PUT: NIFTY 22850 PE 27 MAR 25
2025-03-21 22:14:32,870 - INFO - Placing BUY order for NSE_FO|48212…
2025-03-21 22:14:32,870 - INFO - {“quantity”: 1, “product”: “D”, “validity”: “DAY”, “price”: 0, “tag”: “string”, “instrument_token”: “NSE_FO|48212”, “order_type”: “MARKET”, “transaction_type”: “BUY”, “disclosed_quantity”: 0, “trigger_price”: 0, “is_amo”: true}

Would much appreciate any guidance on this roadblock to my automation.

Thanks,
Srinivas

Hi,

Please enter the price and the quantity should be multiple of lot size.

Thanks & Regards

Hi Sanjay,

Thanks for your response. Ok Quantity, will use 75.

I am placing a Market Order (AMO). Shouldn’t the price be 0 for a Market Order?

Would much appreciate an early response.

Thanks & Regards,
Srinivas

i had updated the quantity to 75 and price to 100. Still I get the same error. Please see the dump of the latest json

2025-03-22 13:48:20,107 - INFO - OTM CALL: NIFTY 23850 CE 27 MAR 25
2025-03-22 13:48:20,107 - INFO - OTM PUT: NIFTY 22850 PE 27 MAR 25
2025-03-22 13:48:20,107 - INFO - Placing BUY order for NSE_FO|54719…
2025-03-22 13:48:20,107 - INFO - {“quantity”: 75, “product”: “D”, “validity”: “DAY”, “price”: 100, “tag”: “string”, “instrument_token”: “NSE_FO|54719”, “order_type”: “MARKET”, “transaction_type”: “BUY”, “disclosed_quantity”: 0, “trigger_price”: 0, “is_amo”: true}

Experiencing the same error even with quanity (75) and price (0)
2025-03-22 14:04:02,205 - INFO - OTM CALL: NIFTY 23850 CE 27 MAR 25
2025-03-22 14:04:02,205 - INFO - OTM PUT: NIFTY 22850 PE 27 MAR 25
2025-03-22 14:04:02,205 - INFO - Placing BUY order for NSE_FO|54719…
2025-03-22 14:04:02,205 - INFO - {“quantity”: 75, “product”: “D”, “validity”: “DAY”, “price”: 0, “tag”: “string”, “instrument_token”: “NSE_FO|54719”, “order_type”: “MARKET”, “transaction_type”: “BUY”, “disclosed_quantity”: 0, “trigger_price”: 0, “is_amo”: true}


Something else… Could someone help please…?

I have figured it out myself. It is to do with the tabs that Python editor induced. Since the order is AMO Quantity should be 75 (thanks to Sanjay;s advise) and price should be 0. In addition, I had to replace the tabs in json with spaces. The order was successfully placed.

Closing this query. Thanks Sanjay.

1 Like

Hope API support team sees this. The examples provided need correction. Quantity should be 75 not 1. Please see the detail below:

The JSON that worked is:

{‘quantity’: 75, ‘product’: ‘D’, ‘validity’: ‘DAY’, ‘price’: 0, ‘tag’: ‘NSE_FO|54719’, ‘instrument_token’: ‘NSE_FO|54719’, ‘order_type’: ‘MARKET’, ‘transaction_type’: ‘BUY’, ‘disclosed_quantity’: 0, ‘trigger_price’: 0, ‘is_amo’: True}

Whereas, the documentation example has this:

data = {
‘quantity’: 1,
‘product’: ‘D’,
‘validity’: ‘DAY’,
‘price’: 0,
‘tag’: ‘string’,
‘instrument_token’: ‘NSE_EQ|INE669E01016’,
‘order_type’: ‘MARKET’,
‘transaction_type’: ‘BUY’,
‘disclosed_quantity’: 0,
‘trigger_price’: 0,
‘is_amo’: True,
}

Hello,

The examples in the documentation are for buying stocks, not derivatives. That’s why the quantity is set to 1. For derivatives, you need to input the quantity as a multiple of the lot size (e.g., 75 for NIFTY).

Thanks & Regards,
Sanjay Jain

Thanks Sanjay. A note in the documentation that explicitly states this would be extremely helpful. May be its there but I missed it. However, I suggest that such information be appropriately marked in highlight so that early users like me do not miss. Thanks for your assistance and response.

Regards,
Srinivas