Trade Lib
A reusable Node.js trading library for broker integrations.
Currently supports Angel One SmartAPI and is designed to support additional brokers such as AliceBlue and Upstox in the future.
Features
Trading
- Place Orders
- Place Stop Loss Orders
- Place Target Orders
- Modify Orders
- Fetch Order Book
- Fetch Positions
- Check Open Orders
- Check Open Positions
- Exit All Open Positions
Master Data & Contracts
- Download Master Data
- Store Contracts Locally
- Symbol → Token Search
- Token → Contract Search
- Option Contract Search
- Exchange-wise Contract Storage
Redis Integration
- Redis Contract Cache
- Symbol Lookup Index
- Token Lookup Index
- Option Lookup Index
- Automatic Redis Cache Rebuild
Automation
- Daily Master Data Download
- Automatic Log Cleanup
- Scheduled Cron Jobs
Logging
- Order Logs
- Error Logs
- Master Data Download Errors
- Log Retention Management
Installation
npm install @anjana_75/trade-libEnvironment Variables
Create a .env file in your project:
ANGEL_API_KEY=YOUR_API_KEY
ANGEL_CLIENT_ID=YOUR_CLIENT_ID
ANGEL_PIN=YOUR_PIN
ANGEL_TOTP_SECRET=YOUR_TOTP_SECRET
REDIS_URL=redis://localhost:6379
Redis Setup (Docker)
This library uses Redis for fast contract and option searches.
Docker Compose
Create a docker-compose.yml file:
services:
redis:
image: redis:latest
container_name: trade_lib_redis
ports:
- "6379:6379"
redisinsight:
image: redis/redisinsight:latest
container_name: trade_lib_redisinsight
ports:
- "5540:5540"Start Services
docker compose up -dVerify Containers
docker psRedisInsight
Open:
http://localhost:5540
Create a connection:
Host: localhost
Port: 6379
Stop Services
docker compose downOrder Tag Support
Order tags can be passed while placing orders for easier tracking and logging.
const result = await placeOrder('angelone', {
tradingsymbol: 'SBIN-EQ',
symboltoken: '3045',
transactiontype: 'BUY',
exchange: 'NSE',
quantity: 1,
ordertype: 'MARKET',
ordertag: 'strategy_1'
});Contract Search Architecture
Local Storage
Full contract master data is stored locally:
contracts/
└── angelone/
└── masterdata/
├── nse.json
├── nfo.json
├── bse.json
├── bfo.json
├── cds.json
├── mcx.json
├── ncdex.json
└── nco.json
Redis Search Indexes
Redis stores only search indexes to reduce memory usage.
contracts:angelone:masterdata:nse:symbol:SBIN-EQ
contracts:angelone:masterdata:nse:token:3045
contracts:angelone:option:NIFTY:30JUN2026:30000:PE
Search Flow
Stock Search:
Symbol
↓
Redis
↓
Token
↓
Contract Details
Option Search:
Underlying + Expiry + Strike + Option Type
↓
Redis Option Index
↓
Token
↓
Contract Details
Redis Memory Optimization
The library stores:
- Symbol → Token
- Token → Contract
- Option → Token
Full exchange master data remains in JSON files, reducing Redis memory usage while maintaining fast searches.
Broker Architecture
The library is designed to support multiple brokers through a common interface.
Current Structure:
brokers/
├── angelone.js
├── aliceblue.js
└── upstox.js
Currently Supported:
- Angel One
Planned:
- AliceBlue
- Upstox
Usage
require('dotenv').config();
const {
placeOrder,
placeSLOrder,
placeTPOrder,
modifyOrder,
getOrders,
getPositions,
hasOpenOrder,
hasOpenPosition,
loopExitPositions,
downloadMasterData,
readMasterData,
downloadAndStoreContracts,
getTokenBySymbol,
getContractByToken,
getOptionContract
} = require('@anjana_75/trade-lib');Place Order
const result = await placeOrder('angelone', {
tradingsymbol: 'SBIN-EQ',
symboltoken: '3045',
transactiontype: 'BUY',
exchange: 'NSE',
quantity: 1,
ordertype: 'MARKET'
});
console.log(result);Place Stop Loss Order
const result = await placeSLOrder('angelone', {
tradingsymbol: 'SBIN-EQ',
symboltoken: '3045',
quantity: 1
});Place Target Order
const result = await placeTPOrder('angelone', {
tradingsymbol: 'SBIN-EQ',
symboltoken: '3045',
quantity: 1
});Get Orders
const orders = await getOrders('angelone');Get Positions
const positions = await getPositions('angelone');Download Master Data
const result = await downloadMasterData('angelone');
console.log(result);Download And Store Contracts
const result = await downloadAndStoreContracts(
'angelone'
);
console.log(result);Search Token By Symbol
const result = await getTokenBySymbol(
'angelone',
'nse',
'SBIN-EQ'
);
console.log(result);Search Contract By Token
const result = await getContractByToken(
'angelone',
'nse',
'3045'
);
console.log(result);Search Option Contract
const result = await getOptionContract(
'angelone',
'NIFTY',
'30JUN2026',
30000,
'PE'
);
console.log(result);Contract Storage Structure
contracts/
└── angelone/
└── masterdata/
├── nse.json
└── nfo.json
Redis Structure
contracts:angelone:masterdata:nse:symbol:SBIN-EQ
contracts:angelone:masterdata:nse:token:3045
contracts:angelone:option:NIFTY:30JUN2026:30000:PE
Cron Jobs
Master Data Download
Runs daily and downloads the latest contract master data.
Log Cleanup
Automatically removes logs older than the configured retention period.
Supported Brokers
Currently Supported:
- Angel One
Planned:
- AliceBlue
- Upstox
License
MIT License
Author
Anjana George