I have added pseudocode for both trailing types.
trading_symbol = "NIFTY 05 DEC 24400 CALL" # Example stock symbol
qty = 125 # Example quantity
buying_trigger = 205.3 # Lower
buying_limit_price = buying_trigger + 0.50 # Higher
stop_loss_offset = 181.4 # Example stop-loss offset
target_offset = 33 # Example target price
exchange = "NFO"
ordertype = "STOPLIMIT"
# 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, ordertype, 'BUY', 'MIS')
print(f"Buy Order Placed. Order ID: {buy_entry_orderid}")
time.sleep(0.2)
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)
# Check for pending status and cancel order if stop_loss_offset is breached
if order_status == "PENDING":
current_market_price_dict = tsl.get_ltp_data(names=[trading_symbol])
current_market_price = current_market_price_dict[trading_symbol]
if current_market_price <= stop_loss_offset:
print(f"Market price {current_market_price} is below stop-loss offset {stop_loss_offset}. Canceling order.")
tsl.Dhan.cancel_order(buy_entry_orderid)
print(f"Order {buy_entry_orderid} canceled. Stopping algorithm.")
break # Exit the main loop and stop the algorithm
if order_status == "TRADED":
buy_price = order_details['price']
target_price = buy_price + target_offset
stop_loss_price = stop_loss_offset
sl_limit_price = stop_loss_price - 1
breakeven_happened = "no"
initial_sl_price = stop_loss_price
# 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.2)
while True:
try:
# Monitor for target price
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.2) # Adjust sleep interval if necessary
# 1: if points reach 20 so it should be ctc sl , BREAKEVEN,
if (current_market_price > (buy_price + 20)) and (breakeven_happened == "no"):
stop_loss_price = buy_price
breakeven_happened = "yes"
# 2: normal way it 1point in our favor 1point sl up
# or we can say, the entry_sl_distace should remain same, this way if current_market_price increase by 1 stop_loss_price will also increase by 1, keeping entry_sl_distace constant
entry_sl_distace = (buy_price - initial_sl_price)
stop_loss_price = current_market_price - entry_sl_distace
except Exception as e:
print(f"Error in monitoring target: {traceback.format_exc()}")
time.sleep(0.2)
break
except Exception as e:
print(f"Error in fetching order details or status: {traceback.format_exc()}")
time.sleep(0.2)
except Exception as e:
print(f"Unexpected error: {traceback.format_exc()}")