Zerodha Kite Connect API: Fetch Live Stock Prices Using Python
Introduction
पिछले tutorial में हमने Zerodha Developer Account setup करना और Kite Connect API के माध्यम से login करना सीखा था।
इस tutorial में हम सीखेंगे कि Python का उपयोग करके Zerodha से market data कैसे प्राप्त करें।
इस tutorial को पूरा करने के बाद आप:
- Zerodha के tradable instruments की पूरी list डाउनलोड कर सकेंगे।
- किसी stock का Instrument Token खोज सकेंगे।
- Last Traded Price (LTP) प्राप्त कर सकेंगे।
- Open, High, Low और Close (OHLC) data निकाल सकेंगे।
- Full Quote और Market Depth प्राप्त कर सकेंगे।
- एक ही API call में कई instruments का data fetch कर सकेंगे।
1. What Is an Instrument Token?
Zerodha में प्रत्येक tradable instrument को पहचानने के लिए एक unique numerical identifier दिया जाता है, जिसे Instrument Token कहते हैं।
यह stocks, futures और options जैसे instruments के market data को पहचानने में मदद करता है।
उदाहरण के लिए, यदि आपको Infosys का market data प्राप्त करना है, तो आप दो तरीकों से instrument identify कर सकते हैं:
- Exchange और Trading Symbol के माध्यम से, जैसे
NSE:INFY - Instrument Token के माध्यम से
Instrument Token खोजने के लिए Zerodha की instrument master list डाउनलोड करनी होती है।
2. Download the Complete Instrument List
Kite Connect API का kite.instruments() method tradable instruments की सूची प्राप्त करने के लिए इस्तेमाल किया जाता है।
सबसे पहले आवश्यक libraries import करें और अपने authenticated KiteConnect object kite का उपयोग करें।
import pandas as pd
# Download all tradable instruments
instruments = kite.instruments()
# Convert the list into a DataFrame
instruments_df = pd.DataFrame(instruments)
# Display the first five rows
display(instruments_df.head())इस DataFrame में instruments से संबंधित कई fields मिलती हैं, जैसे:
instrument_token- exchange
- tradingsymbol
- name
- expiry
- strike
- instrument_type
- segment
Instrument master में कई exchanges और segments के instruments शामिल हो सकते हैं। इसलिए किसी specific stock या contract को खोजते समय सही exchange और trading symbol से filter करना जरूरी है।
3. Find the Instrument Token of Infosys
मान लीजिए आपको NSE पर Infosys का instrument token खोजना है।
इसके लिए exchange और tradingsymbol columns पर filter लगाएँ।
# Filter Infosys on NSE
infy_df = instruments_df[
(instruments_df["exchange"] == "NSE") &
(instruments_df["tradingsymbol"] == "INFY")
]
display(
infy_df[
["exchange", "tradingsymbol", "instrument_token"]
]
)यह code NSE पर INFY के matching instrument record को खोजेगा और उसका instrument token दिखाएगा।
यदि कोई row नहीं मिलती है, तो exchange और trading symbol की spelling जाँचें।
4. Fetch the Last Traded Price (LTP)
LTP वह सबसे हाल की कीमत है जिस पर instrument में trade हुई थी।
Kite Connect API में इसके लिए kite.ltp() method का इस्तेमाल किया जाता है।
Method 1: Exchange and Trading Symbol
ltp_data = kite.ltp(["NSE:INFY"])
print(ltp_data)Example response:
{
"NSE:INFY": {
"instrument_token": 408065,
"last_price": 1599.50
}
}ऊपर दिया गया response केवल illustrative example है। वास्तविक price market के अनुसार बदलती रहेगी।
LTP निकालने के लिए:
ltp = ltp_data["NSE:INFY"]["last_price"]
print("Infosys LTP:", ltp)Method 2: Using the Instrument Token
पहले instrument master से token निकालें और फिर उस token को kite.ltp() में pass करें।
if not infy_df.empty:
infy_token = int(
infy_df.iloc[0]["instrument_token"]
)
ltp_by_token = kite.ltp([infy_token])
print(ltp_by_token)
else:
print("NSE:INFY instrument not found.")इस तरह आप trading symbol के बजाय instrument token से भी LTP प्राप्त कर सकते हैं।
5. Fetch Open, High, Low and Close (OHLC)
यदि आपको केवल LTP नहीं, बल्कि पूरे trading session की price summary चाहिए, तो kite.ohlc() इस्तेमाल करें।
ohlc_data = kite.ohlc(["NSE:INFY"])
print(ohlc_data)Example response:
{
"NSE:INFY": {
"instrument_token": 408065,
"last_price": 1599.50,
"ohlc": {
"open": 1590.10,
"high": 1605.25,
"low": 1587.30,
"close": 1596.00
}
}
}यहाँ close field previous trading session के close को दर्शाती है, न कि वर्तमान session की final closing price।
OHLC data का इस्तेमाल इन कामों के लिए किया जा सकता है:
- Daily high-low range निकालना
- Opening gap calculate करना
- Basic stock screening
- Price movement को monitor करना
- Trading dashboard में daily summary दिखाना
6. Fetch Full Quote and Market Depth
यदि आप stock के buy और sell orders का विवरण देखना चाहते हैं, तो kite.quote() method का उपयोग करें।
quote_data = kite.quote(["NSE:INFY"])
print(quote_data)Full Quote में price summary के अलावा volume, average price और market depth जैसी जानकारी भी मिलती है।
Market depth में सामान्यतः:
- Bids: Buy orders
- Asks: Sell orders
- Price: संबंधित order level की कीमत
- Quantity: उस level पर buy या sell quantity
- Orders: उस level पर orders की संख्या
उदाहरण के लिए, best bid और best ask से spread निकाला जा सकता है।
quote = quote_data["NSE:INFY"]
best_bid = quote["depth"]["buy"][0]["price"]
best_ask = quote["depth"]["sell"][0]["price"]
spread = best_ask - best_bid
print("Best Bid:", best_bid)
print("Best Ask:", best_ask)
print("Spread:", spread)यह code मानकर चलता है कि quote response में buy और sell depth उपलब्ध है। Production application में empty depth या missing fields को भी handle करना चाहिए।
Market depth current supply और demand को समझने में मदद करती है, लेकिन इससे future price movement या order execution की गारंटी नहीं मिलती।
7. Fetch Live Data for Multiple Instruments
Kite Connect API की एक उपयोगी सुविधा यह है कि आप कई instruments को एक request में pass कर सकते हैं।
उदाहरण:
instruments_list = [
"NSE:INFY",
"NSE:RELIANCE",
"NSE:TCS"
]
# Fetch LTP for multiple instruments
ltp_data = kite.ltp(instruments_list)
print(ltp_data)इसी तरह आप OHLC और Full Quote के लिए भी multiple instruments pass कर सकते हैं।
ohlc_data = kite.ohlc(instruments_list)
quote_data = kite.quote(instruments_list)Batch requests से हर stock के लिए अलग API call करने की जरूरत कम हो सकती है। हालांकि request size और API rate limits का पालन करना जरूरी है।
8. Which Method Should You Use?
| Method | Information | Typical Use |
|---|---|---|
kite.instruments() | Instrument master list | Instrument Token खोजने के लिए |
kite.ltp() | Last Traded Price | Watchlist और price monitoring |
kite.ohlc() | Open, High, Low, previous Close और LTP | Daily price analysis |
kite.quote() | Price, volume, average price और market depth | Liquidity और bid-ask analysis |
ध्यान दें कि kite.ltp(), kite.ohlc() और kite.quote() market data snapshots देते हैं। लगातार बदलती prices प्राप्त करने के लिए आगे चलकर WebSocket streaming का इस्तेमाल किया जा सकता है।
Conclusion
इस tutorial में हमने सीखा कि Zerodha Kite Connect API के माध्यम से Python में tradable instruments की list कैसे डाउनलोड करें और Infosys जैसे stock का Instrument Token कैसे खोजें।
हमने यह भी सीखा कि:
kite.ltp()से latest traded price प्राप्त होती है।kite.ohlc()से session price summary मिलती है।kite.quote()से Full Quote और market depth मिलती है।- एक ही request में कई instruments का data fetch किया जा सकता है।
इन concepts की मदद से आप अपना Python-based stock market dashboard, price scanner और algo trading research application विकसित करने की दिशा में आगे बढ़ सकते हैं।