您可以使用递归变量。有两种方法可以做到这一点:
def gVal = if b > 0 then b else gVal[1];
plot g = gVal;
def gVal =
CompoundValue(
1,
if GetValue(b, 0) > 0 then GetValue(b, 0) else GetValue(gVal, 1),
GetValue(b, 0)
);
plot g = gVal;
通常,递归变量可以正常工作。如果您的代码中有不同的“长度”或“偏移量”,那么 CompoundValue 是必要的(请查看我的答案以了解其工作原理)。
我用于测试的代码:
#hint: SO q: https://stackoverflow.com/q/66805478/1107226
def price_to_beat = 2.06;
declare lower;
# b could also be a plot; I had a separate plot, so I `def`d it here
def b =
if open > price_to_beat
then open
else 0;
def gVal = if b > 0 then b else gVal[1];
plot g = gVal;
AddChartBubble(yes, gVal, "gVal:" + gVal, Color.YELLOW, no);
AddLabel(yes, "RecursiveVariable", Color.CYAN);
#hint: SO q: https://stackoverflow.com/q/66805478/1107226
def price_to_beat = 2.06;
declare lower;
# b could also be a plot; I had a separate plot, so I `def`d it here
def b = if open > price_to_beat
then open
else 0;
def gVal =
CompoundValue(
1,
if GetValue(b, 0) > 0 then GetValue(b, 0) else GetValue(gVal, 1),
GetValue(b, 0)
);
plot g = gVal;
g.SetDefaultColor(Color.CYAN);
AddChartBubble(yes, g, "b: " + b + ", g: " + g, Color.YELLOW, yes);
AddLabel(yes, "CompoundValue", Color.CYAN);
6 条形图上的测试结果图像: