Pine脚本的新功能:str.format() 和 array.from()

May 21, 2021

Pine腳本語言的這項更新增加了用於處理字串和陣列的函數。

str.format() 函數允許您將傳遞給它的參數轉換為帶有指定格式的字串:

str.format(formatString, arg0, arg1, ...) -> string

formatString 在括號 “{}” 中包含一個 N 下標以替換相應的格式化參數(arg0, arg1..argN,它們是字串或數字),以及下面討論的一些形態。

因此,對於解析字串,您不再需要運用使用 str.replace_all() 函數的結構:

txt := "Time to {0} some {1}!"

txt := str.replace_all(txt, "{0}", close > open ? "buy" : "sell")

txt := str.replace_all(txt, "{1}", syminfo.ticker)

但是你可以用一種新的、更方便、更短的方式來做到這一點:

txt = str.format("Time to {0} some {1}!", close > open ? "buy" : "sell", syminfo.ticker)

括號中的下標 {0}{1} 替換為相應的序數參數:{0} – 三元運算符執行的結果,字串 “buy”“sell” ,根據收盤價和開盤價的不等式返回,{1} – syminfo.ticker,圖表上的當前交易品種:

使用 number 模板,可以將轉換為字串的數字轉換為不同的表示形式,例如限制小數位數:

str.format("{0,number,#.#}", 1.34) // returns: 1.3

去掉數字的小數部分:

str.format("{0,number,integer}", 1.34) // returns: 1

獲取財務資訊:

str.format("The cash turnover amounted to {0,number,currency}", 1340000) 

// “The cash turnover amounted to $1,340,000.00”.

獲取百分比:

str.format("Expected return is {0,number,percent} - {1,number,percent}", 0.1, 0.2)  

// returns: Expected return is 10% - 20%.

str.format() 函數支援用於格式化日期/時間的 date 模板。

此腳本顯示自年初以來的月數、周數、和天數:

//@version=4
study("str_format_date")

//formatting string with date pattern
txt = str.format("Current bar date: {0,date, Y-MM-d'.' \n M 'months \n' F 'weeks \n' D 'days \n from the beginning of the year' }", timenow)

//printing label
var l1 = label.new(na, na, color = #FFFFFF00)
label.set_x(l1, bar_index)
label.set_yloc(l1, yloc.belowbar)
label.set_text(l1, txt)

plot(close)

只有一個變數 timenow 被傳遞到 str.format() 函數,該函數包含 UNIX 時間表現形式:圖表上交易品種的當前日期、時間、和時區。該字串使用字符 Y, M, D, F(年、月、日、本月的一周)進行格式化,\n 特殊字符用於換行,單引號 ‘ ’ 用於在字符之間插入文字。

此腳本將格式化的字串輸出到標籤,並使用各種字符輸出 str.format() 函數的相應結果:

//@version=4
study("str_format_date_time")

// creating date-time variable: 2021.05.04. 14:30:59
t = timestamp("GMT+0", 2021, 05, 04, 14, 30, 59)

// function for left side of the label
f_print_left(_text)  => var _label = label.new(bar_index, na, _text, xloc.bar_index, yloc.price, color(na), label.style_label_right, color.green, size.large, text.align_right), label.set_xy(_label, bar_index, 0), label.set_text(_label, _text)

// function for right side of the label
f_print_right(_text) => var _label = label.new(bar_index, na, _text, xloc.bar_index, yloc.price, color(na), label.style_label_left, color.red, size.large, text.align_left), label.set_xy(_label, bar_index, 0), label.set_text(_label, _text)

var string format = 
  "{0,date,y.MM.dd hh:mm:ss}\n" +
  "{1,date,short}\n" +
  "{2,date,medium}\n" +
  "{3,date,long}\n" +
  "{4,date,full}\n" +
  "{5,date,h a z (zzzz)}\n" +
  "{6,time,short}\n" +
  "{7,time,medium}\n" +
  "{8,time,long}\n" +
  "{9,time,full}\n" + 
  "{10,time,hh:mm:ss}\n" +
  "{11,time,HH:mm:ss}\n"

// printing format string 
f_print_left(format)
//printing formatted string
f_print_right(str.format(format, t, t, t, t, t, t, t, t, t, t, t, t))

標籤的左列包含 {} 括起來的字符,右列是 str.format 函數的結果:

t 變數傳遞給 str.format() 函數,該函數包含 UNIX 時間表現形式:當前日期、時間、和時區。

下一個創新是 array.from() 函數,它採用以下類型之一的可變數量的輸入參數:float, int, bool, string, label, line, color,並返回相應類型的陣列。該函數允許您在一行代碼中聲明一個陣列,為其分配初始值並將結果分配給任何變數。

以前,要建立一個陣列並用初始值填充它,您必須使用 array.new() 函數和 array.push()array.set() 函數:

color[] plotColors = array.new_color(size=0)
array.push(plotColors, color.red)
array.push(plotColors, color.green)
array.push(plotColors, #0000FF)

現在,要建立一個包含圖表顏色的陣列,您只需一行即可:

plotColors = array.from(color.red, color.green, #0000FF)

該函數將根據接收到的參數自動將 plotColors 陣列轉換為 color[] 類型:

//@version=4
study(title="array_from")

f_arr (series1, series2, color1, color2) =>
    [ array.from(series1, series2), array.from(color1, color2) ]

[lines, colors] = f_arr(close, open, color.red, color.green)    

plot ( array.get(lines, 0), color = array.get(colors, 0))
plot ( array.get(lines, 1), color = array.get(colors, 1))

我們的參考手冊中始終提供有關這些 str.format()array.from() 函數的資訊。

如果您想了解 Pine 腳本更新,請查看發行說明部分。 PineCoders 帳戶還在其 Squawk Box Telegram 频道、Twitter 和 TradingView 上的 Pine脚本公開聊天室中公布更新。

我們希望大家發現這些改進有用,並請繼續與我們分享您的意見。我們正在為用戶打造 TradingView,很高興聽到您對我們創新的看法。

Look first Then leap

TradingView專門為您打造,請確保您充分利用我們出色的功能
開啟圖表