Alex Eñder Safezone For TradigView

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

// SafeZone

//@version=2
study("SafeZone", overlay=true)

coeff = input(2.0, type=float)


lookback = input(10, type=integer)

countShort = high > high[1] ? 1 : 0


diffShort = high > high[1] ? high - high[1] : 0
totalCountShort = sum(countShort, lookback)
totalSumShort = sum(diffShort, lookback)
penAvgShort = (totalSumShort / totalCountShort)
safetyShort = high[1] + (penAvgShort[1] * coeff)
finalSafetyShort = close < finalSafetyShort[ 1 ] ? min( safetyShort,
finalSafetyShort[ 1 ] ) : high[1]

count = low < low[1] ? 1 : 0


diff = low < low[1] ? low[1] - low : 0
totalCount = sum(count, lookback)
totalSum = sum(diff, lookback)
penAvg = (totalSum / totalCount)
safety = low[1] - (penAvg[1] * coeff)
finalSafetyLong = close > finalSafetyLong[ 1 ] ? max( safety,
finalSafetyLong[ 1 ] ) : low[1]

trend = close > finalSafetyShort[ 1 ] ? 1: close < finalSafetyLong[ 1 ] ? -1:


nz( trend[ 1 ], 1 )
lcolor = trend == 1 ? blue : red
value = trend == 1 ? finalSafetyLong : finalSafetyShort

plot(value, color=lcolor)

You might also like