How to place a BUY Order at 11.15.00 PM using API

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.

1 Like

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