--
In my previous post — AlgoTrading — Live PNL alerts on Telegram — I went through basics of connecting to zerodha account using kite connect APIs and how we can track PNL and send timely updates to telegram.
In this post I want to list down few examples on how to get LTP, how to place or modify orders etc. When I started with kite connect — I found it hard to find direct examples to help me with coding — I had to go through the documentation and then do lots of trials to get going with APIs.
Stocks — First I will go through Stocks
1. Getting last traded price
2. Placing various orders for stocks
Indexes — Then I will touch on Indexes and how to get last traded prices of few of them
1. Get LTP
Next we get to Options —
1. Get LTP for Calls and Options
2. Place order
Finally I will show some more operations related to orders in general
First we will go through some codes related to stocks
Kite login —
# Kite Login Details
access_token='dfjngjgngtkgjvbngkgjmvnnhdnd' # Access token for Zerodha Kite
kite = KiteConnect(api_key="gnghgngnhgpgg")
kite.set_access_token(access_token)
Fetching last traded price —
# get last traded price
# you can get trading symbols or script names/ticker names from zerodha - go to charts and in the name you can see the trading symbol.
temp = 'NSE:'+'BHARTIARTL'
LTP = kite.quote(temp)[temp]['last_price']# Another approach
LTP = kite.quote('BSE:INFY')['BSE:INFY']['last_price'] # for Infosys
Plaincg order
# Place a market order
def place_order_market(symbol='symb', buysell = 'buy', quantity=1):
if buysell == 'sell':
transaction_type = kite.TRANSACTION_TYPE_SELL
else:
transaction_type = kite.TRANSACTION_TYPE_BUY order_id = kite.place_order(tradingsymbol=symbol,
exchange=kite.EXCHANGE_NSE,
transaction_type=transaction_type,
quantity=quantity,
variety=kite.VARIETY_REGULAR,
order_type=kite.ORDER_TYPE_MARKET,
product=kite.PRODUCT_MIS) # This is for intraday
def place_order_limit(limit_price=1, symbol='symb', buysell = 'buy', quantity=1):
if buysell == 'sell':
transaction_type = kite.TRANSACTION_TYPE_SELL
else:
transaction_type = kite.TRANSACTION_TYPE_BUY
order_id = kite.place_order(tradingsymbol=symbol,
exchange=kite.EXCHANGE_NSE,
transaction_type=transaction_type,
quantity=quantity,
variety=kite.VARIETY_REGULAR,
order_type=kite.ORDER_TYPE_LIMIT,
price=limit_price,
product=kite.PRODUCT_MIS) # this is for intraday
order_id_1 = place_order_market(symbol='BHARTIARTL', buysell = 'buy', quantity=quantity)
limit_price = (int(your_target*10))/10+0.05 # this step makes sure that our price follows a step of 0.05, ie 100.05, 100.1 etc
order_id_2 = place_order_limit(limit_price=limit_price, symbol='BHARTIARTL', buysell = buysell, quantity=quantity_2)
# Direct approach
order_id = kite.place_order(tradingsymbol='BHARTIARTL',
exchange=kite.EXCHANGE_NSE,
transaction_type=kite.TRANSACTION_TYPE_SELL,
quantity=100,
variety=kite.VARIETY_REGULAR,
order_type=kite.ORDER_TYPE_LIMIT,
price=115.25,
product=kite.PRODUCT_MIS) # can be kite.PRODUCT_CNC to carry over the position or cash and carry
# Nifty 50
symbol = 'NIFTY 50'
temp = 'NSE:'+symbol
Nifty_ltp = (kite.quote(temp)[temp]['last_price'])
# Direct
Nifty_ltp = (kite.quote('NSE:Nifty50')['NSE:Nifty50']['last_price'])# Financial Nifty
FinNifty_ltp = (kite.quote('NSE:NIFTY FIN SERVICE')['NSE:NIFTY FIN SERVICE']['last_price'])
# BANK Nifty
BankNifty_ltp = (kite.quote('NSE:NIFTY BANK')['NSE:NIFTY BANK']['last_price'])
Here I will show how to get LTP for Nifty options — call and put both. I will also show how using Nifty LTP you can select the ATM or ITM call/put strike.
# Finding the strike Price
symbol = 'NIFTY 50'
temp = 'NSE:'+symbol
Nifty_ltp = (kite.quote(temp)[temp]['last_price'])CE_SP = int((Nifty_ltp+50)/50)*50 # Get CE strike based on Nifty spot
PE_SP = int((Nifty_ltp)/50)*50 # Get PE Strike based on Nifty spot
# 50 is used in above formulae since Nifty options are currently spaced 50 points apart.
# Get CE LTP
symbol = symbol_1+str(CE_SP)+"CE"
temp = 'NFO:'+symbol
CE_LTP = kite.quote(temp)[temp]['last_price']
# Direct approach
CE_LTP = kite.quote('NFO:NIFTY2341319500CE')['NFO:NIFTY2341319500CE']['last_price'] # for Nifty Call 19500 expiring on 13 April 2023
# You can get symbol names from Zerodha chart view
# In general you can follow below code -
# for weekly The month format is 1 for JAN, 2 for FEB, 3, 4, 5, 6, 7, 8, 9, O(capital o) for October, N for November, D for December.
symbol_1 = 'NIFTY21D09'
# for monthly expiry
symbol_1 = 'NIFTY21NOV'
symbol = symbol_1+str(CE_SP)+"CE"
temp = 'NFO:'+symbol
CE_LTP = kite.quote(temp)[temp]['last_price']
# Direct approach
CE_LTP = kite.quote('NFO:NIFTY2341319500CE)[temp]['last_price'] # for Nifty Call 19500 expiring on 13 April 2023
Place Call order
# for monthly expiry
symbol_1 = 'NIFTY21NOV'symbol = symbol_1+str(CE_SP)+"CE"
temp = 'NFO:'+symbol
CE_LTP = kite.quote(temp)[temp]['last_price']
trig_price = (int(CE_LTP*10))/10 + 0.1
order_id_1 = kite.place_order(tradingsymbol=symbol,
exchange=kite.EXCHANGE_NFO,
transaction_type=kite.TRANSACTION_TYPE_BUY,
quantity=order_quantity,
variety=kite.VARIETY_REGULAR,
order_type=kite.ORDER_TYPE_LIMIT,
price=trig_price,
product=kite.PRODUCT_NRML)
Convert a limit order to market order —
def convert_Order(order_id='1'):
kite.modify_order(order_id = order_id,
variety=kite.VARIETY_REGULAR,
order_type=kite.ORDER_TYPE_MARKET)convert_Order(order_id = order_id)
Modify an order — incase you want to change the quantities or price of an existing order
# You need order_id and then you can pass on new trig_price for your orderdef modify_REGULAR_price(order_id='1', trigger_price=1000, quantity = 25):
kite.modify_order(variety=kite.VARIETY_REGULAR,
order_id = order_id,
quantity=quantity,
trigger_price=trigger_price)
modify_REGULAR_price(order_id=order_id_2, trigger_price=trig_price, quantity=25)
Cancel an existing order
kite.cancel_order(variety=kite.VARIETY_REGULAR,
order_id=order_id,
parent_order_id=None)
Get order details — like trading symbol, quantity, etc.
def get_order_quantity(order_id=1):
my_orders = kite.orders()
for order in my_orders:
if(order['order_id']==order_id):
return order['quantity']
return -1def get_order_tradingsymbol(order_id=1):
my_orders = kite.orders()
for order in my_orders:
if(order['order_id']==order_id):
return order['tradingsymbol']
def get_order_price(order_id=1):
my_orders = kite.orders()
for order in my_orders:
if(order['order_id']==order_id):
return order['average_price']
return -1
quantity = get_order_quantity(order_id=order_id)
buy_price = get_order_price(order_id=order_id)
You can also loop through all the orders as below —
all_orders = kite.orders() for order in all_orders:
order_id = order['order_id']
status = order['status']
order_type = order['transaction_type']
symbol = order['tradingsymbol']
quantity = order['quantity']
# Some of the Order status can be 'COMPLETE', 'CANCELLED', 'REJECTED', etc
Let me know if you need help with more complex order types. Incase of an error in above codes, feel free to get back to me or just drop in a comment.