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 许可证发布。