自v5推出以來,Pine團隊一直在努力繼續改進Pine。讓我們回顧一下他們在過去兩個月中增加的一些期待已久的功能。
函數多載 Function overloads
函數多載(overloads)是可以在程式庫中定義或包含在腳本中的函數的變體。多載與原始函數同名,但使用不同數量的參數或不同類型的參數。它們在必須使用參數類型的程式庫中尤其有用。
在這個指標中,我們定義了一個接受三個參數的 mult() 函數的多載:
//@version=5
indicator("Function overload")
// Two parameters
mult(x1, x2) =>
x1 * x2
// Three parameters
mult(x1, x2, x3) =>
x1 * x2 * x3
plot(mult(7, 4))
plot(mult(7, 4, 2))
在這裡,我們定義了不同操作的多載,具體取決於使用的參數類型。當重載與原始函數具有相同數量的參數時,必須使用不同的顯式類型定義參數:
//@version=5
indicator("Function overload")
// Accepts both 'int' and 'float' values because any 'int' can be automatically cast to 'float'
mult(float x1, float x2) =>
x1 * x2
// Returns a 'bool' value instead of a number
mult(bool x1, bool x2) =>
x1 and x2 ? true : false
mult(string x1, string x2) =>
str.tonumber(x1) * str.tonumber(x2)
// Has three parameters, so explicit types are not required
mult(x1, x2, x3) =>
x1 * x2 * x3
plot(mult(7, 4))
plot(mult(7.5, 4.2))
plot(mult(true, false) ? 1 : 0)
plot(mult("5", "6"))
plot(mult(7, 4, 2))
for…in
如果數組為空,則使用 for 結構疊代數組需要防止循環進入並防止越界數組索引。
新的 for…in 結構通過為您疊代數組的所有元素使您的生活更輕鬆。語法很簡單:對於 array_id in array_element 將從索引零開始疊代 array_id 的元素,在每次疊代時將數組元素的值分配給 array_element 變數。如果數組為空,則不會出現疊代或錯誤,並且可以在循環疊代中增加或刪除數組元素。
在下面的腳本中,我們使用 for…in 來查找 a1 數組中的最高數字:
//@version=5
indicator("For...in cycle")
var int[] a1 = array.from(1, 3, 6, 3, 8, 0, -9, 5)
highest(array) =>
var int highestNum = na
for element in array
if na(highestNum) or element > highestNum
highestNum := element
highestNum
plot(highest(a1))
新的字串操作函數
這些新函數提供了更多處理字串的方法,並為 Pine 引入了正規表達式。請注意,與使用數組的用戶定義字串函數相反,它們可以返回“simple”形式的值,例如,可以使用它為 request.security() 建立symbol參數。
str.contains(source, str)
確定 source 字串是否包含 str 子字串。例如,我們可以透過在 syminfo.tickerid 內建變數中的子字串中查找 “!” 來確定當前圖表是否為連續期貨圖表:
var isContinuous = str.contains(syminfo.tickerid, "!")
plot(isContinuous ? 1 : 0)
str.pos(source, str)
返回 str 字串在 source 字串中的位置。
str.substring(source, begin_pos, end_pos)
從 source 字串中提取子字串。在這個例子中,我們使用 str.substring() 和 str.pos() 從 s 輸入字串中獲取“AAPL”子字串:
s = input.string("Time to sell some NASDAQ:AAPL")
pos = str.pos(s, ":") // Get the position of the ":" character
tkr = str.substring(s, pos + 1) // "AAPL"
str.replace(source, target, replacement, occurrence)
與現有的 str.replace_all() 函數相反,str.replace() 允許用替換字串選擇性地替換匹配的子字串。在此示例中,我們將擴展字串中第一個商品代碼的“FTX”前綴替換為“BINANCE”:
var source = "FTX:BTCUSD / FTX:BTCEUR"
// Replace the first occurrence of "FTX" with the "BINANCE" replacement string
var newSource = str.replace(source, "FTX", "BINANCE", 0)
str.lower(source) 和 str.upper(source)
將 source 字串的所有字母轉換為小寫或大寫:
s = str.lower("Time to Sell Some AAPL") // time to sell some aapl!
s = str.upper("Time to Sell Some AAPL!") // TIME TO SELL SOME AAPL!
str.startswith(source, str) 和 str.endswith(source, str)
確定 source 字串是以 str 子字串開頭還是結尾。
str.match(source, regex)
提取與指定正規表達式匹配的子字串。例如,使用正規表達式 [\\w]+:[\\w]+ 可以從“是時候出售一些 NASDAQ:AAPL!”源字串中找到並返回商品名稱:
s = "It's time to sell some NASDAQ:AAPL!"
var string tickerId = str.match(s, "[\\w]+:[\\w]+") //"NASDAQ:AAPL"
貨幣轉換 Currency Conversion
request.* 命名空間中的大多數函數現在可以將它們返回的值轉換為另一種貨幣。如果在函數調用中指定了currency參數,則函數返回的價格值將從源貨幣轉換為目標貨幣。currency 參數被增加到以下函數中:
文字框 Textboxes
Pine中的方框圖現在支援文字!只要在繪製框時將 text 參數增加到 box.new() 函數,文字就會顯示在其中。您可以透過在建立框的時候設定 text_size、text_color、text_valign 和 text_halign 參數的值來微調文字的行為,或者透過使用以下新函數之一引用框:
作為此功能的一個示例,以下是修改後的內建多時間周期圖表指標,以顯示其突出顯示的一周的最高/最低價:

要隨時了解Pine的新功能,請留意我們的Pine用戶手冊的發行說明。我們的PineCoders還在Squawk Box Telegram頻道、Twitter和TradingView上的Pine腳本公共聊天中廣播更新。
我們希望您發現這些廣受歡迎的功能很有用。請繼續向我們提供反饋和改進建議。我們為您建構TradingView,總是渴望收到您的來信。