SP Weekly Gain MA
// 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 Weekly Gain MA")

years_back = 5
candles_back = 52 * years_back // weeks in a year

i_n = 0
gain = (close[i_n] - open[i_n]) / (open[i_n]) * 100
while gain < 0
    i_n := i_n + 1
    gain := (close[i_n] - open[i_n]) / (open[i_n]) * 100
    

ma = ta.sma(gain, candles_back)
plot(ma)
