why some of these forever orders failed and how to resolve it. If I want to place buy , SL and Target in MTF or Cash delivery how I can code it. Please help.
HI @GautamB ,
Refer this code -
import time
from Dhan_Tradehull import Tradehull
client_code = ""
tsl = Tradehull(client_code, mode="pin_totp", pin="", totp_secret="")
name = "Trident"
exchange = "NSE"
quantity = 1
trade_type = "CNC" # CNC = cash delivery, MTF = margin trading facility
entry_price = 26.13
entry_trigger = 26.15
target_price = 9.1
target_trigger = 9.01
sl_price = 8.30
sl_trigger = 8.35
buy_forever_id = tsl.place_forever_order(
tradingsymbol = name,
exchange = exchange,
transaction_type = "BUY",
quantity = quantity,
order_type = "LIMIT",
trade_type = trade_type,
price = entry_price,
trigger_price = entry_trigger,
order_flag = "SINGLE"
)
print(f"BUY forever order placed: {buy_forever_id}")
a = True
while a:
status = tsl.get_order_status(buy_forever_id)
status = str(status).upper().strip()
print(f"BUY order status: {status}")
if status in ["TRADED", "EXECUTED", "COMPLETE", "COMPLETED"]:
a = False
print("BUY order executed. Now placing OCO order.")
elif status in ["REJECTED", "CANCELLED", "CANCELED", "EXPIRED"]:
print(f"BUY order not executed. OCO order skipped. Status: {status}")
else:
print("Check again")
time.sleep(2)
# Run this only after BUY is executed / stock is visible in holding.
exit_forever_id = tsl.place_forever_order(
tradingsymbol = name,
exchange = exchange,
transaction_type = "SELL",
quantity = quantity,
order_type = "LIMIT",
trade_type = trade_type,
price = target_price,
trigger_price = target_trigger,
order_flag = "OCO",
quantity_1 = quantity,
price_1 = sl_price,
trigger_price_1 = sl_trigger
)
print(f"Target + SL OCO forever order placed: {exit_forever_id}")
1 Like