OpenPine 擴充
以下功能是 OpenPine 擴充,不存在於標準 TradingView Pine Script v6 中。它們使得 OpenPine 的標準函式庫和 prelude 可以完全用 Pine Script 原始碼檔案編寫。
getter — 零參數屬性函式
getter 關鍵字聲明一個不能有參數且不使用括號呼叫的函式,使其看起來像變數或屬性存取。
getter size() => 42
// 像變數一樣存取 — 不需要括號:
value = size // 42
// 使用括號呼叫會報錯:
value = size() // error: function `size` not definedgetter 不能有參數:
getter value(int a) => a + 1 // error: getter functions cannot have parameters存在原因: 在標準 TradingView Pine 中,像 close、open、math.pi 和 color.red 這樣的內建值是由執行階段作為變數暴露的。OpenPine 使用 getter 在純 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() => #2962FFstaticmethod — 類型上的靜態方法
staticmethod 關鍵字定義與特定物件類型關聯的靜態方法。靜態方法在類型本身上呼叫,而不是在實例上。
type AB
int a
staticmethod(AB) int add(int a, int b) => a + b
// 在類型名稱上呼叫:
result = AB.add(20, 5) // 25靜態方法只能為物件類型(用 type 聲明的)定義:
staticmethod(int) add(int a, int b) => a + b
// error: static methods can only be defined for object types標準函式庫範例
// 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 關鍵字為使用者定義類型定義自訂的算術運算子行為。
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 語法來建立由現有類型支撐的獨立類型:
type MyInt => int
MyInt a = 10 // 接受底層類型的值Newtype 可以層疊:
type MyInt => int
type SpecialInt => MyInt
SpecialInt x = 42 // OK這在標準函式庫中被廣泛使用,為繪圖物件、繪圖樣式和其他領域特定值建立獨立類型:
// 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 => intvoid 傳回類型
不產生有意義傳回值的函式可以聲明為 void:
void log(string message) =>
label.new(bar_index, high, message)顯式傳回類型
OpenPine 支援在函式名稱前註解傳回類型,以進行更嚴格的類型檢查:
string greet(string name) => "Hello, " + name
[int, bool] getInfo() => [42, true]適用於所有函式形式 — 普通函式、方法和多行函式主體:
float average(float a, float b) =>
(a + b) / 2.0
method bool isPositive(float self) =>
self > 0泛型函式
OpenPine 允許在函式名稱後使用 <...> 聲明類型參數:
identity<T>(T value) => value
first<T>(array<T> arr) => array.get(arr, 0)
method abc<T>(T self) => self可變參數
OpenPine 允許最後一個參數使用 ... 作為可變參數:
sum(int first, int rest...) => first + rest可變參數必須是參數列表中的最後一個。