SP moving average manual
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © jackdriscoll777

//@version=5
indicator("SP moving average manual")

final_count = 52 * 5 // 5 years back of weekly candles
count = 0
i_n = 0
total = 0.0
for i = 0 to final_count
    gain = (close[i] - open[i]) / open[i] * 100
    if gain > 0 // if it was a positive week, increase count and total
        count := count + 1
        total := total + gain
    
        
average = total / count

plot(average)
        