# @backtest-kit/pinets 中文文档

规范地址：https://npmcn.com/package/%40backtest-kit/pinets/

版本：23\.7\.0
npm 元数据更新时间：2026-10-29T15:13:21.541Z
中文译文更新时间：2026-11-07T20:05:34.843Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40backtest-kit/pinets
原文对照：https://npmcn.com/package/%40backtest-kit/pinets/?lang=original
项目主页：https://backtest-kit.github.io/documents/article_07_ai_news_trading_signals.html
源码仓库：https://github.com/tripolskypetr/backtest-kit

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：93.7 kB，5 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- 运行依赖：4 个
- peer 依赖：pinets ^0\.9\.34，typescript ^5\.0\.0，backtest-kit ^23\.7\.0
- 首次发布：2026-01-23T20:08:05.375Z
- 最新版本发布：2026-10-29T15:13:21.111Z
- 近 30 天下载量：2750（2026-10-08 至 2026-11-06，npm 官方下载统计）
- 已发布版本数：179（近 12 个月 179 个）

## 中文简介

在Node.js自我托管环境中运行TradingView Pine脚本策略. 通过 PineTS 运行时间执行已存在的Pine Script 指标并生成1:1语法相容的交易信号.

## 安装

```sh
npm install @backtest-kit/pinets
```

也可以使用：`pnpm add @backtest-kit/pinets`、`yarn add @backtest-kit/pinets`、`bun add @backtest-kit/pinets`

## README

