Page 1 of 1

Set a fixed 0 to 100 range for a plotted line

Posted: Wed Apr 13, 2022 8:08 pm
by Alex100
Is it possible to change a plotted indicator line so that it will always fluctuate in an exact range, say 0 to 100 ?

As an example, here's this basic script that plots the difference between a stock closing in green, or in red.

Code: Select all

//@version=5
indicator("My Script")

var n = 0

if (close > close[1])
    n += 1
else
    n -= 1

plot (n)

To make this script plot a line that is always fixed in a 0 to 100 range, you first need to find the low and the high of the 'n' series variable, and then apply some basic math to all the values of the series. In return, this will make the plotted line preserve its original shape, but also keep it bound into the same range, no matter what stock symbol you choose to check. As it is now, its high-low range differs wildly from one stock symbol to another.

My problem wasn't the math, but the fact that I could not find a solution to change the values of PAST bars and plot NEW ones. That was the problem. As you may know, past values of series variables are non-editable ('n [ 1 ] := x' does not work!), and I could not find any workaround for this issue.

Any solutions, please?

Alex

Re: Set a fixed 0 to 100 range for a plotted line

Posted: Fri Apr 15, 2022 11:15 pm
by Shenpai
Im pretty new to pine script so please be patient with me if I'm totally wrong.

My workaround ideas:

1) Wouldnt it be possible to get all values at the last bar e.g. via array and then plotting them with offset -1,-2,etc ?
At the last bar it should be able to calculate ur range cause u know the absolut high and the absolute low

2) Plot your weird numbers and add an additional scale at the left side from 0 to 100. Would be just visual maybe thats what you want.

3) If I understood you right, the problem is that in older bars pine does not know the future absolute high and low.
Wouldnt it be possible that you as a user type it via user input? Would be just writing two numbers, if they do not change to often it could be a possibility

Re: Set a fixed 0 to 100 range for a plotted line

Posted: Sat Apr 16, 2022 2:40 pm
by Alex100
Thank you for your answer, Shenpai!

> If I understood you right, the problem is that in older bars pine does not know the future absolute high and low.

Yes, you are right.

In regards to your second and third ideas, they may work from a visual point of view, but it's not what I'm looking for. In short, I have a few signal lines that I'd like to average into a single, unified line. But considering the high-low ranges differ very much for these multiple lines, I'd first like to set the same range for each of them, and only then average them into a single line. This is the best solution to have an equal-weighted picture of all the lines that make up the average. Otherwise, the average will give greater weight to the line with the highest value and skew the average shape in its favour.

Your first idea might be what I'm looking for, but I'm not sure how to code that - I tried plotting the elements of an array, but without any success. I'm also new to Pine.

Yes, by knowing the high and the low at the last bar, I would be able to recalculate all the past values of a plotted line so that the line will have a 0 - 100 range. The main problem is finding the way to plot the line (or the unified average line, in my case) with the new, recalculated values. That's where I'm stuck! ;-) If you believe you can transform your first idea into any working code that gets this job done, I would appreciate if you could list it here. I'll take care of the rest. Storing the values in an array and using the offset option is fine with me, as long as it solves this issue.

Alex

Re: Set a fixed 0 to 100 range for a plotted line

Posted: Sun Apr 17, 2022 5:49 pm
by Shenpai
Two more questions before it gets complicated.

1) I guess it's not enough to get just one value (the present one) out of your desired single line. So you need the whole line with all its historic values?

2) Another option would be similar to what I already suggested: Wouldnt it be possible to manual input all max and min values? For ten signals this would be 20 values, I know that kinda sucks.

I want to experiment a bit if i could find another solution but I'm not too hopeful to be honest

Re: Set a fixed 0 to 100 range for a plotted line

Posted: Sun Apr 17, 2022 8:27 pm
by Shenpai
I spent some time with finding a solution but it's definetely complicated and I learned a lot about arrays today.

Still I couldn't find out how to put plot function into a loop so this is the part where still some manual work has to be done
Also the data is not drawn as a line but as single values. I think that can be fixed easily.

I tried my idea with the closing price. This is how far I got:

Code: Select all

//
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Shenpaiz

@version=5
indicator("try multi index 100")

//counts the bars
barcount = bar_index + 1

//defines a new array which will later include close data
//"100" could be replaced by a much higher number like "999999" 
//the exact number doesnt matter, just needs to be high enough
//replace with barcount does not work here
a = array.new_float(100)

//checking if the current bar is the present one. must be set manually right now
//getting all close prices and store them in array "a"
if bar_index == 49
    for i = 0 to barcount-1
	    array.set(a, i, close[i])

//getting min and max values from array "a" to calculate the "100index"
max = array.max(a)
min = array.min(a)

//plotting the data backwards
//plot(max)
plot((array.get(a, 0)-min)*100/(max-min))
plot((array.get(a, 1)-min)*100/(max-min), offset=-1)
plot((array.get(a, 2)-min)*100/(max-min), offset=-2)
plot((array.get(a, 3)-min)*100/(max-min), offset=-3)
plot((array.get(a, 4)-min)*100/(max-min), offset=-4)

Re: Set a fixed 0 to 100 range for a plotted line

Posted: Mon Apr 18, 2022 4:30 pm
by Alex100
Thank you for taking the time to write the code! I'm happy to hear you've also learned new things about arrays.

> So you need the whole line with all its historic values?

Definitely yes, as I look at trends, divergences, etc.

> Wouldnt it be possible to manual input all max and min values? For ten signals this would be 20 values, I know that kinda sucks.

I'm afraid this is also out of the question, because I look at tens of stocks, not only one or two. Their charts must load fairly quickly, as they normally do.

> Still I couldn't find out how to put plot function into a loop so this is the part where still some manual work has to be done

Given the limited options we have, I guess this is not a major issue!

> Also the data is not drawn as a line but as single values. I think that can be fixed easily.

To fix this, are you thinking about using TradingView's "Add indicator/strategy" function on your script? That means a second script that would just plot a line based on all the values individually plotted by your script. I guess this should work, shouldn't it? Anyhow, it's important the data is drawn as a line, not as single values. That's yet another condition that I can't pass.

One more thing... the script, as it is now, doesn't plot anything. It does display the values in the top-left corner of the chart (next to the script name), but that's all. No plotting at all - i checked multiple stocks, reset the scale, etc.

Also, if I drop out your formulas and expect it to use only the closing prices, like this ...

Code: Select all

plot(array.get(a, 0))
plot(array.get(a, 1), offset=-1)
plot(array.get(a, 2), offset=-2)
plot(array.get(a, 3), offset=-3)
plot(array.get(a, 4), offset=-4)

... the values displayed in the corner are not the actual closing prices of the stock. They are very much different.

Alex