high of certain period before certain time

Alex17

New member
Hello everyone,
I'm wondering if there is a way to possible have an indicator or maybe a column in the watchlist that shows the time of the current High of the Day? Meaning it will show the time, for example 14:56, at which the HOD was set. I really need this feature, and I hope someone can help me out!

Perhaps a column that doesn't have to be maintained thorough the day, meaning it will work only at the end of the day, so that it requires less coding. I just want to look at my watchlist at the end of the day and see the high and its time. What do you mean by checking it in the center? I checked out the learning center a bit, but I haven't seen custom scripts there or something. I also contacted the customer support and they told me to come here and ask.
 
Last edited by a moderator:
I am new to ThinkScript but have good experience in other trading languages like MQL4 so go easy on me here.

Here is what I am trying to do: I am trying to make a script that looks at the last 14 days and gives me the average time of the HoD (if the daily candle closed red) or the LoD (if the daily candle closed green). If this has already been done then immaculate! please link!


this has 4 labels to show,
the average time of HOD and LOD bars,
for the past x days.
show the number of bars after 9:30 and the 24 hour time of them.

rules
..enter a number for days back to look at
..find the average time of the HoD (if the daily candle closed red)
..find the average time of the LoD (if the daily candle closed green)

i left the test bubbles on so you can see where the data is coming from.
..bubbles above , up days and down days.
..bubbles below , HOD and LOD

