指标

共 95 个内置指标。通过 id 添加指标;未传入的参数使用默认值, 无效的参数也会回退到默认值。

添加指标

const ema = chart.addIndicator('ema', { period: 50 })          // on the price pane
const rsi = chart.addIndicator('rsi', { period: 14 }, 'bottom') // in a pane of its own
chart.updateIndicator(rsi, { period: 21 })
chart.removeIndicator(rsi)

addIndicator 返回一个实例 id:同一个指标可以添加多次, 每个实例都有各自的参数、颜色和水平线。

数据源与指标叠加指标

下表中标记为数据源的指标,除收盘价外还可以基于其他价格计算 (open、high、low、hl2、hlc3、 ohlc4、hlcc4),也可以基于另一个指标的线计算。 例如 RSI 的移动平均会画在 RSI 所在的窗格中、使用 RSI 的坐标,并在 RSI 被删除时一同删除。

import { indicatorSource } from '@tradecanvas/chart'

chart.updateIndicator(ema, { source: 'hlc3' })
const smoothed = chart.addIndicator('sma', { period: 9, source: indicatorSource(rsi, 'value') })

副图指标也可以共用同一个窗格: chart.addIndicator('stochastic', {}, 'bottom', { pane: rsi })。

水平线、颜色与数值

chart.setIndicatorLevels(rsi, [20, 50, 80])   // null restores 30 / 70
chart.updateIndicatorStyle(rsi, { colors: ['#f2a93b'], lineWidths: [2] })
chart.setIndicatorVisible(rsi, false)

const series = chart.getIndicatorOutput(rsi)?.series ?? []
const latest = series[series.length - 1]?.value   // keyed by the line keys below

每条线的最新值会以该线的颜色标注在坐标轴上;可通过 features.indicatorValueLabels: false 或 setIndicatorValueLabelsVisible(false) 关闭这些标签。 同一窗格中的线、水平线、坐标轴和十字光标共用一套坐标。

主图指标(35)

id名称默认参数线
smaSimple Moving Average· 数据源period: 20, source: "close"SMA
emaExponential Moving Average· 数据源period: 20, source: "close"EMA
bbBollinger Bands· 数据源period: 20, stdDev: 2, source: "close"Upper, Basis, Lower
vwapVolume Weighted Average Price—VWAP
ichimokuIchimoku Cloudtenkan: 9, kijun: 26, senkou: 52, displacement: 26Conversion, Base, Lead A, Lead B
psarParabolic SARstep: 0.02, max: 0.2SAR
supertrendSupertrendperiod: 10, multiplier: 3Supertrend
keltnerKeltner ChannelemaPeriod: 20, atrPeriod: 10, multiplier: 1.5Upper, Basis, Lower
donchianDonchian Channelperiod: 20Upper, Basis, Lower
pivotsPivot Points (Classic)lookback: 24R3, R2, R1, P, S1, S2, S3
avwapAnchored VWAPanchorTime: 0AVWAP
zigzagZigZagdeviation: 5Pivot
lrcLinear Regression Channel· 数据源period: 100, stdDev: 2, source: "close"Upper, Basis, Lower
hmaHull Moving Average· 数据源period: 21, source: "close"HMA
mtfmaMTF Moving Average· 数据源period: 50, timeframe: "1d", source: "close"MA
svwapSession VWAPbands: 1VWAP, +1σ, −1σ, +2σ, −2σ, +3σ, −3σ
chandelierChandelier Exitperiod: 22, multiplier: 3Long, Short
alligatorAlligatorjaw: 13, teeth: 8, lips: 5, jawShift: 8, teethShift: 5, lipsShift: 3Jaw, Teeth, Lips
vwmaVolume Weighted Moving Average· 数据源period: 20, source: "close"VWMA
wmaWeighted Moving Average· 数据源period: 20, source: "close"WMA
envelopeMoving Average Envelope· 数据源period: 20, percent: 2.5, source: "close"Upper, Basis, Lower
temaTriple EMA· 数据源period: 20, source: "close"TEMA
volumeProfileVolume Profilerows: 24—
demaDouble Exponential Moving Average· 数据源period: 20, source: "close"DEMA
smmaSmoothed Moving Average· 数据源period: 14, source: "close"SMMA
almaArnaud Legoux Moving Average· 数据源period: 9, offset: 0.85, sigma: 6, source: "close"ALMA
kamaKaufman Adaptive Moving Average· 数据源period: 10, fast: 2, slow: 30, source: "close"KAMA
lsmaLeast Squares Moving Average· 数据源period: 25, offset: 0, source: "close"LSMA
mcginleyMcGinley Dynamic· 数据源period: 14, source: "close"McGinley
macrossMA Cross· 数据源fast: 9, slow: 21, type: "sma", source: "close"Fast, Slow
fractalsWilliams Fractalsperiod: 2Up, Down
cksChande Kroll Stopp: 10, x: 1, q: 9Stop Long, Stop Short
sebStandard Error Bandsperiod: 21, mult: 2, smooth: 3Upper, Middle, Lower
gmmaGuppy Multiple Moving Average—EMA 3, EMA 5, EMA 8, EMA 10, EMA 12, EMA 15, EMA 30, EMA 35, EMA 40, EMA 45, EMA 50, EMA 60
maribbonMoving Average Ribbonma1: 20, ma2: 50, ma3: 100, ma4: 200, exponential: 0MA 1, MA 2, MA 3, MA 4

