Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

trading-signals

bennycode7.7kMIT6.8.0TypeScript support: included

Technical indicators to run technical analysis with JavaScript / TypeScript.

adx, analysis, atr, average, bollinger, dema, dma, ema, exponential-moving-average, indicator, macd, math, moving-average, roc, rsi, signals, simple-moving-average, sma, smma, technical, technical-indicators, technical-overlays, trading

readme

Trading Signals

Language Details Code Coverage License Package Version

Technical indicators and overlays to run technical analysis with JavaScript / TypeScript.

Motivation

The "trading-signals" library provides a TypeScript implementation for common technical indicators with arbitrary-precision decimal arithmetic.

The main focus of this library is on the accuracy of calculations, but using the provided faster implementations you can also use it where performance is important.

All indicators can be updated over time by streaming data (prices or candles) to the add method. Some indicators also provide static batch methods for further performance improvements when providing data up-front during a backtest or historical data import. You can try it out streaming input data by running the provided demo script with npm start, which uses a keyboard input stream.

Installation

npm install trading-signals

Usage

CommonJS:

const {Big, SMA} = require('trading-signals');

ESM:

import {Big, SMA} from 'trading-signals';

Example:

import {Big, SMA} from 'trading-signals';

const sma = new SMA(3);

// You can add values individually:
sma.add(40);
sma.add(30);
sma.add(20);

// You can add multiple values at once:
sma.updates([20, 40, 80]);

// You can add stringified values:
sma.add('10');

// You can replace a previous value (useful for live charting):
sma.replace('40');

// You can add arbitrary-precision decimals:
sma.add(new Big(30.0009));

// You can check if an indicator is stable:
console.log(sma.isStable); // true

// If an indicator is stable, you can get its result:
console.log(sma.getResult()?.toFixed()); // "50.0003"

// You can also get the result without optional chaining:
console.log(sma.getResultOrThrow().toFixed()); // "50.0003"

// Various precisions are available too:
console.log(sma.getResultOrThrow().toFixed(2)); // "50.00"
console.log(sma.getResultOrThrow().toFixed(4)); // "50.0003"

// Each indicator also includes convenient features such as "lowest" and "highest" lifetime values:
console.log(sma.lowest?.toFixed(2)); // "23.33"
console.log(sma.highest?.toFixed(2)); // "53.33"

When to use add(...)?

To input data, you need to call the indicator's add method. Depending on whether the minimum required input data for the interval has been reached, the add method may or may not return a result from the indicator.

When to use getResultOrThrow()?

You can call getResultOrThrow() at any point in time, but it throws errors unless an indicator has received the minimum amount of data. If you call getResultOrThrow(), before an indicator has received the required amount of input values, a NotEnoughDataError will be thrown.

Example:

import {SMA} from 'trading-signals';

// Our interval is 3, so we need 3 input values
const sma = new SMA(3);

// We supply 2 input values
sma.update(10);
sma.update(40);

try {
  // We will get an error, because the minimum amount of inputs is 3
  sma.getResultOrThrow();
} catch (error) {
  console.log(error.constructor.name); // "NotEnoughDataError"
}

// We will supply the 3rd input value
sma.update(70);

// Now, we will receive a proper result
console.log(sma.getResultOrThrow().valueOf()); // "40"

Most of the time, the minimum amount of data depends on the interval / time period used.

Technical Indicator Types

  • Trend indicators: Measure the direction of a trend (uptrend, downtrend or sideways trend)
  • Volume indicators: Measure the strength of a trend (based on volume)
  • Volatility indicators: Measure how much disagreement there is in the market based on price (statistical measure of its dispersion)
  • Momentum indicators: Measure the strength of a trend (based on price / speed of price movement)

