//@version=5
indicator("MA Crossover Cloud", shorttitle="MA-X", overlay=true)
// --- 1. Define Inputs ---
// Input for the source price
src = input.source(close, "Price Source")
// Inputs for the MA lengths
length1 = input.int(10, "Length 1 (Faster MA)", minval=1)
length2 = input.int(20, "Length 2 (Slower MA)", minval=1)
// New input for the Alpha parameter, used ONLY by HWEMA
alphaInput = input.float(1.0, "HWEMA Alpha (Smoothing)", minval=0.01, maxval=1.0, step=0.01, group="HWEMA Settings")
// Input for selecting the MA types, now including HWEMA
typeOptions = input.string(title="Average Type Selection",
options=["SMA", "EMA", "WMA", "HullMA", "WildersMA", "HWEMA"],
defval="EMA")
// --- 2. Moving Average Custom Functions ---
// Hull Weighted Exponential Moving Average (HWEMA) function
hwema(src, len, alpha) =>
wma = ta.wma(src, len)
// The wema calculation needs to be slightly refactored for Pine Script v5 variable declaration rules
var wema = 0.0
wema := alpha * wma + (1 - alpha) * nz(wema[1])
ta.hma(wema, len)
// Universal MA selector function, now includes HWEMA
getMA(type, source, length, alpha) =>
switch type
"SMA" => ta.sma(source, length)
"WMA" => ta.wma(source, length)
"WildersMA"=> ta.rma(source, length)
"HullMA" => ta.hma(source, length)
"HWEMA" => hwema(source, length, alpha)
=> ta.ema(source, length)
// --- 3. Calculate MAs ---
ma1 = getMA(typeOptions, src, length1, alphaInput)
ma2 = getMA(typeOptions, src, length2, alphaInput)
// --- 4. Define Colors and Plotting ---
// Define standard colors for up/down
lineUpColor = color.lime
lineDownColor = color.red
lineNeutralColor = color.gray
// --- UPDATED SLOPE-BASED COLOR LOGIC ---
// 🎯 Color MA1 based on its slope: rising (green) or falling (red)
// Checks if the current MA value is greater/less than the previous bar's value (ma1[1])
ma1Color = ma1 > ma1[1] ? lineUpColor : ma1 < ma1[1] ? lineDownColor : lineNeutralColor
// 🎯 Color MA2 based on its slope: rising (green) or falling (red)
ma2Color = ma2 > ma2[1] ? lineUpColor : ma2 < ma2[1] ? lineDownColor : lineNeutralColor
// Plot the two moving averages with dynamic slope-based colors
p1 = plot(ma1, title="MA 1", color=ma1Color, linewidth=2)
p2 = plot(ma2, title="MA 2", color=ma2Color, linewidth=2)
// --- 5. Cloud Plotting (Crossover Logic Maintained) ---
// Define cloud colors (using slightly transparent versions of the line colors)
cloudUpColor = color.new(color.green, 85)
cloudDownColor = color.new(color.red, 85)
// The cloud remains based on the crossover relationship (MA1 > MA2)
fillColor = ma1 > ma2 ? cloudUpColor : cloudDownColor
fill(p1, p2, color=fillColor, title="MA Cloud")