Take a look

Tech

Placing Orders with Zerodha kite Connect

The order APIs let you place orders of different varieties, modify and cancel pending orders, retrieve the daily order, and more. Here is an example of Python code that uses the Kite Connect API to place a regular buy order for a specific stock/option:

import kiteconnect

# Initialize Kite Connect client
kite = kiteconnect.KiteConnect(api_key="your_api_key")

# Authenticate user and get access token
kite.generate_session("request_token", secret="your_secret")

# Place a buy order for a specific stock/option

def place_buy_order(trading_symbol, quantity):
    """Places buy order based on the input trading symbol and             quantity"""
    try:
        order_id = kite.place_order(tradingsymbol=trading_symbol,
                                    exchange=kite.EXCHANGE_NFO,
                            transaction_type=kite.TRANSACTION_TYPE_BUY,
                       order_type=kite.ORDER_TYPE_MARKET,
                                    quantity=quantity,
                                    variety=kite.VARIETY_REGULAR,
                                    product=kite.PRODUCT_NRML,
                                    validity=kite.VALIDITY_DAY)

        logging.info("Order placed. ID is: {}".format(order_id))
        return order_id
    except Exception as e:
        logging.info("Order placement failed: {}".format(str(e)))
print("Order placed. Order ID:", order_id)

To place a sell order, you need to change the ‘transaction_type’ from BUY to SELL, and the rest of the code will remain the same.

def place_sell_order(trading_symbol, quantity):
    """Places sell order based on the input trading symbol"""
    try:
        order_id = kite.place_order(tradingsymbol=trading_symbol,
                                    exchange=kite.EXCHANGE_NFO,
                                       transaction_type=kite.TRANSACTION_TYPE_SELL,
                                    order_type=kite.ORDER_TYPE_MARKET,
                                    quantity=quantity,
                                    variety=kite.VARIETY_REGULAR,
                                    product=kite.PRODUCT_NRML,
                                    validity=kite.VALIDITY_DAY)
        # if success place stop loss order
        logging.info("Order placed. ID is: {}".format(order_id))
        # print("order_id", order_id)
        return order_id
    except Exception as e:
        logging.info("Order placement failed: {}".format(str(e)))

To place a stop loss option for a sell position, keeping a difference of 3 points between the trigger price and price.

def place_stop_loss_order(trading_symbol, price, quantity, stop_loss):
    """Places stop loss limit order for input trading symbol"""
    try:
        order_id = kite.place_order(tradingsymbol=trading_symbol,
                                    exchange=kite.EXCHANGE_NFO,
                                    transaction_type=kite.TRANSACTION_TYPE_BUY,
                                    quantity=quantity,
                                    trigger_price=price+stop_loss,
                                    price=price+stop_loss+3,
                                    variety=kite.VARIETY_REGULAR,
                                    order_type=kite.ORDER_TYPE_SL,
                                    product=kite.PRODUCT_NRML,
                                    validity=kite.VALIDITY_DAY)
        # if success place stop loss order
        logging.info("Order placed. ID is: {}".format(order_id))
        return order_id
    except Exception as e:
        logging.info("stop loss Order placement failed: {}".format(str(e)))

To place a stop loss option for a Buy position, keeping a difference of 3 points between the trigger price and price.

def place_stop_loss_order(trading_symbol, price, quantity, stop_loss):
    """Places stop loss limit order based on input trading symbol"""
    try:
        order_id = kite.place_order(tradingsymbol=trading_symbol,
                                    exchange=kite.EXCHANGE_NFO,
                                    transaction_type=kite.TRANSACTION_TYPE_SELL,
                                    quantity=quantity,
                                    trigger_price=price-stop_loss,
                                    price=price-stop_loss-3,
                                    variety=kite.VARIETY_REGULAR,
                                    order_type=kite.ORDER_TYPE_SL,
                                    product=kite.PRODUCT_NRML,
                                    validity=kite.VALIDITY_DAY)
        # if success place stop loss order
        logging.info("Order placed. ID is: {}".format(order_id))
        return order_id
    except Exception as e:
        logging.info("stop loss Order placement failed: {}".format(str(e)))

Please note that the above code is just an example, you may need to adjust the parameters based on your specific requirements. For more information, please refer to the below documentation.

https://kite.trade/docs/connect/v3/orders/

Leave a Reply