why is my position getting exited as soon as entered?

being
Hi I am using this code and cant understand why this is happening. As soon as it places the order it also exits it. I am placing the order and then creating a stop loss order for it with the code below. Any help please.

def placeBNOrder(symbol, buy_sell, quantity, sl_price):
# Place an intraday market order on NSE
if buy_sell == "buy":
t_type = kite.TRANSACTION_TYPE_BUY
t_type_sl = kite.TRANSACTION_TYPE_SELL
elif buy_sell == "sell":
t_type = kite.TRANSACTION_TYPE_SELL
t_type_sl = kite.TRANSACTION_TYPE_BUY

# Place the market order
market_order_response = kite.place_order(
tradingsymbol=symbol,
exchange=kite.EXCHANGE_NFO,
transaction_type=t_type,
quantity=quantity,
order_type=kite.ORDER_TYPE_MARKET,
product=kite.PRODUCT_MIS,
variety=kite.VARIETY_REGULAR
)

if market_order_response:
market_order_id = market_order_response["order_id"]
print("Main Buy Order ID:", market_order_id)

# Wait for the market order to complete
max_retries = 10
retries = 0
while retries < max_retries:
time.sleep(2) # Wait for a few seconds before checking order status
order_status = kite.order_status(order_id=market_order_id)
if order_status and order_status["status"] == "COMPLETE":
# If the market order is complete, place the SL order
try:
sl_order_response = kite.place_order(
tradingsymbol=symbol,
exchange=kite.EXCHANGE_NFO,
transaction_type=t_type_sl,
quantity=quantity,
order_type=kite.ORDER_TYPE_LIMIT,
trigger_price=sl_price,
price=sl_price,
product=kite.PRODUCT_MIS,
variety=kite.VARIETY_REGULAR
)
if sl_order_response:
sl_order_id = sl_order_response["order_id"]
print("Stop Loss Order ID:", sl_order_id)
else:
print("Failed to place Stop Loss order.")
except Exception as e:
print(f'Error placing SL for addon position for {symbol}, SL to be set {sl_price}.')
print(e)
return
retries += 1

# If market order is not completed after the retries, cancel the order
kite.cancel_order(order_id=market_order_id, variety=kite.VARIETY_REGULAR)
print("Market order not executed. Order cancelled.")
else:
print("Failed to place Market order.")
Tagged:
Sign In or Register to comment.