想要将三元运算符转换为循环

时间:2019-09-05 13:33:56

标签: pine-script

我正在做一个指标,如果之后的99个candelse小于high[100],我只想保存high[100]的值。

我想一直保留该值,直到得到另一个条件为止,最后一个high[100]高于还是低于新的high[100]条件都没关系。

我尝试在循环中执行此操作,但是由于我对循环非常陌生,所以尚未成功。

 //@version=3
strategy("trend hybrid",overlay=true)

long_2 = highest(high,100)
high_long = high[100]
long_test = highest(high,99)

long = na
long := long_test <= high_long ? high_long : long[1]

plot(long) 


//@version=3
study("For Loop tutorial - Example 6")
l = high[100]
y = high
for i = 99 to 1
    if high[i] <= high[l]
        break    
    y := l

plot(y, style=line, color=green, linewidth=3)

2 个答案:

答案 0 :(得分:0)

这可能是您要寻找的。请记住,检测您的状况需要100条,因此蓝色标签和线条的打印位置是过去length - 1的偏移量。仅在检测到新高时出现的紫红色圆圈才会绘制在检测到的蜡烛上。您可以在输入中更改长度。

//@version=4
study("", "", true)
length = input(100)
highBar = -highestbars(length) == length - 1
var highValue = 0.
if highBar
    highValue := high[length - 1]
    label.new(bar_index[length - 1], highValue, tostring(highValue), xloc.bar_index, yloc.price, size = size.normal)
plot(highValue, "highValue", offset = -(length - 1))
plotchar(highBar, "High detected", "•", location.abovebar, color.fuchsia, size = size.small)

enter image description here

答案 1 :(得分:0)

我将引导您完成两种方法,使用循环,因为这是您想要的,并使用推荐的方法。

使用循环

//@version=4
//@author=lucemanb
study("Highest", overlay=true)

// your bars limit
limit = input(100)

// we start comparing from 100th bar to the next 100 candles
highest = high[limit]

// if we find a candle greater than that, we set the high to the new value
//for i=limit to limit+limit // refer bottom 11
for i=0 to limit
    if high[i] > highest
        highest := high[i]

// we check - if our 100th high is equal to the highest value that we were searching for, it means its the highest.
newValue = high[limit] == highest

// we draw a background color on the place that we find
// note that, since we are checking the level 100 candles behind, we need to set the offset so
// that we can see where the level is
bgcolor(newValue?color.red:na, offset=-limit)

// 11 Your after is ambiguous, if you mean the candles older than 100th candle, remove the first '//' on line 12 and put it on lime 13

推荐方式

//@version=4
//@author=lucemanb
study("Highest", overlay=true)

// your bars limit
limit = input(100)

// we simply check if our 100th high is equal to the highest high 
// from the 100th candle to the other 99 candles after it.
newValue = high[limit] == highest(limit) //[limit] // refer bottom 11

// we draw a background color on the place that we find
// note that, since we are checking the level 100 candles behind, we need to set the offset so
// that we can see where the level is
bgcolor(newValue?color.red:na, offset=-limit)

// 11 Your after is ambiguos, if you mean the candles older than 100th candle, remove the first '//' on the line 10

请仔细阅读评论


我希望这对您有帮助