Option Chain Algotrade
This files contains all the necessary function required to run the Kite Ticker API to Stream and Fetch Option Data
Install / Use
npx skills add surnr/option-chain-algotradeInstalls into whichever agent you are using.
README
Option Chain Stream for Indian Stock Market built with Kite Ticker APIs
This library utilizes Kite Connect APIs to fetch the Option Chain of all the derivatives traded in the Indian stock market. With support for all exchanges, including NFO, MCX, CDS, and BCD, comprehensive option chain data can be streamed effortlessly in real-time. The library provides a convenient API to fetch the option chain of the assets.
Some major features are
- ✅ Light weight
- ⚡️ Optimized for speed - Uses parallel connections through Python Multiprocessing
- 📈 Supports subscribing to all the derivatives across all exchanges (NFO for equity, MCX, CDS, BCD) in a single API
- ⏱️ No considerable difference in execution time even when subscribed to all the symbols present in Indian exchanges - Uses cache wherever possible to eliminate network calls
- 📊 Filters to remove unwanted options
- 💡 Option chain is enriched by additional data like underlying asset price, lot size etc.
- 🔧 Installable via pip
Prerequisites
This tool requires basic understanding of the working of kite websockets.
You will also need
- Working redis database
- Kite developer account and application secrets (api_key and api_secret)
Installation
git clone https://github.com/SurendarSingh/Option-Chain-Kite-Ticker.git
cd Option-Chain-Kite-Ticker
pip install -r requirements.txt
Usage
There are 2 main classes in this library. OptionStream to fetch and store the option chain in the database. OptionChainFetcher retrieves the stored option chain from the database.
Subscribing to the option chain stream requires you to pass the trading symbols you want to monitor and expiry date. Trading symbol must be prefixed with the exchange to which the derivative belongs. E.g. NFO:HDFCBANK, NFO:RELIANCE, MCX:CRUDEOIL, CDS:EURINR. By adding the symbol NFO:HDFCBANK, all the call and put options of NSE:HDFCBANK will be added to the option chain.
Note that All equity derivatives are present in NFO exchange.
Basic example
Redis service should be running in your machine before following the next steps.
Running this code requires kite developer app secrets (API key, API secret and Access token).
Secrets can be provided
- By placing the secrets json in the filepath
$HOME/.kite/secretsby default or pass any filename to the get_secrets() method. The secrets json looks like this
{
"api_key": "your_api_key",
"api_secret": "your_api_secret",
"access_token": "generated_access_token"
}
- By hard coding the secrets in the file
Here's a simple code snippet to get started:
from option_stream import OptionStream
from helper import get_secrets
from option_chain import OptionChainFetcher
# the option stream start should be in main module
if __name__ == '__main__':
# by default secrets are fetched from ~/.kite/secrets
secrets = get_secrets() # or get_secrets(filename)
# or
# secrets = {
# 'api_key': 'your_api_key',
# 'api_secret': 'your_api_secret',
# 'access_token': 'generated_access_token'
# }
# there is no limit on the number of symbols to subscribe to
symbols = ['NFO:HDFCBANK', 'NFO:INFY', 'NFO:RELIANCE', 'NFO:DRREDDY', 'NFO:EICHERMOT']
# symbols = ['CDS:EURINR', 'CDS:GBPINR', 'CDS:JPYINR', 'CDS:USDINR', 'BCD:EURINR']
# symbols = ['MCX:GOLD', 'MCX:GOLDM', 'MCX:NATURALGAS', 'MCX:NICKEL', 'MCX:SILVER', 'MCX:SILVERM']
stream = OptionStream(symbols, secrets, expiry='23-02-2023')
# start the stream in a background thread
# start will return once the subscription is started and the first ticks are received
# this usually takes 20 sec.
# By default, threaded is False. This allows you to run this process in foreground while you fetch the option chain
# somewhere else.
stream.start(threaded=True)
# start fetching option chain
option_chain_fetcher = OptionChainFetcher()
# option chain for each trading symbol can be fetched in 3 ms
option_chain = option_chain_fetcher.get_option_chain('NFO:HDFCBANK')
# fetch option chain in bulk
option_chains = option_chain_fetcher.get_option_chains(
['NFO:HDFCBANK', 'NFO:INFY', 'NFO:RELIANCE', 'NFO:DRREDDY', 'NFO:EICHERMOT'])
# do some processing here
start method on the OptionStream class starts the process which subscribes to websockets, stores the quotes received and
creates the option chain.
By default, this is a blocking method call. This allows you to fetch the option chain in another process.
You can pass threaded=True to run the option stream in the background.
Ideally you don't want to fetch the option chain the same python module that you use to start the option stream.
Fetching the option chain
Make sure you start the option stream process before you fetch the option chain to avoid receiving the outdated data. You can only get the option chain for the trading symbols subscribed to the OptionStream.
from option_chain import OptionChainFetcher
option_chain_fetcher = OptionChainFetcher()
# option chain for each trading symbol can be fetched in 3 ms
option_chain = option_chain_fetcher.get_option_chain('NFO:HDFCBANK')
while True:
option_chains = option_chain_fetcher.get_option_chains(
['NFO:HDFCBANK', 'NFO:INFY', 'NFO:RELIANCE', 'NFO:DRREDDY', 'NFO:EICHERMOT'])
# do some processing on option chains
break
Option Chain Response:
Example of option chain could be found here.
<details> <summary>See the shortened response here. </summary>{
"trading_symbol": "HDFCBANK",
"segment": "NFO-OPT",
"underlying_value": "1658.1",
"expiry": {
"23-02-2023": [
{
"strike_price": 1600.0,
"ce": {
"bid_quantity": 1650,
"bid_price": 63.4,
"ask_quantity": 550,
"ask_price": 65.2,
"premium": 65.0,
"last_trade_time": "13-02-2023 10:59:43",
"exchange_timestamp": "13-02-2023 12:57:46",
"last_traded_quantity": 550,
"change": 3.916866506794569,
"oi": 398750,
"oi_day_high": 430100,
"oi_day_low": 398750,
"total_buy_quantity": 73700,
"ohlc": {
"open": 60.0,
"high": 66.4,
"low": 52.6,
"close": 62.55
},
"total_sell_quantity": 47300,
"volume": 202400,
"bid": [
{
"quantity": 1650,
"price": 63.4,
"orders": 3
},
{
"quantity": 550,
"price": 63.35,
"orders": 1
},
{
"quantity": 1100,
"price": 63.3,
"orders": 2
},
{
"quantity": 550,
"price": 63.15,
"orders": 1
},
{
"quantity": 550,
"price": 62.7,
"orders": 1
}
],
"ask": [
{
"quantity": 550,
"price": 65.2,
"orders": 1
},
{
"quantity": 550,
"price": 65.25,
"orders": 1
},
{
"quantity": 550,
"price": 65.3,
"orders": 1
},
{
"quantity": 550,
"price": 68.25,
"orders": 1
},
{
"quantity": 550,
"price": 69.0,
"orders": 1
}
],
"tradable": true,
"depth": {
"buy": [
{
"quantity": 1650,
"price": 63.4,
"orders": 3
},
{
"quantity": 550,
"price": 63.35,
"orders": 1
},
{
"quantity": 1100,
"price": 63.3,
"orders": 2
},
{
"quantity": 550,
"price": 63.15,
"orders": 1
},
{
"quantity": 550,
"price": 62.7,
"orders": 1
}
],
"sell": [
{
"quantity": 550,
"price": 65.2,
"orders": 1
},
{
"quantity": 550,
"price": 65.25,
"orders": 1
},
{
"quantity": 550,
"price": 65.3,
"orders": 1
},
{
"quantity": 550,
"price": 68.25,
"orders": 1
},
{
"quantity": 550,
"price": 69.0,
"orders": 1
}
]
},
"instrument_token": 20601602
},
"pe": {
"bid_quantity": 550,
"bid_price": 4.55,
"ask_quantity": 550,
"ask_price": 4.7,
"premium": 4.55,
"last_trade_time": "13-02-2023 10:59:55",
"exchange_timestamp": "13-02-2023 12:57:46",
"last_traded_quantity": 550,
"change": -12.500000000000005,
"oi": 1410200,
"oi_day_high": 1499850,
"oi_day_low": 1408000,
"total_buy_quantity": 151250,
"ohlc": {
"open": 5.05,
"high": 6.0,
"low": 4.25,
"close": 5.2
},
"total_sell_quantity": 191950,
"volume": 2049300,
"bid": [
{
"quantity": 550,
"price": 4
Related Skills
node-connect
385.5kDiagnose OpenClaw Android, iOS, or macOS node pairing, QR/setup code, route, auth, and connection failures.
python-debugpy
385.5kDebug Python with pdb, breakpoint(), post-mortem inspection, and debugpy remote attach.
skill-creator
385.5kCreate, edit, audit, tidy, validate, or restructure AgentSkills and SKILL.md files.
claude-opus-4-5-migration
140.7kMigrate prompts and code from Claude Sonnet 4.0, Sonnet 4.5, or Opus 4.1 to Opus 4.5
