Skip to content

标准库

OpenPine 附带了使用 Pine Script 编写的标准库。这些模块在每个脚本中自动可用。

模块概览

模块描述
ta技术分析 — SMA、EMA、RSI、MACD、布林带等
math数学函数 — abs、round、max、min、pow、log 等
str字符串操作 — tostring、contains、replace、split 等
array动态数组操作 — push、pop、get、set、sort 等
map键值映射操作 — put、get、contains、keys、values 等
matrix二维矩阵操作 — get、set、rows、columns、transpose 等
color颜色函数 — new、r、g、b、from_gradient
input用户输入 — int、float、bool、string、source、color、timeframe
label图表标签对象 — new、set_text、set_xy、delete
line图表线条对象 — new、set_xy1、set_xy2、delete
box图表框对象 — new、set_lefttop、set_rightbottom、delete
table图表表格对象 — new、cell、merge_cells、delete
strategy策略函数 — entry、close、exit、order
runtime运行时信息 — error
log日志 — info、warning、error
timeframe时间周期工具
syminfo标的信息
barstateK 线状态 — isfirst、islast、isconfirmed、isrealtime

技术分析(ta

pine
// 移动平均线
sma20 = ta.sma(close, 20)
ema12 = ta.ema(close, 12)
wma10 = ta.wma(close, 10)
vwma20 = ta.vwma(close, 20)

// 振荡器
rsi = ta.rsi(close, 14)
[macdLine, signalLine, histogram] = ta.macd(close, 12, 26, 9)
[k, d] = ta.stoch(close, high, low, 14)

// 波动率
atr = ta.atr(14)
[middle, upper, lower] = ta.bb(close, 20, 2.0)

// 趋势
[diPlus, diMinus, adx] = ta.dmi(14, 14)
supertrend = ta.supertrend(3.0, 10)

// 价格行为
highest = ta.highest(high, 20)
lowest = ta.lowest(low, 20)
change = ta.change(close)
crossover = ta.crossover(ema12, sma20)
crossunder = ta.crossunder(ema12, sma20)

数学(math

pine
rounded = math.round(3.14159, 2)    // 3.14
absolute = math.abs(-42)             // 42
maximum = math.max(open, close)      // 较大的值
power = math.pow(2, 10)             // 1024
squareRoot = math.sqrt(144)         // 12
logarithm = math.log(100)           // 自然对数

字符串(str

pine
text = str.tostring(close, "#.##")
combined = str.format("{0}: {1}", "Price", close)
hasWord = str.contains("hello world", "world")    // true
upper = str.upper("hello")                         // "HELLO"
replaced = str.replace("foo bar", "foo", "baz")    // "baz bar"
length = str.length("hello")                        // 5

数组

pine
var prices = array.new<float>(0)
array.push(prices, close)

size = array.size(prices)
first = array.get(prices, 0)
last = array.get(prices, size - 1)

avg = array.avg(prices)
maxPrice = array.max(prices)
minPrice = array.min(prices)

// 从值创建
var levels = array.from(100.0, 110.0, 120.0)

输入

pine
length = input.int(14, "Length", minval = 1, maxval = 200)
factor = input.float(2.0, "Factor", step = 0.1)
useEMA = input.bool(true, "Use EMA")
src = input.source(close, "Source")
maColor = input.color(color.blue, "MA Color")
tf = input.timeframe("D", "Timeframe")

下一步

  • 集成 — 从 Rust 以编程方式运行脚本
  • C/C++ API — 在 C++ 应用中嵌入 OpenPine

基于 MIT 许可证发布。