Persistency - Days over MA
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © kulturdesken

//@version=4

study(title="Persistency - Days over MA", shorttitle="Over MA", overlay=false)

// Input parameters
lookback_period = input(125, "Lookback Period")
ma_period = input(65, "MA Period")

// Calculate the moving average
ma = sma(close, ma_period)

// Initialize the counter
over_ma_days = 0

// Loop through the past days
for i = 0 to lookback_period - 1
    // Check if price is above the moving average
    if close[i] > ma[i]
        over_ma_days := over_ma_days + 1

// Plot the result
plot(over_ma_days, title="Over MA Days", color=color.gray, linewidth=1)