Identify the code in option trading

from kiteconnect import KiteConnect
import pandas as pd
import datetime
import time

=======================

:one: SETUP API

=======================

api_key = “YOUR_API_KEY”
api_secret = “YOUR_API_SECRET”
access_token = “YOUR_ACCESS_TOKEN”

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

=======================

:two: MARKET TREND FUNCTION

=======================

def get_trend(symbol=“NSE:NIFTY”):
“”“Return UP, DOWN or HOLD based on MA crossover”“”
end_date = datetime.date.today()
start_date = end_date - datetime.timedelta(days=30)

# Historical data fetch
data = pd.DataFrame(kite.historical_data(symbol, start_date, end_date, "15minute"))

# Moving averages
data['MA5'] = data['close'].rolling(5).mean()
data['MA20'] = data['close'].rolling(20).mean()

if data['MA5'].iloc[-1] > data['MA20'].iloc[-1]:
    return "UP"
elif data['MA5'].iloc[-1] < data['MA20'].iloc[-1]:
    return "DOWN"
else:
    return "HOLD"

=======================

:three: ATM OPTION SELECTION

=======================

def get_atm_option(symbol=“NSE:NIFTY”, option_type=“CE”):
“”“Return ATM option symbol”“”
ltp = kite.ltp(symbol)[symbol][‘last_price’]
strike_price = round(ltp / 50) * 50 # Nearest 50 points
option_symbol = f"{symbol}{strike_price}{option_type}" # Zerodha style
return option_symbol

=======================

:four: PLACE ORDER WITH SL & TG

=======================

def place_option_order(option_symbol, quantity=1, sl_points=20, tg_points=40):
“”“Place option order and manage SL/TG”“”
ltp = kite.ltp(option_symbol)[option_symbol][‘last_price’]

# Buy order
order_id = kite.place_order(
    variety=kite.VARIETY_REGULAR,
    exchange=kite.EXCHANGE_NSE,
    tradingsymbol=option_symbol,
    transaction_type=kite.TRANSACTION_TYPE_BUY,
    quantity=quantity,
    order_type=kite.ORDER_TYPE_MARKET,
    product=kite.PRODUCT_MIS
)
print(f"✅ Order Placed: {order_id} for {option_symbol} at {ltp}")

sl_price = ltp - sl_points
tg_price = ltp + tg_points

# Monitor price for SL/TG
while True:
    current_price = kite.ltp(option_symbol)[option_symbol]['last_price']
    if current_price <= sl_price:
        kite.place_order(
            variety=kite.VARIETY_REGULAR,
            exchange=kite.EXCHANGE_NSE,
            tradingsymbol=option_symbol,
            transaction_type=kite.TRANSACTION_TYPE_SELL,
            quantity=quantity,
            order_type=kite.ORDER_TYPE_MARKET,
            product=kite.PRODUCT_MIS
        )
        print(f"❌ SL Hit! Sold {option_symbol} at {current_price}")
        break
    elif current_price >= tg_price:
        kite.place_order(
            variety=kite.VARIETY_REGULAR,
            exchange=kite.EXCHANGE_NSE,
            tradingsymbol=option_symbol,
            transaction_type=kite.TRANSACTION_TYPE_SELL,
            quantity=quantity,
            order_type=kite.ORDER_TYPE_MARKET,
            product=kite.PRODUCT_MIS
        )
        print(f"🏆 Target Hit! Sold {option_symbol} at {current_price}")
        break
    time.sleep(15)  # Poll every 15 seconds

=======================

:five: MAIN AUTOMATION LOOP

=======================

def auto_trade():
while True:
try:
trend = get_trend()
print(f":chart_increasing: Market Trend: {trend} at {datetime.datetime.now()}")

        if trend == "UP":
            ce_option = get_atm_option(option_type="CE")
            place_option_order(ce_option)
            
        elif trend == "DOWN":
            pe_option = get_atm_option(option_type="PE")
            place_option_order(pe_option)
            
        else:
            print("⏸️ Market sideways. No trade.")
            
        time.sleep(900)  # Wait 15 minutes before next check
    except Exception as e:
        print(f"⚠️ Error: {e}")
        time.sleep(60)

=======================

RUN BOT

=======================

if name == “main”:
auto_trade()