Pix APIs - Realtime and Historical Data in Node.js

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 application/portal, all heavy weight work are back lifted.

Simple steps to up and running

For Streaming Data

  1. Initialize
  2. Register required callbacks in apidata.callbacks
  3. Do subscribe with symbols list in apidata.stream

For History Data

  1. Initialize
  2. Async call to respective methods exposed in apidata.history

Working sample is available at the bottom of this help page

Installation

  1. npm i pix-apidata --save
Import
  1. const apidata = require("pix-apidata");
Browser
Modules are available in ‘apidata’ from the bundle.js

  1. <script src=".srcbundle.js"></script>

Initialize

This should be the first api call before making any other

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

  5. // *** IMPORTANT ***

  6. // *** initialize(...) - returns a Promise and wait for it 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)

With LOT size


  1. // response data
  2. // Returns Master[] with following structure
  3. Master {
  4.   xid: 1, // segment id. 1 - EQ, 2 - F&O, 3 - NCD, 5 - MCX and so on
  5.   tkr: '20MICRONS', // ticker name - used to communicate with server for data
  6.   atkr: null, // alternative ticker - used for mapping or display purpose
  7.   ctkr: null, // contract ticker or current name of FUT - used for mapping or display purpose
  8.   exp: 1970-01-01T00:00:00.000Z, // contract expiry or default to UNIX time
  9.   utkr: null, // underlying ticker of F&O contract, eg. NIFTY for NIFTY FUT or NIFTY OPT
  10.   inst: 'EQUITY', // instrument name of the symbol - EQUITY, FUTSTK, FUTIDX, OPTSTK, OPTIDX etc.
  11.   a3tkr: null, // another alternative ticker - used for mapping or display purpose
  12.   sp: '0.00', // strike price of the option contract
  13.   tk: 16921, // exchange defined token of the symbol
  14.   lot: 0 //Lot size
  15. }

Modules available

  1. apidata.history
  2. apidata.stream
  3. apidata.callbacks

Callbacks for live streaming

Trade data

  1. //callback to listen for trade data
  2. apidata.callbacks.onTrade(msg => {
  3.   console.log(msg);
  4. })

  5. //response data
  6. Trade {
  7.   id: 0,
  8.   ticker: 'BANKNIFTY-1',
  9.   segmentId: 2,
  10.   time: 2024-01-16T15:30:00.000Z,
  11.   price: 46560,
  12.   qty: 300,
  13.   volume: 7499525,
  14.   oi: 3215240,
  15.   kind: 'T'
  16. }

Trade snapshot data
  1. //callback to listen for trade snapshot, called during subcription process, 
  2. //listen to onTrade callback for continuouse stream data
  3. apidata.callbacks.onTradeSnapshot(msg => {
  4.   console.log(msg);
  5. })

  6. //response data
  7. Trade {
  8.   id: 0,
  9.   ticker: 'BANKNIFTY-1',
  10.   segmentId: 2,
  11.   time: 2024-01-12T15:30:00.000Z,
  12.   price: 47810,
  13.   qty: 300,
  14.   volume: 7499525,
  15.   oi: 1503450,
  16.   kind: 'T'
  17. }

Best data

  1. //callback to listen for bid, ask and respective qty
  2. apidata.callbacks.onBest(msg => {
  3.   console.log(msg);
  4. })

  5. //response data
  6. Best {
  7.   ticker: 'NIFTY-1',
  8.   segmentId: 2,
  9.   kind: 'B',
  10.   bidPrice: 21780,
  11.   bidQty: 350,
  12.   askPrice: 21782,
  13.   askQty: 250,
  14.   time: 2061-09-02T07:00:00.000Z
  15. }

Recent change in Refs data

  1. //callback to listen for change in o, h, l, c, oi and avg data
  2. apidata.callbacks.onRefs(msg => {
  3.   console.log(msg);
  4. })

  5. //response data
  6. Refs {
  7.   kind: 'A',
  8.   ticker: 'NIFTY-1',
  9.   segmentId: 2,
  10.   price: 21760
  11. }

Refs snapshot data

  1. //callback to listen for o, h, l, c, oi and avg snapshot
  2. apidata.callbacks.onRefsSnapshot(msg => {
  3.   console.log(msg);
  4. })

  5. //response data
  6. RefsSnapshot {
  7.   kind: 'V',
  8.   ticker: 'BANKNIFTY-1',
  9.   segmentId: 2,
  10.   open: 47656.8,
  11.   close: 47573.7,
  12.   high: 48047,
  13.   low: 47568.6,
  14.   avg: 47540.8,
  15.   oi: 2013795
  16.   upperBand: 0,
  17.   lowerBand: 0,
  18.   poi: 2275950
  19. }

Option Greek data

  1. //callback to listen for Greeks data
  2. apidata.callbacks.onGreeks(msg => {
  3.   console.log(msg);
  4. })

  5. //response data
  6. Greeks {
  7.   kind: 'G',
  8.   token: 0,
  9.   ticker: 'BANKNIFTY2411748500CE',
  10.   iv: 0.2588011622428894,
  11.   delta: 0.8822246193885803,
  12.   theta: -77.46974182128906,
  13.   vega: 2.561089515686035,
  14.   gamma: 0.0005879841046407819,
  15.   ivvwap: 0.18449798226356506,
  16.   vanna: -3.667503833770752,
  17.   charm: 1.109373927116394,
  18.   speed: -0.000002092838712997036,
  19.   zomma: 0.0009008663473650813,
  20.   color: 0.0002725007652770728,
  21.   volga: 5643.75439453125,
  22.   veta: 2929.610107421875,
  23.   tgr: -131754.828125,
  24.   tv: -30.24874496459961,
  25.   dtr: -0.011387989856302738,
  26.   highiv: 0,
  27.   lowiv: 0,
  28.   twapiv: 0.19661587476730347,
  29.   timestamp: 0  timestamp:1333597502420254464 //Time in nanoseconds from 01-Jan-1980 00:00:00 UTC
  30. }

Greek snapshot data

  1. //callback to listen greek snapshot
  2. apidata.callbacks.onGreekSnapshot(msg => {
  3.   console.log(msg);
  4. })

  5. //response data
  6. Greeks {
  7.   kind: 'G',
  8.   token: 0,
  9.   ticker: 'BANKNIFTY2411748500CE',
  10.   iv: 0.2588011622428894,
  11.   delta: 0.8822246193885803,
  12.   theta: -77.46974182128906,
  13.   vega: 2.561089515686035,
  14.   gamma: 0.0005879841046407819,
  15.   ivvwap: 0.18449798226356506,
  16.   vanna: -3.667503833770752,
  17.   charm: 1.109373927116394,
  18.   speed: -0.000002092838712997036,
  19.   zomma: 0.0009008663473650813,
  20.   color: 0.0002725007652770728,
  21.   volga: 5643.75439453125,
  22.   veta: 2929.610107421875,
  23.   tgr: -131754.828125,
  24.   tv: -30.24874496459961,
  25.   dtr: -0.011387989856302738,
  26.   highiv: 0,
  27.   lowiv: 0,
  28.   twapiv: 0.19661587476730347,
  29.   timestamp:1333597502420254464 //Time in nanoseconds from 01-Jan-1980 00:00:00 UTC
  30. }

Callbacks for connection status

  1. // Fired when connection is successful
  2. apidata.callbacks.onConnected(() => {
  3.   console.log("Connected successfully...");
  4. })

  5. // Fired when the connection is closed after automatic retry or some issues in networking
  6. // Need to re-establish the connection manually
  7. apidata.callbacks.onClosed((err) => {
  8.   console.log("Connected close due to...", err);
  9. })

Live stream subscription

Subscribe to receive updates of segments entitled to you

  1. var needSnapshot = false;
  2. var status = await apidata.stream.subscribeSegments(needSnapshot)
  3. // IMPORTANT NOTE:
  4. // If needSnapshot = true, then buffer the updates received on 'apidata.callbacks.onTradeSnapshot' and 'apidata.callbacks.onRefsSnapshot' 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 'apidata.stream.subscribeSegments' method returns.

Subscribe to receive ALL updates of the symbols subscribed

  1. //subscribe single symbol
  2. await apidata.stream.subscribeAll(['NIFTY-1'])
  3. //subscribe multiple symbol
  4. await apidata.stream.subscribeAll(['NIFTY-1','BANKNIFTY-1'])

Subscribe to receive TRADE updates of the symbols subscribed

  1. await apidata.stream.subscribeTrade(['NIFTY-1','BANKNIFTY-1'])

Subscribe to receive REFS and BEST updates of the symbols subscribed

  1. await apidata.stream.subscribeBestAndRefs(['NIFTY-1','INFY-1']);

Subscribe Greek live stream

  1. await apidata.stream.subscribeGreeks(['NIFTY2411822200CE'])

