[英]How to get the peak that is closest to the current price in Pinescript
我正在尝试获取最接近当前价格、高于当前价格且在最后 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)
在这种情况下,当您想要根据 for 循环的结果修改现有变量(如targetprice
)时,您必须使用:=
而不是=
。
尝试将targetprice = targetpricenew
替换为targetprice:= targetpricenew
。 如果您针对要执行的操作正确编写了逻辑代码,这将使您的脚本绘制出正确的目标价格!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.