@cesarvarela/indicators
Technical indicators for financial and time series analysis, built with a functional, composable philosophy.
Features
- 🎯 Type-safe - Full TypeScript support with generics
- 🔧 Flexible - Works with any data structure
- 📦 Tree-shakeable - Import only what you need
- 🚀 Fast - Optimized calculations
- 🧩 Composable - Built on the
compute
pattern for consistent transformations - 0️⃣ Zero dependencies - No external packages
Installation
npm install @cesarvarela/indicators
Usage
import { rsi, macd, bollingerBands, atr, obv } from '@cesarvarela/indicators';
// Your data can be any structure with numeric fields
const bars = [
{ open: 100, high: 105, low: 95, close: 102, volume: 1000, timestamp: new Date() },
{ open: 102, high: 108, low: 100, close: 106, volume: 1200, timestamp: new Date() },
// ... more bars
];
// Most indicators work on any numeric field
const withRSI = rsi(bars, 14, 'close'); // RSI with period 14
const withMACD = macd(bars, 'close'); // MACD with default periods
const withBB = bollingerBands(bars, 'close'); // Bollinger Bands
// Some indicators need specific fields (TypeScript enforces this)
const withATR = atr(bars); // Requires open, high, low, close
const withOBV = obv(bars); // Requires volume field
The Compute Philosophy
All indicators are built using the compute
function, which enables:
- Immutable transformations (original data is never modified)
- Access to previously computed values during calculation
- Type-safe property additions
- Consistent, predictable API
import { compute } from '@sigma/indicators';
// Create your own indicators using compute
const withCustom = compute(bars, 'myIndicator', (bar, index, allBars, params) => {
if (index < params.period - 1) return undefined;
// Your calculation here
return bar.close * params.multiplier;
}, { period: 10, multiplier: 2 });
Available Indicators
Momentum Indicators
- RSI (Relative Strength Index)
- MACD (Moving Average Convergence Divergence)
Moving Averages
- SMA (Simple Moving Average)
- EMA (Exponential Moving Average)
Volatility Indicators
- Bollinger Bands
- ATR (Average True Range)
- Donchian Channel
Volume Indicators
- OBV (On-Balance Volume)
- OBV MA (OBV Moving Average)
Utility Functions
- slope - Linear regression slope
- relativeChange - Percentage change between values
- crossover/crossunder - Detect line crossings
- zscore - Statistical z-score
Type Requirements
Most indicators work with any object structure. Some require specific fields:
// Minimal interfaces for specific indicators
interface OHLCBar {
open: number;
high: number;
low: number;
close: number;
}
interface VolumeBar {
volume: number;
}
// ATR requires OHLC data
const withATR = atr(ohlcBars);
// OBV requires volume data
const withOBV = obv(volumeBars);
// Most indicators work on any field
const withRSI = rsi(anyDataArray, 14, 'anyNumericField');
Examples
Chaining Multiple Indicators
// Each indicator preserves all previous fields
const data = bollingerBands(
macd(
rsi(bars, 14, 'close'),
'close'
),
'close'
);
// Result has RSI, MACD, and Bollinger Bands fields
Custom Indicators
import { compute } from '@sigma/indicators';
// Create a custom momentum indicator
const momentum = <T>(data: T[], field: keyof T, period: number = 10) => {
return compute(data, 'momentum', (item, index, allItems) => {
if (index < period) return undefined;
const oldValue = allItems[index - period][field] as number;
const currentValue = item[field] as number;
return ((currentValue - oldValue) / oldValue) * 100;
}, { period });
};
API Documentation
See the full API documentation for detailed usage of each indicator.
License
MIT © Cesar Varela