Accelpix Realtime APIs - Realtime and Historical Data in Python

Pix APIs - Realtime and Historical Data in Python

Introduction

Python library to connect and stream the market data.
This is websocket and fallback transport based library with all the functionalities to get EOD and live streaming data

Simple and easy integration with your web application, all heavyweight work are back lifted.

What’s new
a) v1.3.3 : 29-Aug-2022
Options Chain and Option Chain Range subscription and unsubscription method.

b) v1.3.2 : 02-Aug-2022
- Introducing Option chain subscription and unsubscription method.

c) v1.3.1 : 28-Jan-2022
New callback for Trade snapshot during subscription, earlier it was provided along with Trade callback
- Segment subscription for the entitled user
- Introducing Option Greeks

d) v1.3.0 : 06-May-2021
- Upper and lower price band for EQ market added – refer ‘Refs snapshot data’ section
- Live ticks aggregation which provides current day minutes bar – refer ‘History data – Inraday’ section

Simple steps to up and running

For Streaming Data:

Initialize

Register required callbacks
Do subscribe with symbols list
For History Data:

Initialize

Async call to respective methods exposed
Working sample is available at the bottom of this help page

Install the library using pip:
  1. pip install pix-apidata 

Import the required modules:
  1. import asyncio
  2. from pix_apidata import *

Initialize the library by creating an instance of the ApiData class:

  1. api = apidata_lib.ApiData()
  2. event_loop = asyncio.get_event_loop()

Set up the connection parameters by providing your API key, API host, and scheme:

  1. apiKey = "api-access-key" //provided by your data vendor
  2. apiHost = "apidata.accelpix.in" OR "apidata5.accelpix.in" //provided by your data vendor
  3. scheme = "https" // use either https (default scheme) or http 
  4. await api.initialize(apiKey, apiHost, scheme) 

  5. # *** IMPORTANT ***

  6. # *** initialize(...) - wait for initialize to complete before making any other API calls.

Symbol Master

Use below REST API to get Master Data
(Only for master data download due to larger data size)


  1. #response data
  2. {
  3.   xid: 1,
  4.   tkr: "20MICRONS",
  5.   atkr: null,
  6.   ctkr: null,
  7.   exp: "1970-01-01 00:00:00",
  8.   utkr: null,
  9.   inst: "EQUITY",
  10.   a3tkr: null,
  11.   sp: 0.00,
  12.   tk: 16921
  13. }

Callbacks for live streaming

Available 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_tradeSnap)
api.on_greeks_update(on_greeks)

Trade

  1. #callback to listen for trade data
  2. api.on_trade_update(on_trade)
  3. def on_trade(msg):
  4.   print(msg) # Trade msg
  5.   t = apidata_models.Trade(msg)
  6.   print(t.ticker , t.oi) #likewise object can be called for id, kind, ticker, segment, price, qty, volume, oi

  7. #response data
  8.   {
  9.     id: 0, 
  10.     ticker: 'NIFTY-1', 
  11.     segmentId: 2, 
  12.     time: 1293704493, 
  13.     price: 13958.6, 
  14.     qty: 75, 
  15.     volume: 1587975, 
  16.     oi: 9700275,
  17.     kind: 'T'
  18.   } 

Trade snapshot data

  1. #callback to listen for trade snapshot, called during subcription process
  2. #listen to onTrade callback for continuouse stream data
  3. api.on_tradeSnapshot_update(on_tradeSnapshot)
  4. def on_tradeSnapshot(msg):
  5.   print(msg) # TradeSnapshot msg
  6.   t = apidata_models.Trade(msg)
  7.   print(t.ticker , t.oi) #likewise object can be called for id, kind, ticker, segment, price, qty, volume, oi

  8. #response data
  9.   {
  10.     id: 0, 
  11.     ticker: 'NIFTY-1', 
  12.     segmentId: 2, 
  13.     time: 1293704493, 
  14.     price: 13958.6, 
  15.     qty: 75, 
  16.     volume: 1587975, 
  17.     oi: 9700275,
  18.     kind: 'T'
  19.   } 

Best

  1. #callback to listen for bid, ask and respective qty
  2. api.on_best_update(on_best)
  3. def on_best(msg):
  4.   print(msg) # Best msg
  5.   b = apidata_models.Best(msg)
  6.   print(b.ticker , b.bidPrice) #likewise object can be called for segmentId, kind, bidQty, askPrice, askQty

  7. #response data
  8.   {
  9.     ticker: 'NIFTY-1', 
  10.     segmentId: 2, 
  11.     kind: 'B', 
  12.     bidPrice: 13957.45, 
  13.     bidQty: 75, 
  14.     askPrice: 13958.8, 
  15.     askQty: 75, 
  16.     time: 1293704493
  17.   }  

