chart label plus aggreation


Category:
0
0

Dear pete,

This is my first attempt at writing script ( joke hijacking script) , because I don’t know what I am doing.  I am an old fart, so I hope I am not embarrassing myself.

 

This coding, is helping me, so the doctors tell me, it will help me return my memory back.

 

I really enjoy your website, you are an excellent instructor.

 

Also, I hope I am doing this properly, I don’t want to be scolded for improper procedures.

 

I saw your video on autotrade, you show how simple it is to code, but it is not easy trying to piece together other code.

 

From what I can see is that using the INPUT command it then creates a screen so you can input variables.

 

What I am first trying to accomplish is to get up to 5 aggregations, available to me.

 

2nd . I am trying to create a RSI label to give me the value of either the open RSI or the previous day’s closing RSI, plus the current RSI.

 

3rd I am I interpreting the code  correctly , I was getting coding errors I don’t know what they mean.

 

Here is the code I attempted to execute.

 

input AP=getaggregationperiod();
def daily=if AP=aggregationPeriod.DAY then 1 else 0;

 

if daily equal 1 then

def agg = AggregationPeriod.DAY

def data = close(period = agg)

else double.NaN;

def RSI = RSIWilder(length = 2, price = close(period = agg )).RSI;

 

def priorRSI = RSIWilder(length = 2, price = close(period = agg )[1]).RSI;

 

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”

 

AddLabel(yes, Concat(Round(priorRSI, 2), ”   “),  Concat(“Rsi 2 = “, Round(RSI, 2)), if rsi < 2 then Color.GREEN else color.RED);

 

I am also going to submit a second question but because it it is scan question I must submit separately. Thanks.

Marked as spam
Posted by (Questions: 4, Answers: 5)
Asked on July 24, 2018 8:39 am
524 views
0

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.

( at July 26, 2018 6:15 am)
0

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?

( at July 26, 2018 7:36 am)
0
Private answer

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);

Attachments:
Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on July 24, 2018 11:12 am
0

Pete, Thank you very much for this outstanding label and the tutorial that you included. I have modified this label a little and I am using it on 3 and 10-minute charts. It is great to see the Daily RSI ratio on multi-time frame charts. Thanks again for outstanding help.

( at July 27, 2018 1:24 pm)