Skip to main content

Historical OHLCV market data downloader for Zerodha (Kite API)

Project description

PyZData – Zerodha Historical Market Data Downloader

Python License GitHub repo size GitHub last commit GitHub stars

Download historical OHLCV and Open Interest candle data for any stock, index, or F&O instrument from Zerodha — as a Python library, a CLI command, or a browser-based web app.


What you can do with PyZData

  • Download price history for any stock or index listed on Zerodha (NSE, BSE, NFO, MCX)
  • Choose intervals from 1-minute to daily
  • Export clean pandas DataFrames ready for backtesting, charting, or ML
  • Use the web app — no coding needed
  • Use the CLI — one-liner downloads from the terminal
  • Use the Python library — import and integrate into your own scripts

Web Interface (no coding required)

The easiest way to use PyZData is the built-in Streamlit web app.

Run it:

pip install "git+https://github.com/vikassharma545/Historical-Market-data-From-Zerodha.git#egg=pyzdata[web]"
pyzdata-web

Then open http://localhost:8501 in your browser.

Features:

  • Click popular stocks (NIFTY 50, RELIANCE, TCS, HDFC Bank …) — no typing needed
  • Quick date presets: Last Week, Last Month, Last Year, Last 3 Years …
  • Plain-English frequency selector with descriptions
  • Download result as CSV or Excel (.xlsx)
  • Built-in Help tab with step-by-step guides

Installation

# Library only (Python API + CLI)
pip install git+https://github.com/vikassharma545/Historical-Market-data-From-Zerodha.git

# Library + web interface (adds streamlit and openpyxl)
pip install "git+https://github.com/vikassharma545/Historical-Market-data-From-Zerodha.git#egg=pyzdata[web]"

Requirements: Python 3.8+, pandas ≥ 1.3, requests ≥ 2.25

For development:

git clone https://github.com/vikassharma545/Historical-Market-data-From-Zerodha.git
cd Historical-Market-data-From-Zerodha
pip install -e ".[web,dev]"

Python Library

from pyzdata import PyZData, Interval

# Login with enctoken (paste from browser cookies after logging into kite.zerodha.com)
client = PyZData(enctoken="your_enctoken_here")

# OR login with credentials
client = PyZData(user_id="AB1234", password="your_password", totp="123456")

# Find the instrument token for a symbol
token = client.get_instrument_token("NIFTY 50", "NSE")

# Not sure of the exact symbol name? Search for it
results = client.search_instruments("NIFTY", exchange="NSE")
print(results[["tradingsymbol", "instrument_token", "exchange"]])

# Download daily candles for a full year
df = client.get_data(token, "2024-01-01", "2024-12-31", Interval.DAY)
print(df.head())

# Download 1-minute candles with Open Interest (F&O instruments)
fut_token = client.get_instrument_token("NIFTY24JANFUT", "NFO")
df = client.get_data(fut_token, "2024-01-02", "2024-01-25", Interval.MINUTE_1, oi=True)

Output columns: tradingsymbol, datetime, open, high, low, close, volume (+ open_interest when oi=True)


CLI

# Daily data for NIFTY 50 — print to terminal
pyzdata download --enctoken TOKEN --symbol "NIFTY 50" --exchange NSE \
        --start 2024-01-01 --end 2024-12-31

# 1-minute data — save to CSV
pyzdata download --enctoken TOKEN --symbol RELIANCE --exchange NSE \
        --start 2024-01-01 --end 2024-06-30 \
        --interval minute --output reliance.csv

# Login with credentials
pyzdata download --user-id AB1234 --password pw --totp 123456 \
        --symbol RELIANCE --exchange NSE \
        --start 2024-01-01 --end 2024-01-31

# Search for a symbol
pyzdata search --enctoken TOKEN --query HDFC --exchange NSE

# All options
pyzdata download --help
pyzdata search --help

Available Intervals

Enum Interval Best for
Interval.MINUTE_1 1 minute Intraday scalping analysis
Interval.MINUTE_5 5 minutes Intraday charts
Interval.MINUTE_15 15 minutes Intraday / short-term
Interval.MINUTE_30 30 minutes Swing trading
Interval.HOUR_1 1 hour Swing / positional
Interval.DAY Daily Long-term investing & backtesting