Recent change in Refs data
  1. #callback to listen for change in o, h, l, c, oi and avg data
  2. api.on_trade_ref(on_ref)
  3. def on_ref(msg):
  4.   print(msg) # Refs msg
  5.   ref = apidata_models.Refs(msg)
  6.   print(ref.price) #likewise object can be called for segmentId, kind, ticker

  7. #response data
  8.   {
  9.     kind: 'A',
  10.     ticker: 'NIFTY-1',
  11.     segmentId: 2,
  12.     price: 11781.08984375
  13.   }

Refs snapshot data
  1. #callback to listen for o, h, l, c, oi and avg snapshot
  2. api.on_srefs_update(on_srefs)
  3. def on_srefs(msg):
  4.   print(msg) # Srefs msg
  5.   sref = apidata_models.RefsSnapshot(msg)
  6.   print(sref.high) #likewise object can be called for kind, ticker, segmentId, open, close, high, low, avg, oi, upperBand and lowerBand

  7. #response data
  8.   {
  9.     kind: 'V', 
  10.     ticker: 'NIFTY-1',
  11.     segmentId: 2, 
  12.     open: 11749.650390625, 
  13.     close: 11681.5498046875,
  14.     avg: 11780.8603515625,
  15.     high: 11822,
  16.     low: 11731.2001953125,
  17.     oi: 10615950,
  18.     upperBand: 0,
  19.     lowerBand: 0

  20.   }

OptionGreeks data

  1. #callback to listen for optionGreek data
  2. api.on_greeks_update(on_greeks)
  3. def on_greeks(msg):
  4.   print(msg) # Option Greeks Data
  5.   greeks = apidata_models.Greeks(msg)
  6.   print(greeks.gamma) # likewise object can be called for kind, ticker, iv, delta, theta, vega, gamma, ivvwap, vanna, charm, speed, zomma, color, volga, veta, tgr, tv and dtr

  7. #response data
  8.   {
  9.     kind:'G',
  10.     ticker:'NIFTY2220318500CE',
  11.     charm:13.510149002075195,
  12.     color:0.0010876863962039351,
  13.     ivvwap:0.2657951712608337,
  14.     speed:-0.25761404633522034,
  15.     tgr:1437.1766357421875,
  16.     theta:-3.690974235534668,
  17.     tv:-28860.783203125,
  18.     vega:1.935671329498291,
  19.     veta:57653.11328125,
  20.     volga:0.000020740208128700033,
  21.     zomma:3.531916377141897e-7,
  22.     iv:0.2650300860404968,
  23.     gamma:0.00012788892490789294,
  24.     dtr:-1.9068187475204468,
  25.     delta:0.03707314282655716
  26.   }

OptionGreeks Snapshot data
  1. #callback to listen for optionGreek snap data
  2. api.on_greeks_update(on_greeks)
  3. def on_greekSnapshot(msg):
  4.   print(msg) # Option Greeks snap data
  5.   greeks = apidata_models.Greeks(msg)
  6.   print(greeks.gamma) # likewise object can be called for kind, ticker, iv, delta, theta, vega, gamma, ivvwap, vanna, charm, speed, zomma, color, volga, veta, tgr, tv and dtr

  7. #response data
  8.   {
  9.     kind:'G',
  10.     ticker:'NIFTY2220318500CE',
  11.     charm:13.510149002075195,
  12.     color:0.0010876863962039351,
  13.     ivvwap:0.2657951712608337,
  14.     speed:-0.25761404633522034,
  15.     tgr:1437.1766357421875,
  16.     theta:-3.690974235534668,
  17.     tv:-28860.783203125,
  18.     vega:1.935671329498291,
  19.     veta:57653.11328125,
  20.     volga:0.000020740208128700033,
  21.     zomma:3.531916377141897e-7,
  22.     iv:0.2650300860404968,
  23.     gamma:0.00012788892490789294,
  24.     dtr:-1.9068187475204468,
  25.     delta:0.03707314282655716
  26.   }

Callbacks for connection status

api.on_connection_started(connection_started)
api.on_connection_stopped(connection_stopped)
  1. # Fired when connection is successful
  2. def connection_started():
  3.   print("Connection started callback")

  4. # Fired when the connection is closed after automatic retry or some issues in networking
  5. # Need to re-establish the connection manually

  6. def connection_stopped():
  7.   print("connection Stopped callback")

Live stream subscription
Subscribe to receive updates of segments entitled to you

  1. needSnapshot = False
  2. await api.subscribeSegments(needSnapshot)
  3. # IMPORTANT NOTE:
  4. # If needSnapshot = True, then buffer the updates received on 'api.on_tradeSnapshot_update' and 'api.on_srefs_update' before processing. 
  5. # Data transfer is huge and you may get disconnected from server in-case if you don't process the incoming updates as fast enough.
  6. # It's advised to buffer the data then process it once 'api.subscribeSegments' method returns.

