Technical indicators and overlays to run technical analysis with JavaScript / TypeScript.
The "trading-signals" library provides a TypeScript implementation for common technical indicators. It is well-suited for algorithmic trading, allowing developers to perform signal computations for automated trading strategies.
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.
npm install trading-signals
CommonJS:
const {SMA} = require('trading-signals');
ESM:
import {SMA} from 'trading-signals';
Example:
import {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 replace a previous value (useful for live charting):
sma.replace(40);
// 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()); // 50.0003
// You can also get the result without optional chaining:
console.log(sma.getResultOrThrow()); // 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"
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.
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.add(10);
sma.add(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.add(70);
// Now, we will receive a proper result
console.log(sma.getResultOrThrow()); // 40
Most of the time, the minimum amount of data depends on the interval / time period used. If you’re not sure, take a look at the test files for the indicator to see examples of correct usage.
- Exhaustion indicators: Identify trend exhaustion and potential reversal.
- Momentum indicators: Measure the speed and strength of price movements in a particular direction
- Trend indicators: Measure the direction of a trend (uptrend, downtrend or sideways trend)
- Volatility indicators: Measure the degree of variation in prices over time, regardless of direction
- Volume indicators: Measure the strength of a trend based on volume
- Acceleration Bands (ABANDS)
- Accelerator Oscillator (AC)
- Average Directional Index (ADX)
- Average True Range (ATR)
- Awesome Oscillator (AO)
- Bollinger Bands (BBANDS)
- Bollinger Bands Width (BBW)
- Center of Gravity (CG)
- Commodity Channel Index (CCI)
- Directional Movement Index (DMI / DX)
- Double Exponential Moving Average (DEMA)
- Dual Moving Average (DMA)
- Exponential Moving Average (EMA)
- Interquartile Range (IQR)
- Linear Regression (LINREG)
- Mean Absolute Deviation (MAD)
- Momentum (MOM / MTM)
- Moving Average Convergence Divergence (MACD)
- On-Balance Volume (OBV)
- Parabolic SAR (PSAR)
- Range Expansion Index (REI)
- Rate-of-Change (ROC)
- Relative Moving Average (RMA)
- Relative Strength Index (RSI)
- Simple Moving Average (SMA)
- Spencer's 15-Point Moving Average (SMA15)
- Stochastic Oscillator (STOCH)
- Stochastic RSI (STOCHRSI)
- Tom Demark's Sequential Indicator (TDS)
- True Range (TR)
- Volume-Weighted Average Price (VWAP)
- Weighted Moving Average (WMA)
- Wilder's Smoothed Moving Average (WSMA / WWS / SMMA / MEMA)
- Zig Zag Indicator (ZigZag)
Utility Methods:
- Average / Mean
- Maximum
- Median
- Minimum
- Quartile
- Standard Deviation
- Streaks
JavaScript uses double-precision floating-point arithmetic. For example, 0.1 + 0.2
yields 0.30000000000000004
due to binary floating-point representation.
While this isn’t perfectly accurate, it usually doesn’t matter in practice since indicators often work with averages, which already smooth out precision. In test cases, you can control precision by using Vitest’s toBeCloseTo assertion.
Earlier versions of this library (up to version 6) used big.js for arbitrary-precision arithmetic, but that made calculations about 100x slower on average. For this reason, support for big.js was removed starting with version 7.
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.
- Cloud9Trader Indicators (JavaScript)
- Crypto Trading Hub Indicators (TypeScript)
- Highcharts Indicators
- Indicator TS (TypeScript)
- Jesse Trading Bot Indicators (Python)
- LEAN Indicators (C#)
- libindicators (C#)
- Pandas TA (Python)
- Stock Indicators for .NET (C#)
- StockSharp (C#)
- ta-math
- ta4j (Java)
- Technical Analysis for Rust (Rust)
- Technical Analysis Library using Pandas and Numpy (Python)
- Tulip Indicators (ANSI C)
This package was built by Benny Code. Checkout my TypeScript course to become a coding rockstar!
This project is MIT licensed.