Line code -previous x bars back?


Category:
0
0

Hi Pete, I am new to programming. How can I write a simple code-  close is greater than high from 15 bars ago. If I write it like that -the program just looks at the close of the bar -15 bars ago. I want it to look at the last 15 bars.

close is greater than high from 1-15 bars ago. but this don’t work either. Any ideas or examples? thanks

Marked as spam
Posted by (Questions: 3, Answers: 6)
Asked on December 30, 2017 10:30 am
188 views
0
Private answer

The short answer is this:

def signal = high > Highest(high[1], 15);

Interpreting this statement: define a variable named “signal” that checks if the current high is greater than the highest high (**excluding the current high) looking back at the last 15 bars.

**That index reference, [1], after the word high is what ensures it will exclude the current bar’s high from the comparison.

Marked as spam
Posted by (Questions: 37, Answers: 4086)
Answered on December 30, 2017 11:38 am
0

thanks it works by itself, But. what if I want to combine it with another command. Example-close is greater than a certain price (and the the current high is greater than the highest high of the last 15 bars.)-already got this part from your answer above.

I tried these 2-but no luck? thanks

def signal = high > Highest(high[1], 15);
close is greater than 100 and close is greater than signal;

close is greater than 100 and close is greater then def signal = high > Highest(high[1], 15);

( at December 30, 2017 12:45 pm)
0

“signal” is a true/false result. It is either true or it is false. So you cannot use it in a comparison against price. So “close is greater than signal” is meaningless. Because “signal” is either true or 0 false. Can the close every be greater than true? Or greater than false? No, makes no sense whatsoever.

Here is how we accomplish this:
def signal = high > Highest(high[1], 15);
def otherSignal = close > 100;
plot scan = signal and otherSignal;

Everything here is converted to true/false statements. Because the scan only understands true or false. The scan returns a result ONLY when signal equals true AND otherSignal equals true.

Seeing where you are at, I highly recommend you stop what you are working on and work through the entire Thinkscript tutorial, published by TD Ameritrade.
Basic: http://tlc.thinkorswim.com/center/howToTos/tutorials/Basic.html
Advanced: http://tlc.thinkorswim.com/center/howToTos/tutorials/Advanced.html

( at December 30, 2017 1:01 pm)
0

Thanks for your help Pete, What a great website. keep up the good work. Have a great New Years!!!

( at December 30, 2017 3:29 pm)