@anjana_75/trade-lib
v1.3.4
Published
Reusable Angel One trading library for placing orders, modifying orders, fetching positions, order management and master data operations.
Maintainers
Readme
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:6379Redis 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:5540Create a connection:
Host: localhost
Port: 6379Stop 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.jsonRedis 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:PESearch Flow
Stock Search:
Symbol
↓
Redis
↓
Token
↓
Contract DetailsOption Search:
Underlying + Expiry + Strike + Option Type
↓
Redis Option Index
↓
Token
↓
Contract DetailsRedis 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.jsCurrently 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.jsonRedis Structure
contracts:angelone:masterdata:nse:symbol:SBIN-EQ
contracts:angelone:masterdata:nse:token:3045
contracts:angelone:option:NIFTY:30JUN2026:30000:PECron 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 ✅
- AliceBlue ✅
Planned:
- Upstox 🚧
AliceBlue Support
Trade Lib now supports AliceBlue Open API alongside Angel One.
AliceBlue Environment Variables
Add the following values to your .env file:
ALICEBLUE_BASE_URL=https://a3.aliceblueonline.com/open-api/od/v1
ALICEBLUE_USER_ID=YOUR_USER_ID
ALICEBLUE_API_KEY=YOUR_API_KEY
ALICEBLUE_API_SECRET=YOUR_API_SECRET
ALICEBLUE_AUTH_CODE=YOUR_AUTH_CODEAliceBlue Login
const { aliceblue } = require('@anjana_75/trade-lib');
const result = await aliceblue.login();
console.log(result);AliceBlue Place Order
const result = await placeOrder('aliceblue', {
exchange: 'NSE',
tradingsymbol: 'SBIN',
transactiontype: 'BUY',
quantity: 1,
ordertype: 'MARKET',
producttype: 'INTRADAY'
});
console.log(result);AliceBlue Modify Order
const result = await modifyOrder('aliceblue', {
brokerOrderId: 'ORDER_ID',
quantity: 1,
price: 100
});
console.log(result);AliceBlue Get Orders
const orders = await getOrders('aliceblue');
console.log(orders);AliceBlue Get Positions
const positions = await getPositions('aliceblue');
console.log(positions);AliceBlue Check Open Orders
const hasOpenOrder = await hasOpenOrder(
'aliceblue'
);
console.log(hasOpenOrder);Check specific token:
const hasOpenOrder = await hasOpenOrder(
'aliceblue',
'123456'
);
console.log(hasOpenOrder);AliceBlue Check Open Positions
const hasOpenPosition = await hasOpenPosition(
'aliceblue'
);
console.log(hasOpenPosition);Check specific token:
const hasOpenPosition = await hasOpenPosition(
'aliceblue',
'123456'
);
console.log(hasOpenPosition);AliceBlue Exit All Positions
const result = await loopExitPositions(
'aliceblue'
);
console.log(result);AliceBlue Profile
const result = await aliceblue.getProfile();
console.log(result);AliceBlue Limits
const result = await aliceblue.getLimits();
console.log(result);AliceBlue Cancel Order
const result = await aliceblue.cancelOrder(
'BROKER_ORDER_ID'
);
console.log(result);AliceBlue Master Data
Download latest master data:
const result = await downloadMasterData(
'aliceblue'
);
console.log(result);AliceBlue Contract Download
Download contracts and build Redis indexes:
const result = await downloadAndStoreContracts(
'aliceblue'
);
console.log(result);Example Response:
{
success: true,
broker: 'aliceblue',
total: 50000,
nseTotal: 20000,
nfoTotal: 30000,
storedCount: 50000,
optionIndexCount: 25000
}AliceBlue Token Search
const result = await getTokenBySymbol(
'aliceblue',
'nse',
'SBIN'
);
console.log(result);AliceBlue Contract Search
const result = await getContractByToken(
'aliceblue',
'nse',
'123456'
);
console.log(result);AliceBlue Option Contract Search
const result = await getOptionContract(
'aliceblue',
'NIFTY',
'31JUL2026',
25000,
'CE'
);
console.log(result);AliceBlue Contract Storage
contracts/
└── aliceblue/
└── masterdata/
├── nse.json
└── nfo.jsonAliceBlue Redis Contract Structure
contracts:aliceblue:masterdata:nse:symbol:SBIN
contracts:aliceblue:masterdata:nse:token:123456
contracts:aliceblue:masterdata:nfo:option:NIFTY:31JUL2026:25000:CEAliceBlue Redis Auth Tokens
Trade Lib automatically stores AliceBlue authentication tokens in Redis after successful login.
auth:aliceblue:userSession
auth:aliceblue:clientIdAccount specific keys:
auth:aliceblue:2675340:userSession
auth:aliceblue:2675340:clientIdAliceBlue Rate Limiting
Trade Lib automatically applies broker-safe delays when fetching:
await getOrders('aliceblue');
await getPositions('aliceblue');This helps prevent broker API throttling and excessive request rates.
License
MIT License
Author
Anjana George
