script am {
input day = 0;
input begin = 0930;
input end = 1230;
def ymd = GetYYYYMMDD();
def candles = !IsNaN(close);
def capture = candles and ymd != ymd[1];
def dayCount = CompoundValue(1, if capture then dayCount[1] + 1 else dayCount[1], 0);
def thisDay = (HighestAll(dayCount) - dayCount) ;
#High between begin and end ---------------------
def amhi = if thisDay == day and SecondsTillTime(begin)[1] >= 0 and SecondsFromTime(begin) >= 0 then high else if thisDay == day and SecondsTillTime(end) > 0 then Max(high, amhi[1]) else amhi[1];
plot hi = amhi;
#Low between begin and end ----------------------
def amlo = if SecondsTillTime(begin)[1] >= 0 and SecondsFromTime(begin) >= 0 then low else if SecondsTillTime(end) > 0 then Min(low, amlo[1]) else amlo[1];
plot lo = amlo;
#Close at end -----------------------------------
def amc = if SecondsTillTime(end) > 0 then close else amc[1];
plot cl = amc;
}
script pm {
input day = 0;
input begin = 1230;
input end = 1600;
def ymd = GetYYYYMMDD();
def candles = !IsNaN(close);
def capture = candles and ymd != ymd[1];
def dayCount = CompoundValue(1, if capture then dayCount[1] + 1 else dayCount[1], 0);
def thisDay = (HighestAll(dayCount) - dayCount) ;
#High between begin and end ---------------------
def phi = if thisDay == day and SecondsTillTime(begin)[1] >= 0 and SecondsFromTime(begin) >= 0 then high else if thisDay == day and SecondsTillTime(end) > 0 then Max(high, phi[1]) else phi[1];
plot pmhi = phi;
#Low between begin and end ----------------------
def plo = if SecondsTillTime(begin)[1] >= 0 and SecondsFromTime(begin) >= 0 then low else if SecondsTillTime(end) > 0 then Min(low, plo[1]) else plo[1];
plot pmlo = plo;
}
#Average True Range over 14 AM periods================
def tr0 = TrueRange(am().hi, am().cl, am().lo);
def tr1 = TrueRange(am(1).hi, am(1).cl, am(1).lo);
def tr2 = TrueRange(am(2).hi, am(2).cl, am(2).lo);
def tr3 = TrueRange(am(3).hi, am(3).cl, am(3).lo);
def tr4 = TrueRange(am(4).hi, am(4).cl, am(4).lo);
def tr5 = TrueRange(am(5).hi, am(5).cl, am(5).lo);
def tr6 = TrueRange(am(6).hi, am(6).cl, am(6).lo);
def tr7 = TrueRange(am(7).hi, am(7).cl, am(7).lo);
def tr8 = TrueRange(am(8).hi, am(8).cl, am(8).lo);
def tr9 = TrueRange(am(9).hi, am(9).cl, am(9).lo);
def tr10 = TrueRange(am(10).hi, am(10).cl, am(10).lo);
def tr11 = TrueRange(am(11).hi, am(11).cl, am(11).lo);
def tr12 = TrueRange(am(12).hi, am(12).cl, am(12).lo);
def tr13 = TrueRange(am(13).hi, am(13).cl, am(13).lo);
def amatr = (tr0 + tr1 + tr2 + tr3 + tr4 + tr5 + tr6 + tr7 + tr8 + tr9 + tr10 + tr11 + tr12 + tr13) / 14;
#Scan -------------------------------------------
def test2 = if (pm().pmhi - pm().pmlo) < .25 * (am().hi - am().lo) then 1 else 0;
def test1 = if (am().hi - am().lo) > (1.3 * amatr) then 1 else 0;
plot scan = if test1 == 1 and test2 == 1 then 1 else 0;
#Vertical line added to ensure that approximately 1230 is captured if that time is not shown on the chart
input debug = yes;
AddVerticalLine(debug and SecondsTillTime(0930)[1] >= 0 and SecondsFromTime(0930) >= 0, "");
AddLabel(debug, "AH: " + am().hi + " AC: " + am().cl + " AL: " + am().lo + " ATR: " + amatr, Color.WHITE);
addlabel(debug, "PH: " + " " + pm().pmhi + " PL:" + pm().pmlo + " PM Diff: " + (pm().pmhi - pm().pmlo) + " AM Diff: " + (.25 * (am().hi - am().lo)), if test2==1 then color.green else color.red);
AddLabel(debug, "SCAN: Test1: " + Test1 + " Test2: " + test2 + " == " + scan, if scan == 1 then Color.GREEN else Color.RED);