mytt-ts
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

MyTT-ts

npm npm bundle size npm downloads NPM License

MyTT 的 TypeScript 实现版本,将通达信、同花顺、文华麦语言等指标公式移植到 TypeScript 中。这是一个轻量级的技术分析指标库,提供了常用的股票、期货技术分析指标。

本项目是 MyTT 的 TypeScript 实现版本。

特点

  • 🎯 完全使用 TypeScript 编写,提供完整的类型定义
  • 📦 支持 ESM 和 CommonJS 两种模块规范
  • 🚀 基于数组运算,性能优异
  • 💡 API 设计与通达信、同花顺等软件保持一致
  • 🔧 不依赖其他第三方库,体积小巧
  • ✨ 支持所有主流技术指标:MACD、KDJ、RSI、BOLL等

安装

# npm
npm install mytt-ts

# yarn
yarn add mytt-ts

# pnpm
pnpm add mytt-ts

类型定义

该库提供完整的 TypeScript 类型定义:

import { Series, MACD, KDJ } from 'mytt-ts';

// 基础类型
type Series = number[];           // 数值序列
type BoolSeries = boolean[];      // 布尔序列

// 指标函数类型示例
function MACD(
  close: Series,
  short?: number,   // 默认值 12
  long?: number,    // 默认值 26
  m?: number        // 默认值 9
): [Series, Series, Series];  // 返回 [DIF, DEA, MACD]

function KDJ(
  close: Series,
  high: Series,
  low: Series,
  n?: number,       // 默认值 9
  m1?: number,      // 默认值 3
  m2?: number       // 默认值 3
): [Series, Series, Series];  // 返回 [K, D, J]

使用示例

import { MA, MACD, KDJ, Series } from 'mytt-ts';

// 定义数据
const closePrice: Series = [10, 11, 12, 11, 10];
const highPrice: Series = [12, 13, 14, 12, 11];
const lowPrice: Series = [9, 10, 11, 10, 9];

// 计算指标
const ma5 = MA(closePrice, 5);
const [dif, dea, macd] = MACD(closePrice);
const [k, d, j] = KDJ(closePrice, highPrice, lowPrice);

// 类型安全
const wrongInput: Series = ['10', 11, 12]; // TS 会报错
MA(wrongInput, 5); // TS 会报错

错误处理

所有指标函数都会对输入进行基本验证:

  • 输入序列必须是数字数组
  • 参数必须是有效的数字
  • 序列长度必须足够计算指标
import { MA, Series } from 'mytt-ts';

// 序列太短
const shortSeries: Series = [1, 2];
const ma5 = MA(shortSeries, 5); // 前面的值会是 NaN

// 处理 NaN
const result = ma5.map(v => isNaN(v) ? null : v);

Package Sidebar

Install

npm i mytt-ts

Weekly Downloads

4

Version

1.0.1

License

MIT

Unpacked Size

77.2 kB

Total Files

8

Last publish

Collaborators

  • jackiotyu