indicator('Slope Adaptive Moving Average (MZ SAMA)', shorttitle='MZ SAMA', overlay=true)
chartResolution = input.timeframe('', title='Chart Resolution')
src = input.source(close, 'Source')
string grp_1 = 'SAMA Length Inputs'
length = input(200, title='Adaptive MA Length', group = grp_1)
majLength = input(14, title='Major Length', group = grp_1)
minLength = input(6, title='Minor Length', group = grp_1)
string grp_2 = 'Slope and Dynamic Coloring Parameters'
slopePeriod = input.int(34, title='Slope Period', group = grp_2)
slopeInRange = input.int(25, title='Slope Initial Range', group = grp_2)
flat = input.int(17, title='Consolidation area is when slope below:', group = grp_2)
bull_col = input.color(color.green, 'Bull Color ', inline='dyn_col', group = grp_2)
bear_col = input.color(color.red, 'Bear Color ', inline='dyn_col', group = grp_2)
conc_col = input.color(color.yellow, 'Reversal/Consolidation/Choppiness Color ', inline='dyn_col', group = grp_2)
showSignals = input.bool(true, title='Show Signals on Chart', group='Plot Parameters')
calcslope(_ma, src, slope_period, range_1) =>
pi = math.atan(1) * 4
highestHigh = ta.highest(slope_period)
lowestLow = ta.lowest(slope_period)
slope_range = range_1 / (highestHigh - lowestLow) * lowestLow
dt = (_ma[2] - _ma) / src * slope_range
c = math.sqrt(1 + dt * dt)
xAngle = math.round(180 * math.acos(1 / c) / pi)
maAngle = dt > 0 ? -xAngle : xAngle
maAngle
dynColor(_flat, slp, col_1, col_2, col_r) =>
var col = color.new(na,0)
col := slp > _flat ? col_1:
slp <= -_flat ? col_2:
slp <= _flat and slp > -_flat ? col_r : col_r
col
ama(src,length,minLength,majLength)=>
minAlpha = 2 / (minLength + 1)
majAlpha = 2 / (majLength + 1)
hh = ta.highest(length + 1)
ll = ta.lowest(length + 1)
mult = hh - ll != 0 ? math.abs(2 * src - ll - hh) / (hh - ll) : 0
final = mult * (minAlpha - majAlpha) + majAlpha
final_alpha = math.pow(final, 2)
var _ama = float(na)
_ama := (src - nz(_ama[1])) * final_alpha + nz(_ama[1])
_ama
sama = request.security(syminfo.tickerid, chartResolution, ama(src,length,minLength,majLength))
slope = calcslope(sama, src, slopePeriod, slopeInRange)
sama_col = request.security(syminfo.tickerid, chartResolution, dynColor(flat, slope, bull_col, bear_col, conc_col))
plot(sama, 'MZ SAMA', sama_col, 4)
_up = sama_col == bull_col
_downn = sama_col == bear_col
_chop = sama_col == conc_col
buy = _up and not _up[1]
sell = _downn and not _downn[1]
chop_zone = _chop and not _chop[1]
_signal() =>
var sig = 0
if buy and sig <= 0
sig := 1
if sell and sig >= 0
sig := -1
sig
sig = _signal()
longsignal = sig == 1 and (sig != 1)[1]
shortsignal = sig == -1 and (sig != -1)[1]
atrOver = 1 * ta.atr(5)
plotshape(showSignals and longsignal ? (sama - atrOver) : na , style=shape.triangleup, color=color.new(color.green, 30), location=location.absolute, text='Buy', size=size.small)
plotshape(showSignals and shortsignal ? (sama + atrOver): na , style=shape.triangledown, color=color.new(color.red, 30), location=location.absolute, text='Sell', size=size.small)
alertcondition(longsignal, "Buy", "Go Long" )
alertcondition(shortsignal, "Sell", "Go Short")
alertcondition(chop_zone, "Chop Zone", "Possible Reversal/Consolidation/Choppiness")
if longsignal
alert("Buy at" + str.tostring(close), alert.freq_once_per_bar_close)
if shortsignal
alert("Sell at" + str.tostring(close), alert.freq_once_per_bar_close)