jackterrapin
Pine Script Rookie
Pine Script Rookie
Posts: 2
Joined: March 14th, 2023
Contact: TradingView Profile

eliminate multiple signals printing

Hi everybody, I've been using pine script for a while now and I am fairly new to coding I've been able to do everything that I want to do with pine script so far but this problem I have not been able to fix on my own so I would really appreciate some help.

this is a simple indicator script below that has two conditions to print a signal, pivot points and moving average. the problem I'm having is that I only want the indicator to print a buy or sell signal once per pivot point, at the moment it prints everytime the conditions are met. I am sure that it will be a loop of some kind to fix this but i cant seem to get my head around it. cheersImage

//@version=5
indicator(title='Pivot Breakout + ma', shorttitle='pvt Breakout + ma', overlay=true)

pvtLenL = input.int(3, minval=1, title='Pivot Length Left Hand Side')
pvtLenR = input.int(3, minval=1, title='Pivot Length Right Hand Side')
maxLvlLen = input.int(0, minval=0, title='Maximum Extension Length')
ShowHHLL = input(false, title='Show HH,LL,LH,HL Markers On Pivots Points')
WaitForClose = input(false, title='Wait For Candle Close Before Printing Pivot') // <-- changed boolean value to false

// Get High and Low Pivot Points
pvthi_ = ta.pivothigh(high, pvtLenL, pvtLenR)
pvtlo_ = ta.pivotlow(low, pvtLenL, pvtLenR)

// Force Pivot completion before plotting.
Shunt = WaitForClose ? 1 : 0
pvthi = pvthi_[Shunt]
pvtlo = pvtlo_[Shunt]

// ||-----------------------------------------------------------------------------------------------------||
// ||--- Higher Highs, Lower Highs, Higher Lows, Lower Lows -------------------------------------------||
valuewhen_1 = ta.valuewhen(pvthi, high[pvtLenR + Shunt], 1)
valuewhen_2 = ta.valuewhen(pvthi, high[pvtLenR + Shunt], 0)
higherhigh = na(pvthi) ? na : valuewhen_1 < valuewhen_2 ? pvthi : na
valuewhen_3 = ta.valuewhen(pvthi, high[pvtLenR + Shunt], 1)
valuewhen_4 = ta.valuewhen(pvthi, high[pvtLenR + Shunt], 0)
lowerhigh = na(pvthi) ? na : valuewhen_3 > valuewhen_4 ? pvthi : na
valuewhen_5 = ta.valuewhen(pvtlo, low[pvtLenR + Shunt], 1)
valuewhen_6 = ta.valuewhen(pvtlo, low[pvtLenR + Shunt], 0)
higherlow = na(pvtlo) ? na : valuewhen_5 < valuewhen_6 ? pvtlo : na
valuewhen_7 = ta.valuewhen(pvtlo, low[pvtLenR + Shunt], 1)
valuewhen_8 = ta.valuewhen(pvtlo, low[pvtLenR + Shunt], 0)
lowerlow = na(pvtlo) ? na : valuewhen_7 > valuewhen_8 ? pvtlo : na


// Count How many candles for current Pivot Level, If new reset.
counthi = ta.barssince(not na(pvthi))
countlo = ta.barssince(not na(pvtlo))
pvthis = fixnan(pvthi)
pvtlos = fixnan(pvtlo)
hipc = ta.change(pvthis) != 0 ? na : color.yellow
lopc = ta.change(pvtlos) != 0 ? na : color.blue

//top and bottom level plot variable declaration
pivothigh = maxLvlLen == 0 or counthi < maxLvlLen ? pvthis : na
pivotlow = maxLvlLen == 0 or countlo < maxLvlLen ? pvtlos : na


//plot levels
plot(pivothigh, color=hipc, linewidth=1, offset=0, title='pivot high')
plot(pivotlow, color=lopc, linewidth=1, offset=0, title='pivot low')

// moving average
len = input.int(75, minval=1, title="Length", group = "MA")
src = input(close, title="Source", group="MA")
smma = 0.0
sma = ta.sma(src, len)
smma := na(smma[1]) ? sma : (smma[1] * (len - 1) + src) / len
plot(smma, title="MA", color=#06f8f8)

// signals
BUY = ta.crossover(close, pivothigh) and close > smma
SELL = ta.crossunder(close, pivotlow) and close < smma

// //plot buy/sell signal
plotshape(BUY, title='BUY', style = shape.labelup, color=#dc06f8, text = "BUY", textcolor=color.white, location=location.belowbar, size=size.tiny)
plotshape(SELL, title='SELL', style = shape.labeldown, color=#dc06f8, text = "SELL", textcolor=color.white, location=location.abovebar, size=size.tiny)

heres a picture of what is happening
https://imgbox.com/yCSjLgKf

and here is the desired outcome of the script
https://imgbox.com/IZKrEE4R

(noticehow it breaks through the pivot high a second time while above the moving average but does not print second buy signal)
Last edited by jackterrapin on Wed Jun 28, 2023 7:15 am, edited 1 time in total.

Steve Burman
Moderator
Moderator
Posts: 109
Joined: January 13th, 2023

Re: eliminate multiple signals printing

Hi Jack,

Sorry for the delay. The images you sent are located on your hard drive. Can you pls share them on a file sharing platform such as imgbox.io so I can see what you mean?

jackterrapin
Pine Script Rookie
Pine Script Rookie
Posts: 2
Joined: March 14th, 2023
Contact: TradingView Profile

Re: eliminate multiple signals printing

Steve Burman wrote:
Thu Jun 22, 2023 1:08 am
Hi Jack,

Sorry for the delay. The images you sent are located on your hard drive. Can you pls share them on a file sharing platform such as imgbox.io so I can see what you mean?
Hi Steve, I have uploaded the photos into the original post they should be available on imgbox.com.
Cheers

Steve Burman
Moderator
Moderator
Posts: 109
Joined: January 13th, 2023

Re: eliminate multiple signals printing

Jack I need you to send me the imgbox link to the photos

Return to “Pine Script Q&A”