AggregationPeriods error


Category:
0
0

Hi Pete .Question.

I have a script here I wrote that has no errors before applying, and it won’t plot the labels onto a chart. It keeps saying” Two different secondary periods cannot be used in a single variable” in the upper left hand corner. I don’t get it….I have other indicators that have multiple Aggregation Periods built into one indicator, so how is this any different? . By looking at the code, think you can somewhat tell what I’m trying to create.

Marked as spam
Posted by (Questions: 11, Answers: 10)
Asked on January 10, 2021 3:06 am
63 views
0
AddLabel(yes, ” 1h ”, if close(period=AggregationPeriod.Hour) > Open(period=AggregationPeriod.Hour) then color.green else color.red); AddLabel(yes, ” 30m ”, if close(period=AggregationPeriod.Thirty_Min) > Open(period=AggregationPeriod.Thirty_Min) then color.green else color.red); AddLabel(yes, ” 15m ”, if close(period=AggregationPeriod.Fifteen_Min) > Open(period=AggregationPeriod.Fifteen_Min) then color.green else color.red);
( at January 10, 2021 3:07 am)
1
Private answer

For the rest of our viewers, the author of this post included the code in the comment section immediately below their question. In the future, it is always best to provide the code along with the question. If the code is less than a couple dozen lines, it should be included in the body of the question. If the code exceeds the character limit of the question field than it should be included as an attached plain text file.

Here is the code provided by the author of the post:

AddLabel(yes, ” 1h ”, if close(period=AggregationPeriod.Hour) > Open(period=AggregationPeriod.Hour) then color.green else color.red);
AddLabel(yes, ” 30m ”, if close(period=AggregationPeriod.Thirty_Min) > Open(period=AggregationPeriod.Thirty_Min) then color.green else color.red);
AddLabel(yes, ” 15m ”, if close(period=AggregationPeriod.Fifteen_Min) > Open(period=AggregationPeriod.Fifteen_Min) then color.green else color.red);

I tested this and analysized it carefully. I cannot find any reason why it is producing this error. Each of the three labels work when applied separately but not when two or more are included together.

My own style of writing code is very different. I find is much simpler to split complicated statements into very basic individual pieces. I also like to include user inputs so everyone else that comes across my solutions will be able to adjust them to their liking without having to change the code.

So I wrote this solution in my own way from scratch. And it works.

input timeFrameOne = AggregationPeriod.FIFTEEN_MIN;
input timeFrameTwo = AggregationPeriod.THIRTY_MIN;
input timeFrameThree = AggregationPeriod.HOUR;
def priceCloseOne = close(period = timeFrameOne);
def priceOpenOne = open(period = timeFrameOne);
def priceCloseTwo = close(period = timeFrameTwo);
def priceOpenTwo = open(period = timeFrameTwo);
def priceCloseThree = close(period = timeFrameThree);
def priceOpenThree = open(period = timeFrameThree);
AddLabel(yes, "TF One", if priceCloseOne > priceOpenOne then Color.GREEN else Color.RED);
AddLabel(yes, "TF Two", if priceCloseTwo > priceOpenTwo then Color.GREEN else Color.RED);
AddLabel(yes, "TF Three", if priceCloseThree > priceOpenThree then Color.GREEN else Color.RED);

Believe it or not, this style of writing code is much easier to maintain and easier to read. You can replicate elements in this style of writing code using simple copy/paste and find/replace. Troubleshooting the code is also very simple because each key metric has been listed separately. Sometimes, having more lines of code is easier and simpler. Save the complexity for where it absolutely necessary.

 

Marked as spam
Posted by (Questions: 37, Answers: 4086)
Answered on January 10, 2021 9:43 am