Need Help - Trying to place a Cover Order on dhan using python

Dear Community,

I’m trying to place a Cover order using below method on python, but I’m getting an error. Please provide me some solution so that I can place cover order’s using python.

My code:

print (dhan.place_order(security_id= ‘1333’, exchange_segment= dhan.NSE, transaction_type= dhan.BUY, quantity=1, order_type=dhan.CO, product_type= dhan.INTRA, price=1626, trigger_price=1624.00 ))

Error :
{‘status’: ‘failure’, ‘remarks’: {‘error_code’: ‘RS-9005’, ‘message’: ‘JSON parse error: Cannot deserialize value of type co.dhan.api.model.enums.OrderType from String “CO”: not one of the values accepted for Enum class: [STOP_LOSS, STOP_LOSS_MARKET, MARKET, LIMIT]; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type co.dhan.api.model.enums.OrderType from String “CO”: not one of the values accepted for Enum class: [STOP_LOSS, STOP_LOSS_MARKET, MARKET, LIMIT]\n at [Source: (PushbackInputStream); line: 1, column: 127] (through reference chain: co.dhan.api.model.OrderRequest[“orderType”])’}, ‘data’: ‘’}

It work’s fine with Limit order and market order’s.

Hey @anksliv

First of all, welcome to Dhan Community!
Over here, as I can see, you have added CO as order_type where in fact Cover Order is a product type.

You can add order_type=dhan.LIMIT and product_type=dhan.CO and cover order should be placed. You can always refer to Dhan HQ API Documentation for reference.

Hope this helps!

2 Likes

Thanks @Hardik , It was helpful

1 Like

Hi @Hardik , first question will this work on options buy as well? If i want to buy a call option at market price and add the stop loss then what should be my parameters, could you share it? I see order type as LIMIT, Market, SL, SLM in API documentation. Should it be SLM or Market or it is LIMIT only? Second question, whether product_type should be ‘INTRA’ in this case or ‘CO’? Could you share the request parameters for call option buy at market price with stop loss.

Hey @Rampage

Welcome to Dhan community!

If you are using Python, you can get started with Quick Start on the library.

You can also look at the detailed documentation here at DhanHQ API docs, even for Python.

@Hardik , I have visited those links already, what I am asking is the request parameters.

Hello @Rampage

If you want to place an order with stop loss, you need to place cover order i.e. CO. You need to add Trigger Price parameter for your Stop Loss order.

Here is a sample python function for your reference:

dhan.place_order(security_id='52175',  #NiftyPE
    exchange_segment=dhan.NSE_FNO,
    transaction_type=dhan.BUY,
    quantity=550,
    order_type=dhan.CO,
    product_type=dhan.INTRA,
    trigger_price=200,
    price=0)

This example is with dummy values. Over here, the main entry order will be placed at Market (since price is 0) and Stop Loss order is at 200.

Hi Hardik,

Need one more clarification, If I want to buy a security(equity) and want to hold as in the case of swing trading, what parameter value do I use for validity and product_type, could you share with me the request structure I need to pass? Also, could you tell me which parameters I do not need to pass since this will be a market order. I want to keep the request structure simple and minimal.

Here is my current request,

try:
buy_order = dhan.place_order(
tag=‘’,
transaction_type=dhan.BUY,
exchange_segment=dhan.NSE,
product_type=dhan.MARGIN,
order_type=dhan.MARKET,
validity=‘DAY’,
security_id=S_ID,
quantity=qty,
disclosed_quantity=0,
price=0,
trigger_price=0,
after_market_order=False,
amo_time=‘OPEN’,
bo_profit_value=0,
bo_stop_loss_value=0,
drv_expiry_date=None,
drv_options_type=None,
drv_strike_price=None
)

thanks in advance!

Hello @Rampage

In case of swing trading, you cannot use BO for same, as Bracket Order is valid only for single day.

You can keep the check at your end, using Live Market Feed and place orders whenever price is reached. We do have Forever Order API on our roadmap, till then, you can keep this at your end.

So the parameters I sent you will work right, is that what you mean? Could you please confirm the parameters for validity and product_type I sent you for placing a market order?

Since BO order is only available for intraday, if you wish to place the same, product_type needs to be INTRADAY. The validity is correct here.

Thank you Hardik, I will try to explain better. I do not want to place a BO order, I just want to buy at market price for positional trades, I am not looking to trade intraday. if I want to buy 1 share of TCS at market price and hold, in this case what required parameters should I pass? I got this error today:

Error placing buy order for VEDL: dhanhq.place_order() got an unexpected keyword argument ‘bo_stop_loss_value’

Hello @Rampage

Got it. Adding JSON structure for your reference here:

try:
buy_order = dhan.place_order(
tag=‘’,
transaction_type=dhan.BUY,
exchange_segment=dhan.NSE,
product_type=dhan.CNC,
order_type=dhan.MARKET,
validity=‘DAY’,
security_id=S_ID,
quantity=qty,
disclosed_quantity=0,
price=0,
trigger_price=0,
after_market_order=False,
amo_time=‘OPEN’)
1 Like

Hey Hardik,

How do we close open positions using json.

for ex: If i am long and i want to go short would want to close long positions first and then initiate short positions ( using json ) webhooks

currently i need to manually exit long positions after short position is initiated.
how can we do it using webhooks. ( exit current open positions )

essentially im looking for Exit all positions Json for webhooks.

thanks

Hello @ami_99

Welcome to Dhan Community!

There is no Exit All Position option with Webhooks, as Webhook Orders are for alert based execution from charts. To exit any position, you can simply trigger alert with JSON which has opposite order type of same quantity.

For example, if you have long position, you can configure sell JSON with same quantity to exit the position.

Thanks… Appreciate your time… that worked.

1 Like

Hi Hardik,
Ive placed a BO order with the below parameters, but the stock was immediately purchased even before it touched the trigger price which was much higher than the current price.

dhan.place_order(
tag=‘’,
transaction_type=dhan.BUY,
exchange_segment=dhan.NSE,
product_type=dhan.BO,
order_type=dhan.LIMIT,
validity=‘DAY’,
security_id=‘2303’,
quantity=25,
disclosed_quantity=0,
price=3692,
trigger_price=3691.9,
after_market_order=False,
amo_time=‘OPEN’,
bo_profit_value=40,
bo_stop_loss_Value=40,
drv_expiry_date=None,
drv_options_type=None,
drv_strike_price=None
)

it shoul,d have waited for the price to hit trigger price and buy at the limit price but it didnt but still went ahead and bouhgt it even before the price reached trigger price.
WHat could be the error in the code?

thank you

Hello @abrsetti

Over here, BO order doesn’t work with Trigger Price. So, it is considered as limit order only with the price entered by you.

In case of buy order, if the price is higher than top ask on market depth, then it will be executed immediately as per price matching.