SMA, EMA, RSI, MACD, Bollinger Bands, ATR
Các chỉ báo kỹ thuật là pure functions, nhận QuoteHistory[] và trả kết quả. Từ v1.4 có thêm MACD, Bollinger Bands, ATR.
import { sma, ema, rsi, macd, bollinger, atr } from 'vnstock-js';
const history = await vnstock.stock.quote.history({
symbols: ['FPT'], start: '2024-01-01', timeFrame: '1D'
});
const sma20 = sma(history, { period: 20 });
// → [{ date: "2024-01-15", sma: null }, ..., { date: "2024-02-15", sma: 25.6 }, ...]
// SMA theo volume
const volumeSma = sma(history, { period: 10, field: 'volume' });
const ema12 = ema(history, { period: 12 });
// → [{ date: "...", ema: null }, ..., { date: "...", ema: 25.8 }, ...]
const rsi14 = rsi(history); // period mặc định = 14
// → [{ date: "...", rsi: null }, ..., { date: "...", rsi: 65.3 }, ...]
const rsi7 = rsi(history, { period: 7 });
const macdData = macd(history); // default fast=12, slow=26, signal=9
// → [{ date, macd, signal, histogram }]
const fast = macd(history, { fast: 5, slow: 13, signal: 5 });
histogram > 0 đang bullish, histogram < 0 đang bearish. Crossover khi histogram đổi dấu.
const bb = bollinger(history); // default period=20, stddev=2
// → [{ date, upper, middle, lower, percentB }]
const bb10 = bollinger(history, { period: 10, stddev: 1.5 });
percentB = vị trí giá trong band (0..1+). 1.0 = chạm upper, 0.0 = chạm lower.middle = SMA(period). upper/lower = middle ± stddev × σ.const atr14 = atr(history, 14); // Wilder smoothing
// → [{ date, atr }]
Đo biên độ dao động trung bình. Dùng cho stop-loss dynamic, position sizing theo volatility.
Note:
Giá trị null ở đầu mảng là do chưa đủ dữ liệu để tính. Ví dụ SMA(20) cần tối thiểu 20 điểm dữ liệu, MACD cần ≥26 phiên.