issue in order placing

chandra0412
KeyError: 'NFO:BANKNIFTY2320DEC2023CE'. Check the structure of the quote_data response.
  • chandra0412
    from datetime import datetime, timedelta
    from kiteconnect import KiteConnect
    import time
    from datetime import datetime, timedelta



    # Replace with your Bank Nifty symbol
    symbol = "BANKNIFTY"

    # Define order parameters
    quantity = 1
    order_type = "MARKET"
    product = "MIS" # NRML for normal orders, MIS for intraday

    # Initialize KiteConnect
    kite = KiteConnect(api_key=api_key)
    kite.set_access_token(access_token)

    from datetime import datetime, timedelta

    # Function to get the expiry date for the current week (Wednesday expiry)
    def get_current_week_expiry():
    today = datetime.today()
    # Find the next Wednesday, which is the weekly expiry day
    next_wednesday = today + timedelta((2 - today.weekday() + 7) % 7)
    # Format the expiry date in the required format (DDMMMYYYY, e.g., 20DEC2023)
    expiry_date = next_wednesday.strftime('%d%b%Y').upper()
    return expiry_date

    # Construct the symbol with the current week's expiry
    symbol = "BANKNIFTY"
    current_expiry = get_current_week_expiry()
    symbol_with_expiry = f"{symbol}23{current_expiry}CE"

    # Call kite.quote with the dynamically generated symbol
    quote_data = kite.quote("NFO:" + symbol_with_expiry)

    # Access the ATM values from the quote data
    try:
    atm_call_price = quote_data["NFO:" + symbol_with_expiry]["depth"]["buy"][0]["price"]
    atm_put_price = quote_data["NFO:" + symbol_with_expiry]["depth"]["sell"][0]["price"]

    print(f"ATM Call Price: {atm_call_price}")
    print(f"ATM Put Price: {atm_put_price}")
    except KeyError as e:
    print(f"KeyError: {e}. Check the structure of the quote_data response.")

  • rakeshr
    'NFO:BANKNIFTY2320DEC2023CE'.
    This is a wrong weekly options tradingsymbol. Go through this thread to know more.
Sign In or Register to comment.