Convert this pine script to mql4: //@version=5 indicator("Triple Kaufman Adaptive Moving…
- User
Convert this pine script to mql4: //@version=5
indicator("Triple Kaufman Adaptive Moving Average", shorttitle="Triple KAMA", overlay=true)
// Input parameters for KAMA 1
length1 = input.int(3, title="KAMA 1 Length", minval=1)
fastLength1 = input.int(5, title="KAMA 1 Fast EMA Length", minval=1)
slowLength1 = input.int(30, title="KAMA 1 Slow EMA Length", minval=1)
// Input parameters for KAMA 2
length2 = input.int(3, title="KAMA 2 Length", minval=1)
fastLength2 = input.int(5, title="KAMA 2 Fast EMA Length", minval=1)
slowLength2 = input.int(30, title="KAMA 2 Slow EMA Length", minval=1)
// Input parameters for KAMA 3
length3 = input.int(3, title="KAMA 3 Length", minval=1)
fastLength3 = input.int(5, title="KAMA 3 Fast EMA Length", minval=1)
slowLength3 = input.int(30, title="KAMA 3 Slow EMA Length", minval=1)
// Custom sum function
sum(src, length) =>
s = 0.0
for i = 0 to length - 1
codes := s + src[i]s
// Function to calculate KAMA
kama(src, length, fast, slow) =>
mom = math.abs(ta.change(src, length))
volatility = sum(math.abs(ta.change(src)), length)
er = volatility != 0 ? mom / volatility : 0
fastAlpha = 2 / (fast + 1)
slowAlpha = 2 / (slow + 1)
alpha = math.pow(er * (fastAlpha - slowAlpha) + slowAlpha, 2)
var float kama = na
kama := na(kama) ? src : alpha * src + (1 - alpha) * nz(kama[1], src)
kama
// Calculate KAMAs
kama1 = kama(close, length1, fastLength1, slowLength1)
kama2 = kama(kama1, length2, fastLength2, slowLength2)
kama3 = kama(kama2, length3, fastLength3, slowLength3)
// Plot KAMAs
plot(kama1, title="KAMA 1", color=color.blue, linewidth=2)
plot(kama2, title="KAMA 2", color=color.green, linewidth=2)
plot(kama3, title="KAMA 3", color=color.red, linewidth=2)
// Detect crossovers
crossover = ta.crossover(kama1, kama3)
crossunder = ta.crossunder(kama1, kama3)
// Plot arrows
plotshape(crossover, title="Up Arrow", location=location.belowbar, color=color.green, style=shape.triangleup, size=size.small)
plotshape(crossunder, title="Down Arrow", location=location.abovebar, color=color.red, style=shape.triangledown, size=size.small)
// Alert conditions
alertcondition(crossover, title="KAMA 1 crosses above KAMA 3", message="KAMA 1 has crossed above KAMA 3!")
- Assistant