J12. Zerodha Kite Connect API: LTP vs OHLC vs Full Quote in Python

 

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 इस्तेमाल किए जाते हैं:

  1. LTP (Last Traded Price) — सबसे हाल में हुई trade की कीमत।
  2. OHLC (Open, High, Low, Close) — वर्तमान trading session की price summary और previous close।
  3. 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 में क्या मिलता है?

  1. instrument_token
  2. last_price

LTP का इस्तेमाल कहाँ करें?

  1. Live price watchlist
  2. Portfolio value monitoring
  3. Price alerts
  4. कई instruments की price monitoring
  5. 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 भी मिलती है, जैसे:

  1. Volume
  2. Average price
  3. Buy और sell market depth
  4. Top five bid levels
  5. Top five ask levels
  6. प्रत्येक 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 कैसे समझें?

  1. Bid: खरीदार जिस कीमत पर खरीदने के लिए order रखते हैं।
  2. Ask: विक्रेता जिस कीमत पर बेचने के लिए order रखते हैं।
  3. Quantity: उस price level पर उपलब्ध buy या sell quantity।
  4. 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

FeatureLTPOHLCFull Quote
Latest traded priceहाँहाँहाँ
Open, High, Low, previous Closeनहींहाँहाँ
Volumeनहींनहींहाँ
Average priceनहींनहींहाँ
Market depthनहींनहींहाँ
मुख्य उपयोगPrice monitoringPrice analysisLiquidity 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 में इस्तेमाल कर सकते हैं:

  1. Watchlist: LTP से latest traded prices दिखाएँ।
  2. Market Overview: OHLC से open, high, low और previous close दिखाएँ।
  3. Liquidity Scanner: Full Quote से bid-ask spread और market depth का analysis करें।
  4. Price Gap Scanner: Open और previous close की तुलना करके gap percentage निकालें।
  5. 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 तीनों के अपने अलग उपयोग हैं।

  1. LTP — कम data में latest price।
  2. OHLC — session price summary और basic analysis।
  3. Full Quote — volume, average price और market depth सहित अधिक विस्तृत जानकारी।

अपने scanner या dashboard में वही quote format चुनें जिसकी वास्तव में जरूरत है। इससे data retrieval अधिक efficient रहेगा और application को maintain करना आसान होगा।




Post a Comment

Previous Post Next Post