mtfma 在当前图表上绘制更高周期的移动平均——例如在 1h K线上显示日线的 50 均线。 它只对更高周期已完成的收盘价求平均,因此会在每个周期边界处呈阶梯状变化,且永不重绘。

副图指标(60)

id名称默认参数线水平线
rsiRelative Strength Index· 数据源period: 14, source: "close"RSI30, 70
macdMACD· 数据源fast: 12, slow: 26, signal: 9, source: "close"Histogram, MACD, Signal—
stochasticStochastic OscillatorkPeriod: 14, dPeriod: 3, smooth: 3%K, %D20, 80
atrAverage True Rangeperiod: 14ATR—
adxAverage Directional Indexperiod: 14ADX, +DI, −DI—
obvOn Balance Volume—OBV—
williamsRWilliams %Rperiod: 14%R-80, -20
cciCommodity Channel Indexperiod: 20CCI-100, 0, 100
mfiMoney Flow Indexperiod: 14MFI20, 80
aroonAroonperiod: 25Up, Down—
rocRate of Change· 数据源period: 12, source: "close"ROC0
tsiTrue Strength Index· 数据源longPeriod: 25, shortPeriod: 13, signalPeriod: 7, source: "close"TSI, Signal0
cmfChaikin Money Flowperiod: 20CMF—
stddevStandard Deviation· 数据源period: 20, source: "close"StdDev—
adAccumulation/Distribution—A/D—
vrocVolume Rate of Changeperiod: 14VROC0
aoAwesome Oscillatorfast: 5, slow: 34AO—
chaikinOscChaikin Oscillatorfast: 3, slow: 10Chaikin—
voldeltaVolume Deltamode: 0Delta—
vortexVortex Indicatorperiod: 14VI+, VI−1
chopChoppiness Indexperiod: 14CHOP38.2, 61.8
uoUltimate Oscillatorfast: 7, mid: 14, slow: 28UO30, 70
fiForce Indexperiod: 13EFI0
crsiConnors RSI· 数据源rsiPeriod: 3, streakPeriod: 2, rankPeriod: 100, source: "close"CRSI10, 90
coppockCoppock Curve· 数据源longRoc: 14, shortRoc: 11, wma: 10, source: "close"Coppock0
kstKnow Sure Thing· 数据源roc1: 10, roc2: 15, roc3: 20, roc4: 30, sma1: 10, sma2: 10, sma3: 10, sma4: 15, signal: 9, source: "close"KST, Signal0
elderrayElder Rayperiod: 13Bull, Bear—
stcSchaff Trend Cycle· 数据源fast: 23, slow: 50, cycle: 10, source: "close"STC25, 75
kvoKlinger Oscillatorfast: 34, slow: 55, signal: 13KVO, Signal0
fisherFisher Transformperiod: 9Fisher, Trigger0
dpoDetrended Price Oscillator· 数据源period: 20, source: "close"DPO—
bopBalance of Powersmooth: 14BOP0
massindexMass Indexema: 9, sum: 25Mass26.5, 27
cmoChande Momentum· 数据源period: 9, source: "close"CMO-50, 0, 50
trixTRIX· 数据源period: 15, signal: 9, source: "close"TRIX, Signal0
emvEase of Movementperiod: 14EOM0
pvtPrice Volume Trend—PVT—
wadWilliams A/D—WAD—
chaikinvolChaikin Volatilityema: 10, roc: 10CV0
rviRelative Vigor Indexperiod: 10RVGI, Signal0
stochrsiStochastic RSI· 数据源rsiPeriod: 14, stochPeriod: 14, k: 3, d: 3, source: "close"%K, %D20, 80
ppoPercentage Price Oscillator· 数据源fast: 12, slow: 26, signal: 9, source: "close"Histogram, PPO, Signal0
acAccelerator Oscillator—AC—
rmiRelative Momentum Index· 数据源period: 20, momentum: 5, source: "close"RMI30, 70
disparityDisparity Index· 数据源period: 14, source: "close"DI0
qstickQstickperiod: 8Qstick0
pgoPretty Good Oscillatorperiod: 14PGO-3, 0, 3
bbpbBollinger Bands %B· 数据源period: 20, stdDev: 2, source: "close"%B0, 0.5, 1
bbwBollinger BandWidth· 数据源period: 20, stdDev: 2, source: "close"BBW—
momMomentum· 数据源period: 10, source: "close"MOM0
hvHistorical Volatility· 数据源period: 10, annual: 365, source: "close"HV—
voVolume Oscillatorshort: 5, long: 10Vol Osc0
ulcerUlcer Index· 数据源period: 14, source: "close"Ulcer—
smiStochastic Momentum Indexperiod: 10, smooth: 3, signal: 3SMI, Signal40, -40
rvixRelative Volatility Indexperiod: 10, smooth: 14RVI80, 50, 20
trendstrengthTrend Strength Indexperiod: 14Trend0
lrslopeLinear Regression Slopeperiod: 14Slope0
stderrorStandard Errorperiod: 14StdErr—
adrAverage Day Rangeperiod: 14ADR—
netvolumeNet Volume—Net0

