获取从上一次条件到现在的最低价格

人气:122 发布:2022-10-16 标签: pine-script algorithmic-trading

问题描述

我想找出从黄色蜡烛到白色蜡烛(相距75根蜡烛)的最低价格

黄条=最后条件

白色条=现在出现此情况

我使用了一些代码,我不知道哪些是对的,哪些是错的。请帮帮忙

// The problem with this code (barssince)is when the white bar happens it zeros the plot 
because the Long_Condition happens in that candle so it zeros the amount i want to use


since = barssince(Long_Condition)
lowest_price = lowest(since)

现在的地块有一些问题:

问题1

plot(since , title = "bars back" , color = color.blue)

在所附图片中,您可以看到蓝色的图示为零,即在 最后一根蜡烛是74支,当我想拿到75支时,它给了零。(从黄条到白条是75支蜡烛) 基本上,下面的代码是错误的,它不会运行脚本。

lowest_price = lowest(since)

现在,如果我使用这个代码,我不知道它是对是错,但它是这样的:

index_white = valuewhen(Long_Condition , bar_index , 0)

index_yellow = valuewhen(Long_Condition , bar_index , 1)

int final_index = index_white - index_yellow 

Lowest_price = lowest(final_index)
plot_1 = plot(final_index , color = color.blue)
plot_2 = plot(Lowest_price)

现在plot_1运行得很好,但当我将plot_2添加到脚本时,它不会运行它。

FINAL_INDEX不是Lowest(final_index)不工作的整数吗?

请帮帮我。谢谢

图片2已启用plot_1编码。

Click to see the Picture #1

Click to see the Picture #2

推荐答案

您可以使用其他变量跟踪低点并使用ta.valuewhen()获取所需的值来获取这些值。

//@version=5
indicator("lowest between conditions", overlay = true)

long_condition = ta.crossover(ta.ema(close, 13), ta.ema(close, 30))

var float lowest_since_long = na
var int lowest_since_long_index = na

if long_condition
    lowest_since_long := low
    lowest_since_long_index := bar_index
else
    if low < lowest_since_long
        lowest_since_long := low
        lowest_since_long_index := bar_index


prev_long_low = ta.valuewhen(long_condition, lowest_since_long[1], 0)
prev_long_index = ta.valuewhen(long_condition, lowest_since_long_index[1], 0)

l = line.new(x1 = prev_long_index, y1 = prev_long_low, x2 = bar_index, y2 = prev_long_low, color = color.red)
line.delete(l[1])


plotshape(long_condition, color = color.lime, location = location.belowbar, size = size.small, style = shape.triangleup)

978