Widest first 5-min bar range in 5 days


Category:
0
0

->Reposting as previous question code got corrupted
I’m checking if the first 5-min bar of the day is the widest range in 5 days and write a Label in a Custom Quote. My code is based on the approach used in another question here. It has the Ext Hour unchecked. Unfortunately it seems not firing so I wonder if there’s anybody seeing a problem with my code:

#Opening Bar Wide Range
def newDay = GetDay()GetDay()[1];

rec range1 = if newDay then high-low else 0; #today’s Open Bar (OB)
rec range2 = if newDay then high[78]-low[78] else 0; #previous day OB – 78 is the number of 5-min bars between 9:30AM and 4:00PM
rec range3 = if newDay then high[156]-low[156] else 0; #OB 2 days ago
rec range4 = if newDay then high[234]-low[234] else 0; #OB 3 days ago
rec range5 = if newDay then high[312]-low[312] else 0; #OB 4 days ago

def WR = if newDay and range1>range2 and range1>range3 and range1>range4 and range1>range5 then 1 else 0;

AddLabel(1, if WR then “WR” else ” “, color.WHITE);

Thanks in advance.

Marked as spam
Posted by (Questions: 7, Answers: 9)
Asked on June 7, 2020 5:51 am
102 views
0
Private answer

You cannot even begin to imagine how difficult this is for a scripting language like Thinkorswim. In order to do this properly we need to have the ability to create and work with custom sized arrays. Since we do not, the solution is not very robust. This only works if you have 5 days of 5 min data on the chart. Anything more or less and it fails. And as you mentioned, this approach also requires that the chart NOT included extended hours session.

def newDay = GetDay() <> GetDay()[1];
def firstBarRange = if newDay then high - low else 0;
rec trackRange = if newDay then firstBarRange else trackRange[1];
rec trackWidestRange = if newDay then Max(firstBarRange, trackRange[1]) else trackWidestRange[1];
plot conditionWidestRange = newDay[1] and firstBarRange[1] > trackWidestRange[2];
rec todayIsWidest = if newDay then 0 else if conditionWidestRange then 1 else todayIsWidest[1];
AddLabel(1, if todayIsWidest then “WR” else ” “, color.WHITE);

Don't even bother trying to figure out exactly how this code works unless you want to bring on a severe headache.

I will make some closing comments about your code. You need to include spaces between your operators and variable names.

Example One:

You entered def newDay = GetDay()<>GetDay()[1]; and it was reformatted to def newDay = GetDay()GetDay()[1];

Because you failed to included spaces the HTML compiler in every web browser treats those greater than and less than symbols as special characters and removes them.

Example Two:

You entered rec range3 = if newDay then high[156]-low[156] else 0; and it should have been def range3 = if newDay then high[156] - low[156] else 0;

See the space between the minus sign and the two elements on either side?

Example Three:

You entered def WR = if newDay and range1>range2 and range1>range3 and range1>range4 and range1>range5 then 1 else 0; and it should have been def wr = if newDay and range1 > range2 and range1 > range3 and range1 > range4 and range1 > range5 then 1 else 0;

These are the reasons that your code from the previous post got butchered. I have no idea why some folks fail to include spaces between operators and other elements. Drives me nuts to see that. So stop it. It causes problems you cannot fully fathom.

I understand the approach you took. However that is much more fragile than the solution I generated. The reason is that you can never be certain any give stock is going to have a full complement of 5 min bars. So if you have gaps in the trading day your indexes will miss their mark. The other point of failure in your approach is the statement for "wr" will only be true on the first bar of the trading session. So your label would go blank as soon as the second bar appears on the chart. You have to use recursion to carry that value forward as new bars are added to the chart.

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on June 7, 2020 8:46 am
0
Hey Pete, thanks a lot for your solution and comments. Re: the solution, I totally agree with you that what I proposed is very fragile. Plus as you mentioned the condition for firing the WR label was not bringing forward the initial value and therefore could be fired only during the first bar. I have to admit it took me some time to decode your solution but I eventually understood the logic behind it. Sometimes I believe I still don't have a full understanding of the way ThinkScript think and I confuse it with a fully fledge programming language... Re: your comments, all noted. I'll make sure to add spaces. I used to do that but then got loose when started to use StackOverflow or similar where this doesn't matter. I should have checked what posted on the first place. Once again, very much appreciated your time spent on my question and hop this can be useful for others here in the forum.
( at June 7, 2020 10:50 am)
0
I'm glad you were able to follow the logic in that solution and understand it. Yes, having to work in a scripting environment is very different then a functional or object base programming language. It has taken many years for me to develop the internal circuitry to work effectively in this environment. But I find that when I work in a full featured programming language I can do almost everything I do in Thinkorswim in other languages. I guess what I am saying is I took the handicap of working in Thinkorswim and converted it to an asset that gives me a broader set of tools for working in other languages.
( at June 7, 2020 2:29 pm)