v1.5.0

Trendyways

A JavaScript library for financial technical analysis — moving averages, oscillators, indicators, and more.

Moving Averages Bollinger Bands MACD RSI Stochastic ADX Support & Resistance Fibonacci Statistics Vector Ops

📈 Moving Averages

Simple (MA), Exponential (EMA), and Weighted (WMA) moving averages on a price series.

Price vs MA(4) vs EMA(4)

const serie = [{c:2},{c:6},{c:5},{c:7},{c:10},{c:9},{c:12},{c:5}];
const ma  = tw.ma(serie, 4);   // simple moving average, window=4
const ema = tw.ema(serie, 4);  // exponential moving average
const wma = tw.wma(serie, [0.6, 0.3, 0.1]); // weighted MA

🎯 Bollinger Bands

Upper and lower bands at k standard deviations around a moving average, highlighting volatility.

Price with Bollinger Bands (n=3, k=2)

const serie  = [{c:2.1},{c:4.3},{c:4.5},{c:4.8},{c:5.0},{c:5.8},{c:7.1},{c:9.1}];
const bands  = tw.bollinger(serie, 3, 2);  // window=3, k=2
// bands[i].ub  → upper band
// bands[i].ma  → middle band (moving average)
// bands[i].lb  → lower band

MACD

Moving Average Convergence Divergence — trend-following momentum indicator.

MACD Line & Signal (sample IBM data, first 50 values with signal)

const results = tw.macd(serie);
// results[i].macd.line   → MACD line (12-EMA minus 26-EMA)
// results[i].macd.signal → signal line (9-EMA of MACD)
// results[i].macd.hist   → histogram (line minus signal)

🔋 RSI — Relative Strength Index

Momentum oscillator measuring speed and change of price movements (0–100 scale).

RSI(14) with overbought / oversold bands

const values = [{c:44.34}, {c:44.09}, ... /* 33 candles */];
const result = tw.rsi(values, 14);
// result[i].rsi → RSI value at index i

📊 Stochastic Oscillator

Momentum oscillator measuring position within high-low range (%K and %D lines, 0–100 scale).

Stochastic %K & %D (14-period, 3-period smoothing)

const data = [{h:30.20, l:29.41, c:29.87}, ...];
const result = tw.stochastic(highs, lows, closes, 14, 3);
// result[i].k  → %K value (0–100)
// result[i].d  → %D value (3-EMA of %K)

🧭 ADX — Average Directional Index

Measures trend strength. DI+ and DI- show directional movement.

ADX, DI+, DI− (38 candles)

const data   = [{h:30.20, l:29.41, c:29.87}, ...];
const result = tw.adx(data);
// result[i].adx    → ADX value (starts at index 28)
// result[i].di14p  → DI+ (starts at index 14)
// result[i].di14n  → DI−

🏗️ Support & Resistance

Floor pivots, Camarilla, Woodie's, Tom DeMark's points, and Fibonacci retracements.

Floor Pivots with Support & Resistance

Candle 100%61.8%50%38.2%23.6%0%

Fibonacci UPTREND retracements

const pivot  = tw.floorPivots([{c:15, h:18, l:5}]);
const cam    = tw.camarillaPoints(points);
const wood   = tw.woodiesPoints(points);
const tom    = tw.tomDemarksPoints(points);
const fibs   = tw.fibonacciRetrs(points, 'UPTREND');

📊 Statistics

Basic descriptive statistics: min, max, mean, standard deviation.

const serie = [2,6,5,7,10,9,12,5];
tw.max(serie)   // → 12
tw.min(serie)   // → 2
tw.mean(serie)  // → 7
tw.sd(serie)    // → standard deviation

🔢 Vector Operations

Element-wise arithmetic operations on numeric arrays.

const a = [5, 3, 8], b = [2, 1, 6];
tw.diffVectors(a, b)          // → [3, 2, 2]
tw.divVector(a, b)            // → [2.5, 3, 1.33]
tw.powVector(a)               // → [25, 9, 64]  (squares)
tw.absVector([-1, -2, 3])     // → [1, 2, 3]
tw.sumVector(a)               // → 16
tw.avgVector(a)               // → 5.33
tw.combineVectors(a, b, (x,y) => x*y) // → [10, 3, 48]

📐 Error Metrics

Measure prediction accuracy: MSE, RMSE, and MAE between two series.

const actual    = [1.2, 3.4, -7.8, 2.3, 8.9, 5];
const predicted = [2.2, 8.4,  7.8, -2.3, -8.9, 5.1];
tw.mse(actual, predicted)   // → 101.23
tw.rmse(actual, predicted)  // → 10.06
tw.mae(actual, predicted)   // → 7.35