Subscribe to receive ALL updates

  1.  await api.subscribeAll('NIFTY-1')
Subscribe to receive TRADE updates
  1.  await api.subscribeTrade(['NIFTY-1','BANKNIFTY-1','NIFTY 50'])
Subscribe to receive REFS and BEST updates

  1. await api.subscribeBestAndRefs(['NIFTY-1','BANKNIFTY-1'])
Subscribe to receive Greeks updates
  1. await api.subscribeGreeks(['NIFTY2220318500CE'])
Subscribe to receive Optionchain updates

  1. # params: spotName, Expiry Date
  2. # subscribe to full chain
  3. await api.subscribeOptionChain('BANKNIFTY','20220901')
  4. # subscribe to range of strikes(CE, PE) considering current spot value(at time of subscribe) as the mid-point, essentially 10 strike(CE,PE) below mid-point and 10 strikes(CE, PE) above mid-point and total of 40 contracts subscribed for the below call.
  5. await api.subscribeOptionChainRange('NIFTY','20220901',10)

Unsubscribe live stream

  1. # unsubscribe single symbol
  2.  await api.unsubscribeAll(['NIFTY-1'])
  3. # unsubscribe multiple symbol
  4.  await api.unsubscribeAll(['NIFTY-1','BANKNIFTY-1'])
Unsubscribe Optionchain updates

  1. await api.unsubscribeOptionChain('NIFTY','20220609')

Unsubscribe Greeks updates

  1. await api.unsubscribeGreeks(['NIFTY2220318500CE'])
History data – Eod

  1. # Continues data
  2. #params: ticker, startDate, endDate
  3. await api.get_eod("NIFTY-1", "20200828", "20200901")

  4. # Contract data
  5. #params: underlying ticker, startDate, endDate, contractExpiryDate
  6. await api.get_eod_contract("NIFTY", "20200828", "20200901", "20201029")

  7. #response data
  8. {
  9.   td: '2020-08-28T00:00:00',
  10.   op: 11630,
  11.   hp: 11708,
  12.   lp: 11617.05,
  13.   cp: 11689.05,
  14.   vol: 260625,
  15.   oi: 488325
  16. }

Intraday History Data

The library provides the ability to retrieve intraday bars with a customizable time resolution, measured in minutes. By default, the time resolution is set to '5' minutes, but you can adjust it according to your needs.

To set the minute resolution, you can use values such as '1', '5', '10', and so on. Additionally, the library supports custom minute resolutions, allowing you to specify values like '3', '7', and more.

When making a request for historical data by passing the "CURRENT DATE" as a parameter in the 'toDate' field, the API will respond with the aggregated live ticks up to the latest trading time for the current day. Please note that the last bar of the current day may be incomplete, as it may not have been fully formed yet. However, the tick aggregation response for the current day always provides a complete list of bars starting from the beginning of the day.

  1. # Continues data 
  2. #params: ticker, startDate, endDate, resolution
  3. await api.get_intra_eod("NIFTY-1", "20210603", "20210604", "5")

  4. # Contract data
  5. #params: underlying ticker, startDate, endDate, contractExpiryDate
  6. await api.get_intra_eod_contract("NIFTY", "20200828", "20200901", "20201029", "5")

  7. #response data
  8. {
  9.   td: '2020-08-28T09:15:00',
  10.   op: 11630,
  11.   hp: 11643.45,
  12.   lp: 11630,
  13.   cp: 11639.8,
  14.   vol: 4575,
  15.   oi: 440475
  16. }

History data – Ticks

Provides back log ticks from the date time specified till current live time, that is the ticks available till request hit the server.

  1. #params: ticker, fromDateTime
  2. await api.get_back_ticks("BANKNIFTY-1", "20201016 15:00:00")

  3. #response data
  4. {
  5.   td: 2020-11-16T15:00:01.000Z,
  6.   pr: 23600,
  7.   vol: 125,
  8.   oi: 1692375
  9. }
