陣列#

陣列 (Array) 是一組有序的數值集合,可以動態新增或刪除元素。當需要管理一批相同性質的資料(例如最近 N 根 K 棒的收盤價、多個訊號標記的位置)時,陣列非常有用。

建立陣列#

array.new#

使用 array.new<型別>() 建立指定型別的空陣列:

prices  = array.new<float>()   // 浮點數陣列
flags   = array.new<bool>()    // 布林值陣列
labels  = array.new<string>()  // 字串陣列

建立時同時填入初始大小與預設值:

// 建立長度為 5 的陣列,初始值全為 0.0
prices = array.new<float>(5, 0.0)

array.from#

用已知數值直接建立陣列(最方便的方式):

levels = array.from(100.0, 105.0, 110.0, 95.0, 90.0)

新增與刪除元素#

函式說明
array.push(id, value)在陣列 末尾 新增元素
array.unshift(id, value)在陣列 開頭 新增元素
array.pop(id)移除並回傳 末尾 元素
array.shift(id)移除並回傳 開頭 元素
array.remove(id, index)移除指定索引的元素
array.clear(id)清空陣列
//@version=6
indicator("陣列新增刪除")

var recentCloses = array.new<float>()

// 每根 K 棒推入最新收盤價,保持最多 5 個元素
array.push(recentCloses, close)
if array.size(recentCloses) > 5
    array.shift(recentCloses)  // 移除最舊的

// 顯示陣列大小
plot(array.size(recentCloses), "陣列大小")

讀取與修改元素#

函式說明
array.get(id, index)取得指定索引的值
array.set(id, index, value)設定指定索引的值
array.size(id)回傳陣列長度
array.first(id)取得第一個元素
array.last(id)取得最後一個元素
levels = array.from(90.0, 95.0, 100.0, 105.0, 110.0)

firstLevel = array.first(levels)   // 90.0
lastLevel  = array.last(levels)    // 110.0
thirdLevel = array.get(levels, 2)  // 100.0(索引從 0 開始)

array.set(levels, 2, 101.0)        // 將第 3 個元素改為 101.0

統計函式#

Pine Script 提供一系列陣列統計函式:

函式說明
array.avg(id)平均值
array.sum(id)總和
array.max(id)最大值
array.min(id)最小值
array.stdev(id)標準差
array.median(id)中位數
array.mode(id)眾數
//@version=6
indicator("陣列統計")

var closes = array.new<float>()
array.push(closes, close)
if array.size(closes) > 20
    array.shift(closes)

if array.size(closes) == 20
    plot(array.avg(closes),   "平均",   color=color.blue)
    plot(array.max(closes),   "最高",   color=color.green)
    plot(array.min(closes),   "最低",   color=color.red)
    plot(array.stdev(closes), "標準差", color=color.purple)

排序與搜尋#

prices = array.from(30.0, 10.0, 50.0, 20.0, 40.0)

// 排序(修改原陣列)
array.sort(prices, order.ascending)   // 遞增:10, 20, 30, 40, 50
array.sort(prices, order.descending)  // 遞減:50, 40, 30, 20, 10

// 搜尋
idx = array.indexof(prices, 30.0)     // 回傳 30.0 的索引,找不到回傳 -1
contains = array.includes(prices, 30.0)  // true / false

複製與組合#

original = array.from(1.0, 2.0, 3.0)

// 複製陣列
copied = array.copy(original)

// 將另一個陣列附加到末尾
extra = array.from(4.0, 5.0)
array.concat(original, extra)  // original 變為 [1, 2, 3, 4, 5]

實用範例:動態支撐/壓力位#

//@version=6
indicator("動態支撐壓力", overlay=true)

lookback  = input.int(50, "回顧根數")
threshold = input.float(0.5, "合併門檻(%)", step=0.1) / 100

// 收集近期高低點
var levels = array.new<float>()

// 每根 K 棒檢查是否為轉折點
isPivotHigh = ta.pivothigh(high, 3, 3)
isPivotLow  = ta.pivotlow(low, 3, 3)

if not na(isPivotHigh)
    array.push(levels, isPivotHigh)
if not na(isPivotLow)
    array.push(levels, isPivotLow)

// 只保留最近 20 個關鍵價位
while array.size(levels) > 20
    array.shift(levels)

// 繪製所有關鍵價位
if barstate.islast
    for level in levels
        line.new(bar_index - lookback, level, bar_index, level,
                 color=color.new(color.gray, 50), width=1)

Reference#