Count the number of times a previous high has been breached


Category:
0
0

Hi Steve
In your Q&A https://www.hahn-tech.com/ans/dynamic-hilo-watchlist/ you ended the session with…
“In order to show the number of times a previous high has been breached requires a very complicated solution that is not provided in the scope of this forum.”
I found an “embedded study” solution here http://tlc.thinkorswim.com/center/reference/thinkScript/Reserved-Words/script.html

script MyEMA {
input data = close;
input length = 12;
def EMA = compoundValue(1, 2 / (length + 1) * data + (length – 1) / (length + 1) * EMA[1], Average(data, length));
plot MyEma = EMA;
}
declare lower;
plot Osc = MyEMA(close, 25) – MyEMA(close, 50);

I would like to adapt this technique to count the number of times a previous high has been breached but my attempt to adapt this technique failed when I know the embedded portion works… can you help?….

script NEWHIGHS {
plot data = high >= Highest(high[1], 256);
}

Thanx in advance
Dave

Attachments:
Marked as spam
Posted by (Questions: 1, Answers: 0)
Asked on August 26, 2018 12:35 am
115 views
0
Private answer

What you refer to as an “embedded study” is nothing more than a piece of code that you can call later on in the script. If you don’t have any programming experience it is really difficult to find analogies that help you understand it’s use. But I will try….

And before explaining this I will let you know this does not provide any path for you to achieve your goal.

Ok, here goes. When you use the script keyword, everything in brackets runs independently of the rest of the lines in the study. You are essentially wrapping code inside the script brackets. Whatever you place within the brackets only runs when you ‘call’ it. What is that? What do you mean ‘call’? Let’s use your code as the example:

script NEWHIGHS { plot data = high >= Highest(high[1], 256); }

By itself, that line of code just sits there. Nothing happens until you call it:

def callNewHighs = NEWHIGHS();

Now we have called it. And when this second line of code is run it goes through and finds the script named NEWHIGHS and runs whatever code is inside it. Until we call it, that script does nothing at all. Got it? So what does this mean?

Well it means that these two lines of code….

script NEWHIGHS { plot data = high >= Highest(high[1], 256);
def callNewHighs = NEWHIGHS();

…perform the same exact result is this single line of code:

def data = high >= Highest(high[1], 256)

Same exact result. After these lines are run, callNewHighs is perfectly equal to data.

So what’s the point? Why would anyone use a script in their studies? Seems like it just adds additional lines of code that aren’t needed.

The whole point of writing a script in your study is to reduce the lines of code. So this only makes sense when your script contains multiple lines of code AND you need to run that code more than once from other points in your study.

Got that? More then once. So you have a computation that contains, let’s say 3 lines of code. If you need to use that same exact code in 3 other places of your study, it would take 9 lines of code total. But if you wrap those lines of code into a script, you will have 6 lines of code. 3 lines wrapped inside the script, and 3 lines that ‘call’ that script to run those three lines wrapped inside the script.

So, please be sure that when say:  “In order to show the number of times a previous high has been breached requires a very complicated solution that is not provided in the scope of this forum.” It’s not because I’m trying to make money. I have plenty of work to keep me busy. It truly is a massive and gargantuan task to accomplish. (well not for me but for the novice) I’m talking about the kind of mental gymnastics that will keep you up at night. Unless you are a programer. A very seasoned and experienced programmer.

Marked as spam
Posted by (Questions: 37, Answers: 4086)
Answered on August 26, 2018 8:07 am