简体   繁体   中英

Pinescript -Plot happening on every bar, not just highest and lowest

I am trying to have the highest and lowest histogram bars in the last 140 indicated with a plotshape. My code is working a bit too enthusiastically because it is plotting on every bar instead of just the highest and lowest.

I have played around with it for a while and searched for answers with no success. Would love some suggestions please if you have some time to spare.

Thanks Ali

study("Oscillator (AO)")
nLengthSlow = input(34, minval=1, title="Length Slow")
nLengthFast = input(5, minval=1, title="Length Fast")
xSMA1_hl2 = sma(hl2, nLengthFast)
xSMA2_hl2 = sma(hl2, nLengthSlow)

//indicator
AOval = xSMA1_hl2 - xSMA2_hl2

// Determine colour
lineColour = (AOval > AOval[1]) and (AOval > 0) ? lime :
            (AOval < AOval[1]) and (AOval > 0) ? green :
                (AOval > AOval[1]) and (AOval < 0) ? red :
                maroon

UPpeak = highest(AOval, 140) and (AOval > 0)
DNpeak = lowest(AOval, 140) and (AOval < 0)

plot(AOval, style=histogram, linewidth=3, color=lineColour)

plotshape(UPpeak, title="UPpeak", text="3", style=shape.circle, location=location.bottom, color=blue, size=size.auto, transp=60)
plotshape(DNpeak, title="DNpeak", text="3", style=shape.circle, location=location.bottom, color=orange, size=size.auto, transp=60)

highest() and lowest() functions return series . You then "and" this result with a condition. Which looks like this: 150 and true . As a result, UPpeak or DNpeak variables stay true for some period.

What you can do is, check if the current value of AOval is equal to the highest/lowest within the last 140 bars. This way, you would know that that bar at that point is the peak.

UPpeak = (AOval == highest(AOval, 140)) and (AOval > 0)
DNpeak = (AOval == lowest(AOval, 140)) and (AOval < 0)

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM