Take profit example using python please

Hello @saisaigraph

You can reach out to our support team at help@dhan.co, or any of our official customer support channels for resolution to your queries. As you may know, this is an open community and threads might get lost - refer to community guidelines.

We try our best to check every open thread but some might get missed or unaddressed, or there might be other community members who might reply to it. Apologies from my side to have missed this particular topic.


On your query, this is occurring as you are placing limit order for both buy and sell. It might happen that your Buy order is not matched (as the ask is higher than the limit price) and later the Sell order gets executed (due to price matching).

How do we handle this . pls suggest with code example please.

@saisaigraph

Instead of Limit order for entry, you can rather place market order, if you intend to get in and out of trades fast.

        # for entering
        response1 = dhan.place_order(
                    transaction_type=dhan.BUY, #ttype_buy_sell,
                    exchange_segment=dhan.NSE,
                    product_type=dhan.INTRA, #, #.INTRA,
                    order_type=dhan.MARKET,
                    security_id=f'{security_id}',
                    quantity=1,
                    price=0                    
                    )
        if response1['status'] == 'success':
            # for exit
            time.sleep(1)
            response2 = dhan.place_order(
                        transaction_type=dhan.SELL, #ttype_buy_sell,
                        exchange_segment=dhan.NSE,
                        product_type=dhan.INTRA, #.INTRA,
                        order_type=dhan.LIMIT,
                        security_id=f'{security_id}',
                        quantity=1,
                        price= take_price, #price*bo_profit_val_calc                    
                        )

But this will only work when your algo knows exact point of entry and exit here.

HArdik
our client wants us to specifically place a limit order only. Any suggestions please.
If we can try to check if the buy limit order is successful and then try to place the sell order then there might be a time gap. Please let me know any suggestions please.
Thanks
Sai

@saisaigraph

Understood. What you can try is placing LIMIT order instead but also polling Trade Book API to get order status at defined interval. If you get order executed as success, then you can place simultaneous take_profit order. I won’t be able to help you with code for this, as this is too custom and will depend on your trading strategy.

Hi Hardik

Heres what i m doing

# get order from dhan api list_orders()
df_orders_from_dhan,fname = get_orders_list_from_dhan()

# get ordr_id of the buy order from local var
ordr_id = dct_args['ordr_id'] 

# get the specific row of that buy orderid 
df_order = df_orders_from_dhan[df_orders_from_dhan['orderId']==ordr_id]

if len(df_order) == 1:

    order_status = df_order['orderStatus'].iloc[0]
    
    if order_status.lower()=='traded':                                

	symbol = dct_args['symbol'] 
	ttype_buy_sell = dct_args['ttype_buy_sell'] 
	security_id = dct_args['security_id']
	qty = dct_args['qty']
	price = dct_args['price']
	bo_profit_val = dct_args['bo_profit_val']
	take_price = price + price*(bo_profit_val) 
	
	

*** is it possible to specify the orderid of the buy order when placing sell order 
so that specific buy orderid is placed for sell and not any other order as there may be many orders for a symbol.securityId ***

	response_sell = dhan.place_order(
	    transaction_type=dhan.SELL,
	    exchange_segment=dhan.NSE,
	    product_type=dhan.INTRA, 
	    order_type=dhan.LIMIT,
	    security_id=f'{security_id}',
	    quantity=1,
	    price= take_price
	    )