April 24, 2019 at 8:07 am #52769
Hi there, I am writing a show me and want to reference a value on a particular bar when a certain date is met via a calculation. I want to then use this same value on every bar from this point. However, the value keeps changing as each new bar is encountered, and does not keep the original value that was set, is there a way to carry values forward and prevent them from changing with each new bar?April 24, 2019 at 8:38 am #52771
The best way to do this would be using the VALUEWHEN() function and the FIRST() function.
The following is a general example of how it can be used:12345678910111213//Set Moving AveragesV1 = MA(BARS=25);V2 = MA(BARS=50) ;//Find MA Cross OverV3 = V1 CrossesBelow V2 ;//Set RSI ValueV4 = RSI() ;//Find Value of RSI with MA Cross OccursV5 = VALUEWHEN(V4,V3);//Set the script to show the RSI value at the first MA Crossover on the chart then set as a static valueFIRST(NONZERO(V5))
The above script would set the indicator value to the RSI’s value when the first Moving Average crossover occurred and it would not change.
Red Dashed line is produced from the example script listed above.April 25, 2019 at 7:01 am #52783
Great, thanks Matthew, perfect I will attempt to apply the first function, is it also possible to assign a variable several values.
a1 = 5
a2 = 7
a3 = 8
b1 = a1 or a2 or a3;
plot0 = b1;
I saw the possibility to use #a1 to pass variables, but not sure what the difference is between #a1 or just declaring a1, and also not sure on how to assign the same variable to several values. I am currently using:
plot0 = a1
plot1 = a2
plot2 = a3 ect, but I run out of plots by the time I get to plot13 🙂April 26, 2019 at 7:17 am #52789
A variable can only display one value at a time, but you can swap between which value is being plotted using an IF() function.
For example:12345V1 = MA(BARS=20) ;V2 = MA(BARS=50) ;plot1 = IF(V1 > V2,V1,V2)
In this example the plot would swap between the V1 and V2 values based on the rule specified (V1 being greater than V2). This is how it looks on a chart…
You can see the red dashed line (script) swaps between following the 20SMA and 50SMA.April 26, 2019 at 7:46 am #52791
Thank you, is there a way I can assign several values, there are 20+ values I would like to plot, or is the only way to do nested if statements ?
MarioApril 26, 2019 at 8:07 am #52793
Scripts support plot1, plot2, etc to show multiple values. If you are wanting Plot1 to show several different values (not at the same time but swapping between them on a bar by bar basis), the only way is to use IF() / nested IF() where rules are outlined on which of the multiple values should be displayed and when.
- You must be logged in to reply to this topic.