Board logo

标题: 【PART 2】导言:TradingView Pine 中操作的优先级 [打印本页]

作者: 龙听    时间: 2024-3-29 11:17     标题: 【PART 2】导言:TradingView Pine 中操作的优先级

TradingView 有多种运算符供我们在编写脚本时使用,包括算术运算符、比较运算符和条件三元运算符。但是,如果一行代码中有多个运算符,它们的计算顺序是什么?

TradingView 运算符和运算符的优先级

通过运算符,TradingView Pine 脚本可以执行复杂的操作。运算符是对一个或多个值执行特定操作的代码元素(Stephens,2014 年)。操作符所 "操作 "的值就是我们所说的操作数(Sharp,2013 年)。返回值的代码片段称为表达式(Albahari & Albahari, 2012),其中通常包含运算符。表达式包括 close + high(返回条形图的收盘价和最高价之和)和 open > open[1](当开盘价高于上一个开盘价时返回 true)。

当一个表达式包含多个运算符时,运算符会按照一定的顺序进行运算。Pine 根据运算符的优先级决定计算顺序(Pine 脚本语言教程,未注明日期)。这些规则规定了哪个运算符先运算,哪个运算符后运算,依此类推,直到表达式中的所有运算都执行完毕。

由于运算符的优先级,Pine 能以可预测的方式对表达式进行运算。它还能澄清像 2 + 10 * 5 这样不清楚的表达式(程序员是想先加法再乘法,还是先乘法再加法?) 由于乘法的优先级高于加法,因此计算结果是 52(5 x 10,然后加 2),而不是 60(2 + 10,乘以 5)。

TradingView Pine 中计算的优先级

TradingView 中运算符的优先级如下所示。优先级较高的运算符先于优先级较低的运算符进行计算(Pine 脚本语言教程,未注明出处):
作者: 龙听    时间: 2024-3-29 11:17

PriorityOperatorNameExample
10( )parentheses; overrides operators’ priority((34 - 3) + (8 - close[2])) / 5
9[ ]history referencing operatorclose[2], myVariable[9]
8+addition operator (unary); leaves operand unchanged+ta.mom(close, 10), +volumeChange
-subtraction operator (unary); returns operand’s opposite-ta.ema(high, 3), -maxLoss
notlogical not operator; returns logical oppositenot (high > high[1]), not enterLong
7*multiplication operatorhl2 * 2, 10 * volumeDifference
/division operatorlow / high, 9 / 2
%modulus operator; returns remainder of integer division9 % 3, bar_index % 20 == 0
6+addition operator (binary)10 + 6, (close + close[1]) / 2
-subtraction operator (binary)high - low, ta.ema(close, 10) - ta.ema(close, 3)
5>greater than operator10 > 9, high > high[1]
<less than operator9 < 1, ta.mom(close, 10) < ta.mom(close, 10)[1]
>=greater than or equal to operatorclose <= ta.sma(close, 10), open <= close
<=less than or equal to operatorhigh <= high[5], 19 <= 20
4==equality operatorhigh = ta.highest(high, 20), low = low[2]
!=unequal to operatorclose != close[4], myVariable != 100
3andlogical and operatornewHigh and volumeIncrease, 10 > 2 and 9 != 8
2orlogical or operatorenterLong or stopTriggered, not (9 < 3 or 500 > 8)
1?:conditional ternary operatorhighestHigh == true ? 200 : 3, close < open ? close : close

作者: 龙听    时间: 2024-3-29 11:24

如表所示,我们可以使用一元和二元形式的加法运算符 (+) 和减法运算符 (-)。当运算符作用于单个操作数时,它被称为一元运算符(如 -close,得到收盘价的负数);当运算符作用于两个值时,它被称为二元运算符(如 high - low)(Albahari & Albahari,2012 年)。

从表中还可以看出,多个运算符具有相同的优先级。当一个表达式有多个具有相同优先级的运算符时,它们将从左到右依次计算(Pine Script Language Tutorial, n.d.)。例如,加法(+)和减法(-)具有相同的优先级,因此表达式 9 - 3 + 22 - 3 返回 25(9 - 3 为 6,加上 22 为 28,减去 3 为 25)。

运算符优先规则无需记忆;有疑问时,只需使用括号来说明表达式(另见下文)。

用括号改变运算符优先级

我们可以通过用括号将表达式的各个部分分组来改变运算符的优先级(Pine Script Language Tutorial,n.d.)。由于括号的优先级最高,TradingView 会先评估括号内的表达式。

例如,10 + 9 * close 首先将 9 与 close 相乘,然后将 10 加到结果中,因为乘法的优先级高于加法。但是 (10 + 9) * close 会先将 10 和 9 相加,然后再与 close 相乘。

我们有时需要使用括号来获得正确的计算结果,比如在这个脚本中,我们试图计算柱形图的中点:
  1. //@version=5
  2. indicator(title="Bar midpoint", overlay=true)

  3. plot(high + low / 2, color=color.blue, linewidth=2)
复制代码
在此,我们使用 indicator() 设置指标属性,然后使用 plot() 函数在图表上绘制一系列数据(TradingView,注)。绘制值为 high + low / 2,试图绘制出柱状图的中点。

但当添加到图表中时,这个示例指标看起来就像



这里显然有问题。问题在于我们忽略了高 + 低 / 2 表达式中运算符的优先级。由于除法 (/) 的优先级高于加法 (+),所以我们没有得到条形图的中点。相反,我们用 2 除以条形图的低点,然后再加上高点。

要解决这个错误,我们需要用括号改变运算符的优先级。因此,我们像这样将 high + low 放在括号内:
  1. //@version=5
  2. indicator(title="Bar midpoint", overlay=true)

  3. plot((high + low) / 2, color=color.blue, linewidth=2)
复制代码
当我们将这个更新的示例添加到图表中时,它能正确绘制条形图的中点:



TradingView Pine 中的括号嵌套

有时,我们的表达式很复杂,需要多个括号相互嵌套。在这种情况下,TradingView Pine 会先评估最内层的括号(换句话说,它从内向外工作)。

比方说,我们计算 x 变量的方法如下:
  1. x = (7 % 3) * (4 + (6 / 2))
复制代码
由于最内层的括号先计算,因此 Pine 首先计算 6 / 2:
  1. x = (7 % 3) * (4 + 3)       // 6 / 2 = 3
复制代码
然后依次对每组括号进行这样的评估:
  1. x = 1 * 7           // (7 mod 3) = 1, (4 + 3) = 7
复制代码
因此,变量结束时的值为 7:
  1. x = 7
复制代码
摘要

TradingView Pine 如何评估表达式中的多个运算符取决于两点:运算符的优先级和顺序。优先级较高的运算符先于优先级较低的运算符进行运算。具有相同优先级的运算符从左到右进行运算。括号的优先级最高,因此我们可以用它来改变运算符的优先级。




欢迎光临 龙听期货论坛 (http://www.qhlt.cn/) Powered by Discuz! 7.2