Skip to content

eliabejr/technical-indicators

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Technical Indicators

A lightweight TypeScript library for technical analysis indicators used in financial markets. Built with zero external dependencies and real-time streaming support. Have fun.

Installation

yarn add technical-indicators

Quick Start

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);

Available Indicators

Moving Averages

  • SMA - Simple Moving Average
  • EMA - Exponential Moving Average
  • VWMA - Volume Weighted Moving Average
  • MACD - Moving Average Convergence Divergence

Momentum Indicators

  • RSI - Relative Strength Index

Trend Indicators

  • PSAR - Parabolic SAR (Stop and Reverse)

Volume Indicators

  • OBV - On-Balance Volume

Volatility Indicators

  • Bollinger Bands - Standard deviation bands

Usage Examples

Basic Usage

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]);

Streaming Data

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);

Configuration

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 });

API Reference

Base Interface

All indicators implement the IIndicator interface:

interface IIndicator<TInput, TOutput> {
  calculate(data: TInput[]): TOutput[];
  calculateNext(newData: TInput): TOutput | null;
  reset(): void;
}

Data Types

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';
}

Mathematical Accuracy

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)

Performance

Designed for high-frequency trading environments:

  • O(1) complexity for calculateNext() operations
  • Minimal memory footprint
  • No unnecessary object allocations
  • Optimized mathematical operations

Error Handling

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

License

GNU Lesser General Public License v2.1

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Support

If you encounter any issues or have questions, please file an issue on the GitHub repository.

About

Lightweight TypeScript library for technical analysis indicators used in financial markets.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published