Zerodha Kite Connect API: LTP vs OHLC vs Full Quote in Python
Introduction
Algo Trading और Stock Market Analysis में सही market data प्राप्त करना बहुत जरूरी है। Zerodha Kite Connect API Python के माध्यम से अलग-अलग प्रकार का quote data fetch करने की सुविधा देती है।
Kite Connect में मुख्य रूप से तीन quote formats इस्तेमाल किए जाते हैं:
- LTP (Last Traded Price) — सबसे हाल में हुई trade की कीमत।
- OHLC (Open, High, Low, Close) — वर्तमान trading session की price summary और previous close।
- Full Quote — price information के साथ volume, average price और market depth जैसी अतिरिक्त जानकारी।
इस tutorial में हम समझेंगे कि इन formats में क्या अंतर है और अपने Python trading dashboard या scanner में किसका इस्तेमाल करना चाहिए।
1. LTP — Last Traded Price
LTP किसी instrument की वह कीमत है जिस पर सबसे हाल में trade हुई थी।
यह सबसे lightweight quote format है और watchlist या price monitoring जैसे कामों में उपयोगी है।
Python Example
# Fetch LTP for multiple instruments
ltp_data = kite.ltp([
"NSE:INFY"
])
print(ltp_data)Example Response
{
"NSE:INFY": {
"instrument_token": 408065,
"last_price": 1599.50
}
}LTP में क्या मिलता है?
instrument_token- last_price
LTP का इस्तेमाल कहाँ करें?
- Live price watchlist
- Portfolio value monitoring
- Price alerts
- कई instruments की price monitoring
- Lightweight trading dashboard
ध्यान दें: LTP केवल हाल की traded price बताता है। इसमें Open, High, Low, previous Close और market depth जैसी पूरी जानकारी नहीं होती।
2. OHLC — Open, High, Low, Close
OHLC format instrument की current trading session की price summary देता है। इसमें Open, High, Low और previous session का Close शामिल होता है, साथ में current Last Traded Price भी मिलती है।
Python Example
# Fetch OHLC data
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
}
}
}OHLC का इस्तेमाल कहाँ करें?
Daily price range analysis
Technical analysis
Basic stock screening
Opening price और previous close की तुलना
Daily high-low movement की monitoring
उदाहरण के लिए, आप Open और previous Close की मदद से gap percentage calculate कर सकते हैं:
open_price = ohlc_data["NSE:INFY"]["ohlc"]["open"]
previous_close = ohlc_data["NSE:INFY"]["ohlc"]["close"]
if previous_close:
gap_percent = (
(open_price - previous_close) / previous_close
) * 100
print(f"Gap %: {gap_percent:.2f}%")यहाँ close previous session का close है, न कि आवश्यक रूप से current session की closing price।
महत्वपूर्ण: OHLC price summary देता है, लेकिन bid-ask market depth नहीं देता।
3. Full Quote — Complete Market Quote
Full Quote format में LTP और OHLC के अलावा अतिरिक्त market information भी मिलती है, जैसे:
- Volume
- Average price
- Buy और sell market depth
- Top five bid levels
- Top five ask levels
- प्रत्येक price level पर quantity और order count
यह उन applications के लिए उपयोगी है जहाँ केवल price जानना पर्याप्त नहीं है और market liquidity को भी समझना है।
Python Example
# Fetch full quote
quote_data = kite.quote([
"NSE:INFY"
])
print(quote_data)Example Response Structure
{
"NSE:INFY": {
"last_price": 1599.50,
"volume": 1245678,
"ohlc": {
"open": 1590.10,
"high": 1605.25,
"low": 1587.30,
"close": 1596.00
},
"depth": {
"buy": [
{
"price": 1599.40,
"quantity": 150,
"orders": 3
}
],
"sell": [
{
"price": 1599.55,
"quantity": 200,
"orders": 4
}
]
}
}
}यह एक simplified illustrative response है। वास्तविक API response में अन्य fields और अधिक depth levels भी हो सकते हैं।
Market Depth कैसे समझें?
- Bid: खरीदार जिस कीमत पर खरीदने के लिए order रखते हैं।
- Ask: विक्रेता जिस कीमत पर बेचने के लिए order रखते हैं।
- Quantity: उस price level पर उपलब्ध buy या sell quantity।
- Orders: उस price level पर orders की संख्या।
Bid और ask के अंतर को spread कहा जाता है:
[
\text{Spread} = \text{Best Ask} - \text{Best Bid}
]
उदाहरण के लिए, यदि best bid ₹1599.40 और best ask ₹1599.55 है, तो spread ₹0.15 होगा।
Full Quote liquidity analysis और spread monitoring में उपयोगी है, लेकिन market depth अकेले यह सुनिश्चित नहीं करती कि कोई order उसी price पर execute होगा।
4. LTP vs OHLC vs Full Quote
| Feature | LTP | OHLC | Full Quote |
|---|---|---|---|
| Latest traded price | हाँ | हाँ | हाँ |
| Open, High, Low, previous Close | नहीं | हाँ | हाँ |
| Volume | नहीं | नहीं | हाँ |
| Average price | नहीं | नहीं | हाँ |
| Market depth | नहीं | नहीं | हाँ |
| मुख्य उपयोग | Price monitoring | Price analysis | Liquidity analysis |
सही format कैसे चुनें?
LTP चुनें: जब केवल current traded price चाहिए और कम data के साथ अधिक instruments monitor करने हैं।
OHLC चुनें: जब daily range, opening gap या basic technical analysis करना है।
Full Quote चुनें: जब market depth, volume, bid-ask spread और liquidity का analysis करना है।
5. Best Practices for Efficient API Calls
A. Multiple Instruments को एक Request में Fetch करें
कई instruments का data एक ही request में माँगना आम तौर पर अधिक efficient होता है।
ltp_data = kite.ltp([
"NSE:INFY",
"NSE:RELIANCE",
"BSE:TCS"
])इसके बजाय हर instrument के लिए अलग-अलग API call करना अनावश्यक requests बढ़ा सकता है।
B. जरूरत के अनुसार Quote Format चुनें
यदि आपको केवल price चाहिए, तो Full Quote की जगह LTP इस्तेमाल करें। यदि bid-ask depth की जरूरत नहीं है, तो केवल price monitoring के लिए अतिरिक्त data fetch न करें।
C. API Errors को Handle करें
यदि Full Quote request fail हो जाए, तो जरूरत के अनुसार OHLC या LTP से सीमित जानकारी प्राप्त करने की कोशिश कर सकते हैं।
def get_quote_with_fallback(kite, instrument):
try:
return "quote", kite.quote([instrument])
except Exception as quote_error:
print("Full quote failed:", quote_error)
try:
return "ohlc", kite.ohlc([instrument])
except Exception as ohlc_error:
print("OHLC request failed:", ohlc_error)
try:
return "ltp", kite.ltp([instrument])
except Exception as ltp_error:
print("LTP request failed:", ltp_error)
return None, Noneइस function में यदि कोई request fail होती है, तो अगला हल्का format आजमाया जाता है। सभी requests fail होने पर None लौटता है।
Production application में केवल Exception पकड़ने के बजाय authentication, network और rate-limit errors को अलग-अलग handle करना बेहतर है। API limits का पालन करें और errors के बाद अनावश्यक rapid retries न करें।
D. Price Data को Validate करें
Strategy में data भेजने से पहले यह जाँचें कि price और previous close valid हैं।
def validate_price_data(current_price, previous_close):
if current_price is None or previous_close is None:
return False
if current_price <= 0 or previous_close <= 0:
return False
change_percent = abs(
(current_price - previous_close) / previous_close
) * 100
if change_percent > 20:
return False
return Trueयहाँ 20% केवल illustrative sanity-check threshold है। इसे सभी instruments पर लागू होने वाला circuit limit न समझें। वास्तविक circuit limits instrument और exchange rules पर निर्भर कर सकती हैं।
6. Trading Dashboard में उपयोग
आप अपने Python और Streamlit आधारित trading dashboard में तीनों formats को अलग-अलग modules में इस्तेमाल कर सकते हैं:
- Watchlist: LTP से latest traded prices दिखाएँ।
- Market Overview: OHLC से open, high, low और previous close दिखाएँ।
- Liquidity Scanner: Full Quote से bid-ask spread और market depth का analysis करें।
- Price Gap Scanner: Open और previous close की तुलना करके gap percentage निकालें।
- Trading Research: Price, volume और market depth को दूसरे indicators के साथ evaluate करें।
ध्यान रखें कि ये quote formats snapshots देते हैं। लगातार market updates के लिए polling या Kite Connect का streaming WebSocket इस्तेमाल करने की जरूरत हो सकती है। API limits और instrument coverage का ध्यान रखें।
Conclusion
Zerodha Kite Connect API में LTP, OHLC और Full Quote तीनों के अपने अलग उपयोग हैं।
- LTP — कम data में latest price।
- OHLC — session price summary और basic analysis।
- Full Quote — volume, average price और market depth सहित अधिक विस्तृत जानकारी।
अपने scanner या dashboard में वही quote format चुनें जिसकी वास्तव में जरूरत है। इससे data retrieval अधिक efficient रहेगा और application को maintain करना आसान होगा।