change length to 70 //@version=4
strategy(title="Magic MA Strategy", shorttitle="Magic MA Strat", overlay=true, max_bars_back=2000)

// 
// Description:
// A trend-following strategy that uses fast QQE crosses with Moving Averages
// for trend direction filtering.
//

// - INPUTS START
// Slow MA - type, source, length
type3   = input(defval="HullMA", title="Slow MA Type: ", options=["SMA", "EMA", "WMA", "VWMA", "SMMA", "DEMA", "TEMA", "HullMA", "ZEMA", "TMA", "SSMA"], type=input.string)
len3    = input(defval=37, title="Slow Length", minval=1)  // Updated default slow length to 37

// QQE RSI Length, Smoothing, fast ATR factor, source
RSILen  = input(6,title='RSI Length')
SF      = input(3,title='RSI Smoothing Factor')
QQE     = input(2.618,title='Fast QQE Factor')
threshhold = input(10, title="RSI Threshhold")

filter  = input(false, title="Use Moving Average Filter")
dfilter = input(false, title="Use Trend Directional Filter")
ushunt  = input(false, title="Use Shunt Alerts to prevent Repaint")
RSIsrc  = input(close, title="Source")
srcclose= RSIsrc
shunt  = ushunt ? (RSIsrc == open ? 0 : 1) : 0
// - INPUTS END

// - FUNCTIONS
// Returns MA input selection variant, default to SMA if blank or typo.
variant(type, src, len) =>
    v1 = sma(src, len)                                                  // Simple
    v2 = ema(src, len)                                                  // Exponential
    v3 = wma(src, len)                                                  // Weighted
    v4 = vwma(src, len)                                                 // Volume Weighted
    v5 = 0.0
    v5 := na(v5[1]) ? sma(src, len) : (v5[1] * (len - 1) + src) / len    // Smoothed
    v6 = 2 * v2 - ema(v2, len)                                          // Double Exponential
    v7 = 3 * (v2 - ema(v2, len)) + ema(ema(v2, len), len)               // Triple Exponential
    v8 = wma(2 * wma(src, len / 2) - wma(src, len), round(sqrt(len)))   // Hull
    v11 = sma(sma(src, len), len)                                        // Triangular
    // SuperSmoother filter
    // © 2013  John F. Ehlers
    a1 = exp(-1.414 * 3.14159 / len)
    b1 = 2 * a1 * cos(1.414 * 3.14159 / len)
    c2 = b1
    c3 = (-a1) * a1
    c1 = 1 - c2 - c3
    v9 = 0.0
    v9 := c1 * (src + nz(src[1])) / 2 + c2 * nz(v9[1]) + c3 * nz(v9[2])
    // Zero Lag Exponential
    e = ema(v1, len)
    v10 = v1 + (v1 - e)
    // return variant, defaults to SMA if input invalid.
    type == "EMA" ? v2 : type == "WMA" ? v3 : type == "VWMA" ? v4 : type == "SMMA" ? v5 : type == "DEMA" ? v6 : type == "TEMA" ? v7 : type == "HullMA" ? v8 : type == "SSMA" ? v9 : type == "ZEMA" ? v10 : type == "TMA" ? v11 : v1

// - FUNCTIONS END

// - Fast ATR QQE
Wilders_Period = RSILen * 2 - 1

Rsi = rsi(RSIsrc, RSILen)
RSIndex = ema(Rsi, SF)
AtrRsi = abs(RSIndex[1] - RSIndex)
MaAtrRsi = ema(AtrRsi, Wilders_Period)
DeltaFastAtrRsi = ema(MaAtrRsi, Wilders_Period) * QQE

newshortband = RSIndex + DeltaFastAtrRsi
newlongband = RSIndex - DeltaFastAtrRsi
longband = 0.0
shortband = 0.0
trend = 0
longband := RSIndex[1] > longband[1] and RSIndex > longband[1] ? max(longband[1], newlongband) : newlongband
shortband := RSIndex[1] < shortband[1] and RSIndex < shortband[1] ? min(shortband[1], newshortband) : newshortband
trend := cross(RSIndex, shortband[1]) ? 1 : cross(longband[1], RSIndex) ? -1 : nz(trend[1], 1)
FastAtrRsiTL = trend == 1 ? longband : shortband

// Trading logic series variable
var int tradeCount = 0
var int lastTradeDay = na

currentDay = dayofweek(time)

// Reset the trade count at the beginning of a new day
if na(lastTradeDay) or (currentDay != lastTradeDay)
    tradeCount := 0
    lastTradeDay := currentDay

// - SERIES VARIABLES
// MA's
ma_slow = variant(type3, srcclose, len3)

// - STRATEGY LOGIC
entryCondition = crossover(RSIndex, FastAtrRsiTL) and (not filter or (filter and RSIndex > ma_slow)) and tradeCount < 1
exitCondition = crossunder(RSIndex, FastAtrRsiTL) or (filter and RSIndex < ma_slow)

if entryCondition
    strategy.entry("Buy", strategy.long)
    tradeCount := tradeCount + 1

if exitCondition
    strategy.close("Buy")

// - PLOTTING
plot(ma_slow, title="MA Slow", color=color.blue, linewidth=2, transp=0)

//EOF