Supported Technical Indicators

  1. Acceleration Bands (ABANDS)
  2. Accelerator Oscillator (AC)
  3. Average Directional Index (ADX)
  4. Average True Range (ATR)
  5. Awesome Oscillator (AO)
  6. Bollinger Bands (BBANDS)
  7. Bollinger Bands Width (BBW)
  8. Center of Gravity (CG)
  9. Commodity Channel Index (CCI)
  10. Directional Movement Index (DMI / DX)
  11. Double Exponential Moving Average (DEMA)
  12. Dual Moving Average (DMA)
  13. Exponential Moving Average (EMA)
  14. Interquartile Range (IQR)
  15. Linear Regression (LINREG)
  16. Mean Absolute Deviation (MAD)
  17. Momentum (MOM / MTM)
  18. Moving Average Convergence Divergence (MACD)
  19. On-Balance Volume (OBV)
  20. Parabolic SAR (PSAR)
  21. Rate-of-Change (ROC)
  22. Relative Moving Average (RMA)
  23. Relative Strength Index (RSI)
  24. Simple Moving Average (SMA)
  25. Stochastic Oscillator (STOCH)
  26. Stochastic RSI (STOCHRSI)
  27. Tom Demark's Sequential Indicator (TDS)
  28. True Range (TR)
  29. Volume-Weighted Average Price (VWAP)
  30. Weighted Moving Average (WMA)
  31. Wilder's Smoothed Moving Average (WSMA / WWS / SMMA / MEMA)

Utility Methods:

  1. Average / Mean
  2. Median
  3. Quartile
  4. Standard Deviation
  5. Streaks

Performance

Arbitrary-precision decimal arithmetic

JavaScript is very bad with numbers. When calculating 0.1 + 0.2 it shows you 0.30000000000000004, but the truth is 0.3.

JavaScript arithmetic

As specified by the ECMAScript standard, all arithmetic in JavaScript uses double-precision floating-point arithmetic, which is only accurate until certain extent. To increase the accuracy and avoid miscalculations, the trading-signals library uses big.js which offers arbitrary-precision decimal arithmetic. However, this arbitrary accuracy comes with a downside: Calculations with it are not as performant as with the primitive data type number.

Faster implementations

To get the best of both worlds (high accuracy & high performance), you will find two implementations of each indicator (e.g. SMA & FasterSMA). The standard implementation uses big.js and the Faster-prefixed version uses common number types. Use the standard one when you need high accuracy and use the Faster-one when you need high performance:

import {FasterSMA} from './SMA/SMA';

const fasterSMA = new FasterSMA(5);
console.log(fasterSMA.updates([1, 2, 3, 4, 5]));
import {FasterStochasticRSI, FasterSMA} from 'trading-signals';

const fasterStochRSI = new FasterStochasticRSI(3, FasterSMA);
fasterStochRSI.update(1);
fasterStochRSI.update(2);
fasterStochRSI.update(3);
fasterStochRSI.update(4);
fasterStochRSI.update(5);
fasterStochRSI.update(6);

console.log(fasterStochRSI.getResultOrThrow());

Benchmarks

You can run npm run start:benchmark to see the runtime performance of each technical indicator on your machine. This will give you an understanding of which indicators can be calculated faster than others.

Disclaimer

The information and publications of trading-signals do not constitute financial advice, investment advice, trading advice or any other form of advice. All results from trading-signals are intended for information purposes only.

It is very important to do your own analysis before making any investment based on your own personal circumstances. If you need financial advice or further advice in general, it is recommended that you identify a relevantly qualified individual in your jurisdiction who can advise you accordingly.

Alternatives

Maintainers

Benny Neugebauer on Stack Exchange

⭐️ Become a TypeScript rockstar! ⭐️

This package was built by Benny Code. Checkout my TypeScript course to become a coding rockstar!

License

This project is MIT licensed.

changelog

6.8.0 (2025-05-20)

6.7.0 (2025-05-20)

