Pine的新功能:多载、新的字符串函数等等!

Dec 23, 2021

自v5推出以来,Pine团队一直在努力继续改进Pine。让我们回顾一下他们在过去两个月中添加的一些期待已久的功能。

函数多载 Function 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_sizetext_colortext_valigntext_halign 参数的值来微调文本的行为,或者通过使用以下新函数之一引用框:

作为此功能的一个示例,以下是修改后的内置多时间周期图表指标,以显示其突出显示的一周的最高/最低价:

要随时了解Pine的新功能,请留意我们的Pine用户手册的发行说明。 我们的PineCoders还在Squawk Box Telegram频道、Twitter和TradingView上的Pine脚本公共聊天中广播更新。

我们希望您发现这些广受欢迎的功能很有用。请继续向我们提供反馈和改进建议。我们为您构建TradingView,总是渴望收到您的来信。


 

还没有关注我们的中文微信公众号?快来扫二维码吧!

Look first Then leap

TradingView专门为您而打造,请确保您充分利用我们出色的功能
开启图表