Example

  1. import asyncio
  2. import time
  3. #import requests
  4. from pix_apidata import *

  5. api = apidata_lib.ApiData()
  6. event_loop = asyncio.get_event_loop()

  7. async def main():
  8.     api.on_connection_started(connection_started)
  9.     api.on_connection_stopped(connection_stopped)
  10.     api.on_trade_update(on_trade)
  11.     api.on_best_update(on_best)
  12.     api.on_refs_update(on_refs)
  13.     api.on_srefs_update(on_srefs)
  14.     api.on_tradeSnapshot_update(on_tradeSnapshot)
  15.     api.on_greeks_update(on_greeks)
  16.     api.on_greekSnapshot_update(on_greekSnapshot)

  17.     key = "your-api-key"
  18.     host = "apidata.accelpix.in"
  19.     scheme = "http"
  20.     s = await api.initialize(key, host,scheme)
  21.     print(s)

  22.     his = await api.get_intra_eod("NIFTY-1","20210603", "20210604", "5")
  23.     print("History : ",his)

  24.     syms = ['NIFTY-1', 'BANKNIFTY-1']
  25.     symsGreeks = ["BANKNIFTY2290126500CE"]
  26.     await api.subscribeAll(syms)
  27.     # await api.subscribeOptionChain('NIFTY','20220901')
  28.     # await api.subscribeGreeks(symsGreeks)
  29.     # await api.subscribeOptionChainRange('NIFTY','20220901',3)
  30.     print("Subscribe Done")
  31.     #needSnapshot = False
  32.     #await api.subscribeSegments(needSnapshot)
  33.     #time.sleep(5)
  34.     # await api.unsubscribeAll(['NIFTY-1'])
  35.     # await api.unsubscribeGreeks(['BANKNIFTY2290126500CE'])
  36.     # await api.unsubscribeOptionChain('NIFTY','20220901')

  37. def on_trade(msg):
  38.     trd = apidata_models.Trade(msg)
  39.     print("Trade : ",msg) # or print(trd.volume) likewise object can be called for id, kind, ticker, segment, price, qty, oi

  40. def on_best(msg):
  41.     bst = apidata_models.Best(msg)
  42.     print("Best : ",msg) # or print(bst.bidPrice) likewise object can be called for ticker, segmentId, kind, bidQty, askPrice, askQty

  43. def on_refs(msg):
  44.     ref = apidata_models.Refs(msg)
  45.     print("Refs snapshot : ",msg) # or print(ref.price) likewise object can be called for segmentId, kind, ticker

  46. def on_srefs(msg):
  47.     sref = apidata_models.RefsSnapshot(msg)
  48.     print("Refs update : ",msg) # or print(sref.high) likewise object can be called for kind, ticker, segmentId, open, close, low, avg, oi, lowerBand,upperBand

  49. def on_tradeSnapshot(msg):
  50.     trdSnap = apidata_models.Trade(msg)
  51.     print("TradeSnap : ",msg) # or print(trdSnap.volume) likewise object can be called for id, kind, ticker, segment, price, qty, oi

  52. def on_greeks(msg):
  53.     greeks = apidata_models.Greeks(msg)
  54.     print("OptionGreeks : ",msg) # or print(greeks.gamma) likewise object can be called for kind, ticker, iv, delta, theta, vega, gamma, ivvwap, vanna, charm, speed, zomma, color, volga, veta, tgr, tv and dtr

  55. def on_greekSnapshot(msg):
  56.     gr = apidata_models.Greeks(msg)
  57.     print(msg) # or print(greeks.gamma) likewise object can be called for kind, ticker, iv, delta, theta, vega, gamma, ivvwap, vanna, charm, speed, zomma, color, volga, veta, tgr, tv and dtr

  58. def connection_started():
  59.     print("Connection started callback")

  60. def connection_stopped():
  61.     print("Connection stopped callback")

  62. event_loop.create_task(main())
  63. try:
  64.    event_loop.run_forever()
  65. finally:
  66.    event_loop.close()

    • Related Articles

    • Pix APIs - Realtime and Historical Data in Node.js

      Introduction JavaScript library to connect and stream the market data. This is websocket and fallback transport based library with all the functionalities to get eod and live streaming data Simple and easy integration with your web ...
    • Pix APIs - Realtime and Historical Data in REST

      ACCELPIX REST API REST API for historical and tick data download EOD Data: GET http://{server}/api/fda/rest/{ticker}/{yyyyMMdd:start}/{yyyyMMdd:end}?api_token={your api key} Example: http://apidata5.accelpix.in/api/fda/rest/NIFTY ...
    • How to configure Realtime NSE Data in Excel ?

      India's Fastest Realtime (Tick by Tick 1 Sec Updates) Data in MS Excel. The following are the minimum requirements: - 1. Microsoft Office Excel 2010 or above 2. Pix Connect Elite and the Above plans can be used with Excel. How to Setup Pix Connect ...
    • How to download Daily IEOD Incremental Data using Pix Connect ?

      Intraday End of Day Data ( IEOD ) is too crucial for backtesting purposes, that's why we do provide for all our subscribers at FREE OF COST with REALTIME Data Subscription. We are updating these data every day within 25 min after the market close (at ...
    • NinjaTrader 7 Market Profile TPO / Volume Profile Settings using Pix Connect !

      Market Profile TPO is a widely used indicator in NInjaTrader 7 and here will show you the settings which you need to implement in your NinjaTrader 7. Hope you have properly installed our Pix Connect and TA Extention (If not, please install it from ...