help me in this code

chandra0412

# 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)

# 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 f"{symbol}{next_wednesday.strftime('%d%b%Y')}".upper()


# Function to place short straddle order
def place_short_straddle():
current_expiry = get_current_week_expiry()
print(f"Placing orders for expiry: {current_expiry}")

# Check Instrument Details
ltp_response = kite.ltp("NFO:" + f"{symbol}{current_expiry}")
print("LTP Response:", ltp_response)

if "NFO:" + f"{symbol}{current_expiry}" not in ltp_response:
print("Instrument not found or expired. Exiting.")
return None, None

instrument_token = ltp_response["NFO:" + f"{symbol}{current_expiry}"]["instrument_token"]

ce_order_info = kite.place_order(
variety="regular",
exchange="NFO",
tradingsymbol=f"{symbol}{current_expiry}",
quantity=quantity,
transaction_type="SELL",
order_type=order_type,
product=product,
)
pe_order_info = kite.place_order(
variety="regular",
exchange="NFO",
tradingsymbol=f"{symbol}{current_expiry}",
quantity=quantity,
transaction_type="SELL",
order_type=order_type,
product=product,
)

return ce_order_info, pe_order_info



# Function to monitor orders and re-enter if necessary
def monitor_orders(ce_order_info, pe_order_info):
while True:
# Monitor order status
ce_order_status = kite.orders(order_ids=[ce_order_info["order_id"]])[0]
pe_order_status = kite.orders(order_ids=[pe_order_info["order_id"]])[0]

if ce_order_status["status"] == "COMPLETE" and pe_order_status["status"] == "COMPLETE":
print("Straddle order completed successfully.")
break
elif ce_order_status["status"] == "REJECTED" or pe_order_status["status"] == "REJECTED":
print("Order rejected. Exiting.")
break
elif ce_order_status["status"] == "CANCELLED" or pe_order_status["status"] == "CANCELLED":
print("Order cancelled. Exiting.")
break

# Check for stop-loss or target
if ce_order_status["average_price"] <= ce_order_info["average_price"] * 0.8:
print("Stop-loss hit. Exiting.")
break
elif pe_order_status["average_price"] <= pe_order_info["average_price"] * 0.8:
print("Stop-loss hit. Exiting.")
break
elif ce_order_status["average_price"] >= ce_order_info["average_price"] * 1.9:
print("Target hit. Exiting.")
break
elif pe_order_status["average_price"] >= pe_order_info["average_price"] * 1.9:
print("Target hit. Exiting.")
break

time.sleep(60) # Wait for 1 minute before checking again

# Main trading logic
for _ in range(4):
ce_order_info, pe_order_info = place_short_straddle()
if ce_order_info and pe_order_info:
monitor_orders(ce_order_info, pe_order_info)
time.sleep(300) # Wait for 5 minutes before re-entering

print("End of trading.")
ERROR i am getting: Placing orders for expiry: BANKNIFTY20DEC2023
LTP Response: {}
Instrument not found or expired. Exiting.
Sign In or Register to comment.