【问题标题】:How to get the peak that is closest to the current price in Pinescript如何在Pinescript中获取最接近当前价格的峰值
【发布时间】:2022-12-14 05:01:14
【问题描述】:

我正在尝试获取最接近当前价格、高于当前价格且在最后 15 根蜡烛内的峰值价格

例如,最后 15 根蜡烛中可能有 3 个峰值。我试图对它们进行排序以获得最接近最近蜡烛价格的峰值价格,并且高于最近蜡烛的价格,无论最近发生哪个峰值

我试着设置它,但目前它正在绘制图表的收盘价而不是目标价

--

如何获取最接近当前价格、高于当前价格且在最近 15 根蜡烛内的峰值价格?

--

到目前为止的代码:

//@version=5
indicator(title="peak", overlay = true)

peak = close[0] < close[1] and close[1] > close[2]

////previouscandle = ta.valuewhen(peak, close[1], 0)
////barssince_last_peak = ta.barssince(peak)

////targetPrice = barssince_last_peak <= 15 and barssince_last_peak > 0 ? previouscandle : na


startprice = close
targetprice = close

//loop through the last 15 candles
for i=0 to 15-1
    
    //if a peak price is greater than the start price set its peak price to the targetpricenew variable
    targetpricenew = ta.valuewhen(peak and close[1] > startprice, close[1], 0)
    
    // if the distance between targetpricenew's peak is less than the distance between the current targetprices's peak 
    if ( targetpricenew - startprice ) < ( targetprice - startprice )

        //Set the targetpricenew peak as the targetprice
        targetprice = targetpricenew


//plot the targetprice
plot(targetprice, style = plot.style_linebr)


【问题讨论】:

    标签: pine-script pinescript-v5 pine-script-v4


    【解决方案1】:

    当你有一个现有的变量,如targetprice,你想根据你的for循环的结果在这种情况下修改,你必须使用:=而不是=

    尝试用 targetprice := targetpricenew 替换 targetprice = targetpricenew。 如果您针对要执行的操作正确编写了逻辑代码,这将使您的脚本绘制出正确的目标价格!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-13
      • 2019-05-31
      • 1970-01-01
      • 1970-01-01
      • 2016-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多