Configuration

Copy .env.example to .env and set any values you want to override:

Variable Default Description
PYZDATA_MAX_WORKERS 4 Parallel download threads
PYZDATA_MAX_RETRIES 5 Retry attempts on failures
PYZDATA_TIMEOUT 30 HTTP timeout in seconds
PYZDATA_CACHE_TTL_HOURS 24 How long to cache the instruments list
PYZDATA_LOG_LEVEL WARNING DEBUG / INFO / WARNING / ERROR

Or pass a Config object in code:

from pyzdata import Config, PyZData

cfg = Config(max_workers=8, log_level="DEBUG")
client = PyZData(enctoken="...", config=cfg)

Error Handling

from pyzdata.exceptions import (
    AuthenticationError,    # wrong credentials or expired enctoken
    InstrumentNotFoundError, # symbol not found
    DataFetchError,          # API or network failure
    PyZDataError,            # catch-all base class
)

try:
    client = PyZData(enctoken="...")
    token  = client.get_instrument_token("RELIANCE", "NSE")
    df     = client.get_data(token, "2024-01-01", "2024-12-31", Interval.DAY)
except AuthenticationError as e:
    print(f"Login failed: {e}")
except InstrumentNotFoundError as e:
    print(f"Symbol not found: {e}")
except DataFetchError as e:
    print(f"Download failed: {e}")

Project Structure

pyzdata/
├── client.py        PyZData — main entry point
├── auth.py          Two-step Zerodha login
├── instruments.py   Symbol lookup with 24-hour disk cache
├── downloader.py    Parallel monthly data fetching
├── models.py        Interval enum
├── config.py        Settings + environment variable loading
├── exceptions.py    Typed exception hierarchy
├── cli.py           pyzdata command-line tool
├── _app.py          Streamlit web interface
└── py.typed         PEP 561 type-checking marker

app.py               Local dev launcher for Streamlit
tests/               Unit tests (no credentials needed)
CONTRIBUTING.md      How to contribute
CHANGELOG.md         Release history
SECURITY.md          Security policy

Running Tests

pip install -e ".[dev]"
pytest

No Zerodha account or internet connection needed — all HTTP calls are mocked.


How to get your enctoken

  1. Log in to kite.zerodha.com
  2. Press F12Application tab → Cookieskite.zerodha.com
  3. Copy the value of the enctoken cookie
  4. Paste it into the app or pass it as PyZData(enctoken="...")

The enctoken refreshes each time you log in to Kite.


License

MIT — see LICENSE

Contributing

Contributions are welcome! See CONTRIBUTING.md for guidelines.

Changelog

See CHANGELOG.md for the full release history.

Security

To report a vulnerability, see SECURITY.md.

Author

Built by Vikas Sharma

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyzdata-1.0.0.tar.gz (36.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyzdata-1.0.0-py3-none-any.whl (33.4 kB view details)

Uploaded Python 3

File details

Details for the file pyzdata-1.0.0.tar.gz.

File metadata

  • Download URL: pyzdata-1.0.0.tar.gz
  • Upload date:
  • Size: 36.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pyzdata-1.0.0.tar.gz
Algorithm Hash digest
SHA256 8e1a41b18f2b2cd7389c695e04f7e8258c86a27d76ac664c125c66927737a73b
MD5 e35c78fee498c323f9d07f71ceef900c
BLAKE2b-256 d18e9cfc135c47839758cd27010c293ba3b4c7f5dca060c123b33e3aed1a4f37

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyzdata-1.0.0.tar.gz:

Publisher: publish.yml on vikassharma545/Historical-Market-data-From-Zerodha

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyzdata-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: pyzdata-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 33.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pyzdata-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e2af4f6dffb8e3c7d11fb146943c4729a8d4f2222f5733e260e2a0247dd4097c
MD5 18b868070aed65654cc72ec36e06f2bd
BLAKE2b-256 3fbe6481236b8cf8a6c86969bac8294691b6eb210827a75e17b7dbd51e937e88

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyzdata-1.0.0-py3-none-any.whl:

Publisher: publish.yml on vikassharma545/Historical-Market-data-From-Zerodha

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page