0

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:

enter image description here 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.

RFA_
  • 37
  • 4

1 Answers1

1

To get the highest value of your 'average' variable for a given number of bars, you should use :

ta.highest(average, length)

if you want to know the highest value of the 'average' since the beginning of your chart, you can use :

ta.highest(average, bar_index)
G.Lebret
  • 2,826
  • 2
  • 16
  • 27
  • First, let me thank you for the answer. While it does compute the highest value of average, this does not satisfy the needs of my code. Let me explain why: Since I want to plot **average_normalized** and it uses the highest value of average **up to that bar**, the highest value is different in the beginning of the chart than the end of it. I would need some code that first computes all the values of average, it assigns the absolute highest value across all bars, and then uses that to calculate average_normalized. I would encourage you to run the code in the original question. Thanks – RFA_ Dec 21 '22 at 13:31
  • 1
    @RFA_ in pinescript, your script is executed at each bar. So at bar 10 (for example), with ta.highest(average, bar_index) you will have the highest average for those 10 first bars. Isn't it what you want' ? – G.Lebret Dec 21 '22 at 17:00
  • No, that is precisely not what I want! Since I want to normalize the values of average between 1 and 0, and plot them, highest and lowest must be the highest of all values, not up to that point. For now I have been working it around by manually writing the extremes, but that takes a lot of time. Pushing to an array also does not work, since at bar 10 you have the highest up to 10 (same problem). I was thinking that using a loop to compute all averages values and assign the highest (and lowest) to some variable could do the trick, but I have struggled to write the code for that idea. – RFA_ Dec 21 '22 at 21:07
  • 1
    You should completely edit and clarify your question : it is not clear at all. Gives us examples, what you get so far, what you expect, so we can help you – G.Lebret Dec 21 '22 at 22:45
  • Just updated with clearer information. – RFA_ Dec 22 '22 at 13:43