AccelPix Data API is a Python library designed to connect and stream market data seamlessly. Leveraging WebSocket and fallback transport mechanisms, this library provides functionalities to access both End of Day (EOD) and live streaming data with ease. It ensures simple and straightforward integration with your web applications, handling all the heavy lifting behind the scenes.
scheme
parameter from the initialize
methodInstall the AccelPix Data API library using pip
:
pip install pix-apidata
Note: A working sample is available at the bottom of this documentation.
Import the necessary modules in your Python script:
import asyncio from pix_apidata import *
Initialize the API with your credentials:
api = apidata_lib.ApiData() event_loop = asyncio.get_event_loop() apiKey = "api-access-key" # Provided by your data vendor apiHost = "apidata.accelpix.in" # Provided by your data vendor await api.initialize(apiKey, apiHost) # *** IMPORTANT *** # Ensure that you wait for the `initialize(...)` method to complete before making any other API calls.
Use the following REST API to retrieve master data. This endpoint is specifically for master data downloads due to the larger data size.
{ "xid": 1, "tkr": "20MICRONS", "atkr": null, "ctkr": null, "exp": "1970-01-01 00:00:00", "utkr": null, "inst": "EQUITY", "a3tkr": null, "sp": 0.00, "tk": 16921 }
The AccelPix Data API provides various callbacks to handle live streaming data effectively.
api.on_trade_update(on_trade)
# Callback to listen for trade data api.on_trade_update(on_trade) def on_trade(msg): print(msg) # Trade message t = apidata_models.Trade(msg) print(t.ticker, t.oi) # Access object attributes like id, kind, ticker, segment, price, qty, volume, oi
{ "id": 0, "ticker": "NIFTY-1", "segmentId": 2, "time": 1293704493, "price": 13958.6, "qty": 75, "volume": 1587975, "oi": 9700275, "kind": "T" }
api.on_tradeSnapshot_update(on_tradeSnapshot)
# Callback to listen for trade snapshots, called during the subscription process # Listen to `on_trade_update` callback for continuous stream data api.on_tradeSnapshot_update(on_tradeSnapshot) def on_tradeSnapshot(msg): print(msg) # TradeSnapshot message t = apidata_models.Trade(msg) print(t.ticker, t.oi) # Access object attributes like id, kind, ticker, segment, price, qty, volume, oi
{ "id": 0, "ticker": "NIFTY-1", "segmentId": 2, "time": 1293704493, "price": 13958.6, "qty": 75, "volume": 1587975, "oi": 9700275, "kind": "T" }
api.on_best_update(on_best)
# Callback to listen for bid, ask, and respective quantities api.on_best_update(on_best) def on_best(msg): print(msg) # Best message b = apidata_models.Best(msg) print(b.ticker, b.bidPrice) # Access object attributes like segmentId, kind, bidQty, askPrice, askQty
{ "ticker": "NIFTY-1", "segmentId": 2, "kind": "B", "bidPrice": 13957.45, "bidQty": 75, "askPrice": 13958.8, "askQty": 75, "time": 1293704493 }
api.on_refs_update(on_ref)
# Callback to listen for changes in open, high, low, close, open interest, and average data api.on_refs_update(on_ref) def on_ref(msg): print(msg) # Refs message ref = apidata_models.Refs(msg) print(ref.price) # Access object attributes like segmentId, kind, ticker
{ "kind": "A", "ticker": "NIFTY-1", "segmentId": 2, "price": 11781.08984375 }
api.on_srefs_update(on_srefs)
# Callback to listen for snapshots of open, high, low, close, open interest, average, and band data api.on_srefs_update(on_srefs) def on_srefs(msg): print(msg) # Refs snapshot message sref = apidata_models.RefsSnapshot(msg) print(sref.high) # Access object attributes like kind, ticker, segmentId, open, close, high, low, avg, oi, upperBand, lowerBand
{ "kind": "V", "ticker": "NIFTY-1", "segmentId": 2, "open": 11749.650390625, "close": 11681.5498046875, "avg": 11780.8603515625, "high": 11822, "low": 11731.2001953125, "oi": 10615950, "upperBand": 0, "lowerBand": 0 }
api.on_greeks_update(on_greeks)
# Callback to listen for Option Greeks data api.on_greeks_update(on_greeks) def on_greeks(msg): print(msg) # Option Greeks Data greeks = apidata_models.Greeks(msg) print(greeks.gamma) # Access object attributes like kind, ticker, iv, delta, theta, vega, gamma, ivvwap, vanna, charm, speed, zomma, color, volga, veta, tgr, tv, dtr
{ "kind": "G", "ticker": "NIFTY2220318500CE", "charm": 13.510149002075195, "color": 0.0010876863962039351, "ivvwap": 0.2657951712608337, "speed": -0.25761404633522034, "tgr": 1437.1766357421875, "theta": -3.690974235534668, "tv": -28860.783203125, "vega": 1.935671329498291, "veta": 57653.11328125, "volga": 0.000020740208128700033, "zomma": 3.531916377141897e-7, "iv": 0.2650300860404968, "gamma": 0.00012788892490789294, "dtr": -1.9068187475204468, "delta": 0.03707314282655716 }
api.on_greeks_update(on_greekSnapshot)
# Callback to listen for Option Greeks snapshot data api.on_greeks_update(on_greekSnapshot) def on_greekSnapshot(msg): print(msg) # Option Greeks snapshot message greeks = apidata_models.Greeks(msg) print(greeks.gamma) # Access object attributes like kind, ticker, iv, delta, theta, vega, gamma, ivvwap, vanna, charm, speed, zomma, color, volga, veta, tgr, tv, dtr
{ "kind": "G", "ticker": "NIFTY2220318500CE", "charm": 13.510149002075195, "color": 0.0010876863962039351, "ivvwap": 0.2657951712608337, "speed": -0.25761404633522034, "tgr": 1437.1766357421875, "theta": -3.690974235534668, "tv": -28860.783203125, "vega": 1.935671329498291, "veta": 57653.11328125, "volga": 0.000020740208128700033, "zomma": 3.531916377141897e-7, "iv": 0.2650300860404968, "gamma": 0.00012788892490789294, "dtr": -1.9068187475204468, "delta": 0.03707314282655716 }
Monitor the connection status using the following callbacks:
api.on_connection_started(connection_started)
# Fired when the connection is successfully established def connection_started(): print("Connection started callback")
api.on_connection_stopped(connection_stopped)
# Fired when the connection is closed after an automatic retry or due to networking issues # You need to re-establish the connection manually def connection_stopped(): print("Connection stopped callback")
Subscribe to various live data streams based on your requirements.
needSnapshot = False await api.subscribeSegments(needSnapshot) # IMPORTANT NOTE: # If `needSnapshot = True`, buffer the updates received on `api.on_tradeSnapshot_update` and `api.on_srefs_update` before processing. # Data transfer is substantial and you may get disconnected from the server if you don't process incoming updates quickly enough. # It's advised to buffer the data and then process it once the `api.subscribeSegments` method returns.
await api.subscribeAll(['NIFTY-1'])
await api.subscribeTrade(['NIFTY-1', 'BANKNIFTY-1', 'NIFTY 50'])
await api.subscribeBestAndRefs(['NIFTY-1', 'BANKNIFTY-1'])
await api.subscribeGreeks(['NIFTY2220318500CE'])
# Parameters: spotName, Expiry Date # Subscribe to full option chain await api.subscribeOptionChain('BANKNIFTY', '20220901') # Subscribe to a range of strikes (CE, PE) considering the current spot value at the time of subscription as the mid-point. # This subscribes to 10 strikes (CE, PE) below and 10 strikes (CE, PE) above the mid-point, totaling 40 contracts. await api.subscribeOptionChainRange('NIFTY', '20220901', 10)
await api.unsubscribeAll(['NIFTY-1'])
await api.unsubscribeAll(['NIFTY-1', 'BANKNIFTY-1'])
await api.unsubscribeOptionChain('NIFTY', '20220609')
await api.unsubscribeGreeks(['NIFTY2220318500CE'])
Access historical market data using the following methods.
Retrieve EOD data for specified tickers and date ranges.
# Continuous data # Parameters: ticker, startDate, endDate await api.get_eod("NIFTY-1", "20200828", "20200901") # Contract data # Parameters: underlying ticker, startDate, endDate, contractExpiryDate await api.get_eod_contract("NIFTY", "20200828", "20200901", "20201029")
{ "td": "2020-08-28T00:00:00", "op": 11630, "hp": 11708, "lp": 11617.05, "cp": 11689.05, "vol": 260625, "oi": 488325 }
Retrieve intra-day data with minute-level resolution.
toDate
returns live tick aggregation up to the time traded today. The last bar of the current day may be incomplete. Current day tick aggregation response always provides a complete list of bars from the beginning of the day.
# Continuous data # Parameters: ticker, startDate, endDate, resolution await api.get_intra_eod("NIFTY-1", "20210603", "20210604", "5") # Contract data # Parameters: underlying ticker, startDate, endDate, contractExpiryDate, resolution await api.get_intra_eod_contract("NIFTY", "20200828", "20200901", "20201029", "5")
{ "td": "2020-08-28T09:15:00", "op": 11630, "hp": 11643.45, "lp": 11630, "cp": 11639.8, "vol": 4575, "oi": 440475 }
Retrieve tick data from a specified date and time up to the current live time.
# Parameters: ticker, fromDateTime await api.get_back_ticks("BANKNIFTY-1", "20201016 15:00:00")
{ "td": "2020-11-16T15:00:01.000Z", "pr": 23600, "vol": 125, "oi": 1692375 }
Below is a working example demonstrating how to use the AccelPix Data API to subscribe to live data streams and retrieve historical data.
import asyncio import time # import requests from pix_apidata import * api = apidata_lib.ApiData() event_loop = asyncio.get_event_loop() async def main(): # Register connection status callbacks api.on_connection_started(connection_started) api.on_connection_stopped(connection_stopped) # Register data callbacks api.on_trade_update(on_trade) api.on_best_update(on_best) api.on_refs_update(on_refs) api.on_srefs_update(on_srefs) api.on_tradeSnapshot_update(on_tradeSnapshot) api.on_greeks_update(on_greeks) api.on_greekSnapshot_update(on_greekSnapshot) # Initialize the API key = "your-api-key" host = "apidata.accelpix.in" scheme = "http" s = await api.initialize(key, host, scheme) print(s) # Retrieve intra-day historical data his = await api.get_intra_eod("NIFTY-1", "20210603", "20210604", "5") print("History:", his) # Define symbols and Greeks symbols syms = ['NIFTY-1', 'BANKNIFTY-1'] symsGreeks = ["BANKNIFTY2290126500CE"] # Subscribe to live data streams await api.subscribeAll(syms) # Uncomment the following lines to subscribe to additional streams # await api.subscribeOptionChain('NIFTY', '20220901') # await api.subscribeGreeks(symsGreeks) # await api.subscribeOptionChainRange('NIFTY', '20220901', 3) print("Subscribe Done") # Optionally, unsubscribe after a delay # needSnapshot = False # await api.subscribeSegments(needSnapshot) # time.sleep(5) # await api.unsubscribeAll(['NIFTY-1']) # await api.unsubscribeGreeks(['BANKNIFTY2290126500CE']) # await api.unsubscribeOptionChain('NIFTY', '20220901') def on_trade(msg): trd = apidata_models.Trade(msg) print("Trade:", msg) # Alternatively, access specific attributes like trd.volume def on_best(msg): bst = apidata_models.Best(msg) print("Best:", msg) # Alternatively, access specific attributes like bst.bidPrice def on_refs(msg): ref = apidata_models.Refs(msg) print("Refs snapshot:", msg) # Alternatively, access specific attributes like ref.price def on_srefs(msg): sref = apidata_models.RefsSnapshot(msg) print("Refs update:", msg) # Alternatively, access specific attributes like sref.high def on_tradeSnapshot(msg): trdSnap = apidata_models.Trade(msg) print("TradeSnap:", msg) # Alternatively, access specific attributes like trdSnap.volume def on_greeks(msg): greeks = apidata_models.Greeks(msg) print("OptionGreeks:", msg) # Alternatively, access specific attributes like greeks.gamma def on_greekSnapshot(msg): gr = apidata_models.Greeks(msg) print(msg) # Alternatively, access specific attributes like gr.gamma def connection_started(): print("Connection started callback") def connection_stopped(): print("Connection stopped callback") # Create and run the main task event_loop.create_task(main()) try: event_loop.run_forever() finally: event_loop.close()