A lightweight TypeScript library for technical analysis indicators used in financial markets. Built with zero external dependencies and real-time streaming support. Have fun.
yarn add technical-indicators
import { SMA, EMA, RSI, PriceData } from 'technical-indicators';
const prices: PriceData[] = [
{ open: 100, high: 105, low: 99, close: 104, volume: 1000 },
{ open: 104, high: 107, low: 103, close: 106, volume: 1200 },
// and so on...
];
// Simple Moving Average
const sma = new SMA(20);
const smaValues = sma.calculate(prices);
// Exponential Moving Average
const ema = new EMA(12);
const emaValues = ema.calculate(prices);
// RSI
const rsi = new RSI(14);
const rsiValues = rsi.calculate(prices);
- SMA - Simple Moving Average
- EMA - Exponential Moving Average
- VWMA - Volume Weighted Moving Average
- MACD - Moving Average Convergence Divergence
- RSI - Relative Strength Index
- PSAR - Parabolic SAR (Stop and Reverse)
- OBV - On-Balance Volume
- Bollinger Bands - Standard deviation bands
import { RSI, BollingerBands, PriceData } from 'technical-indicators';
const data: PriceData[] = [
// OHLCV data
];
// RSI with 14-period
const rsi = new RSI(14);
const rsiValues = rsi.calculate(data);
// Bollinger Bands (20-period, 2 standard deviations)
const bb = new BollingerBands(20, 2);
const bands = bb.calculate(data);
console.log('Current RSI:', rsiValues[rsiValues.length - 1]);
console.log('Current Bands:', bands[bands.length - 1]);
Perfect for real-time trading applications:
import { EMA, PriceData } from 'technical-indicators';
const ema = new EMA(12);
// historical data
ema.calculate(historicalData);
// incoming data
const newCandle: PriceData = {
open: 100, high: 102, low: 99, close: 101, volume: 500
};
const currentEMA = ema.calculateNext(newCandle);
console.log('Current EMA:', currentEMA);
All indicators support configuration:
import { PSAR } from 'technical-indicators';
const psar = new PSAR(0.02, 0.2); // step, maxStep
// reconfigure if needed
psar.configure({ step: 0.01, maxStep: 0.15 });
All indicators implement the IIndicator
interface:
interface IIndicator<TInput, TOutput> {
calculate(data: TInput[]): TOutput[];
calculateNext(newData: TInput): TOutput | null;
reset(): void;
}
interface PriceData {
open: number;
high: number;
low: number;
close: number;
volume?: number;
timestamp?: number;
}
interface BollingerBandsResult {
upper: number;
middle: number;
lower: number;
}
interface PSARResult {
value: number;
isReversal: boolean;
trend: 'up' | 'down';
}
This library implements indicators using standard financial formulas:
- SMA: Arithmetic mean over N periods
- EMA: Exponentially weighted average with α = 2/(N+1)
- RSI: Relative Strength Index using EMA-smoothed gains/losses
- PSAR: Parabolic SAR with configurable acceleration factor
- Bollinger Bands: SMA ± (Standard Deviation × multiplier)
Designed for high-frequency trading environments:
- O(1) complexity for
calculateNext()
operations - Minimal memory footprint
- No unnecessary object allocations
- Optimized mathematical operations
The library handles edge cases gracefully:
- Returns
NaN
for insufficient data periods - Returns
null
for streaming when data is insufficient - Validates input parameters
- Handles division by zero scenarios
GNU Lesser General Public License v2.1
Contributions are welcome! Please feel free to submit a Pull Request.
If you encounter any issues or have questions, please file an issue on the GitHub repository.