So far I have this code:
indicator("Risk", overlay = false)
fiftyD = ta.sma(close, 50)
fiftyW = ta.sma(close, 350)
average = fiftyD/fiftyW * math.pow(bar_index, 0.350)
max = ta.max(average)
min = ta.min(average)
average_normalized = (average - min) / (max - min)
plot(average_normalized)
And the plot looks like this:
As you can see, average_normalized has a range between 1/0, but it does not work like intended. For the displayed range, the local maximum of average is reached in 2014, and that would correspond to 1, but as seen on the graph average_normalized, it already starts at 1 and that value is reached multiple times. This is not good, and it is because of how I calculate the highest and lowest values:
At every bar, max and min are calculated looking back all the bars before that point, and that is why at the first value, average_normalized has a value of 1 since it is the only value.
The solution would be to assign max and min the absolute highest and lowest values that average reaches, not just the highest and lowest value up to that point.
Now I understand that this behaviour is due to the nature of pinescript, which I learnt is a loop in itself. I think that a possible solution would be to create a for loop that goes through all bars and calculates the values of average, then assigns the max and min to 2 variables. I have been struggling with that.