truedata-nodejs
v1.1.0
Published
Truedata's Official Nodejs Package
Maintainers
Readme
Documentation for TrueData Nodejs
- Websocket APIs – Live data
- REST APIs – Historical data
- Corporate Websocket APIs - Live announcement data
- Corporate REST APIs - Corporate Historical data
Installation
Install via npm
npm install truedata-nodejsGetting started
const { rtConnect, rtDisconnect, rtSubscribe, rtUnsubscribe, rtFeed, historical, formatTime, isSocketConnected } = require('truedata-nodejs')
const user = 'your username'
const pwd = 'your password'
const port = 8082
const symbols = ['NIFTY-I', 'BANKNIFTY-I', 'CRUDEOIL-I']; // symbols in array formatSample code for live data feed
rtConnect(user, pwd, symbols, port, bidask = 1, heartbeat = 1, replay = 0, url = 'push');
rtFeed.on('touchline', touchlineHandler); // Receives Touchline Data
rtFeed.on('tick', tickHandler); // Receives Tick data
rtFeed.on('greeks', greeksHandler); // Receives Greeks data
rtFeed.on('bidask', bidaskHandler); // Receives Bid Ask data if enabled
rtFeed.on('bidaskL2', bidaskL2Handler); // Receives level 2 Bid Ask data only for BSE exchange
rtFeed.on('bar', barHandler); // Receives 1min and 5min bar data
rtFeed.on('marketstatus', marketStatusHandler); // Receives marketstatus messages
rtFeed.on('heartbeat', heartbeatHandler); // Receives heartbeat message and time
function touchlineHandler(touchline){
console.log(touchline)
}
function tickHandler(tick){
console.log(tick)
}
function greeksHandler(greeks){
console.log(greeks)
}
function bidaskHandler(bidask){
console.log(bidask)
}
function bidaskL2Handler(bidaskL2){
console.log(bidaskL2)
}
function barHandler(bar){
console.log(bar)
}
function marketStatusHandler(status) {
console.log(status);
}
function heartbeatHandler(heartbeat) {
console.log(heartbeat);
}Default values and available values
| Default values | Available values
| -------------- | ----------------
| bidask = 1 | 0, 1 // 1 = bidask active, 0 = bidask inactive
| heartbeat = 1 | 0, 1 // 1 = print heartbeat message, 0 = don't print heartbeat message
| replay = 0 | 0, 1 // 1 = Feed replay, 0 = Normal feed ( default - If parameter not entered then replay = 0 )
| url = 'push' | 'push' Check websocket connection status
isSocketConnected() // Check if socket is connected or not. Returns Boolean valueCheck market status
getMarketStatus() // Return the current market statusSubscribe and Unsubscribe symbols dynamically
rtSubscribe( newSymbols ) // Dynamically subscribe to new symbols // newSymbols is array of symbol
rtUnsubscribe( oldSymbols ) // Dynamically unsubscribe from currently subscribed symbols // oldSymbols is array of symbolDisconnect live data feed
rtDisconnect() // Disconnect live data feed and close socket connectionAuto re-connect Websocket
The library will check for internet connection and once steady will try to re-connect the Websocket.
Historical REST API
historical.auth(user, pwd); // For authentication.
from = formatTime(2021, 3, 2, 9, 15) // (year, month, date, hour, minute) // hour in 24 hour format
to = formatTime(2021, 3, 5, 9, 15) // (year, month, date, hour, minute) // hour in 24 hour formatAll available API calls
All API calls returns a promise. You can use .then(...) and .catch(...) OR async and await method
- historical.getTickData (symbol, from, to, bidask = 1, response = 'json', getSymbolId = 0) // Max upto last 5 days of tick data
- historical.getBarData (symbol, from, to, interval = '1min', response = 'json', getSymbolId = 0)
- historical.getLastNTicks (symbol, nticks = 2000, bidask = 1, response = 'json', getSymbolId = 0) // Max upto last 5 days of tick data
- historical.getLastNBars (symbol, nbars = 200, interval = '1min', response = 'json', getSymbolId = 0) // interval = '1min' OR 'EOD'
- historical.getBhavCopyStatus (segment = 'FO', response = 'json')
- historical.getBhavCopy (segment = 'FO', date, response = 'json')
- historical.getLTP (symbol, bidask = 1, response = 'json', getSymbolId = 0)
- historical.getTopGainers (topsegment = 'NSEFUT', top = 50, response = 'json')
- historical.getTopLosers (topsegment = 'NSEFUT', top = 50, response = 'json')
- historical.getTopVolumeGainers (topsegment = 'NSEFUT', top = 50, response = 'json')
- historical.getCorpAction (symbol, response = 'json')
- historical.getSymbolNameChange (response = 'json')
Exceptions in :
- historical.getTickData (symbol, from, to, bidask = 1, response = 'json')
- historical.getBarData (symbol, from, to, interval = '1min', response = 'json')
You can use “duration” instead of using “from and to” in both the above functions. If you are using “duration” then, the function will be like:
- historical.getTickData (symbol, duration = '1D' , bidask = 1, response = 'json')
- historical.getBarData (symbol, duration = '2W', interval = '1min', response = 'json')
Durations
- '5D' // D for Day
- '3W' // W for Week
- '2M' // M for Month
- '1Y' // Y for Year
Default values and available values
| Default values | Available values
| -------------- | ----------------
| response = 'json' | response : 'json', 'csv'
| bidask = 1 | bidask: 1, 0 // 1 – bidask feed active , 0 – bidask feed inactive
| interval = '1min' | interval:'1min', '2min', '3min', '5min', '10min', '15min', '30min', '60min', 'EOD'
| nticks = 2000 | nticks: Any number // Max upto last 5 days of ticks
| nbars = 200 | nbars: Any number
| getSymbolId = 0 | getSymbolId : 1, 0 // 1 - symbolId active , 0 - symbolId inactive
| segment = 'FO' | segment: 'FO', 'EQ', 'MCX', 'BSEFO', 'BSEEQ'
| topsegment = 'NSEFUT' | topsegment: 'NSEFUT', 'NSEEQ', 'NSEOPT', 'CDS', 'MCX'
| top = 50 | top: Any number
*symbol in historical api is symbol name. For eg: 'NIFTY-I' or 'RELIANCE' etc.
*In historical.getBhavCopy, current date is default. Date format is 'yyyy-mm-dd'Sample Code
getBarData for a particular symbol using from and to :
historical
.getBarData('NIFTY-I', '210302T09:00:00', '210302T15:30:00', interval = '1min', response ='json', getSymbolId = 0 )
.then((res) => console.log(res))
.catch((err) => console.log(err));getBarData for a particular symbol using duration :
historical
.getBarData('NIFTY-I', duration = '3W', interval = '1min', response ='json', getSymbolId = 0)
.then((res) => console.log(res))
.catch((err) => console.log(err));Sample code for Live and Historical API
const { rtConnect, rtSubscribe, rtUnsubscribe, rtFeed, historical, formatTime } = require('truedata-nodejs');
const user = 'your username';
const pwd = 'your password';
const port = 8082;
const symbols = ['NIFTY-I', 'BANKNIFTY-I', 'CRUDEOIL-I']; // symbols in array format
rtConnect(user, pwd, symbols, port, (bidask = 1), (heartbeat = 1));
rtFeed.on('touchline', touchlineHandler);
rtFeed.on('tick', tickHandler);
rtFeed.on('greeks', greeksHandler);
rtFeed.on('bidask', bidaskHandler);
rtFeed.on('bidaskL2', bidaskL2Handler);
rtFeed.on('bar', barHandler);
rtFeed.on('marketstatus', marketStatusHandler)
rtFeed.on('heartbeat', heartbeatHandler);
function touchlineHandler(touchline) {
console.log(touchline);
}
function tickHandler(tick) {
console.log(tick);
}
function greeksHandler(greeks){
console.log(greeks)
}
function bidaskHandler(bidask) {
console.log(bidask);
}
function bidaskL2Handler(bidaskL2){
console.log(bidaskL2)
}
function barHandler(bar) {
console.log(bar);
}
function marketStatusHandler(status) {
console.log(status);
}
function heartbeatHandler(heartbeat) {
console.log(heartbeat);
}
historical.auth(user, pwd); // For authentication
from = formatTime(2021, 3, 2, 9, 15); // (year, month, date, hour, minute) // hour in 24 hour format
to = formatTime(2021, 3, 5, 9, 15); // (year, month, date, hour, minute) // hour in 24 hour format
historical
.getBarData('NIFTY-I', '210302T09:00:00', '210302T15:30:00', (interval = '1min'), (response = 'json'), (getSymbolId = 0))
.then((res) => console.log(res))
.catch((err) => console.log(err));
historical
.getBarData('RELIANCE', from, to, (interval = '1min'), (response = 'json'), (getSymbolId = 0))
.then((res) => console.log(res))
.catch((err) => console.log(err));
historical
.getBarData('NIFTY 50', (duration = '1W'), (interval = '60min'), (response = 'json'), (getSymbolId = 0))
.then((res) => console.log(res))
.catch((err) => console.log(err));
historical
.getTickData('SBIN', '1D', (bidask = 1), (response = 'csv'), (getSymbolId = 0))
.then((res) => console.log(res))
.catch((err) => console.log(err));
historical
.getLTP('L&TFH')
.then((res) => console.log(res))
.catch((err) => console.log(err));Getting started with corporate WebSocket
const { rtCorpConnect, rtCorpDisconnect, rtCorpFeed, isCorpSocketConnected, corporate } = require('truedata-nodejs')
const user = 'your username'
const pwd = 'your password'
const port = 9092Sample code for corporate announcements live data feed
rtCorpConnect(user, pwd, port, heartbeat = 1);
rtCorpFeed.on('message', messageHandler); // Recieves Messages
rtCorpFeed.on('heartbeat', heartbeatHandler); // Receives heartbeat message and time
function messageHandler(message){
console.log(message);
}
function heartbeatHandler(hertbeat){
console.log(heartbeat);
}check websocket connection status
isCorpSocketConnected() // Check if socket is connected or not. Returns Boolean valueDisconnect corporate live data feed
reCorpDisconnect(); // Disconnect corporate live data feed and close socket connectionAuto re-connect Websocket
The library will check for internet connection and once steady will try to re-connect the Websocket.
Corporate REST API
corporate.auth(user, pwd); // For authentication.All available API calls
All API calls returns a promise. You can use .then(...) and .catch(...) OR async and await method
- corporate.getDescriptors(response='csv')
- corporate.getAnnouncementsList(response='csv', symbol='TCS', fromDate, toDate)
- corporate.getAnnouncementsForCompnies(response='csv', symbols = ['TCS'], fromDateTime, toDateTime, top=0);
- corporate.getAnnouncementsForCompniesFrom(response='csv', fromDateTime, top=0, count=1, duringmarket=1)
- corporate.getAnnouncementsForCompniesRange(response='csv', fromdateTime, toDateTime, top=10, duringmarket=1)
- corporate.getAllAnnouncementsByIds(response='csv',ids= [123]);
- corporate.getAnnouncementFile(id);
- corporate.getAllResultsByCompany(response='csv', symbol='TCS', nature='standalone', period='yearly', type='pl');
- corporate.getAllResultItemsById(id);
- corporate.getPnLById(id, cumulative=1);
- corporate.getBalSheetById(id, cumulative=1);
- corporate.getCashFlowDetailById(id, cumulative=1);
- corporate.getCashFlowSummaryById(id);
- corporate.getSHPListByDate(response='csv', date);
- corporate.getSHPListByCompany(response='csv', symbol);
- corporate.getShpDetailById(id);
- corporate.getShpSummaryById(id);
- corporate.getAllShpById(id);
- corporate.getShpMemberTypes(response='csv');
- corporate.getFIIDIIData(response='csv', date, segment='fo');
- corporate.getCorporateInfo(symbol='TCS');
- corporate.getCorporateAbout(symbol='TCS');
- corporate.getSymbolClassification(response='csv', symbol='TCS');
- corporate.getSymbolClassificationBulk(response='csv', symbols=['TCS']);
- corporate.getresultcalendar(response='csv');
- corporate.getPeerCompanies(response='csv', symbol='TCS');
- corporate.getMarketCap(response='csv', symbols=['TCS']);
Exceptions in :
corporate.getAnnouncementsList(response='csv', symbol, fromDate, toDate)
corporate.getSHPListByDate(response='csv', date);
It allows date format only '251231T09:00:00' and new Date("2025-01-31")
Default values and available values
| Default values | Available values
| -------------- | ----------------
| response = 'csv' | response : 'json', 'csv'
| top = 0 | top : 0, Any Number | 0 -> all records
| count = 0 | count : 0, 1 | 1 -> count field active, 0 -> count field inactive
| duringmarket = 0 | duringmarket: 0, 1
| cumulative = 0 | cumulative: 0, 1
| symbol = 'TCS' | eg. symbol: 'RELIANCE', 'TATASTEEL', etc.
| symbols = ['TCS'] | eg. symbols: ['TCS','RELIANCE']
| id = [12345] | eg. id: 1234
| ids = [123, 456] | eg. ids: [123, 456]
| nature = 'standalone' | nature: 'standalone', 'consolidated'
| period = 'yearly' | period: 'yearly', 'quarterly'
| type = 'pl' | type: 'pl', 'cf', 'bs'
*While calling any function, pass only parameter values in the defined order — do not use parameter names.
*For default parameters, use '' for string values, 0 for numeric values and [] for symbols and ids when you want to skip optional arguments.Sample code for Corporate API
corporate.auth(user, pwd); // For authentication
corporate.getAnnouncementsList('json', 'NIFTY-I', '251201T00:00:00', '251231T00:00:00')
.then((res) => console.log(res))
.catch((err) => console.log(err));
corporate.getAnnouncementsForCompniesFrom('csv', '260104T00:00:00', top=100, count=1, duringmarket=1)
.then((res) => console.log(res))
.catch((err) => console.log(err));
corporate.getMarketCap('', ['TCS','RELIANCE','TATASTEEL'])
.then((res) => console.log(res))
.catch((err) => console.log(err)); // response='' for use default argument
corporate.getCashFlowSummaryById()
.then((res) => console.log(res))
.catch((err) => console.log(err));