类似于PreDefined的研究"PriceChannel“,它只在出现新的低点或新高时改变它的值,我希望它只在满足某个条件时改变它的值,然后保持该值直到再次满足它。
这是我到目前为止的代码,现在它检查最后一个条形的"b“值,如果它> 0,那么它绘制"b",如果不是,它从第二个最近的条形开始,然后第三个,依此类推,直到它找到一个> 0的"b”值。
代码可以工作,但我必须添加一个新的"else if“语句为每第n条在过去,300条就足够了,然而这将意味着我将不得不键入相同的行300次,每次只是改变数字,我想避免这样做,另外,如果它检查n=n+1的次数,它会更干净。
对我应该做什么有什么建议吗?
plot b = if SMA30 crosses below 0 or
SZO crosses below 7 and SMA30 < SMA30[1]
then open
else 0;
plot g = if b>0
then b
else if b[1]>0
then b[1]
else if b[2]>0
then b[2]
else if b[3]>0
then b[3]
else 0;发布于 2021-03-30 01:37:27
您可以使用递归变量。有两种方法可以完成此操作:
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是必要的(查看我的answer here了解它是如何工作的)。
我用来测试的代码:
#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条形图上的测试结果图像:

https://stackoverflow.com/questions/66805478
复制相似问题