First 3 5-min bars bias of the day


Category:
0
0

I’m trying to measure the behavior of the first three 5 min bars. If all 3 are higher highs and higher lows, then my bias is bullish for the day. The code is used as a Custom Quote:

 

input StartTime1 = 0930;

input EndTime1 = 0935;

def h1 = high;
def l1 = low;
def c1 = close;
def ORActive1 = if SecondsFromTime(StartTime1) > 0 and  SecondsTillTime(EndTime1) >= 0 then 1 else 0;
def ORH1 = if ORActive1 and !ORActive1[1] then h1 else if ORActive1 and h1 > ORH1[1] then h1 else ORH1[1];
def ORL1 = if ORActive1 and !ORActive1[1] then l1 else if ORActive1 and l1 < ORL1[1] then l1 else ORL1[1];
def ORhigh1 = if !ORActive1 then ORH1 else Double.NaN;
def ORlow1 = if !ORActive1 then ORL1 else Double.NaN;

[Same for Bar 2 and Bar 3]

AddLabel(1, if ORhigh2 > ORhigh1 and ORhigh3 > ORhigh2 then “UP” else if ORlow2 < ORlow1 and ORlow3 < ORlow2 then “DOWN” else ” “, COLOR.WHITE);

It seems to work but it only starts to update at 9:50AM and not right at the end of the 3rd bar, i.e. at 9:45.

Does anybody see what I’m doing wrong in my code?

Marked as spam
Posted by (Questions: 7, Answers: 9)
Asked on June 4, 2020 1:23 pm
145 views
1
Private answer

Can't really do anything with your code because you left out the rest of it. I do see you added a comment explaining that bars 2 and 3 were same as bar 1. But that does not provide a fully functional piece of code so I can't do anything with it.

Seems that your code is written by an engineer (trained in some discipline OTHER than computer science). They tend to write code using brute force methods rather than making the best use of available data structures to write as few lines as possible while leaving plenty of room for maintenance and growth.

The simplest solution here requires that you set the custom quote column to not include extended hours data. This completely eliminates the need to try to use specific time stamps. The additional advantage to my approach is that you can use this on any intraday time frame and it will still measure the first three bars of that time frame.

We could even write this in a more elegant way that provides a user adjustable input so the number of bars can be adjusted through a user input. But I don't have time to be that fancy when writing solutions in the Q&A forum.

def newDay = GetDay() <> GetDay()[1];
def uptrendBias = if newDay[2] then high > high[1] and high[1] > high[2] and low > low[1] and low[1] > low[2] else no;
def downtrendBias = if newDay[2] then high < high[1] and high[1] < high[2] and low < low[1] and low[1] < low[2] else no; rec bias = if newDay then 0 else if newDay[2] and uptrendBias then 1 else if newDay[2] and downtrendBias then -1 else bias[1]; plot data = bias; data.AssignValueColor(if bias > 0 then Color.GREEN else if bias < 0 then Color.RED else Color.CURRENT);

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on June 4, 2020 4:58 pm
0
Thanks Pete for your answer, indeed it is very straightforward and simpler than what I wrote. Appreciated your time spent on this. By the way, I was not able to put the entire code as I kept exceeding the field characters limit.
( at June 6, 2020 6:41 pm)
0
Yes, the character limit is intentional. To many posters were adding code to the body of their questions that was well over 24 lines. The character limit is there to force posters to attach their code as a plain text document because I could not get folks to do this of their own accord.
( at June 6, 2020 7:09 pm)