Since you have made an effort to do this on your own and are trying to learn, I will spend some time explaining and correcting your code.
Issue One
input AP=getaggregationperiod();
is invalid. Why? Because the function named GetAggregationPeriod() reads the current aggregation period the chart is set to. It does not work as an input.
The correct way to include a user input that allows for the selection of an aggregation period:
input timeFrameOne = AggregationPeriod.DAY;
We have dozens of examples of this throughout the Q&A forum.
Issue Two
def daily=if AP=aggregationPeriod.DAY then 1 else 0;
cannot function because it is trying to get it’s value from the statement above, which is invalid. Furthermore, the if/then/else portion is entirely reduntant. Any true/false statement in Thinkorswim will be equal to 1 or 0 respectively. You don’t need to use if/then/else to assign it. In the past I have used this method myself, but did so to make it easier for ordinary humans to read. So you may continue using it. Just realize it is completely unnecessary.
The correct way to write this statement would be:
def daily = timeFrameOne == AggregationPeriod.DAY;
Issue Three
This entire structure is invalid:
if daily equal 1 then
def agg = AggregationPeriod.DAY
def data = close(period = agg)
else double.NaN;
The correct structure (assuming I understand your intention) is this:
def agg;
def data;
if daily == 1 {
agg = AggregationPeriod.DAY;
data = close(period = agg);
} else {
agg = Double.NaN;
data = Double.NaN;
}
Issue Four
The following two statements do not make use of the variable named data which you have previously assigned as the close of the selected aggregation period. These statements are also referencing a non-existent study name:
def RSI = RSIWilder(length = 2, price = close(period = agg )).RSI;
def priorRSI = RSIWilder(length = 2, price = close(period = agg )[1]).RSI;
Instead, I would expect these statements to be written like this:
def RSI = reference RSI(length = 2, price = data).RSI;
def priorRSI = reference RSI(length = 2, price = data[1]).RSI;
(RSIWilder is a non-existent study)
Issue Five
This statement has no purpose whatsoever. It is an island unto itself and should be deleted:
RSI(“length” = 2, “over bought” = 98, “over sold” = 2).”RSI” is less than 2 within 0 bars and (high is less than SimpleMovingAvg(“length” = 5).”SMA” and (low is greater than SimpleMovingAvg(“length” = 200).”SMA”
Issue Six
Your AddLabel statement is close. The problem is you have not properly nested your Concat() statements. This will not work at all because it will generate an error that prevents the code from being displayed:
AddLabel(yes, Concat(Round(priorRSI, 2), ” “), Concat(“Rsi 2 = “, Round(RSI, 2)), if rsi < 2 then Color.GREEN else color.RED);
If I correctly understand your intention here. I will try to show the correction for your AddLabel statement:
AddLabel(yes, Concat(Round(priorRSI, 2), Concat(“ Rsi 2 = “, Round(RSI, 2))), if rsi < 2 then Color.GREEN else color.RED);
The Solution
Now, let’s take all those corrections and mash this together to see if we get something functional:
input timeFrameOne = AggregationPeriod.DAY;
def daily = timeFrameOne == AggregationPeriod.DAY;
def agg;
def data;
if daily == 1 {
agg = AggregationPeriod.DAY;
data = close(period = agg);
} else {
agg = Double.NaN;
data = Double.NaN;
}
def RSI = reference RSI(length = 2, price = data).RSI;
def priorRSI = reference RSI(length = 2, price = data[1]).RSI;
AddLabel(yes, Concat(Round(priorRSI, 2), Concat(“ Rsi 2 = “, Round(RSI, 2))), if RSI < 2 then Color.GREEN else Color.RED);
Screenshot below shows the result.
Don’t Go Away Yet
Even after correcting the errors in the code we still have a tremendous amount of redundancy here. Allow me to present the very minimum amount of code to accomplish the same thing:
input timeFrameOne = AggregationPeriod.DAY;
def RSI = reference RSI(length = 2, price = close(period = timeFrameOne)).RSI;
def priorRSI = reference RSI(length = 2, price = close(period = timeFrameOne)[1]).RSI;
AddLabel(yes, Concat(Round(priorRSI, 2), Concat(“ Rsi 2 = “, Round(RSI, 2))), if RSI < 2 then Color.GREEN else Color.RED);
WOW, I do have a lot to learn. It is not as easy to just watch videos and paste code together. You are amazing. like I said a great instructor.
One question though, you show “input timeFrameOne” does that mean I need 5 inputs to have an ability to choose an an aggreation.
I’m not sure if you need 5 inputs. Depends what your plans are. To use it as is, no. You will be able to select any of the supported aggregation periods from that one user input. Have you applied this code to a chart, then tried to edit the study settings to change the time frame?