简体   繁体   English

问题在 Pine-Script 中复合变量的值?

[英]Issue Compounding the Value of a Variable in Pine-Script?

I am trying to write an If Else statement in TradingView's Pine-script that allows the value of a variable from one bar back in the series to be added onto the current value if the conditions are met.我正在尝试在 TradingView 的 Pine 脚本中编写 If Else 语句,如果满足条件,该语句允许将来自系列中一个柱状线的变量的值添加到当前值上。 This should allow the value of the variable "hd1" to compound until the If condition isn't met.这应该允许变量“hd1”的值复合,直到不满足 If 条件。

This is what I have so far:这是我到目前为止所拥有的:

hd1 = if (hl < hl[3]) or (hl < hl[1])
    (hl[1] - hl) + hd1[1]
else 
    0

I tried to compound the value with + hd1[1] in the code but I get an error that I am not able to reference hd1 until it is defined, which I understand but am unsure how to work around.我试图在代码中将值与+ hd1[1]复合,但我得到一个错误,我无法引用 hd1,直到它被定义,我理解但不确定如何解决。 Is there another way I could approach this?还有另一种方法可以解决这个问题吗?

You will need to declare variables before hand if you plan on self referencing them.如果您计划自引用变量,则需要事先声明变量。 In this case, most likely you'll be able to declare hd1 with the value of zero.在这种情况下,您很可能能够将hd1的值声明为零。

float hd1 = 0
if hl < hl[3] or hl < hl[1]
     hd1 := (hl[1] - hl) + hd1[1]

In this case the else portion of the if statement is unnecessary as hd1 's default value is already set to zero in the declaration and would only be modified if the if condition is true.在这种情况下, if语句的else部分是不必要的,因为hd1的默认值在声明中已经设置为零,并且只有在if条件为真时才会被修改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM