How to use order_placement (order_payload), I am getting error - 'errorType': 'Input_Exception', 'errorCode': 'DH-905', 'errorMessage': 'Missing required fields, bad values for parameters etc.'

Hi @Tradehull_Imran ,
Despite trying eveything getting the above error: is there anyting i am missing?

First tried format:
‘’’
def create_order_payload(transaction_type, securityId, quantity=QUANTITY):
try:
# Convert securityId to native int if possible
securityId = int(securityId)
except Exception as e:
print(f"[{datetime.now()}] Warning: Could not convert securityId {securityId} to int: {e}")
return {
“dhanClientId”: client_code,
“correlationId”: str(uuid.uuid4()),
“transactionType”: transaction_type, # “BUY” or “SELL”
“exchangeSegment”: exchangeSegment,
“productType”: productType,
“orderType”: “MARKET”,
“validity”: “DAY”,
“securityId”: securityId,
“quantity”: quantity,
“disclosedQuantity”: “”,
“price”: 0,
“triggerPrice”: 0,
“afterMarketOrder”: False,
“amoTime”: “”,
“boProfitValue”: “”,
“boStopLossValue”: “”
}
‘’’
2nd Format Tried:
‘’’
def create_order_payload(security_id, exchange_segment, transaction_type, quantity, order_type, product_type, price=0):
payload = {
“security_id”: int(security_id),
“exchange_segment”: exchange_segment,
“transaction_type”: transaction_type,
“quantity”: quantity,
“order_type”: order_type,
“product_type”: product_type,
“price”: price, # 0 for market order
“correlation_id”: str(uuid.uuid4()), # generate a unique correlation id
“validity”: “DAY” # you can adjust if needed
}
return payload
‘’’
If anyone can help, would be really great.
@Tradehull_Imran
@Tradehull
@Hardik

No worries, got the workaround.

HI @GideonDhan , how did you fix your issue. Im trying to place order thru URL with same data format you have shown abv, still getting DH-905 error. below is my code, could you please help on what im missing?

url_order =‘https://api.dhan.co/v2/orders
ord_data= {
“dhanClientId”:api_key,
“transactionType”:“BUY”,
“exchangeSegment”:“NSE_EQ”,
“productType”:“CNC”,
“orderType”:“MARKET”,
“validity”:“DAY”,
“securityId”:21174,
“quantity”:1,
“price”:0.0,
“after_market_order” : True

}
df_order = requests.request(“POST”,url_order,headers=headers,data= ord_data).json()

below error message when I run abv code
{‘errorType’: ‘Input_Exception’, ‘errorCode’: ‘DH-905’, ‘errorMessage’: ‘Missing required fields, bad values for parameters etc.’}

Change seucrityId to a string instead of int and try.

Thank you for your immd reply. Corrected securityid to string but still same error. attaching screenshot.

“amoTime” field was missing. Try below code:

import requests
import json

access_token: str = "your_access_token"

order_data = {
    "dhanClientId": "99999999999", # your client id
    "transactionType": "BUY",
    "exchangeSegment": "NSE_EQ",
    "productType": "CNC",
    "orderType": "MARKET",
    "validity": "DAY",
    "securityId": "21174",
    "quantity": 1,
    "price": 0.0,
    "afterMarketOrder": True,
    "amoTime": "OPEN"
}

response = requests.post(
    "https://api.dhan.co/v2/orders",
    headers={
        "Content-Type": "application/json",
        "access-token": access_token
    },
    data=json.dumps(order_data)  # Convert dictionary to JSON string
)

print(response.json())  # Print response as JSON

Thanks Syed. tried adding amoTime, but still same error.
looks weird… is it only for me not working :frowning:

@Hardik @Dhan , could you please help me on the abv code. looks like this code works for others but not for me.

Thanks for your support as always

Thanks

Hello @sdp.jd

Are you trying to place AMO order, after market or is it something you are just trying this?