New Features
  • Add Volume Weighted Average Price (VWAP) (#802) (2b5aa53b)

6.6.2 (2025-05-10)

Refactors

6.6.1 (2025-05-04)

6.6.0 (2025-05-04)

New Features
Refactors

6.5.0 (2025-05-02)

New Features
  • TDS: Add Tom Demark's Sequential Indicator (#799) (6c65db46)

6.4.0 (2025-05-01)

New Features
Refactors

6.3.0 (2025-04-23)

New Features

6.2.0 (2025-02-25)

New Features
  • LINREG: Add Linear Regression (6ae426c3)
Refactors
  • LINREG: Expose Linear Regression (4b701bfd)

6.1.0 (2025-02-20)

New Features
Refactors
  • SMA: Use average-calculating code (fa3a57d9)

6.0.1 (2025-01-13)

Documentation Changes
Refactors

6.0.0 (2025-01-13)

Documentation Changes
New Features
Bug Fixes
Other Changes
Refactors

5.0.4 (2024-05-08)

New Features
Bug Fixes
  • WMA: Fix caching of highest and lowest result (#684) (0a8ec39d)

5.0.3 (2024-05-08)

Documentation Changes
  • Fix image link for GitHub Pages (2afb42ba)
New Features
Bug Fixes
  • ATR,AO,CG,SMA,TR: Fix caching of highest and lowest result (#679) (bcb73a66)

5.0.2 (2024-05-06)

Documentation Changes
Bug Fixes

5.0.1 (2024-04-16)

Bug Fixes

5.0.0 (2024-04-05)

Documentation Changes
  • Mention live-charting functionality (03740200)
New Features
  • AC,ADX,CG,DEMA,DMA,EMA,MACD,MAD,MOM,ROC,RSI,SMA,STOCH,TR,WSMA: Allow replacing values (#654) (4f95dfa0)

4.0.0 (2023-10-31)

Documentation Changes
Other Changes
Refactors

3.7.1 (2023-10-31)

Documentation Changes
Other Changes
Refactors

3.7.0 (2023-04-01)

Documentation Changes
New Features
Refactors

3.6.1 (2022-03-29)

Documentation Changes

3.6.0 (2022-01-03)

New Features
Refactors
  • Sort supported technical indicators (c2be2192)

3.5.0 (2021-12-27)

New Features
Bug Fixes
Refactors

3.4.0 (2021-12-27)

New Features

3.3.0 (2021-12-21)

New Features
  • BBANDS,BBW: Add Bollinger Bands Width (BBW) (#381) (e3516d40)
Bug Fixes
  • STOCH,SMA,MAD,ABANDS: Prevent division by zero mistakes (#380) (6837b145)

3.2.1 (2021-12-14)

New Features
  • DEMA: Add faster implementation (e26d546f)
  • CG: Add faster implementation (abd00a6d)
Bug Fixes
  • RSI,STOCH: Prevent division by zero errors (#379) (265f6b04)

3.2.0 (2021-12-06)

New Features
Refactors
  • Run faster implementation after original implementation (1583ed54)

3.1.0 (2021-11-29)

New Features

3.0.0 (2021-11-19)

Documentation Changes
New Features
Refactors
  • ADX: Return direct result and +DI & -DI only via getters (#368) (2c0818fe)
  • SMMA,WSMA: Replaced SMMA with WSMA (#362) (80a0f2c4)
  • Remove "isStable" override (77901cfb)

2.5.0 (2021-11-12)

Documentation Changes
New Features
  • CCI: Add faster CCI implementation (#356) (f90e95b6)
  • CCI,MAD: Add Commodity Channel Index (CCI) & Mean Absolute Deviation (MAD) (#355) (754398f3)

2.4.1 (2021-11-07)

Documentation Changes
New Features
Refactors

2.4.0 (2021-10-24)

Documentation Changes
New Features
  • BBANDS: Add faster Bollinger Bands implementation based on numbers (#338) (d262dad4)
  • Add Standard Deviation (#337) (b89dbcdc)
  • util: Add faster average implementation based on numbers (2029f1c3)
  • SMA: Add faster SMA implementation based on numbers (#336) (ea918088)
Other Changes
  • bennycode/trading-signals into main (c8120128)
Refactors
  • util: Change faster prefix (d4894377)
  • SMA:
    • Export FasterSMA from SMA directory (cfe98337)
    • Re-use static getResultFromBatch method (89e08a6a)
  • EMA: Calculate weight factor only once (80b368f1)

2.3.0 (2021-09-05)

Documentation Changes
  • Specify technical indicator types (#316) (edff17ef)
  • Add Technical Analysis Library using Pandas (7cf77846)
  • Add Technical Analysis Library using Pandas (26baa759)
New Features
  • STOCH: Add Stochastic Oscillator (#314) (6d13ca6a)
  • WSMA: Add Wilder's Smoothed Moving Average (WSMA) (#313) (a9a94343)
  • ADX,ATR,RSI: Add option to use EMA or SMA for smoothing results (#312) (c75f34e7)
Refactors
  • ADX,ATR,STOCH: Share high low close type (e3d677f5)
  • ABANDS,BBANDS: Reorganize source code files (300b4413)

2.2.0 (2021-08-29)

New Features
  • Export SimpleIndicator & Indicator interface (2e7e5447)

2.1.0 (2021-08-28)

New Features
  • EMA,DEMA,MACD: Don't emit values before minimum amount of data is received (#308) (9074514c)
Refactors

2.0.1 (2021-08-28)

New Features
Bug Fixes
  • SMMA,RSI,ATR,ADX: Don't cache more prices than necessary to fill the interval (#307) (2bb0a638)

2.0.0 (2021-08-15)

New Features
  • AC,AO,ATR,CG,DEMA,EMA,MOM,ROC,RSI,SMA,SMMA: Save highest & lowest values for all simple indicators (#295) (7c6433be)

1.10.2 (2021-08-12)

New Features
  • AC,AO: Directly return result on update (1ea3efe4)

1.10.1 (2021-08-05)

Refactors
  • AC,AO: Expose internal indicators (71273704)

1.10.0 (2021-08-05)

New Features
Refactors
  • util: Export getAverage separately (6f4a1c58)

1.9.0 (2021-08-04)

New Features

1.8.0 (2021-07-20)

1.7.0 (2021-06-11)

New Features
  • ADX: Return directional indicators (+DI & -DI) (458466fe)

1.6.1 (2021-04-17)

Bug Fixes
  • macd: Ensure MACD histogram compatibility with Tulip Indicators (#219) (3e27d4e9)

1.6.0 (2021-02-16)

New Features

1.5.1 (2021-02-08)

1.5.0 (2021-02-08)

New Features

1.4.0 (2021-02-06)

New Features
  • Add Acceleration Bands (ABANDS) indicator (#175) (67d1d881)

1.3.0 (2021-01-31)

Bug Fixes
  • Align MACD results with Tulip Indicators (#171) (5923be10)

1.2.1 (2020-12-09)

Bug Fixes
  • Check long interval when using moving average crossover with EMA (#142) (4c218a9e)

1.2.0 (2020-11-05)

New Features
  • Add option to use EMA for DMA calculation (#116) (90323796)

1.1.1 (2020-09-06)

1.1.0 (2020-09-04)

New Features
  • Add option to use EMA for RSI calculation (#70) (33b7b750)

1.0.1 (2020-06-15)

1.0.0 (2020-06-03)

New Features
  • Add Average Directional Index (ADX) indicator (#14) (1a21d531)
  • Add isStable to DMA indicator (5f1f9dcb)

0.4.0 (2020-06-01)

New Features
  • Add Average True Range (ATR) indicator (#13) (e6f88ea8)

0.2.0 (2020-06-01)

0.1.0 (2020-05-31)

New Features
  • Add Moving Average Convergence Divergence (MACD) indicator (#7) (38a645de)

0.0.5 (2020-05-30)

New Features
  • Add Rate-of-Change (ROC) indicator (#4) (f02b5efe)

0.0.4 (2020-05-30)

New Features
  • Add Relative Strength Index (RSI) (#3) (f49f897c)
  • Accept input of type number, string & Big (13aaa996)

0.0.2 (2020-05-28)

Other Changes

0.0.1 (2020-05-28)

0.0.2 (2020-05-28)

0.0.1 (2020-05-28)

Other Changes

0.0.1 (2020-05-28)

Other Changes