Skip to content

OpenPine 擴充

以下功能是 OpenPine 擴充,不存在於標準 TradingView Pine Script v6 中。它們使得 OpenPine 的標準函式庫和 prelude 可以完全用 Pine Script 原始碼檔案編寫。

getter — 零參數屬性函式

getter 關鍵字聲明一個不能有參數不使用括號呼叫的函式,使其看起來像變數或屬性存取。

pine
getter size() => 42

// 像變數一樣存取 — 不需要括號:
value = size           // 42

// 使用括號呼叫會報錯:
value = size()         // error: function `size` not defined

getter 不能有參數:

pine
getter value(int a) => a + 1   // error: getter functions cannot have parameters

存在原因: 在標準 TradingView Pine 中,像 closeopenmath.picolor.red 這樣的內建值是由執行階段作為變數暴露的。OpenPine 使用 getter 在純 Pine 原始碼檔案中實現這些相同的值。

標準函式庫範例

pine
// K 線資料 (prelude/candlestick.1.pine)
export getter series float open() => @native.candlestick(1)
export getter series float close() => @native.candlestick(4)
export getter series float volume() => @native.candlestick(5)
export getter series float hl2() => (high + low) / 2

// 常數 (stdlib/math.1.pine)
export getter const float pi() => 3.1415926535897932
export getter const float e() => 2.7182818284590452

// 顏色常數 (stdlib/color.1.pine)
export getter const color red() => #F23645
export getter const color blue() => #2962FF

staticmethod — 類型上的靜態方法

staticmethod 關鍵字定義與特定物件類型關聯的靜態方法。靜態方法在類型本身上呼叫,而不是在實例上。

pine
type AB
    int a

staticmethod(AB) int add(int a, int b) => a + b

// 在類型名稱上呼叫:
result = AB.add(20, 5)      // 25

靜態方法只能為物件類型(用 type 聲明的)定義:

pine
staticmethod(int) add(int a, int b) => a + b
// error: static methods can only be defined for object types

標準函式庫範例

pine
// chart.point 的工廠方法 (stdlib/chart.1.pine)
export type point
    int index
    int time
    float price

export staticmethod(point) point now(float price = close) =>
    point.new(bar_index, timenow, price)

export staticmethod(point) point from_time(int time, float price) =>
    point.new(na, time, price)

呼叫方式為 chart.point.now()chart.point.from_time(time, price)

operator — 運算子多載

operator 關鍵字為使用者定義類型定義自訂的算術運算子行為。

pine
type Vec2
    float x
    float y

Vec2 operator+(Vec2 a, Vec2 b) =>
    Vec2.new(a.x + b.x, a.y + b.y)

Vec2 operator-(Vec2 a, Vec2 b) =>
    Vec2.new(a.x - b.x, a.y - b.y)

Vec2 a = Vec2.new(1, 2)
Vec2 b = Vec2.new(3, 4)
Vec2 c = a + b          // Vec2.new(4, 6)

支援的運算子:+-*/operator 和符號之間沒有空格)。允許為不同運算元類型提供多個多載。

Newtype 聲明

OpenPine 添加了 type Name => underlying_type 語法來建立由現有類型支撐的獨立類型:

pine
type MyInt => int
MyInt a = 10             // 接受底層類型的值

Newtype 可以層疊:

pine
type MyInt => int
type SpecialInt => MyInt
SpecialInt x = 42        // OK

這在標準函式庫中被廣泛使用,為繪圖物件、繪圖樣式和其他領域特定值建立獨立類型:

pine
// prelude/draw.1.pine
export type plot_style => int
export type hline => int
export type label => int
export type line => int
export type box => int
export type table => int

void 傳回類型

不產生有意義傳回值的函式可以聲明為 void

pine
void log(string message) =>
    label.new(bar_index, high, message)

顯式傳回類型

OpenPine 支援在函式名稱前註解傳回類型,以進行更嚴格的類型檢查:

pine
string greet(string name) => "Hello, " + name
[int, bool] getInfo() => [42, true]

適用於所有函式形式 — 普通函式、方法和多行函式主體:

pine
float average(float a, float b) =>
    (a + b) / 2.0

method bool isPositive(float self) =>
    self > 0

泛型函式

OpenPine 允許在函式名稱後使用 <...> 聲明類型參數:

pine
identity<T>(T value) => value
first<T>(array<T> arr) => array.get(arr, 0)
method abc<T>(T self) => self

可變參數

OpenPine 允許最後一個參數使用 ... 作為可變參數:

pine
sum(int first, int rest...) => first + rest

可變參數必須是參數列表中的最後一個。

下一步

基於 MIT 許可證發佈。