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 許可證發佈。