Skip to content

Technical Indicators

Stock SDK supports two indicator workflows:

  • Use getKlineWithIndicators to fetch K-line and indicators in one call
  • Use standalone functions such as calcMA, calcMACD, and calcOBV

One-stop K-line with Indicators

ts
const data = await sdk.getKlineWithIndicators('sz000001', {
  startDate: '20240101',
  endDate: '20241231',
  indicators: {
    ma: { periods: [5, 10, 20, 60] },
    macd: true,
    boll: true,
    kdj: true,
    rsi: { periods: [6, 12, 24] },
    obv: { maPeriod: 20 },
    roc: { period: 12, signalPeriod: 6 },
    dmi: { period: 14, adxPeriod: 6 },
    sar: true,
    kc: { emaPeriod: 20, atrPeriod: 10, multiplier: 2 },
  },
});

console.log(data[30].ma?.ma5);
console.log(data[30].obv?.obvMa);
console.log(data[30].roc?.signal);
console.log(data[30].dmi?.adx);
console.log(data[30].sar?.sar);
console.log(data[30].kc?.upper);

Supported Indicators

  • ma
  • macd
  • boll
  • kdj
  • rsi
  • wr
  • bias
  • cci
  • atr
  • obv
  • roc
  • dmi
  • sar
  • kc

Standalone Calculation Functions

ts
import {
  calcMA,
  calcMACD,
  calcBOLL,
  calcKDJ,
  calcOBV,
  calcROC,
  calcDMI,
  calcSAR,
  calcKC,
} from 'stock-sdk';

const klines = await sdk.getHistoryKline('sz000001');
const closes = klines.map((item) => item.close);
const ohlc = klines.map((item) => ({
  open: item.open,
  high: item.high,
  low: item.low,
  close: item.close,
  volume: item.volume,
}));

const ma = calcMA(closes, { periods: [5, 10, 20], type: 'sma' });
const macd = calcMACD(closes);
const boll = calcBOLL(closes, { period: 20, stdDev: 2 });
const kdj = calcKDJ(ohlc, { period: 9, kPeriod: 3, dPeriod: 3 });
const obv = calcOBV(ohlc, { maPeriod: 20 });
const roc = calcROC(ohlc, { period: 12, signalPeriod: 6 });
const dmi = calcDMI(ohlc, { period: 14, adxPeriod: 6 });
const sar = calcSAR(ohlc, { afStart: 0.02, afIncrement: 0.02, afMax: 0.2 });
const kc = calcKC(ohlc, { emaPeriod: 20, atrPeriod: 10, multiplier: 2 });

console.log(ma[20].ma20);
console.log(macd[20].dif);
console.log(kdj[20].k);
console.log(obv[20].obvMa);
console.log(roc[20].signal);
console.log(dmi[20].adx);
console.log(sar[20].sar);
console.log(kc[20].upper);

Using addIndicators

ts
import { addIndicators } from 'stock-sdk';

const klines = await sdk.getHistoryKline('sz000001');

const withIndicators = addIndicators(klines, {
  ma: { periods: [5, 10] },
  macd: true,
  obv: { maPeriod: 20 },
  roc: true,
  dmi: true,
  sar: true,
  kc: true,
});

console.log(withIndicators[40].obv?.obv);
console.log(withIndicators[40].roc?.roc);
console.log(withIndicators[40].dmi?.pdi);

Notes

  • Most indicators return null until there is enough history
  • getKlineWithIndicators automatically fetches extra lookback data when needed
  • calcMA accepts a close-price array, while most trend indicators accept OHLCV[]
  • Use addIndicators when you want to attach multiple indicators to the same K-line array

Released under the ISC License.