Please make video on order management using API

Hi Imran sir,

Please make video on order management using API.
like placing order, managing corrosponding orders,
stoploss order, trailing stoplos orders etc.

Thank you!

1 Like

Hello @thakurmhn

We already have this added in our current series as well as earlier youtube playlist. Do check them out on youtube.

1 Like

please share the link

Hello @Himansshu_Joshi

You can refer to this for all information about Order APIs.

Please recommend programer for my order management strategy in Dhan API.
sayendrareddy@gmail.com

Hello @Sayendra_Reddy_D

I cannot personally recommend any programmers here, maybe someone from community can take this up.

Check this out

import time

import traceback

from Dhan_Tradehull_V2 import Tradehul
client_code = "ur id"
token_id    =   "token"

trading_symbol      =   "RELIANCE"  # Example stock symbol
qty                 =   1  # Example quantity
buying_limit_price  =   1293.2 # Higher
buying_trigger      =   1292.8 # Lower
stop_loss_offset    =   7  # Example stop-loss offset
target_offset       =   3  # Example target offset
exchange            =   "NSE"


# Initialize Tradehull
tsl = Tradehull(client_code, token_id)

try:
    # Place buy order
    buy_entry_orderid = tsl.order_placement(trading_symbol, exchange , qty, buying_limit_price, buying_trigger, 'STOPLIMIT', 'BUY', 'MIS')
    print(f"Buy Order Placed. Order ID: {buy_entry_orderid}")
    time.sleep(0.5)

    while True:
        try:
            # Fetch order details and status
            order_details   = tsl.get_order_detail(orderid  =   buy_entry_orderid)
            order_status    = tsl.get_order_status(orderid  =   buy_entry_orderid)
            print(f"Order Status: {order_status}")
            time.sleep(1)

            if order_status == "TRADED" :
                buy_price       = order_details['price']
                target_price    = buy_price + target_offset

                stop_loss_price = buy_price - stop_loss_offset
                sl_limit_price  = stop_loss_price - 1
                

                # Place stop-loss order
                stoploss_orderid    = tsl.order_placement(trading_symbol, exchange, qty, sl_limit_price, stop_loss_price, 'STOPLIMIT', 'SELL', 'MIS')
                print(f"Stop Loss Order Placed. Order ID: {stoploss_orderid}")
                time.sleep(0.5)

                # Monitor for target price
                while True:
                    try:
                        current_market_price_dict   = tsl.get_ltp_data(names=[trading_symbol])
                        current_market_price        = current_market_price_dict[trading_symbol]

                        if current_market_price >= target_price:
                            # Cancel stop-loss order
                            tsl.Dhan.cancel_order(stoploss_orderid)
                            print(f"Stop Loss Order {stoploss_orderid} canceled.")

                            # Place sell order
                            sell_entry_orderid = tsl.order_placement(trading_symbol, exchange, qty, 0, 0, 'MARKET', 'SELL', 'MIS'                            )
                            print(f"Target reached. Selling {trading_symbol} at market price. Sell Order ID: {sell_entry_orderid}")
                            break
                        else:
                            print(f"Buy Price: {buy_price}, Current Price: {current_market_price}, Target: {target_price}")
                            time.sleep (0.5)  # Adjust sleep interval if necessary

                    except Exception as e:
                        print(f"Error in monitoring target: {traceback.format_exc()}")
                        time.sleep(0.5)
                break

        except Exception as e:
            print(f"Error in fetching order details or status: {traceback.format_exc()}")
            time.sleep(0.5)

except Exception as e:
    print(f"Unexpected error: {traceback.format_exc()}")