# custom_times_2nd_bar_inside_01_upper
# halcyonguy
# 22-01-09
#https://usethinkscript.com/threads/first-two-bars-of-the-day.14013/
#First two bars of the day
# _GreenSmile_ 1/9
#I am a day trader. I only trade the first hour of the day. I'm looking for a scan that alerts me when the second bar of the day is inside the first bar of the day. In other words, the range of the 2nd bar is less than the range of the 1st bar. Also, the range of the 2nd bar must be less than or equal to half the range of the 1st bar so the range of the 2nd bar me be at least 50% less than the 1st bar of the day.
#More importantly, I need the scan to work on any time frame including custom time frames.
#For example: At 9:40AM when the second 5 minute bar has closed, I would like to know if that bar is inside the 1st 5 minute bar. And at 9:42AM when the second 6 minute bar has closed I would like to know if that bar is inside the first 6 minute bar. I would then get alerted at 9:44AM when the second 7 minute bar has closed and so on.
#It could continue alerting me for the first hour of the day from 9:40AM-10:30AM or I could select the time frames I want which are a total of 10. They are the 5, 6, 7, 8, 10, 13, 15, 20, 26 and 30 minute time frames.
#Please let me know if this would be possible. Thank you very much.
# 5, 6, 7, 8, 10, 13, 15, 20, 26, 30
script barx {
input minutes = 0;
input insidebar_percent_size = 50;
# input factor = 1;
def factor = 1;
# input start = 0930;
def start = 0930;
def bar1 = if SecondsFromTime(start) >= 0 and SecondsFromTime(start) < (minutes * 60 * 1) then 1 else 0;
def hi1 = if SecondsFromTime(start) == (minutes * 60 * (factor - 1)) then high else if bar1 and high > hi1[1] then high else hi1[1];
def lo1 = if SecondsFromTime(start) == (minutes * 60 * (factor - 1)) then low else if bar1 and low < lo1[1] then low else lo1[1];
def bar2 = if SecondsFromTime(start) >= (minutes * 60 * 1) and SecondsFromTime(start) < (minutes * 60 * 2) then 1 else 0;
def hi2 = if SecondsFromTime(start) == (minutes * 60 * (factor - 0)) then high else if bar2 and high > hi2[1] then high else hi2[1];
def lo2 = if SecondsFromTime(start) == (minutes * 60 * (factor - 0)) then low else if bar2 and low < lo2[1] then low else lo2[1];
#plot zhi1 = hi1;
#plot zlo1 = lo1;
#plot zhi2 = hi2;
#plot zlo2 = lo2;
def rng1 = hi1 - lo1;
def rng2 = hi2 - lo2;
plot inside1 = (hi1 > hi2 and lo1 < lo2);
plot inside2 = (rng1 * insidebar_percent_size/100) > rng2;
plot inside = inside1 and inside2;
}
#-----------------------------------------
# get chart agg
def chartagg = GetAggregationPeriod();
def chartmin = chartagg / (1000 * 60);
#AddLabel(yes, chartmin + " min", Color.YELLOW);
def start = 0930;
def minx = (SecondsFromTime(start) / 60);
input insidebar_percent_size = 50;
def y = 0.999;
#-----------------------------------------
input t1_minutes = 5;
input use_time1 = yes;
def en1 = (t1_minutes % chartmin) == 0 and use_time1;
# compare prev 2 time periods on 3rd period
def t1 = if en1 and minx == (t1_minutes * 2) then 1 else 0;
def t1_insidez = barx(t1_minutes, 50).inside;
def i1 = barx(t1_minutes, 50).inside1;
addchartbubble(t1, low*y,
(t1_minutes + "\nmin\n" + (if i1 then "inside" else "-"))
, (if t1_insidez then color.green else color.gray), no);
#def t1_bar1 = barx(t1_minutes, 50).bar1;
#def t1_hi1 = barx(t1_minutes, 50).zhi1;
#def t1_lo1 = barx(t1_minutes, 50).zlo1;
#def t1_bar2 = barx(t1_minutes, 50).bar2;
#def t1_hi2 = barx(t1_minutes, 50).zhi2;
#def t1_lo2 = barx(t1_minutes, 50).zlo2;
#addchartbubble(0 and t1, low*y,
#(if i1 then (t1_minutes + " min\ninside") else "-") + "\n" +
#t1_bar1 + "\n" +
#t1_hi1 + "\n" +
#t1_lo1 + "\n" +
#t1_bar2 + "\n" +
#t1_hi2 + "\n" +
#t1_lo2
#, (if t1_insidez then color.green else color.gray), no);
#-----------------------------------------
input t2_minutes = 6;
input use_time2 = yes;
def en2 = (t2_minutes % chartmin) == 0 and use_time2;
# compare prev 2 time periods on 3rd period
def t2 = if en2 and minx == (t2_minutes * 2) then 1 else 0;
def t2_insidez = barx(t2_minutes, 50).inside;
def i2 = barx(t2_minutes, 50).inside1;
addchartbubble(t2, low*y,
(t2_minutes + "\nmin\n" + (if i2 then "inside" else "-"))
, (if t2_insidez then color.green else color.gray), no);
#-----------------------------------------
input t3_minutes = 7;
input use_time3 = yes;
def en3 = (t3_minutes % chartmin) == 0 and use_time3;
# compare prev 2 time periods on 3rd period
def t3 = if en2 and minx == (t3_minutes * 2) then 1 else 0;
def t3_insidez = barx(t3_minutes, 50).inside;
def i3 = barx(t3_minutes, 50).inside1;
addchartbubble(t3, low*y,
(t3_minutes + "\nmin\n" + (if i3 then "inside" else "-"))
, (if t3_insidez then color.green else color.gray), no);
#-----------------------------------------
input t4_minutes = 8;
input use_time4 = yes;
def en4 = (t4_minutes % chartmin) == 0 and use_time4;
# compare prev 2 time periods on 3rd period
def t4 = if en4 and minx == (t4_minutes * 2) then 1 else 0;
def t4_insidez = barx(t4_minutes, 50).inside;
def i4 = barx(t4_minutes, 50).inside1;
addchartbubble(t4, low*y,
(t4_minutes + "\nmin\n" + (if i4 then "inside" else "-"))
, (if t4_insidez then color.green else color.gray), no);
#-----------------------------------------
input t5_minutes = 10;
input use_time5 = yes;
def en5 = (t5_minutes % chartmin) == 0 and use_time5;
# compare prev 2 time periods on 3rd period
def t5 = if en4 and minx == (t5_minutes * 2) then 1 else 0;
def t5_insidez = barx(t5_minutes, 50).inside;
def i5 = barx(t5_minutes, 50).inside1;
addchartbubble(t5, low*y,
(t5_minutes + "\nmin\n" + (if i5 then "inside" else "-"))
, (if t5_insidez then color.green else color.gray), no);
#-----------------------------------------
input t6_minutes = 13;
input use_time6 = yes;
def en6 = (t6_minutes % chartmin) == 0 and use_time6;
# compare prev 2 time periods on 3rd period
def t6 = if en6 and minx == (t6_minutes * 2) then 1 else 0;
def t6_insidez = barx(t6_minutes, 50).inside;
def i6 = barx(t6_minutes, 50).inside1;
addchartbubble(t6, low*y,
(t6_minutes + "\nmin\n" + (if i6 then "inside" else "-"))
, (if t6_insidez then color.green else color.gray), no);
#-----------------------------------------
input t7_minutes = 15;
input use_time7 = yes;
def en7 = (t7_minutes % chartmin) == 0 and use_time7;
# compare prev 2 time periods on 3rd period
def t7 = if en7 and minx == (t7_minutes * 2) then 1 else 0;
def t7_insidez = barx(t7_minutes, 50).inside;
def i7 = barx(t7_minutes, 50).inside1;
addchartbubble(t7, low*y,
(t7_minutes + "\nmin\n" + (if i7 then "inside" else "-"))
, (if t7_insidez then color.green else color.gray), no);
#-----------------------------------------
input t8_minutes = 20;
input use_time8 = yes;
def en8 = (t8_minutes % chartmin) == 0 and use_time8;
# compare prev 2 time periods on 3rd period
def t8 = if en8 and minx == (t8_minutes * 2) then 1 else 0;
def t8_insidez = barx(t8_minutes, 50).inside;
def i8 = barx(t8_minutes, 50).inside1;
addchartbubble(t8, low*y,
(t8_minutes + "\nmin\n" + (if i8 then "inside" else "-"))
, (if t8_insidez then color.green else color.gray), no);
#-----------------------------------------
input t9_minutes = 26;
input use_time9 = yes;
def en9 = (t9_minutes % chartmin) == 0 and use_time9;
# compare prev 2 time periods on 3rd period
def t9 = if en9 and minx == (t9_minutes * 2) then 1 else 0;
def t9_insidez = barx(t9_minutes, 50).inside;
def i9 = barx(t9_minutes, 50).inside1;
addchartbubble(t9, low*y,
(t9_minutes + "\nmin\n" + (if i9 then "inside" else "-"))
, (if t9_insidez then color.green else color.gray), no);
#-----------------------------------------
input t10_minutes = 30;
input use_time10 = yes;
def en10 = (t10_minutes % chartmin) == 0 and use_time10;
# compare prev 2 time periods on 3rd period
def t10 = if en10 and minx == (t10_minutes * 2) then 1 else 0;
def t10_insidez = barx(t10_minutes, 50).inside;
def i10 = barx(t10_minutes, 50).inside1;
addchartbubble(t10, low*y,
(t10_minutes + "\nmin\n" + (if i10 then "inside" else "-"))
, (if t10_insidez then color.green else color.gray), no);
#-----------------------------------------
def z =
t1 and t1_insidez or
t2 and t2_insidez or
t3 and t3_insidez or
t4 and t4_insidez or
t5 and t5_insidez or
t6 and t6_insidez or
t7 and t7_insidez or
t8 and t8_insidez or
t9 and t9_insidez or
t10 and t10_insidez;
alert(z, "inside", Alert.BAR, Sound.Chimes);
#