Place Order Help

What is wrong with this code? Everything looks good to me but its giving error. How to know what is the exact issue?
headers = {
‘accept’: ‘application/json’,
‘Api-Version’: ‘2.0’,
‘Content-Type’: ‘application/json’,
‘Authorization’ : f’Bearer {access_tokens}’
}

data = {
“quantity”: 15,
“product”: “D”,
“validity”: “DAY”,
“price”: 0,
“tag”: None,
“instrument_token”: Instrument,
“order_type”: “MARKET”,
“transaction_type”: “BUY”,
“disclosed_quantity”: 0,
“trigger_price”: 0,
“is_amo”: False
}

print(headers)
print(data)

response = requests.request(‘POST’,‘https://api-v2.upstox.com/order/place’,headers=headers,data=data)

print(response)

Output:
nstrument(exchange=‘NSE_FO’, token=59288, parent_token=26009, symbol=‘banknifty23sep46200ce’, name=‘’, closing_price=2.4, expiry=‘1695839400000’, strike_price=46200.0, tick_size=5.0, lot_size=15, instrument_type=‘OPTIDX’, isin=None)

NSE_FO|59288

{‘accept’: ‘application/json’, ‘Api-Version’: ‘2.0’, ‘Content-Type’: ‘application/json’, ‘Authorization’: ‘Bearer {ACCESS_TOKEN}’}
{‘quantity’: 15, ‘product’: ‘D’, ‘validity’: ‘DAY’, ‘price’: 0, ‘tag’: None, ‘instrument_token’: ‘NSE_FO|59288’, ‘order_type’: ‘MARKET’, ‘transaction_type’: ‘BUY’, ‘disclosed_quantity’: 0, ‘trigger_price’: 0, ‘is_amo’: False}

<Response [400]>

You can take my code reference which is working perfectly for placing order.

Mainly check for typecasting.

if instrument_key:
#print(f"Instrument Key for {EntrySymbol}: {instrument_key}“)
log_message = f"Instrument Key for {EntrySymbol}: {instrument_key}”
log(main_log_text, log_message, success=True)
url = ‘https://api-v2.upstox.com/order/place
headers = {
‘accept’: ‘application/json’,
‘Api-Version’: ‘2.0’,
‘Content-Type’: ‘application/json’,
‘Authorization’: f’Bearer {upstox_access_token}',
}
json_data = {
‘quantity’: int(quantity),
‘product’: product_type,
‘validity’: validity,
‘price’: 0,
‘tag’: ‘string’,
‘instrument_token’: instrument_key,
‘order_type’: TypeOfOrder,
‘transaction_type’: order_side,
‘disclosed_quantity’: 0,
‘trigger_price’: float(stop_Loss),
‘is_amo’: False,
}

I still face the same issue. Looks like using these APIs is very difficult.

url = ‘https://api-v2.upstox.com/order/place
headers = {
‘accept’: ‘application/json’,
‘Api-Version’: ‘2.0’,
‘Content-Type’: ‘application/json’,
‘Authorization’: f’Bearer {access_tokens}',
}

json_data = {
‘quantity’: int(15000),
‘product’: ‘D’,
‘validity’: ‘DAY’,
‘price’: 0.0,
‘tag’: ‘string’,
‘instrument_token’: ‘NSE_FO|56684’,
‘order_type’: ‘MARKET’,
‘transaction_type’: ‘SELL’,
‘disclosed_quantity’: 0.0,
‘trigger_price’: float(0.0),
‘is_amo’: False,
}

print(headers)
print(json_data)

response = requests.request(‘POST’,url,headers=headers,data=json_data)

print(response)

Output:

Instrument(exchange=‘NSE_FO’, token=56684, parent_token=26009, symbol=‘banknifty23100444700ce’, name=‘’, closing_price=174.0, expiry=‘1696357800000’, strike_price=44700.0, tick_size=5.0, lot_size=15, instrument_type=‘OPTIDX’, isin=None)

NSE_FO|56684

{‘accept’: ‘application/json’, ‘Api-Version’: ‘2.0’, ‘Content-Type’: ‘application/json’, ‘Authorization’: ‘Bearer ACCESS_TOKEN’}

{‘quantity’: 15000, ‘product’: ‘D’, ‘validity’: ‘DAY’, ‘price’: 0.0, ‘tag’: ‘string’, ‘instrument_token’: ‘NSE_FO|56684’, ‘order_type’: ‘MARKET’, ‘transaction_type’: ‘SELL’, ‘disclosed_quantity’: 0.0, ‘trigger_price’: 0.0, ‘is_amo’: False}
<Response [400]>

Process finished with exit code 0

Hello @Rohit_K.A ,

Print response in Json format you will get detailed description of response.

        response = requests.post(url, headers=headers, json=json_data)
        response = response.json()  # add this part to print response in json format
        print(response)

Added and it says Invalid argument passed. But doesnt say for which field we are passing the invalid argument.

{‘status’: ‘error’, ‘errors’: [{‘errorCode’: ‘UDAPI100038’, ‘message’: ‘Invalid input passed to the API’, ‘propertyPath’: None, ‘invalidValue’: None, ‘error_code’: ‘UDAPI100038’, ‘property_path’: None, ‘invalid_value’: None}]}

response = requests.request(‘POST’,url,headers=headers,data=json_data)

this looks like wrong one.

Try with this one
json_data = {
‘quantity’: int(quantity),
‘product’: product_type,
‘validity’: validity,
‘price’: 0,
‘tag’: ‘string’,
‘instrument_token’: instrument_key,
‘order_type’: TypeOfOrder,
‘transaction_type’: order_side,
‘disclosed_quantity’: 0,
‘trigger_price’: float(stop_Loss),
‘is_amo’: False,
}
response = requests.post(url, headers=headers, json=json_data)
print(response)
response = response.json()
print(response)

data=json_data this looks like wrong one.

Hello @Rohit_K.A ,

I strongly recommend you to use https://curlconverter.com/ this website and copy pest curl code and convert it into python and just change required fields only in case of need to pass anything.


Like this it will come use same code and run it it will work for sure and I am also using the same.

1 Like

Thanks @Chaitanya_Kalyani for help.

Sample working code here for your request

import requests

url = 'https://api-v2.upstox.com/order/place'
headers = {
'accept': 'application/json',
'Api-Version': '2.0',
'Content-Type': 'application/json',
'Authorization': 'Bearer {your_access_token}',
}
data = {
        "quantity": 1,
        "product": "D",
        "validity": "DAY",
        "price": 0,
        "tag": "something",
        "instrument_token": "NSE_EQ|INE848E01016",
        "order_type": "MARKET",
        "transaction_type": "BUY",
        "disclosed_quantity": 0,
        "trigger_price": 0.0,
        "is_amo": False
 }

response = requests.request('POST',url,headers=headers,json=json_data)
print(response)
1 Like

Thank you, @Chaitanya_Kalyani @Pradeep_Jaiswar !
I almost converted my V1 code to V2. Will get back to you in case if I have any questions.

1 Like