excessivechaos
New member
Hello, can anyone tell me why this code does not work?
When I try to use the barsPerYear variable as the length parameter of the Lowest() and Highest() functions I get the following error:
Only constants expected here: barsPerYear CL constant function parameter 'length' at 48:23
Only constants expected here: barsPerYear CL constant function parameter 'length' at 48:23
If I create an input parameter to allow the user to enter the number of bars in a year. I can use that variable in the Highest() and Lowest() functions. I have also tried other methods for determining the number of bars in a year and that fails as well.
BTW, I don't think it matters much what the variable spread is in case you are wondering. I can put any value here, such as 1 and it will still error.
What I'm trying to do here is determine how many bars make up a year of data, which will change depending on the aggregation period. Right now I have the user set the agg period as input, but I wanted to code the dynamically. This is helpful when there isn't a years worth of data (new stocks) and the indicator fails because it's trying to do a calc based on having 252 bars. I just don't understand why this doesn't work. But the following code works fine:
Code:
def currentDate = GetYYYYMMDD();
def yearBack = currentDate - 10000;
def barsPerYear = fold i = 0 to 253 with count = -1 while currentDate[i] >= yearBack do count + 1;
def lowestSpread = Lowest(spread, barsPerYear);
def highestSpread = Highest(spread, barsPerYear);
When I try to use the barsPerYear variable as the length parameter of the Lowest() and Highest() functions I get the following error:
Only constants expected here: barsPerYear CL constant function parameter 'length' at 48:23
Only constants expected here: barsPerYear CL constant function parameter 'length' at 48:23
If I create an input parameter to allow the user to enter the number of bars in a year. I can use that variable in the Highest() and Lowest() functions. I have also tried other methods for determining the number of bars in a year and that fails as well.
BTW, I don't think it matters much what the variable spread is in case you are wondering. I can put any value here, such as 1 and it will still error.
What I'm trying to do here is determine how many bars make up a year of data, which will change depending on the aggregation period. Right now I have the user set the agg period as input, but I wanted to code the dynamically. This is helpful when there isn't a years worth of data (new stocks) and the indicator fails because it's trying to do a calc based on having 252 bars. I just don't understand why this doesn't work. But the following code works fine:
Code:
input AggregationPeriod = {default Daily, Weekly, Monthly, Annual};
def ap;
switch (AggregationPeriod) {
case Daily:
ap = 252;
case Weekly:
ap = 52;
case Monthly:
ap = 12;
case Annual:
ap = 1;
}
def lowestSpread = Lowest(spread, ap);
def highestSpread = Highest(spread, ap);
Last edited: