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
- Initialize
- Register required callbacks in apidata.callbacks
- Do subscribe with symbols list in apidata.stream
For History Data
- Initialize
- Async call to respective methods exposed in apidata.history
Working sample is available at the bottom of this help page
Installation
Import
- const apidata = require("pix-apidata");
Browser
Modules are available in ‘apidata’ from the bundle.js
- <script src=".srcbundle.js"></script>
Initialize
This should be the first api call before making any other
- const apiKey = "api-access-key" //provided by your data vendor
- const apiHost = "apidata.accelpix.in" //provided by your data vendor
- const scheme = "https" // use either http (default scheme) or https
- await apidata.initialize(apiKey, apiHost, scheme)
- // *** IMPORTANT ***
- // *** 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
- // response data
- // Returns Master[] with following structure
- Master {
- xid: 1, // segment id. 1 - EQ, 2 - F&O, 3 - NCD, 5 - MCX and so on
- tkr: '20MICRONS', // ticker name - used to communicate with server for data
- atkr: null, // alternative ticker - used for mapping or display purpose
- ctkr: null, // contract ticker or current name of FUT - used for mapping or display purpose
- exp: 1970-01-01T00:00:00.000Z, // contract expiry or default to UNIX time
- utkr: null, // underlying ticker of F&O contract, eg. NIFTY for NIFTY FUT or NIFTY OPT
- inst: 'EQUITY', // instrument name of the symbol - EQUITY, FUTSTK, FUTIDX, OPTSTK, OPTIDX etc.
- a3tkr: null, // another alternative ticker - used for mapping or display purpose
- sp: '0.00', // strike price of the option contract
- tk: 16921, // exchange defined token of the symbol
- lot: 0 //Lot size
- }
Modules available
- apidata.history
- apidata.stream
- apidata.callbacks
Callbacks for live streaming
Trade data
- //callback to listen for trade data
- apidata.callbacks.onTrade(msg => {
- console.log(msg);
- })
- //response data
- Trade {
- id: 0,
- ticker: 'BANKNIFTY-1',
- segmentId: 2,
- time: 2024-01-16T15:30:00.000Z,
- price: 46560,
- qty: 300,
- volume: 7499525,
- oi: 3215240,
- kind: 'T'
- }
Trade snapshot data
- //callback to listen for trade snapshot, called during subcription process,
- //listen to onTrade callback for continuouse stream data
- apidata.callbacks.onTradeSnapshot(msg => {
- console.log(msg);
- })
- //response data
- Trade {
- id: 0,
- ticker: 'BANKNIFTY-1',
- segmentId: 2,
- time: 2024-01-12T15:30:00.000Z,
- price: 47810,
- qty: 300,
- volume: 7499525,
- oi: 1503450,
- kind: 'T'
- }
Best data
- //callback to listen for bid, ask and respective qty
- apidata.callbacks.onBest(msg => {
- console.log(msg);
- })
- //response data
- Best {
- ticker: 'NIFTY-1',
- segmentId: 2,
- kind: 'B',
- bidPrice: 21780,
- bidQty: 350,
- askPrice: 21782,
- askQty: 250,
- time: 2061-09-02T07:00:00.000Z
- }
Recent change in Refs data
- //callback to listen for change in o, h, l, c, oi and avg data
- apidata.callbacks.onRefs(msg => {
- console.log(msg);
- })
- //response data
- Refs {
- kind: 'A',
- ticker: 'NIFTY-1',
- segmentId: 2,
- price: 21760
- }
Refs snapshot data
- //callback to listen for o, h, l, c, oi and avg snapshot
- apidata.callbacks.onRefsSnapshot(msg => {
- console.log(msg);
- })
- //response data
- RefsSnapshot {
- kind: 'V',
- ticker: 'BANKNIFTY-1',
- segmentId: 2,
- open: 47656.8,
- close: 47573.7,
- high: 48047,
- low: 47568.6,
- avg: 47540.8,
- oi: 2013795
- upperBand: 0,
- lowerBand: 0,
- poi: 2275950
- }
Option Greek data
- //callback to listen for Greeks data
- apidata.callbacks.onGreeks(msg => {
- console.log(msg);
- })
- //response data
- Greeks {
- kind: 'G',
- token: 0,
- ticker: 'BANKNIFTY2411748500CE',
- iv: 0.2588011622428894,
- delta: 0.8822246193885803,
- theta: -77.46974182128906,
- vega: 2.561089515686035,
- gamma: 0.0005879841046407819,
- ivvwap: 0.18449798226356506,
- vanna: -3.667503833770752,
- charm: 1.109373927116394,
- speed: -0.000002092838712997036,
- zomma: 0.0009008663473650813,
- color: 0.0002725007652770728,
- volga: 5643.75439453125,
- veta: 2929.610107421875,
- tgr: -131754.828125,
- tv: -30.24874496459961,
- dtr: -0.011387989856302738,
- highiv: 0,
- lowiv: 0,
- twapiv: 0.19661587476730347,
- timestamp: 0 timestamp:1333597502420254464 //Time in nanoseconds from 01-Jan-1980 00:00:00 UTC
- }
Greek snapshot data
- //callback to listen greek snapshot
- apidata.callbacks.onGreekSnapshot(msg => {
- console.log(msg);
- })
- //response data
- Greeks {
- kind: 'G',
- token: 0,
- ticker: 'BANKNIFTY2411748500CE',
- iv: 0.2588011622428894,
- delta: 0.8822246193885803,
- theta: -77.46974182128906,
- vega: 2.561089515686035,
- gamma: 0.0005879841046407819,
- ivvwap: 0.18449798226356506,
- vanna: -3.667503833770752,
- charm: 1.109373927116394,
- speed: -0.000002092838712997036,
- zomma: 0.0009008663473650813,
- color: 0.0002725007652770728,
- volga: 5643.75439453125,
- veta: 2929.610107421875,
- tgr: -131754.828125,
- tv: -30.24874496459961,
- dtr: -0.011387989856302738,
- highiv: 0,
- lowiv: 0,
- twapiv: 0.19661587476730347,
- timestamp:1333597502420254464 //Time in nanoseconds from 01-Jan-1980 00:00:00 UTC
- }
Callbacks for connection status
- // Fired when connection is successful
- apidata.callbacks.onConnected(() => {
- console.log("Connected successfully...");
- })
- // Fired when the connection is closed after automatic retry or some issues in networking
- // Need to re-establish the connection manually
- apidata.callbacks.onClosed((err) => {
- console.log("Connected close due to...", err);
- })
Live stream subscription
Subscribe to receive updates of segments entitled to you
- var needSnapshot = false;
- var status = await apidata.stream.subscribeSegments(needSnapshot)
- // IMPORTANT NOTE:
- // If needSnapshot = true, then buffer the updates received on 'apidata.callbacks.onTradeSnapshot' and 'apidata.callbacks.onRefsSnapshot' before processing.
- // Data transfer is huge and you may get disconnected from server in-case if you don't process the incoming updates as fast enough.
- // 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
- //subscribe single symbol
- await apidata.stream.subscribeAll(['NIFTY-1'])
- //subscribe multiple symbol
- await apidata.stream.subscribeAll(['NIFTY-1','BANKNIFTY-1'])
Subscribe to receive TRADE updates of the symbols subscribed
- await apidata.stream.subscribeTrade(['NIFTY-1','BANKNIFTY-1'])
Subscribe to receive REFS and BEST updates of the symbols subscribed
- await apidata.stream.subscribeBestAndRefs(['NIFTY-1','INFY-1']);
Subscribe Greek live stream
- await apidata.stream.subscribeGreeks(['NIFTY2411822200CE'])
Subscribe Optionchain live stream
- //params: underlying ticker, ExpiryDate
- await apidata.stream.subscribeOptionChain('NIFTY', '20240118);
Unsubscribe live stream
- //unsubscribe single symbol
- await apidata.stream.unsubscribeAll(['NIFTY-1'])
- //unsubscribe multiple symbol
- await apidata.stream.unsubscribeAll(['NIFTY-1','BANKNIFTY-1'])
Subscribe and Unsubscribe Greeks data
- await apidata.stream.subscribeGreeks(['NIFTY2411822200CE','NIFTY24118221500PE'])
- await apidata.stream.unsubscribeGreeks(['NIFTY2411822200CE','NIFTY24118221500PE'])
Subscribe and Unsubscribe Option Chain
- //subscribe to full chain
- await apidata.stream.subscribeOptionChain('BANKNIFTY','20240117')
- //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.
- await apidata.stream.subscribeOptionChainRange('NIFTY','20240118',10)
- //unsubscribe the chain
- await apidata.stream.unsubscribeOptionChain('NIFTY','20240118')
Subscribe and Unsubscribe Greeks Chain
- //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.
- //params: underlying ticker, ExpiryDate, NoOfStrikes
- await apidata.stream.subscribeGreeksChainRange('NIFTY','20240118',10)
- //unsubscribe the chain
- await apidata.stream.unsubscribeGreeksChain('NIFTY','20240118')
History data – Eod
- //*** Continues data
- //params: ticker, startDate, endDate
- await apidata.history.getEod("NIFTY-1", "20240112", "20240118")
- //*** Contract data
- //params: underlying ticker, startDate, endDate, contractExpiryDate
- await apidata.history.getEodContract("NIFTY", "20240109", "20240118", "20240125")
- //response data
- {
- td: '2024-01-18T00:00:00',
- op: 21461.9
- hp: 21574,
- lp: 21316.8,
- cp: 21520.4,
- vol: 260625,
- oi: 11075450
- }
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.
- //*** Continues data
- //params: ticker, startDate, endDate, resolution
- await apidata.history.getIntraEod("NIFTY-1", "20240112", "20240118", "5")
- //*** Contract data
- //params: underlying ticker, startDate, endDate, contractExpiryDate
- await apidata.history.getIntraEodContract("NIFTY", "20240109", "20240118", "20240125", "5")
- //response data
- {
- td: '2024-01-18T09:15:00',
- op: 21461.9
- hp: 21574,
- lp: 21316.8,
- cp: 21520.4,
- vol: 260625,
- oi: 11075450
- }
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.
- //params: ticker, fromDateTime
- await apidata.history.getBackTicks("BANKNIFTY-1", "20240118 15:00:00")
- //response data
- {
- td: 2024-01-18T15:00:01.000Z,
- pr: 45720,
- vol: 135,
- oi: 2357865
- }
Sample API Example
- var apidata = require('pix-apidata')
- const apiKey = "your-api-key"
- const apiServer = "apidata.accelpix.in"
- apidata.callbacks.onTrade(t => {
- console.log(t);
- })
- apidata.callbacks.onGreeks(greek => {
- console.log(greek);
- })
- apidata.initialize(apiKey, apiServer)
- .then(async () => {
- await apidata.stream.subscribeAll(['NIFTY-1'])
- await apidata.stream.subscribeGreeks(["NIFTY2220318500CE"])
- await apidata.stream.subscribeOptionChainRange('NIFTY', '20240118',7)
- await apidata.stream.subscribeGreeksChainRange('NIFTY', '20240118',5)
- // await apidata.stream.unsubscribeOptionChain('NIFTY', 20240118);
- let eod = await apidata.history.getEod('NIFTY 50', '20240112', '20240116')
- console.log(eod);
- })