Skip to content

技术指标概览

Stock SDK 提供两种方式使用技术指标:

  1. 一站式 APIgetKlineWithIndicators 获取 K 线时自动计算指标
  2. 独立计算函数calcMAcalcMACD 等,灵活控制

getKlineWithIndicators

获取带技术指标的 K 线数据。支持 A 股、港股、美股,自动识别市场。

签名

typescript
getKlineWithIndicators(
  symbol: string,
  options?: {
    market?: 'A' | 'HK' | 'US';
    period?: 'daily' | 'weekly' | 'monthly';
    adjust?: '' | 'qfq' | 'hfq';
    startDate?: string;
    endDate?: string;
    indicators?: IndicatorOptions;
  }
): Promise<KlineWithIndicators[]>

计算说明

getKlineWithIndicators 会自动向前补拉足够的历史数据用于指标计算,并在返回时裁剪为你指定的日期范围。

参数

参数类型默认值说明
symbolstring-股票代码
marketstring自动识别市场类型:'A' / 'HK' / 'US'
periodstring'daily'K 线周期
adjuststring'hfq'复权类型
startDatestring-开始日期
endDatestring-结束日期
indicatorsobject-指标配置

indicators 配置

typescript
interface IndicatorOptions {
  ma?: MAOptions | boolean;      // 均线
  macd?: MACDOptions | boolean;  // MACD
  boll?: BOLLOptions | boolean;  // 布林带
  kdj?: KDJOptions | boolean;    // KDJ
  rsi?: RSIOptions | boolean;    // RSI
  wr?: WROptions | boolean;      // WR 威廉指标
  bias?: BIASOptions | boolean;  // 乖离率
  cci?: CCIOptions | boolean;    // CCI
  atr?: ATROptions | boolean;    // ATR
}

示例

typescript
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] },
    wr: true,
    bias: { periods: [6, 12, 24] },
    cci: { period: 14 },
    atr: { period: 14 },
  }
});

data.forEach(k => {
  console.log(`${k.date}: ${k.close}`);
  console.log(`  MA5=${k.ma?.ma5}, MA10=${k.ma?.ma10}`);
  console.log(`  MACD: DIF=${k.macd?.dif}, DEA=${k.macd?.dea}`);
  console.log(`  BOLL: 上=${k.boll?.upper}, 中=${k.boll?.mid}, 下=${k.boll?.lower}`);
  console.log(`  KDJ: K=${k.kdj?.k}, D=${k.kdj?.d}, J=${k.kdj?.j}`);
  console.log(`  RSI6=${k.rsi?.rsi6}, WR6=${k.wr?.wr6}`);
  console.log(`  BIAS6=${k.bias?.bias6}, CCI=${k.cci?.cci}, ATR=${k.atr?.atr}`);
});

市场自动识别

SDK 会根据代码格式自动识别市场:

typescript
// A 股(自动识别)
const aData = await sdk.getKlineWithIndicators('sz000001', {
  indicators: { ma: true, macd: true }
});

// 港股(5 位代码,自动识别)
const hkData = await sdk.getKlineWithIndicators('00700', {
  indicators: { ma: true, macd: true }
});

// 美股({market}.{ticker} 格式,自动识别)
const usData = await sdk.getKlineWithIndicators('105.MSFT', {
  indicators: { boll: true, rsi: true }
});

识别规则

格式市场示例
6 位数字或 sh/sz/bj 前缀A 股000001, sz000001
5 位数字港股00700, 09988
{市场}.{代码}美股105.MSFT, 106.BABA

支持的指标

指标方法说明
MAcalcMA均线(SMA/EMA/WMA)
MACDcalcMACD指数平滑异同移动平均线
BOLLcalcBOLL布林带
KDJcalcKDJ随机指标
RSIcalcRSI相对强弱指标
WRcalcWR威廉指标
BIAScalcBIAS乖离率
CCIcalcCCI商品通道指数
ATRcalcATR平均真实波幅

独立计算函数

所有指标计算函数都可以独立导入使用:

typescript
import {
  calcMA,
  calcSMA,
  calcEMA,
  calcWMA,
  calcMACD,
  calcBOLL,
  calcKDJ,
  calcRSI,
  calcWR,
  calcBIAS,
  calcCCI,
  calcATR,
  addIndicators,
} from 'stock-sdk';

addIndicators 可以把多个指标一次性挂到 K 线数组上,适合图表渲染场景。

详细用法请参考各指标的专属文档。

Released under the ISC License.