Hi Hardik, i was trying to place AMO orders on weekend, i was getting this error. Today(18-Feb) tried during market hours with below data still throws same error

order_data = {
“dhanClientId”: api_key, # your client id
“transactionType”: “BUY”,
“exchangeSegment”: “NSE_EQ”,
“productType”: “CNC”,
“orderType”: “MARKET”,
“validity”: “DAY”,
“securityId”: “12018”,
“quantity”: 5,
“price”: 0.0
}

Can you enter logging in python and share the JSON request that is generated. Also, do check if the Client ID is being passed correctly.

my client id is correct, im able to retrieve my holdings & positions.
not sure about generated JSON request, attaching the screen shot. is this what you are referring?

Need the json that you are generating in the logs. Otherwise the code looks good. Also, do check if the security ID is correct.

The data you are passing to the request function is a Python dictionary. You need to convert it to JSON first, like:

response = requests.post(
    "https://api.dhan.co/v2/orders",
    headers={
        "Content-Type": "application/json",
        "access-token": access_token
    },
    data=json.dumps(order_data)  # Convert dictionary to JSON string
)

or just pass order data to json parameter like:

response = requests.post(
    "https://api.dhan.co/v2/orders",
    headers={
        "Content-Type": "application/json",
        "access-token": access_token
    },
    json=order_data  # Pass dictionary directly
)
1 Like

Thank you very much @syedasif11 @Hardik , its working.

1 Like

On postman
{
“dhanClientId”: “1107390303”,
“correlationId”: “7e21faf5-94e4-4acd-8e3b-9a755cfd4ca9”,
“transactionType”: “BUY”,
“exchangeSegment”: “NSE_EQ”,
“productType”: “CNC”,
“orderType”: “LIMIT”,
“validity”: “DAY”,
“securityId”: “1333”,
“quantity”: 1,
“disclosedQuantity”: 0,
“price”: 1945.0,
“triggerPrice”: 0.0,
“afterMarketOrder”: false,
“boProfitValue”: 0.0,
“boStopLossValue”: 0.0
}

In code
val params = mutableMapOf<String, Any>(
“dhanClientId” to dhanClientId,
“correlationId” to order.clientOrderId,
“transactionType” to order.transactionType.name,
“exchangeSegment” to getExchangeSegment(order.security.exchange , security.segment),
“productType” to getProductType(order.product),
“orderType” to getOrderType(order.orderType),
“validity” to getValidity(order.validity),
“securityId” to security.id.toString(),
“quantity” to order.quantity * security.lotSize,
“disclosedQuantity” to disclosedQuantity,
“price” to price,
“triggerPrice” to triggerPrice,
“afterMarketOrder” to false,
“boProfitValue” to 0f,
“boStopLossValue” to 0f,
)

@Hardik @Dhan can you help me with this ?
I am getting an error

{
“errorType”: “Input_Exception”,
“errorCode”: “DH-905”,
“errorMessage”: “Missing required fields, bad values for parameters etc.”
}
not able to figure out where the issue is

When I am creating token via dhan web then place order API (/v2/orders) working but when I create token via API Key and Secret then it’s not working getting error,

{
“errorType”: “Input_Exception”,
“errorCode”: “DH-905”,
“errorMessage”: “Missing required fields, bad values for parameters etc.”
}

The payload is same for both cases, Using postman for request

payload
{
“dhanClientId”: “1000000009”,
“correlationId”: “f169ac60”,
“transactionType”: “BUY”,
“exchangeSegment”: “NSE_FNO”,
“productType”: “MARGIN”,
“orderType”: “MARKET”,
“validity”: “DAY”,
“securityId”: “60455”,
“quantity”: 75,
“disclosedQuantity”: 0,
“price”: 0,
“triggerPrice”: 0.0,
“afterMarketOrder”: false,
“amoTime”: “OPEN”,
“boProfitValue”: 0.0,
“boStopLossValue”: 0.0
}

@Dhan can you help me to fix the issue