How to reference a variable’s most recent bar value


Category:
0
0

Hi, the attached script with input “length” defines variable “n” as a function of length and other variables. In follow-on expressions in the script I want to use n’s last, most recent value, not values from all its previous bars. I’m not sure how to do this. Any help would be appreciated. Thanks.

Marked as spam
Posted by (Questions: 4, Answers: 6)
Asked on March 27, 2020 12:40 pm
87 views
0
Thank you very much for your response. You always respond quickly and comprehensively and its really appreciated. I will take your advice on clearly named variables. I didn't express the problem correctly. I want the input "length" to be conditioned according to a function. But applying the conditioning expression causes the result to fail. The study works with this expression: def n = length; This expression causes the study to fail: def n = RoundUp((length / (Round(AbsValue(Average(((close / close[ROClength] - 1) * 100), ROClength)), 0))), 0); This expression is designed to calculate an average rate of change, apply absolute value so results are all positive, and round the result to a whole number (for an adjusted length). I only want the value of the last bar to be applied to this calculation to adjust the length input. Hoping you can help. Thanks.
( at March 29, 2020 9:16 pm)
0
Functions that take an input parameter named "length" will not permit you to use a value that is not constant for that parameter. The error produced will explain which function is throwing the error and that it requires a constant value.
( at March 30, 2020 6:59 am)
0
What you stated was exactly correct: "What I perceive you are asking for, "not values from all its previous bars", is that you want to grab the value of "n" for the last bar on the chart. Then use that value for all the bars on the chart." With some trial and error I was able to use the code you supplied. Thanks so much, your responses are much appreciated.
( at March 30, 2020 9:59 pm)
0
Private answer

The direct answer to you question, how to reference the most recent value of a variable.

def lastValueOfN = n;

However since this is such an obvious answer and your code already contains this exact solution I perceive that you asking for something a bit different. What I perceive you are asking for, "not values from all its previous bars", is that you want to grab the value of "n" for the last bar on the chart. Then use that value for all the bars on the chart. If that is the case then this is how you do it:

def lastBar = IsNaN(close[-1]) and !IsNaN(close);
def closeOfLastBar = if lastBar then close else 0;
def fixedValueOfLastBar = HighestAll(closeOfLastBar);

The final line in that snippet will contain the close of the last bar on the chart and that variable will contain that same value for every bar on the chart. As you can see I am a fan of verbose variable names. I don't approve of variable names that are abbreviated more than required. Single character variable names make the code very difficult to read and maintain. I say this for the benefit of the rest of our viewers who are just learning how to write code. You do whatever you want for your own purposes.

Keep in mind that this solution is NOT: "reference a variable’s most recent bar value" as stated in the title of your question. So I really am taking a stab in the dark here. Please provide clarifications if this is not what you are requesting.

Note that this solution requires a variable that is never less than or equal to zero. If you have a variable that can vary above or below zero you will have to employ some additional methods to make this work.

 

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on March 27, 2020 3:31 pm