How do I buy an option?

being
being edited July 2023 in General
I have the backend calculations code ready but I am not sure how to buy an option. I tried "BANKNIFTY2372044800PE" as an instrument name but no luck. Any help please?
  • rakeshr
    What's the error?
    Go through the order placement APIs here.
  • being
    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 = 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
    )

    # Wait for the market order to complete
    a = 0
    while a < 10:
    try:
    order_list = kite.orders()
    break
    except:
    print("Can't get orders..retrying")
    a += 1

    # Check if the market order is complete
    market_order_status = None
    for order in order_list:
    if order["order_id"] == market_order:
    market_order_status = order["status"]
    break

    # If the market order is complete, place the SL order
    if market_order_status == "COMPLETE":
    try:
    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
    )
    except Exception as e:
    print(f'Error placing SL for addon position for {symbol}, SL to be set {sl_price}.')
    print(e)
    else:
    kite.cancel_order(order_id=market_order, variety=kite.VARIETY_REGULAR)

    Using this to place order but as soon it triggers it also triggers the sell side and basically buy and then immediately exits it. What could be the issue here? @rakeshr
Sign In or Register to comment.