方法语法来到了Pine Script®

Mar 7, 2023

用于其它语言扩展方法的点符号现在可用于Pine Script®。程序员可以通过两种不同的方式使用这种新语法:

  • 使用用户定义的方法,这些方法是用新的method关键字声明的特殊函数。
  • 当第一个参数是该类型对象的 ID 时,使用来自array, box, label, line, linefill, matrix, 和table类型的命名空间的内置函数。

让我们看一下这个简单的例子,我们在其中计算上涨K线滚动窗口的平均low点:

//@version=5
indicator("Long stop", "", true)
int lengthInput = input(20)
var pricesArray = array.new<float>(lengthInput)

method maintainQueue(array<float> srcArray, float value) =>
    // Append a new value to the end of the array.
    srcArray.push(value)
    // Remove the oldest value from the beginning of the array.
    srcArray.shift()

if close > open
    // Track the `low` values of up bars.
    pricesArray.maintainQueue(low)

plot(pricesArray.avg(), "Stop")

我们的maintainQueue()方法与常规函数非常相似,可以这样使用,但是method关键字还允许它在第一个参数类型的变量之后使用点符号。 当我们这样做时,我们从方法调用中省略了第一个参数,如pricesArray.maintainQueue(low)

还要注意我们如何在调用中使用方法符号和array命名空间中的函数,例如方法代码中的srcArray.push()srcArray.shift(),以及示例最后一行中的pricesArray.avg()

您可以在我们的用户手册methods页面中找到这项新功能的更多信息。

要查看实际方法,您可以查看我们内置跳空指标的更新代码,该指标使用内置和用户定义的方法来突出显示带方框的图表跳空:

此外,我们更新了ZigZag脚本库的代码以使用内置和用户定义的方法:

ZigZag 由TradingView提供

请参阅我们的一PineCoders在以下脚本中使用的对象和方法:

Flare 由Fikira提供

Recursive Zigzag [Trendoscope] 由HeWhoMustNotBeNamed提供

Recursive Auto-Pitchfork [Trendoscope] 由HeWhoMustNotBeNamed提供

要随时了解新的 Pine Script® 功能,请留意用户手册的发行说明PineCoders帐户还通过其Telegram上的Squawk Box、其Twitter帐户以及TradingView上的Pine Script® Q&A公共聊天广播更新。

我们希望大家发现这个高需求功能很有用。请继续向我们发送您的反馈和改进建议。我们为您打造TradingView,始终渴望收到您的来信。


 

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

Look first Then leap

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