Skip to content

函數與方法

函數定義

單行函數

pine
double(x) => x * 2
add(a, b) => a + b

多行函數

函數體的最後一個運算式是返回值。沒有 return 關鍵字:

pine
smaCustom(src, length) =>
    sum = 0.0
    for i = 0 to length - 1
        sum += src[i]
    sum / length

返回元組的函數

pine
calcBands(src, length, mult) =>
    basis = ta.sma(src, length)
    dev = mult * ta.stdev(src, length)
    [basis, basis + dev, basis - dev]

[mid, upper, lower] = calcBands(close, 20, 2.0)

預設參數

pine
myPlot(src, length = 14, title = "Default") =>
    sma = ta.sma(src, length)
    plot(sma, title)
    sma

命名參數

調用函數時可以使用命名參數:

pine
plot(close, title = "Close", color = color.blue, linewidth = 2)

函數重載

多個函數可以共享相同的名稱,只要參數類型不同:

pine
format(int x) => str.tostring(x)
format(float x) => str.tostring(x, "#.##")
format(string x) => x

方法

方法是第一個參數為接收者(self)的函數。可以使用點語法調用:

pine
method double(int self) =>
    self * 2

x = 5
x.double()  // 10

方法可以用於自訂類型:

pine
type Position
    float entry
    float size

method pnl(Position self, float currentPrice) =>
    (currentPrice - self.entry) * self.size

method isProfit(Position self, float currentPrice) =>
    self.pnl(currentPrice) > 0

pos = Position.new(entry = 100.0, size = 10.0)
if pos.isProfit(close)
    label.new(bar_index, high, str.tostring(pos.pnl(close), "#.##"))

禁止遞迴調用

Pine Script 不允許遞迴。函數不能直接或透過其他函數間接調用自身。編譯器在編譯時拒絕任何調用循環:

pine
// 錯誤 — 不允許直接遞迴
factorial(n) =>
    n <= 1 ? 1 : n * factorial(n - 1)

// 錯誤 — 間接遞迴也會被拒絕
isEven(n) => n == 0 ? true : isOdd(n - 1)
isOdd(n)  => n == 0 ? false : isEven(n - 1)

這是語言的基本約束,不是實現限制。請使用 forwhile 迴圈進行迭代計算:

pine
factorial(n) =>
    result = 1
    for i = 2 to n
        result *= i
    result

內建函數

Pine Script 提供了許多內建函數:

pine
// 繪圖
plot(close, "Close", color.blue)
plotshape(close > open, style = shape.triangleup)
bgcolor(close > open ? color.new(color.green, 90) : na)

// 技術分析
sma = ta.sma(close, 20)
ema = ta.ema(close, 12)
[macdLine, signal, hist] = ta.macd(close, 12, 26, 9)
rsi = ta.rsi(close, 14)

// 數學
rounded = math.round(close, 2)
maxVal = math.max(open, close)

// 字串操作
text = str.tostring(close, "#.##")

// 輸入
length = input.int(14, "RSI Length", minval = 1)
src = input.source(close, "Source")

下一步

基於 MIT 許可證發佈。