voldelta(Volume Delta)根据 OHLCV 估算买卖压力——收涨的K线计入正成交量, 收跌的K线计入负成交量。mode: 0 为逐根K线的柱状图, mode: 1 为累计 delta。(真正的逐笔 delta 需要每笔成交的买价 / 卖价数据, 而 OHLCV 序列并不包含这些信息。)

自定义指标

继承 IndicatorBase,声明它要绘制的内容(plots)、窗格坐标、 水平线和参数;图表会负责绘制、缩放窗格、标注数值,并把它列入组件的图例和设置中, 你无需编写任何渲染代码。参见 自定义指标示例。

窗格:调整大小、收起、最大化、重新排序

拖动窗格上方的分隔线即可调整其大小。在 ChartWidget 中,每个窗格右上角都有按钮: 上移或下移、收起到只剩标题栏、最大化(其他窗格随之收起,主图保留一条窄条)。 保存的布局会记住每个窗格的大小、顺序、收起与最大化状态。用代码也可以做到:

chart.setPanelSize(rsi, 180)        // px, clamped to a minimum
chart.setPaneCollapsed(macd, true)  // fold to its header
chart.setMaximizedPane(rsi)         // null puts the panes back
chart.movePane(rsi, -1)             // one place up; 1 = down
chart.on('paneChange', (e) => e.payload.change)  // 'collapsed' | 'maximized' | 'order' | 'scale'
chart.setPaneScale(atr, { log: true, invert: false })  // log while its values are above 0
chart.setPaneScale(atr, { percent: true })             // labels in percent of its first value on screen

在 ChartWidget 中右键点击窗格,即可为它单独设置对数坐标、反转坐标或百分比坐标。

把指标移到另一个窗格

指标可以加入另一个指标的窗格(随后与该窗格共用坐标),也可以独占一个窗格,或者回到主图。 当它离开原本归它所有的窗格时,窗格中的其他指标会留下(由下一个副图指标接管该窗格), 而基于它的线计算的指标会随它一起移动。在 ChartWidget 中,图例行上的 ⋯ 按钮 提供上方窗格、下方窗格、新窗格和主图几个去处。

chart.moveIndicatorToPane(cci, rsi)      // into RSI's pane
chart.moveIndicatorToPane(cci, 'new')    // a pane of its own
chart.moveIndicatorToPane(ema, 'price')  // an overlay back to the price pane
chart.canMoveIndicatorToPane(cci, rsi)   // whether it would move

撤销与模板

添加、删除、编辑和移动指标都是撤销步骤,与画线共用同一份历史记录 (Ctrl/⌘ Z、Ctrl/⌘ Shift Z);通过撤销恢复的指标会沿用原来的 id, 因此其线上的提醒仍然对应得上。对同一个指标的一连串编辑(拖动颜色、输入周期)只算一步。 加载布局后,历史记录会重新开始。

所有指标可以作为一个整体取出再放回,ChartWidget 的指标模板正是这样做的: “指标”菜单中的 将指标保存为模板… 会把它们(参数、样式、水平线、窗格) 以一个名称保存下来;选择模板后,它们会替换图表当前的指标,这也只算一次撤销步骤。

const setup = chart.getIndicatorSetup()   // what a layout keeps of them
chart.applyIndicatorSetup(setup)          // in place of the chart's indicators

new ChartWidget(host, { indicatorTemplates: true })  // the default

在图表之外计算

IndicatorWorkerHost 根据K线计算指标,使用的消息与 Web Worker 相同。 Worker 脚本目前尚未包含在发布的包中,因此请传入 null 并注册插件, 以便在当前线程中计算(SSR、测试、脚本):

import { IndicatorWorkerHost, RSIIndicator } from '@tradecanvas/core'

const host = new IndicatorWorkerHost(null)
host.registerFallbackPlugin(new RSIIndicator())
const output = await host.calculate('rsi', { id: 'rsi', instanceId: 'rsi-1', params: { period: 14 } }, bars)