How to place a BUY Order at 11.15.00 PM using API. Pls write a Python program to buy One Share of Reliance Industries @ Rs.1300/- on NSE on delivery basis.
Hello @Neeraj_Goyal
Welcome to MadeForTrade community!
Maybe someone can help you from Dhan Community here. You can use current time function, which takes system price and then checks to execute function at a particular time.
Sir, nobody is helping. Pls do something.
Hello @Neeraj_Goyal
Just to know more, do you code yourself and facing issue in setting up the logic or new to algo trading itself?
Can recommend resources accordingly.
I am totally Layman. I request someone to write code :
from dhanhq import dhanhq
from datetime import datetime, timedelta
import time
Your DhanHQ credentials
client_id = ā1105063691ā
access_token = āeyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJpc3MiOiJkaGFuIiwicGFydG5lcklkIjoiIiwiZXhwIjoxNzMzODI2NTY1LCJ0b2tlbkNvbnN1bWVyVHlwZSI6IlNFTEYiLCJ3ZWJob29rVXJsIjoiIiwiZGhhbkNsaWVudElkIjoiMTEwNTA2MzY5MSJ9.BFsgI5l3fY19-0YqKq_iZCWhz2CWuWsaXe-NiA1vOV_CttnQL2zpqequZZonpV1PZl1UbHRA1nvpetalGFWGiQā
Initialize the DhanHQ client
dhan = dhanhq(client_id, access_token)
Security ID for Reliance Industries on NSE
security_id = ā1INE733E01010ā # Replace with the correct security ID
Desired order execution time
execution_time = datetime.now().replace(hour=11, minute=45, second=0, microsecond=0)
Calculate the time difference
time_diff = (execution_time - datetime.now()).total_seconds()
if time_diff > 0:
print(f"Waiting for {time_diff} seconds to place the order at {execution_time.time()}ā¦")
time.sleep(time_diff)
# Place the order
order_response = dhan.place_order(
transaction_type=dhan.BUY,
exchange_segment=dhan.NSE,
product_type=dhan.CNC,
order_type=dhan.LIMIT,
validity=dhan.DAY,
security_id=security_id,
quantity=1,
price=354.0
)
print("Order Response:", order_response)
else:
print(āThe specified execution time has already passed.ā)
Following Error is coming when targeted time reaches :
PS C:\Python> & C:/Users/Neeraj/AppData/Local/Programs/Python/Python313/python.exe c:/Python/static_api_example2.py
Waiting for 11.202193 seconds to place the order at 11:45:00ā¦
Order Response: {āstatusā: āfailureā, āremarksā: {āerror_codeā: āDH-906ā, āerror_typeā: āOrder_Errorā, āerror_messageā: āMissing required fields, bad values for parameters etc.ā}, ādataā: {āerrorTypeā: āOrder_Errorā, āerrorCodeā: āDH-906ā, āerrorMessageā: āMissing required fields, bad values for parameters etc.ā}}
Hello @Neeraj_Goyal
Over here, only security_id is incorrect. Looks like you are adding ISIN instead.
Security ID is a unique identifier which you can find in Security ID List: Instrument List - DhanHQ Ver 2.0 / API Document