![](https://github.com/tripolskypetr/backtest-kit/raw/refs/heads/master/assets/heraldry.svg)

# @ backtest-kit/ pitchts (英语)

> 在自办的节点.js环境中运行 TradingView Pine 脚本 v5/v6 [反测试- kit](https://www.npmjs.com/package/backtest-kit)。执行您现有的 `.pine` 具有 1:1 语法兼容性并提取结构化交易信号的指标——没有 TradingView 账户,没有重写.

![screenshot](https://raw.githubusercontent.com/tripolskypetr/backtest-kit/HEAD/assets/screenshots/screenshot16.png)

[![Ask DeepWiki](https://deepwiki.com/badge.svg)](https://deepwiki.com/tripolskypetr/backtest-kit) [![npm](https://img.shields.io/npm/v/@backtest-kit/pinets.svg?style=flat-square)](https://npmjs.org/package/@backtest-kit/pinets) ![TypeScript](https://img.shields.io/badge/TypeScript-5.0+-blue)

电源 [松树](https://github.com/QuantForgeOrg/PineTS) ——开源的"松花文稿"转录和运行时间.

📚 **[文档](https://backtest-kit.github.io/documents/article_07_ai_news_trading_signals.html)** • 妇女 **[参考执行情况](https://github.com/tripolskypetr/backtest-kit/tree/master/example)** • 妇女 **[松树病历](https://quantforgeorg.github.io/PineTS/)** • 妇女 **[GitHub 图像](https://github.com/tripolskypetr/backtest-kit)**

```bash
npm install @backtest-kit/pinets pinets backtest-kit
```

* * *

## 为什么

您的边缘已经作为 TradingView Pine 脚本存在—— 在 JavaScript 中重写它是一种容易出错的繁忙工作, 从原作中漂移出. 此软件包运行 `.pine` **作为** 在 backest- kit 的执行上下文中 : `getCandles` 输入其外观安全数据,60+指标被构建在(没有手动TA数学)中,同样的脚本具有回溯测试和活性的力量. 你画它 `plot()` 输出到一个结构化的信号,你完成.

-   📜 **松脚本 v5/v6** ——本地贸易维尤语法, 1:1相容.
-   🎯 **60+指标** ——SMA,EMA,RSI,MACD,博林格乐队,ATR,Stochastic,ADX,
-   🔌 **发动机集成** ——运行于后端测试-kit的时间上下文(无外观-ahead).
-   📁 **文件或内入** 装入 a `.pine` 文件或通过代码字符串.
-   🗺️ **灵活提取** - 任何地图 `plot()` 键入数据,并进行回放和变换.
-   ⚡ **缓存执行** ——回忆文件为重复运行读取.
-   🛡️ **类型安全** ——提取出的数据上完全通用.

* * *

## 快开始

松树脚本只需要揭露几块命名地块; `getSignal` 将其映射到一个 `ISignalDto`。 。 。 .

战略. pine + 得到 Signal

```pine
//@version=5
indicator("EMA cross — 1H, 100 candles")

rsi = ta.rsi(close, 10)
atr = ta.atr(10)
ema_fast = ta.ema(close, 7)
ema_slow = ta.ema(close, 16)

long_cond  = ta.crossover(ema_fast, ema_slow)  and rsi < 65
short_cond = ta.crossunder(ema_fast, ema_slow) and rsi > 35

plot(close, "Close")
plot(long_cond ? 1 : short_cond ? -1 : 0, "Signal")
plot(long_cond ? close - atr*1.5 : close + atr*1.5, "StopLoss")
plot(long_cond ? close + atr*3   : close - atr*3,   "TakeProfit")
plot(60, "EstimatedTime")  // minutes
```

```typescript
import { File, getSignal } from '@backtest-kit/pinets';
import { addStrategy } from 'backtest-kit';

addStrategy({
  strategyName: 'pine-ema-cross', interval: '5m', riskName: 'demo',
  getSignal: async (symbol) =>
    getSignal(File.fromPath('strategy.pine'), { symbol, timeframe: '1h', limit: 100 }),
});
```

内置代码不需要文件 :

```typescript
import { Code, getSignal } from '@backtest-kit/pinets';
const signal = await getSignal(
  Code.fromString(`//@version=5\nindicator("RSI")\nrsi=ta.rsi(close,14)\natr=ta.atr(14)\nplot(close,"Close")\nplot(rsi<30?1:rsi>70?-1:0,"Signal")\nplot(close-atr*2,"StopLoss")\nplot(close+atr*3,"TakeProfit")`),
  { symbol: 'BTCUSDT', timeframe: '15m', limit: 100 });
```

### 所需地块 `getSignal()`

| 绘图名称 | 数值 | 含义 |
| --- | --- | --- |
| `"Signal"` | `1` 编号 : `-1` 编号 : `0` | 长 / 短 / 无信号 |
| `"Close"` | `close` | 入场价格 |
| `"StopLoss"` | 价格 | 停止损失水平 |
| `"TakeProfit"` | 价格 | 盈利水平 |
| `"EstimatedTime"` | 分钟 | 持有时间(可选,默认240) |

自定义地块也很好——使用 `run` + 键 `extract` 以图示他们.

* * *

## 自定义提取

`run()` 返回原始绘图数据; `extract()` 编号 : `extractRows()` 将其拉入有可选回放和变换的打字形状.

已提取( ) - 最新条值

```typescript
import { File, run, extract } from '@backtest-kit/pinets';

const plots = await run(File.fromPath('indicators.pine'), { symbol: 'ETHUSDT', timeframe: '1h', limit: 200 });
const data = await extract(plots, {
  rsi: 'RSI', macd: 'MACD',                                  // plot name → number
  prevRsi: { plot: 'RSI', barsBack: 1 },                     // previous bar
  trendStrength: { plot: 'ADX', transform: (v) => v > 25 ? 'strong' : 'weak' },
});
// { rsi: 55.2, macd: 12.5, prevRsi: 52.1, trendStrength: 'strong' }
```
取出 Rows( ) —— 每条栏, 时间标记

```typescript
import { File, run, extractRows } from '@backtest-kit/pinets';

const plots = await run(File.fromPath('indicators.pine'), { symbol: 'ETHUSDT', timeframe: '1h', limit: 200 });
const rows = await extractRows(plots, {
  rsi: 'RSI', macd: 'MACD',
  prevRsi: { plot: 'RSI', barsBack: 1 },
  trend: { plot: 'ADX', transform: (v) => v > 25 ? 'strong' : 'weak' },
});
// rows[1] = { timestamp: '2024-01-01T01:00:00.000Z', rsi: 52.1, macd: -1.5, prevRsi: 48.3, trend: 'weak' }
```

`extract()` 对 `extractRows()`: 所有栏目的单个最新对象对数组;缺少值 `0` 对 `null`; 无时间戳对 ISO `timestamp`· ; `barsBack` 从最后一个酒吧对 从每个酒吧自己的索引。 使用 `extract` 用于当前栏下的信号生成, `extractRows` 用于数据集导出/历史分析.

至 SignalDto () —— 将提取的值变成信号

帮手 `getSignal` 内部使用,用于自定义图(例如通过 `@backtest-kit/graph`) (中文(简体) ). 地图 `position` `1`编号 :`-1`编号 :`0` → `long`编号 :`short`编号 :`null`,带有TP/SL/估计时间,带有可选的明文 `priceOpen`数字 :

```typescript
import { run, extract, toSignalDto } from '@backtest-kit/pinets';
import { randomString } from 'functools-kit';

const plots = await run(File.fromPath('strategy.pine'), { symbol, timeframe: '15m', limit: 100 });
const data  = await extract(plots, { position: 'Signal', priceTakeProfit: 'TakeProfit', priceStopLoss: 'StopLoss', minuteEstimatedTime: 'EstimatedTime' });
const signal = toSignalDto(randomString(), data, null); // ISignalDto | null
```

* * *

## 调试定制( C)

turnPlotData/mark down——检查地块输出

```typescript
import { File, run, dumpPlotData, toMarkdown } from '@backtest-kit/pinets';
const plots = await run(File.fromPath('strategy.pine'), { symbol: 'BTCUSDT', timeframe: '1h', limit: 100 });
await dumpPlotData('signal-001', plots, 'ema-cross', './dump/ta');  // → markdown files
const md = await toMarkdown(plots);                                 // markdown table as a string
```
使用 Pine / 使用 Indicator / set Logger —— 互换内部

```typescript
import { usePine, useIndicator, setLogger } from '@backtest-kit/pinets';
import { Pine } from 'pinets';

usePine(Pine);                       // register a custom Pine constructor
useIndicator(MyIndicatorCtor);       // register a custom indicator constructor
setLogger({ log: (m, d) => console.log(`[${m}]`, d), info: () => {}, error: console.error });
```

* * *

## 为什么不在JS中重新写?

差别

```typescript
// ❌ Manual rewrite — re-derive every indicator, drift from the original
const candles = await getCandles('BTCUSDT', '5m', 100);
const closes = candles.map(c => c.close);
const rsi = RSI.calculate({ values: closes, period: 14 });
const emaFast = EMA.calculate({ values: closes, period: 9 });
// …port all the Pine logic by hand

// ✅ With pinets — copy the .pine straight from TradingView
const signal = await getSignal(File.fromPath('strategy.pine'), { symbol: 'BTCUSDT', timeframe: '5m', limit: 100 });
```

使用已有的脚本 ^ 60+ 指示器, 没有手动数学 ^ 相同的代码回回测试和活 ^ 完整的时间序列回望语义 ^ 类型安全提取 .

* * *

## API 参考

| 导出 | 说明 |
| --- | --- |
| `getSignal(source, opts)` | 运行 Pine 脚本 结构 `ISignalDto` (位置、TP/SL,估计时间) |
| `run(source, opts)` | 运行 Pine 脚本 → 原始地图数据 |
| `extract(plots, mapping)` | 带有自定义映射的最新列值( 丢失 ) `0`(中文(简体) ) |
| `extractRows(plots, mapping)` | 所有栏为加时标行(缺) `null`(中文(简体) ) |
| `toSignalDto(id, data, priceOpen?)` | 已提取地图 `{ position, … }` → `ISignalDto | null` |
| `dumpPlotData(id, plots, name, dir)` | 向下标记文件丢弃绘图数据 |
| `toMarkdown(plots)` • 妇女 `markdown(...)` | 将平地图作为平地表 |
| `File.fromPath(path)` | 从 a 装入松脚本 `.pine` 文件( 记忆) |
| `Code.fromString(code)` | 使用内线松脚本 |
| `usePine(ctor)` • 妇女 `useIndicator(ctor)` | 注册自定义 Pine / 指标构建器 |
| `setLogger(logger)` | 自定义日志 |
| `lib` | 高级使用的IoC内部容器 |
| `AXIS_SYMBOL` | 轴线提供者符号符号 |

**选项** (单位:千美元)`run`编号 :`getSignal`: `symbol`, (中文(简体) ), `timeframe` (松花烛间隔), `limit` (要取的手提箱——必须覆盖指标热量;加热前栏是: `N/A`) (中文(简体) ).

**类型 :** `PlotExtractConfig`, (中文(简体) ), `PlotMapping`, (中文(简体) ), `ExtractedData`, (中文(简体) ), `ExtractedDataRow`, (中文(简体) ), `CandleModel`, (中文(简体) ), `PlotModel`, (中文(简体) ), `PlotRecord`, (中文(简体) ), `SymbolInfoModel`, (中文(简体) ), `ILogger`, (中文(简体) ), `IPine`编号 :`TPineCtor`, (中文(简体) ), `IIndicator`编号 :`TIndicatorCtor`, (中文(简体) ), `IProvider`。 。 。 .

完整源图

`classes/{Code,File}.ts` • 妇女 `function/{pine,indicator,run,extract,setup,strategy,dump,markdown}.function.ts` • 妇女 `helpers/toSignalDto.ts` • 妇女 `model/{Candle,Plot,SymbolInfo}.model.ts` • 妇女 `interface/{Logger,Pine,Indicator,Provider}.interface.ts` • 妇女 `lib/` 木卫一(`core/{di,provide,types}`, (中文(简体) ), `services/{base/LoggerService, cache/PineCacheService, connection/{Pine,Indicator}ConnectionService, context/ExchangeContextService, data/PineDataService, job/PineJobService, markdown/PineMarkdownService, provider/{Axis,Candle}ProviderService}`) (中文(简体) ). 上方的地图导出到其中之一时 `src/` 没有证件.

## \* 贡献

叉/ PR 打开 [GitHub 图像](https://github.com/tripolskypetr/backtest-kit)。 。 。 .

## 许可证

麻省理工学院 [特立奥尔斯基平板电脑](https://github.com/tripolskypetr)

## 常见问题

### @backtest-kit/pinets 是什么？

在Node\.js自我托管环境中运行TradingView Pine脚本策略\. 通过 PineTS 运行时间执行已存在的Pine Script 指标并生成1:1语法相容的交易信号\.

### 如何安装 @backtest-kit/pinets？

在项目目录运行 npm install @backtest-kit/pinets，或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖：pinets（^0\.9\.34）、typescript（^5\.0\.0）、backtest-kit（^23\.7\.0）。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖，Yarn 需要手动安装。

### @backtest-kit/pinets 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @backtest-kit/pinets 支持 ESM 还是 CommonJS？

同时提供 ES Module 与 CommonJS 入口，import 和 require\(\) 都可以使用，打包工具会自动选择合适的版本。

### @backtest-kit/pinets 可以商用吗？

@backtest-kit/pinets 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @backtest-kit/pinets 的下载量有多少？

根据 npm 官方下载统计，@backtest-kit/pinets 在 2026/10/08 – 2026/11/06 这 30 天内被下载 2,750 次，日均约 92 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @backtest-kit/pinets 还在维护吗？

最新版本 23\.7\.0 发布于 2026/10/29（9 天前）；至今共发布 179 个版本，其中过去 12 个月发布了 179 个；首次发布于 2026/01/24。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