Subscribe Optionchain live stream
  1. //params: underlying ticker, ExpiryDate
  2. await apidata.stream.subscribeOptionChain('NIFTY', '20240118);
Unsubscribe live stream

  1. //unsubscribe single symbol
  2. await apidata.stream.unsubscribeAll(['NIFTY-1'])
  3. //unsubscribe multiple symbol
  4. await apidata.stream.unsubscribeAll(['NIFTY-1','BANKNIFTY-1'])

Subscribe and Unsubscribe Greeks data

  1. await apidata.stream.subscribeGreeks(['NIFTY2411822200CE','NIFTY24118221500PE'])
  2. await apidata.stream.unsubscribeGreeks(['NIFTY2411822200CE','NIFTY24118221500PE'])

Subscribe and Unsubscribe Option Chain

  1. //subscribe to full chain
  2. await apidata.stream.subscribeOptionChain('BANKNIFTY','20240117')
  3. //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.
  4. await apidata.stream.subscribeOptionChainRange('NIFTY','20240118',10)
  5. //unsubscribe the chain
  6. await apidata.stream.unsubscribeOptionChain('NIFTY','20240118')

Subscribe and Unsubscribe Greeks Chain

  1. //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.
  2. //params: underlying ticker, ExpiryDate, NoOfStrikes

  3. await apidata.stream.subscribeGreeksChainRange('NIFTY','20240118',10)

  4. //unsubscribe the chain
  5. await apidata.stream.unsubscribeGreeksChain('NIFTY','20240118')

History data – Eod

  1. //*** Continues data
  2. //params: ticker, startDate, endDate
  3. await apidata.history.getEod("NIFTY-1", "20240112", "20240118")

  4. //*** Contract data
  5. //params: underlying ticker, startDate, endDate, contractExpiryDate
  6. await apidata.history.getEodContract("NIFTY", "20240109", "20240118", "20240125")

  7. //response data
  8. {
  9.   td: '2024-01-18T00:00:00',
  10.   op: 21461.9
  11.   hp: 21574,
  12.   lp: 21316.8,
  13.   cp: 21520.4,
  14.   vol: 260625,
  15.   oi: 11075450
  16. }

History data – Inraday

Provides intra-eod bars with the time resolution in minutes (default:’5′ mins)
You can set minute resolution to ‘1’, ‘5’, ’10’ and so on.
Custom minute resolution also supported like ‘3’, ‘7’ and so on.
Passing CURRENT DATE as parameter in ‘toDate’ will respond the LIVE ticks aggregated upto the time it traded today. Last BAR of the current day may be incomplete. Current day tick aggregation response will always provide complete bar list from the beginning of day.

  1. //*** Continues data
  2. //params: ticker, startDate, endDate, resolution
  3. await apidata.history.getIntraEod("NIFTY-1", "20240112", "20240118", "5")

  4. //*** Contract data
  5. //params: underlying ticker, startDate, endDate, contractExpiryDate
  6. await apidata.history.getIntraEodContract("NIFTY", "20240109", "20240118", "20240125", "5")

  7. //response data
  8. {
  9.   td: '2024-01-18T09:15:00',
  10.  op: 21461.9
  11.   hp: 21574,
  12.   lp: 21316.8,
  13.   cp: 21520.4,
  14.   vol: 260625,
  15.   oi: 11075450
  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 apidata.history.getBackTicks("BANKNIFTY-1", "20240118 15:00:00")

  3. //response data
  4. {
  5.   td: 2024-01-18T15:00:01.000Z,
  6.   pr: 45720,
  7.   vol: 135,
  8.   oi: 2357865
  9. }

Sample API Example

  1. var apidata = require('pix-apidata')

  2. const apiKey = "your-api-key"
  3. const apiServer = "apidata.accelpix.in"

  4. apidata.callbacks.onTrade(t => {
  5.   console.log(t);
  6. })
  7. apidata.callbacks.onGreeks(greek => {
  8.   console.log(greek);
  9. })

  10. apidata.initialize(apiKey, apiServer)
  11.   .then(async () => {
  12.     await apidata.stream.subscribeAll(['NIFTY-1'])
  13.     await apidata.stream.subscribeGreeks(["NIFTY2220318500CE"])
  14.     await apidata.stream.subscribeOptionChainRange('NIFTY', '20240118',7)
  15.     await apidata.stream.subscribeGreeksChainRange('NIFTY', '20240118',5)
  16.     // await apidata.stream.unsubscribeOptionChain('NIFTY', 20240118);
  17.     let eod = await apidata.history.getEod('NIFTY 50', '20240112', '20240116')
  18.     console.log(eod);
  19.   })

    • Related Articles

    • 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 ...
    • 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://apidata.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 ...
    • Symbols Format for realtime data APIs

      We are providing all symbols as per NSE including Futures (Contract and Continuous Both) and Options (Weekly and Monthly). Let's go through some examples and understand the symbology. General Terminology in API:- T = Trade Packet B = Best Packet V = ...
    • 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 ...