文件註解
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)