文档注解
Pine Script 支持特殊注释标签来为声明附加文档。这些注解被 LSP(用于悬停提示和补全)和文档生成器使用。
支持 //@tag 和 // @tag(// 后有空格)两种格式。
可用标签
| 标签 | 位置 | 描述 |
|---|---|---|
@function | 函数声明上方 | 为函数添加自定义描述 |
@param | 函数声明上方 | 为参数添加描述。指定参数名称,然后是描述 |
@returns | 函数声明上方 | 为返回值添加描述 |
@type | 类型或 newtype 声明上方 | 为类型添加自定义描述 |
@enum | 枚举声明上方 | 为枚举添加自定义描述 |
@field | 类型或枚举声明上方 | 为字段或变体添加描述。指定字段名称,然后是描述 |
@variable | 变量声明上方 | 为变量添加自定义描述 |
所有标签支持多行续写:紧跟在标签行之后以 // 开头(不带 @ 标签)的行会被追加到该标签的文本中。
文档化函数
使用 @function、@param 和 @returns 来文档化函数声明:
pine
// @function Outputs a label with `labelText` on the bar's high.
// @param labelText (series string) The text to display on the label.
// @returns The drawn label ID.
export drawLabel(string labelText) =>
label.new(bar_index, high, text = labelText)@param 和 @returns 可以出现在 @function 标签之前或之后:
pine
//@param series Series of values to process.
//@param length Number of bars (length).
//@function Arnaud Legoux Moving Average.
//
// It uses Gaussian distribution as weights for moving average.
export series float alma(series float series, simple int length) =>
// ...多行描述
续写行(以 // 开头但不带 @)会扩展前一个标签的描述:
pine
//@function Calculates the percentage difference
// from the base price to the target price.
//@param basePrice The start price
// used as the reference point.
//@param price The end price.
//@returns The signed deviation percentage.
export calcDeviation(float basePrice, float price) =>
100 * (price - basePrice) / basePrice文档化类型
使用 @type 和 @field 来文档化类型声明:
pine
//@type A point on a chart.
//@field index The index of the bar where the point is located, i.e., its `x` coordinate.
//@field price The price where the point is located, i.e., its `y` coordinate.
type Point
int index
float price@type 标签也适用于 newtype 声明:
pine
//@type Handle for a label drawing object.
export type label => int文档化枚举
使用 @enum 和 @field 来文档化枚举声明及其变体:
pine
//@enum Represents a trading direction.
//@field long A long (buy) direction.
//@field short A short (sell) direction.
//@field both Both directions.
enum Direction
long
short
both = "Both Directions"文档化变量
使用 @variable 来文档化变量声明:
pine
//@variable The highest price over the last 20 bars.
float highest20 = ta.highest(high, 20)
//@variable Tracks cumulative volume since the session started.
var float cumVol = 0.0编辑器支持
当存在文档注解时,OpenPine LSP 使用它们提供:
- 悬停信息 — 悬停在符号上时显示描述、参数、返回值和字段文档
- 补全详情 — 在自动补全建议中显示摘要
- 文档符号 — 在大纲视图中包含摘要
库文档
对于导出的库,文档注解尤其重要。文档生成器(openpine-docgen)提取这些注解来生成 API 参考页面:
pine
//@version=6
library("MyLib")
//@type Configuration for a custom indicator.
//@field length The lookback period.
//@field multiplier The standard deviation multiplier.
export type Config
int length = 14
float multiplier = 2.0
//@function Calculates a custom band around a moving average.
//@param src The source series.
//@param config The indicator configuration.
//@returns The upper band value.
export upperBand(series float src, Config config) =>
basis = ta.sma(src, config.length)
basis + config.multiplier * ta.stdev(src, config.length)