Wednesday, April 7, 2021

Python Code for Straddle Algotrading using Zerodha KITE API

In this post I am sharing code of Short Straddle Order execution using Zerodha Kite API and Python Programming Language explained in Youtube Video 

Like and Subscribe channel for more such videos.

Code:

 


import logging
from datetime import datetime

from dateutil.relativedelta import relativedelta, TH
from kiteconnect import KiteConnect


def get_kite():
    kiteObj = KiteConnect(api_key='API_KEY')
    kiteObj.set_access_token('ACCESS_TOKEN')
    return kiteObj


kite = get_kite()
instrumentsList = None


def getCMP(tradingSymbol):
    quote = kite.quote(tradingSymbol)
    if quote:
        return quote[tradingSymbol]['last_price']
    else:
        return 0


def get_symbols(expiry, name, strike, ins_type):
    global instrumentsList

    if instrumentsList is None:
        instrumentsList = kite.instruments('NFO')

    lst_b = [num for num in instrumentsList if num['expiry'] == expiry and num['strike'] == strike
             and num['instrument_type'] == ins_type and num['name'] == name]
    return lst_b[0]['tradingsymbol']


def place_order(tradingSymbol, price, qty, direction, exchangeType, product, orderType):
    try:
        orderId = kite.place_order(
            variety=kite.VARIETY_REGULAR,
            exchange=exchangeType,
            tradingsymbol=tradingSymbol,
            transaction_type=direction,
            quantity=qty,
            price=price,
            product=product,
            order_type=orderType)

        logging.info('Order placed successfully, orderId = %s', orderId)
        return orderId
    except Exception as e:
        logging.info('Order placement failed: %s', e.message)


if __name__ == '__main__':
    # Find ATM Strike of Nifty
    atm_strike = round(getCMP('NSE:NIFTY 50'), -2)

    next_thursday_expiry = datetime.today() + relativedelta(weekday=TH(1))

    symbol_ce = get_symbols(next_thursday_expiry.date(), 'NIFTY', atm_strike, 'CE')
    symbol_pe = get_symbols(next_thursday_expiry.date(), 'NIFTY', atm_strike, 'PE')

    place_order(symbol_ce, 0, 75, kite.TRANSACTION_TYPE_SELL, KiteConnect.EXCHANGE_NFO, KiteConnect.PRODUCT_MIS,
                KiteConnect.ORDER_TYPE_MARKET)

    place_order(symbol_pe, 0, 75, kite.TRANSACTION_TYPE_SELL, KiteConnect.EXCHANGE_NFO, KiteConnect.PRODUCT_MIS,
                KiteConnect.ORDER_TYPE_MARKET) 

If you have any doubt then write it in comment box below.

Enjoy Coding and Algo Trading. 

Saturday, December 19, 2020

Option Chain Analysis In Index for Intraday Trading

 In Futures and options trading Open Interest is considered to be a very important mertics to track as it represent the oustanding open contracts of the underlying security, for example for Nifty 13500 PE is having open interest as 25 means there are 25 contracts between PE buyer and seller. A new contract is open as soon as new Buyer has been introduced with new seller. If an existing contract has been exchanged between a buyer and seller it doesn't impact the open interest but it increases the volumne of instrument, let's say Person A is having a bought option contract for Nifty 13500 PE and Person B has sold the same contract, after some price action Person B wants to close his trade so he wants to buy the same contract from open market and if this is sold by person C it is just transfer of option contract from person B to person C. If person C wants to increase his traded lot size means he want to sell 1 more lot of Nifty 13500 PE and person D buys 1 lot that means a new contract has been opened on the exchange which increases the Open Interest of the instrument (Nifty 13500 PE).


Usually In Indian derivative market the trend is all big players sell the options and retailers or hedge funds buy, and during Intraday big players move the market as per their desire or positions, this is speculation not concreate but observed for a long time.


So, option interest numbers give an indication about the direction of intraday movement of index or underlying security. for this you need to consider 5-6 strikes on both side to the ATM(At the money, if nifty is trading at 13545 then nearest strike is 13550 which becomes ATM strike) and sum OI of both Puts and Calls which gives about which option is being sold aggresively. 


If put OI sum is greater than Call OI sum that means Puts are being sold by big players means Market is bullish as per OI analysis.


There are many other factors which decide market direction but option chain analysis helps in your trade taken. You should take help from Price action or candlestick pattern as well to support OI analysis.

Sheet is below: Enjoy