Package Exports
- truedata-nodejs
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (truedata-nodejs) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Documentation for TrueData Nodejs
- Websocket APIs – Live data
- REST APIs – Historical data
Installation
Install via npm
npm install truedata-nodejs
Getting started
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
Sample code for live data feed
rtConnect(user, pwd, symbols, port, bidask = 1, heartbeat = 1);
rtFeed.on('touchline', touchlineHandler);
rtFeed.on('tick', tickHandler);
rtFeed.on('bidask', bidaskHandler);
rtFeed.on('bar', barHandler);
function touchlineHandler(touchline){
console.log(touchline)
}
function tickHandler(tick){
console.log(tick)
}
function bidaskHandler(bidask){
console.log(bidask)
}
function barHandler(bar){
console.log(bar)
}
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
Subscribe 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 symbol
Auto 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 format
All 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') // Max upto last 5 days of tick data
- historical.getBarData (symbol, from, to, interval = '1min', response = 'json')
- historical.getLastNTicks (symbol, nticks = 2000, bidask = 1, response = 'json') // Max upto last 5 days of tick data
- historical.getLastNBars (symbol, nbars = 200, interval = '1min', response = 'json') // interval = '1min' OR 'EOD'
- historical.getBhavCopyStatus (segment = 'FO', response = 'json')
- historical.getBhavCopy (segment = 'FO', date, response = 'json')
- historical.getLTP (symbol, bidask = 1, response = 'json')
- 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
| segment = 'FO' | segment: 'FO', 'EQ', 'MCX'
| 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' )
.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')
.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('bidask', bidaskHandler);
rtFeed.on('bar', barHandler);
function touchlineHandler(touchline) {
console.log(touchline);
}
function tickHandler(tick) {
console.log(tick);
}
function bidaskHandler(bidask) {
console.log(bidask);
}
function barHandler(bar) {
console.log(bar);
}
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'))
.then((res) => console.log(res))
.catch((err) => console.log(err));
historical
.getBarData('RELIANCE', from, to, (interval = '1min'), (response = 'json'))
.then((res) => console.log(res))
.catch((err) => console.log(err));
historical
.getBarData('NIFTY 50', (duration = '1W'), (interval = '60min'), (response = 'json'))
.then((res) => console.log(res))
.catch((err) => console.log(err));
historical
.getTickData('SBIN', '1D', (bidask = 1), (response = 'csv'))
.then((res) => console.log(res))
.catch((err) => console.log(err));
historical
.getLTP('L&TFH')
.then((res) => console.log(res))
.catch((err) => console.log(err));