# 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}")
# 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
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.