on the first bar of a new day, it runs loops to look at future bars and find the HOD and LOD.
it reads the chart time and converts it to minutes, chartmin.
after calculating a bar offset to the desired bar, multiply it by the chartmin, and add to 9:30am.
(i didn't worry about proper formatting of the 24 hour time minutes, leading , trailing zeros.)

Code:
# avg_hodlod_xdays_01

# https://usethinkscript.com/threads/average-time-of-hod-or-lod.12499/
# Average Time of HoD or LoD
# look at the last x days,
#  find the average time of the HoD (if the daily candle closed red)
#  find the average time of the LoD (if the daily candle closed green)

# -----------------------------------
# day count
def bn = barnumber();
def istoday = if GetLastDay() == GetDay() then 1 else 0;
def newday = if getday() != getday()[1] then 1 else 0;
def newdaybn = if newday then bn else newdaybn[1];

def daycnt = if bn == 1 then 1 else if (!isnan(close) and newday) then daycnt[1] + 1 else daycnt[1];
def maxdays = highestall(daycnt);
def revdaycnt = maxdays - daycnt + 1;
input days_back = 14;

def validdays = revdaycnt <= days_back;

def daystoomany = if days_back < 1 or days_back > maxdays then 1 else 0;
addlabel(daystoomany, " ONLY " + MAXDAYS + " DAYS ON CHART. enter a different number for days back", color.cyan);


#-----------------------------------

input aggD = AggregationPeriod.day;
def day_opn = open(period = aggd);
def day_hi = high(period = aggd);
def day_lo = low(period = aggd);
def day_cls = close(period = aggd);

def isdaygrn = (day_cls > day_opn);
def isdayred = (day_cls < day_opn);

input test1_grnred = yes;
addchartbubble(validdays and newday and test1_grnred, high*1.01,
revdaycnt + "\n" +
isdaygrn + " G\n" +
isdayred + " R"
, (if isdaygrn then color.green else if isdayred then color.red else color.gray), yes);


input test2_chartmin = no;
def chartagg = GetAggregationPeriod();
def chartmin = (chartagg / 1000) / 60;
AddLabel(test2_chartmin, "chartmin " + chartmin, Color.MAGENTA);
def daybarqty = roundup(390 / chartmin, 0);
AddLabel(test2_chartmin, "bars per day " + daybarqty, Color.MAGENTA);

#------------------------------------------

# on 1st bar of day, look for the hi & lo of each day
def hi;
def hibn;
def lo;
def lobn;
def hodoffsum;
def lodoffsum;
def hodcnt;
def lodcnt;
def grnday;
def redday;

if !validdays then {
 hi = 0;
 hibn = 0;
 lo = 0;
 lobn = 0;
 hodoffsum = 0;
 lodoffsum = 0;
 hodcnt = 0;
 lodcnt = 0;
 grnday = 0;
 redday = 0;
} else if (bn == 1 or newday) and validdays then {
# find high of day
hi = fold i1 = 0 to 400
with p1
while (!isnan(getvalue(close, -i1)) and daycnt == getvalue(daycnt, -i1))
do (if getvalue(high, -i1) > p1 then getvalue(high, -i1) else p1);

# find bn of high of day
hibn = fold i2 = 0 to 400
with p2
while (!isnan(getvalue(close, -i2)) and daycnt == getvalue(daycnt, -i2))
do (if getvalue(high, -i2) == hi then getvalue(bn, -i2) else p2);

# find low of day
lo = fold j1 = 0 to 400
with q1 = 99999
while (!isnan(getvalue(close, -j1)) and daycnt == getvalue(daycnt, -j1))
do (if getvalue(low, -j1) < q1 then getvalue(low, -j1) else q1);

# find bn of low of day
lobn = fold j2 = 0 to 400
with q2
while (!isnan(getvalue(close, -j2)) and daycnt == getvalue(daycnt, -j2))
do (if getvalue(low, -j2) == lo then getvalue(bn, -j2) else q2);


# find the average time of the HoD (if the daily candle closed red)
# HOD, average bar number after open, on red days

# find the average time of the LoD (if the daily candle closed green)
# LOD, average bar number after open, on green days

 hodoffsum = if bn == 1 and isdayred then (hibn - newdaybn) else if isdayred then hodoffsum[1] + (hibn - newdaybn) else hodoffsum[1];
 lodoffsum = if bn == 1 and isdaygrn then (lobn - newdaybn) else if isdaygrn then lodoffsum[1] + (lobn - newdaybn) else lodoffsum[1];

 hodcnt = if bn == 1 and isdayred then 1 else if isdayred then hodcnt[1] + 1 else hodcnt[1];
 lodcnt = if bn == 1 and isdaygrn then 1 else if isdaygrn then lodcnt[1] + 1 else lodcnt[1];

 grnday = if isdaygrn then grnday[1] + 1 else grnday[1];
 redday = if isdayred then redday[1] + 1 else redday[1];

} else {
 hi = hi[1];
 hibn = hibn[1];
 lo = lo[1];
 lobn = lobn[1];

 hodoffsum =  hodoffsum[1];
 lodoffsum =  lodoffsum[1];
 hodcnt =  hodcnt[1];
 lodcnt =  lodcnt[1];

 grnday = grnday[1];
 redday = redday[1];
}


def hodoffavg = if hodcnt == 0 then 0 else round(hodoffsum/hodcnt,1);
def lodoffavg = if lodcnt == 0 then 0 else round(lodoffsum/lodcnt,1);


addlabel(1, " ", color.black);
# find the average time of the HoD (if the daily candle closed red)
#  HOD, average bar number after open, on red days
addlabel(1,  hodoffavg + " bars, HOD average bar number after open. only during " + redday + " of " + days_back + " red days", color.red);

addlabel(1, " ", color.black);
# find the average time of the LoD (if the daily candle closed green)
#  LOD, average bar number after open, on green days
addlabel(1, lodoffavg + " bars, LOD average bar number after open. only during " + grnday + " of " + days_back + " green days", color.green);

addlabel(1, " ", color.black);


#--------------------------

# time
def hihrs2 = 9.5 + ( hodoffavg * (chartmin/60));
def hihrs = floor(hihrs2);
def himins = (hihrs2-hihrs)*60;
addlabel(1,  hihrs + ":" + himins + "  24hr time, HOD average time after open. only during " + redday + " of " + days_back + " red days", color.red);

addlabel(1, " ", color.black);
def lohrs2 = 9.5 + ( lodoffavg * (chartmin/60));
def lohrs = floor(lohrs2);
def lomins = (lohrs2-lohrs)*60;
addlabel(1, lohrs + ":" + lomins + "  24hr time, LOD average time after open. only during " + grnday + " of " + days_back + " green days", color.green);


#--------------------------------------------

input test3_bubbles = yes;
addchartbubble(test3_bubbles and validdays, low*0.99,
bn + "\n" +
high + " H\n" +
hi + " hi\n" +
hibn + " hibn\n" +
hodoffsum + " sum\n" +
hodcnt + " cnt\n" +
hodoffavg + " Hfavg\n" +

lo + " lo\n" +
lobn + " lobn\n" +
lodoffsum + " sum\n" +
lodcnt + " cnt\n" +
lodoffavg + " Lfavg\n" +

newday + " new\n" +
daycnt + " cnt\n" +
revdaycnt + " rev\n"
#maxdays + " max\n"
, (if hi == high then color.green else if lo == low then color.red else color.gray), no);


input test4_bubbles = no;
addchartbubble(test4_bubbles, low,
newday + " new\n" +
daycnt + " cnt\n" +
revdaycnt + " rev\n" +
maxdays + " max\n"
, color.yellow, no);
#
#

4 long labels at the top, show bars past 9:30 and 24 hour time, of the average location of HOD and LOD bars.
this has some test bubbles turned on
1YcKzXA.jpg
 
Last edited:
If not I have some other simpler questions below.

I am having issues understanding how arrays work.
How can I scan every 1m candle in the previous X days to find the high or low? I will probably use the GetTime() formula on the HoD LoD candle and an IF statement to decide to grab Hod or LoD, so that part isn't an issue. But how do I do this scanning process for 14 different days? Getting the average of those 14 days will be easy, so will displaying the average.

...some other simpler questions... how arrays work..
simpler, heh

every variable in TOS is a 1 dimensional array variable.
an offset, a relative index, can be used to read other values, high[1] reads the high from 1 bar ago.
it is not an absolute index. high[3] is not the high of the 3rd bar on the chart.


start by listing what you want to do, figure out how to do it. then define the next step and figure that out,....
find hod and lod.
then find them according to your rules.
then limit the range of bars to the desired days
find the average time of the hod and lod. this doesn't have to be an absolute time. i did it by calculating the average bars to the desired bar, then convert that quantity of bars to hours and minutes. (read chart minutes).
ref: my post#2
 
Yes, thank you. I have altered this to do exactly what I need. thank you!!
I see you asked this question again. The indicator that I posted will only work, as does TOS mostly, is if you have the number of days displayed on your chart. So for 14 days, you will need a chart with a timeframe of a minimum of 20 as this is next increment that TOS allows from 10 days. I forgot to mention that in the above.

Hope everything works the way you want with either solution.
 
I see you asked this question again. The indicator that I posted will only work, as does TOS mostly, is if you have the number of days displayed on your chart. So for 14 days, you will need a chart with a timeframe of a minimum of 20 as this is next increment that TOS allows from 10 days. I forgot to mention that in the above.

Hope everything works the way you want with either solution.

@SleepyZ
i think i searched the unanswered section and found that post (which is the same as post#18 here). i didn't realize you answered the question.
https://usethinkscript.com/threads/average-time-of-hod-or-lod.12499/

@OG Bubba Knox (and everyone)
please don't make duplicate posts. every post is seen, and when someone has time and knowledge, they will work on it.
 
Very very cool code. The average time for HoD and LoD has been pretty well known but this backtest just further proves it. Great teaching tool btw! Is it possible to have the grey bubbles eliminated so it just shows HoD and LoD? Can it also calculate % chance of HoD or LoD by what bar. I believe i know the answer but what is the earliest bar with the highest % chance that the HoD or LoD is in.
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
275 Online
Create Post

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top