/ES Futures Quasi-Algo 1 Hour Out Prediction for ThinkorSwim

Few questions.
Does the code have to updated to get /es levels? How often does the code have to be updated? 60 days?
Can the code used on lower time frame? 2 min chart?
The code for the indicator is generated by a python script somewhere in this thread. It works on 2min I think (depends on the version... I think I updated it, but perhaps not... I'll check sometime).

There is no facility in ThinkOrSwim to generate the statistics that are hard coded into the indicator. That's why the python code exists. Python downloads the 5m data from YFinance, does some statistical magic, and then spits out ThinkScript code that you paste into the code editor in ThinkOrSwim and you have an updated indicator.

I'll run off a new /ES for 'yall ... just a moment.

FYI, if you're happy with the performance of the indicator as you're using it, there is no need to update. Updates are useful for seasonal changes, or just market mood changes. There is a post in here about volatility before and after some event or other... it makes for some interesting statistics. If you're into that sort of thing.

Code:
#########################################################
#
#   Next Hour ES Trading Range
#     Algorithmic Prediction
#
#   Values Derived on Data from
#          2022-07-06 to
#          2022-09-13
#
#   @mashume at usethinkscript.com
#
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#
#  This Version /ES only
#  Functional on 1, 2, 3, 5, 10, 15, 20, 30, 60 minute charts
#
#########################################################

declare upper;

def Y = 31556926;
def M = 2629743;
def D = 86400;
def H = 3600;
def HalfHour = 1800;
def epoch = (getTime() / 1000);
def YMD = (epoch % Y) - (epoch / Y);
def month = (Floor(YMD / M) + 1);
def GMT = Floor((epoch % D) / H);
def HOUR = if GMT > 4 then GMT - 4 else if GMT < 4 then (GMT + 24) - 4 else 0;

def tf = getAggregationPeriod();

def barsPerHour = if tf == AggregationPeriod.HOUR then 1
    else if tf == AggregationPeriod.THIRTY_MIN then 2
    else if tf == AggregationPeriod.TWENTY_MIN then 3
    else if tf == AggregationPeriod.FIFTEEN_MIN then 4
    else if tf == AggregationPeriod.TEN_MIN then 6
    else if tf == AggregationPeriod.FIVE_MIN then 12
    else if tf == AggregationPeriod.THREE_MIN then 20
    else if tf == AggregationPeriod.TWO_MIN then 30
    else if tf == AggregationPeriod.MIN then 60
    else double.nan;
   

def q1 =
    if HOUR == 0 then -2.562000
    else if HOUR == 1 then -3.250000
    else if HOUR == 2 then -4.188000
    else if HOUR == 3 then -5.250000
    else if HOUR == 4 then -3.750000
    else if HOUR == 5 then -3.500000
    else if HOUR == 6 then -4.250000
    else if HOUR == 7 then -5.000000
    else if HOUR == 8 then -7.750000
    else if HOUR == 9 then -9.500000
    else if HOUR == 10 then -7.250000
    else if HOUR == 11 then -7.250000
    else if HOUR == 12 then -6.750000
    else if HOUR == 13 then -7.312000
    else if HOUR == 14 then -4.500000
    else if HOUR == 15 then -6.750000
    else if HOUR == 16 then -4.500000
    else if HOUR == 17 then -9.375000
    else if HOUR == 18 then -2.500000
    else if HOUR == 19 then -4.250000
    else if HOUR == 20 then -3.250000
    else if HOUR == 21 then -1.750000
    else if HOUR == 22 then -1.500000
    else -2.000000;
def q3 =
    if HOUR == 0 then 2.500000
    else if HOUR == 1 then 3.000000
    else if HOUR == 2 then 5.750000
    else if HOUR == 3 then 5.750000
    else if HOUR == 4 then 4.250000
    else if HOUR == 5 then 4.500000
    else if HOUR == 6 then 3.500000
    else if HOUR == 7 then 5.000000
    else if HOUR == 8 then 8.312500
    else if HOUR == 9 then 13.750000
    else if HOUR == 10 then 12.750000
    else if HOUR == 11 then 7.500000
    else if HOUR == 12 then 9.250000
    else if HOUR == 13 then 7.500000
    else if HOUR == 14 then 8.062500
    else if HOUR == 15 then 6.750000
    else if HOUR == 16 then 2.250000
    else if HOUR == 17 then -6.125000
    else if HOUR == 18 then 2.000000
    else if HOUR == 19 then 2.000000
    else if HOUR == 20 then 3.500000
    else if HOUR == 21 then 3.500000
    else if HOUR == 22 then 3.000000
    else 1.812000;
def m2 =
    if HOUR == 0 then -0.500000
    else if HOUR == 1 then -0.250000
    else if HOUR == 2 then 0.500000
    else if HOUR == 3 then -0.125000
    else if HOUR == 4 then -0.250000
    else if HOUR == 5 then 0.000000
    else if HOUR == 6 then -0.250000
    else if HOUR == 7 then 0.500000
    else if HOUR == 8 then 0.500000
    else if HOUR == 9 then 1.250000
    else if HOUR == 10 then 3.250000
    else if HOUR == 11 then 0.750000
    else if HOUR == 12 then 2.375000
    else if HOUR == 13 then 0.250000
    else if HOUR == 14 then 2.250000
    else if HOUR == 15 then 0.250000
    else if HOUR == 16 then -1.250000
    else if HOUR == 17 then -8.000000
    else if HOUR == 18 then -0.250000
    else if HOUR == 19 then -1.250000
    else if HOUR == 20 then 0.500000
    else if HOUR == 21 then 1.000000
    else if HOUR == 22 then 0.750000
    else 0.250000;

def midline = hl2(period = AggregationPeriod.HOUR)[1];
def median = hl2(period = AggregationPeriod.HOUR)[1] + m2;
def upper = median + (q3);
def lower = median + (q1);

plot c = midline;
c.setStyle(CURve.SHORT_DASH);
c.setDefaultColor(getColor(7));

plot median_line = median[barsPerHour];
median_line.SetPaintingStrategy(PaintingStrategy.DASHES);
median_line.SetDefaultColor(getColor(3));
plot expected_upper = upper[barsPerHour];
expected_upper.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
expected_upper.SetDefaultColor(getColor(1));
plot expected_lower = lower[barsPerHour];
expected_lower.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
expected_lower.SetDefaultColor(getColor(5));

addcloud(expected_upper, expected_lower, color.white, color.white);
def upside = (expected_upper[(-1 * barsPerHour)] - close);
def downside = (close - expected_lower[(-1 * barsPerHour)]);
addLabel(yes, "current upside: " + upside + "  current downside: " + downside, if upside > downside then color.dark_green else if downside > upside then color.dark_red else color.dark_gray);

def dist_to_median = (median_line[(-1 * barsPerHour)] - close);
addLabel(yes, "Distance to Median: " + dist_to_median,
if dist_to_median > 5 then color.green
else if dist_to_median > 1 then color.dark_green
else if dist_to_median < -5 then color.red
else if dist_to_median < -1 then color.dark_red
else color.gray);

happy trading,
mashume
 
And here's /CL for good measure:
Code:
#########################################################
#
#   Next Hour CL Trading Range
#     Algorithmic Prediction
#
#   Values Derived on Data from
#          2022-07-06 to
#          2022-09-13
#
#   @mashume at usethinkscript.com
#
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#
#  This Version /CL only
#  Functional on 1, 2, 3, 5, 10, 15, 20, 30, 60 minute charts
#
#########################################################

declare upper;

def Y = 31556926;
def M = 2629743;
def D = 86400;
def H = 3600;
def HalfHour = 1800;
def epoch = (getTime() / 1000);
def YMD = (epoch % Y) - (epoch / Y);
def month = (Floor(YMD / M) + 1);
def GMT = Floor((epoch % D) / H);
def HOUR = if GMT > 4 then GMT - 4 else if GMT < 4 then (GMT + 24) - 4 else 0;

def tf = getAggregationPeriod();

def barsPerHour = if tf == AggregationPeriod.HOUR then 1
    else if tf == AggregationPeriod.THIRTY_MIN then 2
    else if tf == AggregationPeriod.TWENTY_MIN then 3
    else if tf == AggregationPeriod.FIFTEEN_MIN then 4
    else if tf == AggregationPeriod.TEN_MIN then 6
    else if tf == AggregationPeriod.FIVE_MIN then 12
    else if tf == AggregationPeriod.THREE_MIN then 20
    else if tf == AggregationPeriod.TWO_MIN then 30
    else if tf == AggregationPeriod.MIN then 60
    else double.nan;
    

def q1 =
    if HOUR == 0 then -0.150000
    else if HOUR == 1 then -0.240000
    else if HOUR == 2 then -0.270000
    else if HOUR == 3 then -0.550000
    else if HOUR == 4 then -0.420000
    else if HOUR == 5 then -0.380000
    else if HOUR == 6 then -0.370000
    else if HOUR == 7 then -0.250000
    else if HOUR == 8 then -0.640000
    else if HOUR == 9 then -0.640000
    else if HOUR == 10 then -0.445000
    else if HOUR == 11 then -0.460000
    else if HOUR == 12 then -0.410000
    else if HOUR == 13 then -0.468000
    else if HOUR == 14 then -0.340000
    else if HOUR == 15 then -0.220000
    else if HOUR == 16 then -0.180000
    else if HOUR == 17 then 0.000000
    else if HOUR == 18 then -0.110000
    else if HOUR == 19 then -0.160000
    else if HOUR == 20 then -0.280000
    else if HOUR == 21 then -0.150000
    else if HOUR == 22 then -0.140000
    else -0.130000;
def q3 =
    if HOUR == 0 then 0.120000
    else if HOUR == 1 then 0.299995
    else if HOUR == 2 then 0.320000
    else if HOUR == 3 then 0.320000
    else if HOUR == 4 then 0.417500
    else if HOUR == 5 then 0.169998
    else if HOUR == 6 then 0.290001
    else if HOUR == 7 then 0.409996
    else if HOUR == 8 then 0.750000
    else if HOUR == 9 then 0.550003
    else if HOUR == 10 then 0.579998
    else if HOUR == 11 then 0.480003
    else if HOUR == 12 then 0.470001
    else if HOUR == 13 then 0.279999
    else if HOUR == 14 then 0.187502
    else if HOUR == 15 then 0.120003
    else if HOUR == 16 then 0.132504
    else if HOUR == 17 then 0.000000
    else if HOUR == 18 then 0.110001
    else if HOUR == 19 then 0.199997
    else if HOUR == 20 then 0.327501
    else if HOUR == 21 then 0.300003
    else if HOUR == 22 then 0.239998
    else 0.050000;
def m2 =
    if HOUR == 0 then 0.000000
    else if HOUR == 1 then 0.070000
    else if HOUR == 2 then 0.050000
    else if HOUR == 3 then -0.100000
    else if HOUR == 4 then 0.030000
    else if HOUR == 5 then -0.100000
    else if HOUR == 6 then -0.020000
    else if HOUR == 7 then 0.100000
    else if HOUR == 8 then 0.120000
    else if HOUR == 9 then -0.030000
    else if HOUR == 10 then 0.100000
    else if HOUR == 11 then 0.030000
    else if HOUR == 12 then 0.030000
    else if HOUR == 13 then -0.020000
    else if HOUR == 14 then -0.070000
    else if HOUR == 15 then -0.050000
    else if HOUR == 16 then -0.020000
    else if HOUR == 17 then 0.000000
    else if HOUR == 18 then -0.020000
    else if HOUR == 19 then 0.020000
    else if HOUR == 20 then 0.040000
    else if HOUR == 21 then 0.070000
    else if HOUR == 22 then 0.010000
    else -0.040000;

def midline = hl2(period = AggregationPeriod.HOUR)[1];
def median = hl2(period = AggregationPeriod.HOUR)[1] + m2;
def upper = median + (q3);
def lower = median + (q1);

plot c = midline;
c.setStyle(CURve.SHORT_DASH);
c.setDefaultColor(getColor(7));

plot median_line = median[barsPerHour];
median_line.SetPaintingStrategy(PaintingStrategy.DASHES);
median_line.SetDefaultColor(getColor(3));
plot expected_upper = upper[barsPerHour];
expected_upper.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
expected_upper.SetDefaultColor(getColor(1));
plot expected_lower = lower[barsPerHour];
expected_lower.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
expected_lower.SetDefaultColor(getColor(5));

addcloud(expected_upper, expected_lower, color.white, color.white);
def upside = (expected_upper[(-1 * barsPerHour)] - close);
def downside = (close - expected_lower[(-1 * barsPerHour)]);
addLabel(yes, "current upside: " + upside + "  current downside: " + downside, if upside > downside then color.dark_green else if downside > upside then color.dark_red else color.dark_gray);

def dist_to_median = (median_line[(-1 * barsPerHour)] - close);
addLabel(yes, "Distance to Median: " + dist_to_median, 
if dist_to_median > 5 then color.green 
else if dist_to_median > 1 then color.dark_green 
else if dist_to_median < -5 then color.red
else if dist_to_median < -1 then color.dark_red 
else color.gray);
 
@mashume this is a great indicator, thank you for sharing...

is there a way to show arrows once the price crosses below or over the C or the median_line? I have tried adding this below code to your code but price seems to not be recognized..

plot Buy;
plot Sell;
Buy = if price crosses above c then c else Double.NaN;
Sell = if price crosses below median_line then median_line else Double.NaN;

Buy.SetDefaultColor(Color.UPTICK);
Buy.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
Buy.HideBubble();
Sell.SetDefaultColor(Color.DOWNTICK);
Sell.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
Sell.HideBubble();
 
@sumiran

SPX

Code:
#########################################################
#
#   Next Hour SPX Trading Range
#     Algorithmic Prediction
#
#   Values Derived on Data from
#          2022-07-14 to
#          2022-10-06
#
#   @mashume at usethinkscript.com
#
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#
#  This Version /SPX only
#  Functional on 1, 2, 3, 5, 10, 15, 20, 30, 60 minute charts
#
#########################################################

declare upper;

def Y = 31556926;
def M = 2629743;
def D = 86400;
def H = 3600;
def HalfHour = 1800;
def epoch = (getTime() / 1000);
def YMD = (epoch % Y) - (epoch / Y);
def month = (Floor(YMD / M) + 1);
def GMT = Floor((epoch % D) / H);
def HOUR = if GMT > 4 then GMT - 4 else if GMT < 4 then (GMT + 24) - 4 else 0;

def tf = getAggregationPeriod();

def barsPerHour = if tf == AggregationPeriod.HOUR then 1
    else if tf == AggregationPeriod.THIRTY_MIN then 2
    else if tf == AggregationPeriod.TWENTY_MIN then 3
    else if tf == AggregationPeriod.FIFTEEN_MIN then 4
    else if tf == AggregationPeriod.TEN_MIN then 6
    else if tf == AggregationPeriod.FIVE_MIN then 12
    else if tf == AggregationPeriod.THREE_MIN then 20
    else if tf == AggregationPeriod.TWO_MIN then 30
    else if tf == AggregationPeriod.MIN then 60
    else double.nan;
    

def q1 =
    if HOUR == 0 then 0.000000
    else if HOUR == 1 then 0.000000
    else if HOUR == 2 then 0.000000
    else if HOUR == 3 then 0.000000
    else if HOUR == 4 then 0.000000
    else if HOUR == 5 then 0.000000
    else if HOUR == 6 then 0.000000
    else if HOUR == 7 then 0.000000
    else if HOUR == 8 then 0.000000
    else if HOUR == 9 then -9.003000
    else if HOUR == 10 then -8.288000
    else if HOUR == 11 then -8.785000
    else if HOUR == 12 then -8.760000
    else if HOUR == 13 then -8.925000
    else if HOUR == 14 then -3.343000
    else if HOUR == 15 then -13.458000
    else if HOUR == 16 then -23.940000
    else if HOUR == 17 then -25.770000
    else if HOUR == 18 then -17.950000
    else if HOUR == 19 then 96.680000
    else if HOUR == 20 then 0.000000
    else if HOUR == 21 then 0.000000
    else if HOUR == 22 then 0.000000
    else 0.000000;
def q3 =
    if HOUR == 0 then 0.000000
    else if HOUR == 1 then 0.000000
    else if HOUR == 2 then 0.000000
    else if HOUR == 3 then 0.000000
    else if HOUR == 4 then 0.000000
    else if HOUR == 5 then 0.000000
    else if HOUR == 6 then 0.000000
    else if HOUR == 7 then 0.000000
    else if HOUR == 8 then 0.000000
    else if HOUR == 9 then 15.467529
    else if HOUR == 10 then 12.372559
    else if HOUR == 11 then 7.390137
    else if HOUR == 12 then 8.089905
    else if HOUR == 13 then 7.082642
    else if HOUR == 14 then 10.679932
    else if HOUR == 15 then 11.575027
    else if HOUR == 16 then 28.037527
    else if HOUR == 17 then 31.705015
    else if HOUR == 18 then 43.319985
    else if HOUR == 19 then 96.680107
    else if HOUR == 20 then 0.000000
    else if HOUR == 21 then 0.000000
    else if HOUR == 22 then 0.000000
    else 0.000000;
def m2 =
    if HOUR == 0 then 0.000000
    else if HOUR == 1 then 0.000000
    else if HOUR == 2 then 0.000000
    else if HOUR == 3 then 0.000000
    else if HOUR == 4 then 0.000000
    else if HOUR == 5 then 0.000000
    else if HOUR == 6 then 0.000000
    else if HOUR == 7 then 0.000000
    else if HOUR == 8 then 0.000000
    else if HOUR == 9 then 1.265000
    else if HOUR == 10 then 2.990000
    else if HOUR == 11 then -0.470000
    else if HOUR == 12 then 1.705000
    else if HOUR == 13 then -0.330000
    else if HOUR == 14 then 4.105000
    else if HOUR == 15 then -0.850000
    else if HOUR == 16 then -2.295000
    else if HOUR == 17 then -3.150000
    else if HOUR == 18 then -11.090000
    else if HOUR == 19 then 96.680000
    else if HOUR == 20 then 0.000000
    else if HOUR == 21 then 0.000000
    else if HOUR == 22 then 0.000000
    else 0.000000;

def midline = hl2(period = AggregationPeriod.HOUR)[1];
def median = hl2(period = AggregationPeriod.HOUR)[1] + m2;
def upper = median + (q3);
def lower = median + (q1);

plot c = midline;
c.setStyle(CURve.SHORT_DASH);
c.setDefaultColor(getColor(7));

plot median_line = median[barsPerHour];
median_line.SetPaintingStrategy(PaintingStrategy.DASHES);
median_line.SetDefaultColor(getColor(3));
plot expected_upper = upper[barsPerHour];
expected_upper.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
expected_upper.SetDefaultColor(getColor(1));
plot expected_lower = lower[barsPerHour];
expected_lower.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
expected_lower.SetDefaultColor(getColor(5));

addcloud(expected_upper, expected_lower, color.white, color.white);
def upside = (expected_upper[(-1 * barsPerHour)] - close);
def downside = (close - expected_lower[(-1 * barsPerHour)]);
addLabel(yes, "current upside: " + upside + "  current downside: " + downside, if upside > downside then color.dark_green else if downside > upside then color.dark_red else color.dark_gray);

def dist_to_median = (median_line[(-1 * barsPerHour)] - close);
addLabel(yes, "Distance to Median: " + dist_to_median, 
if dist_to_median > 5 then color.green 
else if dist_to_median > 1 then color.dark_green 
else if dist_to_median < -5 then color.red
else if dist_to_median < -1 then color.dark_red 
else color.gray);
 
@sumiran

SPX

Code:
#########################################################
#
#   Next Hour SPX Trading Range
#     Algorithmic Prediction
#
#   Values Derived on Data from
#          2022-07-14 to
#          2022-10-06
#
#   @mashume at usethinkscript.com
#
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#
#  This Version /SPX only
#  Functional on 1, 2, 3, 5, 10, 15, 20, 30, 60 minute charts
#
#########################################################

declare upper;

def Y = 31556926;
def M = 2629743;
def D = 86400;
def H = 3600;
def HalfHour = 1800;
def epoch = (getTime() / 1000);
def YMD = (epoch % Y) - (epoch / Y);
def month = (Floor(YMD / M) + 1);
def GMT = Floor((epoch % D) / H);
def HOUR = if GMT > 4 then GMT - 4 else if GMT < 4 then (GMT + 24) - 4 else 0;

def tf = getAggregationPeriod();

def barsPerHour = if tf == AggregationPeriod.HOUR then 1
    else if tf == AggregationPeriod.THIRTY_MIN then 2
    else if tf == AggregationPeriod.TWENTY_MIN then 3
    else if tf == AggregationPeriod.FIFTEEN_MIN then 4
    else if tf == AggregationPeriod.TEN_MIN then 6
    else if tf == AggregationPeriod.FIVE_MIN then 12
    else if tf == AggregationPeriod.THREE_MIN then 20
    else if tf == AggregationPeriod.TWO_MIN then 30
    else if tf == AggregationPeriod.MIN then 60
    else double.nan;
   

def q1 =
    if HOUR == 0 then 0.000000
    else if HOUR == 1 then 0.000000
    else if HOUR == 2 then 0.000000
    else if HOUR == 3 then 0.000000
    else if HOUR == 4 then 0.000000
    else if HOUR == 5 then 0.000000
    else if HOUR == 6 then 0.000000
    else if HOUR == 7 then 0.000000
    else if HOUR == 8 then 0.000000
    else if HOUR == 9 then -9.003000
    else if HOUR == 10 then -8.288000
    else if HOUR == 11 then -8.785000
    else if HOUR == 12 then -8.760000
    else if HOUR == 13 then -8.925000
    else if HOUR == 14 then -3.343000
    else if HOUR == 15 then -13.458000
    else if HOUR == 16 then -23.940000
    else if HOUR == 17 then -25.770000
    else if HOUR == 18 then -17.950000
    else if HOUR == 19 then 96.680000
    else if HOUR == 20 then 0.000000
    else if HOUR == 21 then 0.000000
    else if HOUR == 22 then 0.000000
    else 0.000000;
def q3 =
    if HOUR == 0 then 0.000000
    else if HOUR == 1 then 0.000000
    else if HOUR == 2 then 0.000000
    else if HOUR == 3 then 0.000000
    else if HOUR == 4 then 0.000000
    else if HOUR == 5 then 0.000000
    else if HOUR == 6 then 0.000000
    else if HOUR == 7 then 0.000000
    else if HOUR == 8 then 0.000000
    else if HOUR == 9 then 15.467529
    else if HOUR == 10 then 12.372559
    else if HOUR == 11 then 7.390137
    else if HOUR == 12 then 8.089905
    else if HOUR == 13 then 7.082642
    else if HOUR == 14 then 10.679932
    else if HOUR == 15 then 11.575027
    else if HOUR == 16 then 28.037527
    else if HOUR == 17 then 31.705015
    else if HOUR == 18 then 43.319985
    else if HOUR == 19 then 96.680107
    else if HOUR == 20 then 0.000000
    else if HOUR == 21 then 0.000000
    else if HOUR == 22 then 0.000000
    else 0.000000;
def m2 =
    if HOUR == 0 then 0.000000
    else if HOUR == 1 then 0.000000
    else if HOUR == 2 then 0.000000
    else if HOUR == 3 then 0.000000
    else if HOUR == 4 then 0.000000
    else if HOUR == 5 then 0.000000
    else if HOUR == 6 then 0.000000
    else if HOUR == 7 then 0.000000
    else if HOUR == 8 then 0.000000
    else if HOUR == 9 then 1.265000
    else if HOUR == 10 then 2.990000
    else if HOUR == 11 then -0.470000
    else if HOUR == 12 then 1.705000
    else if HOUR == 13 then -0.330000
    else if HOUR == 14 then 4.105000
    else if HOUR == 15 then -0.850000
    else if HOUR == 16 then -2.295000
    else if HOUR == 17 then -3.150000
    else if HOUR == 18 then -11.090000
    else if HOUR == 19 then 96.680000
    else if HOUR == 20 then 0.000000
    else if HOUR == 21 then 0.000000
    else if HOUR == 22 then 0.000000
    else 0.000000;

def midline = hl2(period = AggregationPeriod.HOUR)[1];
def median = hl2(period = AggregationPeriod.HOUR)[1] + m2;
def upper = median + (q3);
def lower = median + (q1);

plot c = midline;
c.setStyle(CURve.SHORT_DASH);
c.setDefaultColor(getColor(7));

plot median_line = median[barsPerHour];
median_line.SetPaintingStrategy(PaintingStrategy.DASHES);
median_line.SetDefaultColor(getColor(3));
plot expected_upper = upper[barsPerHour];
expected_upper.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
expected_upper.SetDefaultColor(getColor(1));
plot expected_lower = lower[barsPerHour];
expected_lower.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
expected_lower.SetDefaultColor(getColor(5));

addcloud(expected_upper, expected_lower, color.white, color.white);
def upside = (expected_upper[(-1 * barsPerHour)] - close);
def downside = (close - expected_lower[(-1 * barsPerHour)]);
addLabel(yes, "current upside: " + upside + "  current downside: " + downside, if upside > downside then color.dark_green else if downside > upside then color.dark_red else color.dark_gray);

def dist_to_median = (median_line[(-1 * barsPerHour)] - close);
addLabel(yes, "Distance to Median: " + dist_to_median,
if dist_to_median > 5 then color.green
else if dist_to_median > 1 then color.dark_green
else if dist_to_median < -5 then color.red
else if dist_to_median < -1 then color.dark_red
else color.gray);
thank you, I will give it a try.
 
@mashume Is there anyway you think one can use this for an overnight strategy. Going long or short SPX just before the close of the cash session? I know it says 1 hour but most of the bigger returns come overnight.
 
@mashume Is there anyway you think one can use this for an overnight strategy. Going long or short SPX just before the close of the cash session? I know it says 1 hour but most of the bigger returns come overnight.
You're correct that many of the largest moves do happen over night. however this indicator really doesn't predict which way the market will move... it only shows the average moves from the last hour.

I did toy around with alternate versions of this that worked by finding typical ranges for the next hour when this hour was up or down, and one version where it looked at two hours (up-up, down-up, etc..) and one that looked at whether the up/down hour was "large" or "small" in magnitude. but I ran into a lack of data as the y-finance api will only return 60 days at 5 minutes.

If someone is sitting on years of 5m data, I'll be happy to run the script against it with a different set of conditions, but at some point, patterns are not patterns just random events because you don't have enough data to see the patterns.

-mashume
 
The code for the indicator is generated by a python script somewhere in this thread. It works on 2min I think (depends on the version... I think I updated it, but perhaps not... I'll check sometime).

There is no facility in ThinkOrSwim to generate the statistics that are hard coded into the indicator. That's why the python code exists. Python downloads the 5m data from YFinance, does some statistical magic, and then spits out ThinkScript code that you paste into the code editor in ThinkOrSwim and you have an updated indicator.

I'll run off a new /ES for 'yall ... just a moment.

FYI, if you're happy with the performance of the indicator as you're using it, there is no need to update. Updates are useful for seasonal changes, or just market mood changes. There is a post in here about volatility before and after some event or other... it makes for some interesting statistics. If you're into that sort of thing.

Code:
#########################################################
#
#   Next Hour ES Trading Range
#     Algorithmic Prediction
#
#   Values Derived on Data from
#          2022-07-06 to
#          2022-09-13
#
#   @mashume at usethinkscript.com
#
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#
#  This Version /ES only
#  Functional on 1, 2, 3, 5, 10, 15, 20, 30, 60 minute charts
#
#########################################################

declare upper;

def Y = 31556926;
def M = 2629743;
def D = 86400;
def H = 3600;
def HalfHour = 1800;
def epoch = (getTime() / 1000);
def YMD = (epoch % Y) - (epoch / Y);
def month = (Floor(YMD / M) + 1);
def GMT = Floor((epoch % D) / H);
def HOUR = if GMT > 4 then GMT - 4 else if GMT < 4 then (GMT + 24) - 4 else 0;

def tf = getAggregationPeriod();

def barsPerHour = if tf == AggregationPeriod.HOUR then 1
    else if tf == AggregationPeriod.THIRTY_MIN then 2
    else if tf == AggregationPeriod.TWENTY_MIN then 3
    else if tf == AggregationPeriod.FIFTEEN_MIN then 4
    else if tf == AggregationPeriod.TEN_MIN then 6
    else if tf == AggregationPeriod.FIVE_MIN then 12
    else if tf == AggregationPeriod.THREE_MIN then 20
    else if tf == AggregationPeriod.TWO_MIN then 30
    else if tf == AggregationPeriod.MIN then 60
    else double.nan;
  

def q1 =
    if HOUR == 0 then -2.562000
    else if HOUR == 1 then -3.250000
    else if HOUR == 2 then -4.188000
    else if HOUR == 3 then -5.250000
    else if HOUR == 4 then -3.750000
    else if HOUR == 5 then -3.500000
    else if HOUR == 6 then -4.250000
    else if HOUR == 7 then -5.000000
    else if HOUR == 8 then -7.750000
    else if HOUR == 9 then -9.500000
    else if HOUR == 10 then -7.250000
    else if HOUR == 11 then -7.250000
    else if HOUR == 12 then -6.750000
    else if HOUR == 13 then -7.312000
    else if HOUR == 14 then -4.500000
    else if HOUR == 15 then -6.750000
    else if HOUR == 16 then -4.500000
    else if HOUR == 17 then -9.375000
    else if HOUR == 18 then -2.500000
    else if HOUR == 19 then -4.250000
    else if HOUR == 20 then -3.250000
    else if HOUR == 21 then -1.750000
    else if HOUR == 22 then -1.500000
    else -2.000000;
def q3 =
    if HOUR == 0 then 2.500000
    else if HOUR == 1 then 3.000000
    else if HOUR == 2 then 5.750000
    else if HOUR == 3 then 5.750000
    else if HOUR == 4 then 4.250000
    else if HOUR == 5 then 4.500000
    else if HOUR == 6 then 3.500000
    else if HOUR == 7 then 5.000000
    else if HOUR == 8 then 8.312500
    else if HOUR == 9 then 13.750000
    else if HOUR == 10 then 12.750000
    else if HOUR == 11 then 7.500000
    else if HOUR == 12 then 9.250000
    else if HOUR == 13 then 7.500000
    else if HOUR == 14 then 8.062500
    else if HOUR == 15 then 6.750000
    else if HOUR == 16 then 2.250000
    else if HOUR == 17 then -6.125000
    else if HOUR == 18 then 2.000000
    else if HOUR == 19 then 2.000000
    else if HOUR == 20 then 3.500000
    else if HOUR == 21 then 3.500000
    else if HOUR == 22 then 3.000000
    else 1.812000;
def m2 =
    if HOUR == 0 then -0.500000
    else if HOUR == 1 then -0.250000
    else if HOUR == 2 then 0.500000
    else if HOUR == 3 then -0.125000
    else if HOUR == 4 then -0.250000
    else if HOUR == 5 then 0.000000
    else if HOUR == 6 then -0.250000
    else if HOUR == 7 then 0.500000
    else if HOUR == 8 then 0.500000
    else if HOUR == 9 then 1.250000
    else if HOUR == 10 then 3.250000
    else if HOUR == 11 then 0.750000
    else if HOUR == 12 then 2.375000
    else if HOUR == 13 then 0.250000
    else if HOUR == 14 then 2.250000
    else if HOUR == 15 then 0.250000
    else if HOUR == 16 then -1.250000
    else if HOUR == 17 then -8.000000
    else if HOUR == 18 then -0.250000
    else if HOUR == 19 then -1.250000
    else if HOUR == 20 then 0.500000
    else if HOUR == 21 then 1.000000
    else if HOUR == 22 then 0.750000
    else 0.250000;

def midline = hl2(period = AggregationPeriod.HOUR)[1];
def median = hl2(period = AggregationPeriod.HOUR)[1] + m2;
def upper = median + (q3);
def lower = median + (q1);

plot c = midline;
c.setStyle(CURve.SHORT_DASH);
c.setDefaultColor(getColor(7));

plot median_line = median[barsPerHour];
median_line.SetPaintingStrategy(PaintingStrategy.DASHES);
median_line.SetDefaultColor(getColor(3));
plot expected_upper = upper[barsPerHour];
expected_upper.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
expected_upper.SetDefaultColor(getColor(1));
plot expected_lower = lower[barsPerHour];
expected_lower.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
expected_lower.SetDefaultColor(getColor(5));

addcloud(expected_upper, expected_lower, color.white, color.white);
def upside = (expected_upper[(-1 * barsPerHour)] - close);
def downside = (close - expected_lower[(-1 * barsPerHour)]);
addLabel(yes, "current upside: " + upside + "  current downside: " + downside, if upside > downside then color.dark_green else if downside > upside then color.dark_red else color.dark_gray);

def dist_to_median = (median_line[(-1 * barsPerHour)] - close);
addLabel(yes, "Distance to Median: " + dist_to_median,
if dist_to_median > 5 then color.green
else if dist_to_median > 1 then color.dark_green
else if dist_to_median < -5 then color.red
else if dist_to_median < -1 then color.dark_red
else color.gray);

happy trading,
mashume
Hi,
I'm not a coder so i'm not sure how the python script works. From what I understood, you used python script to generate this code above or you had to write the code based on data that was provided by python? How can i use the python script on TOS? I will try to look for python script in this thred.
 
Hi,
I'm not a coder so i'm not sure how the python script works. From what I understood, you used python script to generate this code above or you had to write the code based on data that was provided by python? How can i use the python script on TOS? I will try to look for python script in this thred.
The python script will not work in ToS. You need the output of the python notebook (ThinkScript code) to run in ToS.

The python code prints out (to the screen) the complete code for the indicator. You just copy and paste the output (looks like a regular ThiknScript indicator code) into the code editor in ToS.

-mashume
 
I had to manually install yfinance module using this:
pip install -i https://pypi.anaconda.org/ranaroussi/simple yfinance

I ran all pieces of code manually, the resulting script still shows:

# Values Derived on Data from
# 2021-02-03 to
# 2021-04-15

Are those hard coded? and not meant to be updated?

Also, I checked the last two piece (ie, head and tail, they are updating correctly though.

Would it behave differently depending on timezone on ToS?

I am not a programmer, but i can logically try to follow code, and sometimes can get to understand a bit. However lost here trying to hack something rather simple from your code.

I just want to lookback past 10 days and calculate the

mean/median/max/min/q1 and q2 for a given instrument for the whole day (not just an hour). Would be great if script can multiply % with the close value, to give actual levels.
 
Last edited:
I had to manually install yfinance module using this:
pip install -i https://pypi.anaconda.org/ranaroussi/simple yfinance

I ran all pieces of code manually, the resulting script still shows:

# Values Derived on Data from
# 2021-02-03 to
# 2021-04-15

Are those hard coded? and not meant to be updated?

Also, I checked the last two piece (ie, head and tail, they are updating correctly though.

Would it behave differently depending on timezone on ToS?

I am not a programmer, but i can logically try to follow code, and sometimes can get to understand a bit. However lost here trying to hack something rather simple from your code.

I just want to lookback past 10 days and calculate the

mean/median/max/min/q1 and q2 for a given instrument for the whole day (not just an hour). Would be great if script can multiply % with the close value, to give actual levels.
I think I fixed the dates to capture live dates from the data you use. If the script doesn't look updated, do let me know.
 
Wish there was a way to DM you on this forum, but will really appreciate if you can solve this (rather) smaller problem:

------------
I just want to lookback past 10 days and calculate the

mean/median/max/min/q1 and q2 for a given instrument for the whole day (not just an hour). Would be great if script can multiply % with the close value, to give actual levels.
-------------

Also, I am assuming right now mathematically it is using standard probability distribution, is there a library or function that we can use to calculate omega distrubution?

Your code for this rather simple (simple for you) example, would assist a lot for newbies like myself to hack/amend/etc. Also, have you shared rest of your code/library anywhere?
 
@mashume can you please post this code for ticker:AVGO please thank you

AVGO

Code:
#########################################################
#
#   Next Hour AVGO Trading Range
#     Algorithmic Prediction
#
#   Values Derived on Data from
#          2022-08-30 to
#          2022-11-22
#
#   @mashume at usethinkscript.com
#
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#
#  This Version AVGO only
#  Functional on 1, 2, 3, 5, 10, 15, 20, 30, 60 minute charts
#
#########################################################

declare upper;

def Y = 31556926;
def M = 2629743;
def D = 86400;
def H = 3600;
def HalfHour = 1800;
def epoch = (getTime() / 1000);
def YMD = (epoch % Y) - (epoch / Y);
def month = (Floor(YMD / M) + 1);
def GMT = Floor((epoch % D) / H);
def HOUR = if GMT > 4 then GMT - 4 else if GMT < 4 then (GMT + 24) - 4 else 0;

def tf = getAggregationPeriod();

def barsPerHour = if tf == AggregationPeriod.HOUR then 1
    else if tf == AggregationPeriod.THIRTY_MIN then 2
    else if tf == AggregationPeriod.TWENTY_MIN then 3
    else if tf == AggregationPeriod.FIFTEEN_MIN then 4
    else if tf == AggregationPeriod.TEN_MIN then 6
    else if tf == AggregationPeriod.FIVE_MIN then 12
    else if tf == AggregationPeriod.THREE_MIN then 20
    else if tf == AggregationPeriod.TWO_MIN then 30
    else if tf == AggregationPeriod.MIN then 60
    else double.nan;
    

def q1 =
    if HOUR == 0 then 0.000000
    else if HOUR == 1 then 0.000000
    else if HOUR == 2 then 0.000000
    else if HOUR == 3 then 0.000000
    else if HOUR == 4 then -2.560000
    else if HOUR == 5 then -1.255000
    else if HOUR == 6 then -1.310000
    else if HOUR == 7 then -1.410000
    else if HOUR == 8 then -3.020000
    else if HOUR == 9 then -2.908000
    else if HOUR == 10 then -1.834000
    else if HOUR == 11 then -2.096000
    else if HOUR == 12 then -1.650000
    else if HOUR == 13 then -1.662000
    else if HOUR == 14 then -1.011000
    else if HOUR == 15 then -1.529000
    else if HOUR == 16 then -0.448000
    else if HOUR == 17 then -0.528000
    else if HOUR == 18 then -1.158000
    else if HOUR == 19 then -2.510000
    else if HOUR == 20 then 0.000000
    else if HOUR == 21 then 0.000000
    else if HOUR == 22 then 0.000000
    else 0.000000;
def q3 =
    if HOUR == 0 then 0.000000
    else if HOUR == 1 then 0.000000
    else if HOUR == 2 then 0.000000
    else if HOUR == 3 then 0.000000
    else if HOUR == 4 then 1.367500
    else if HOUR == 5 then 1.545000
    else if HOUR == 6 then 2.050000
    else if HOUR == 7 then 1.744993
    else if HOUR == 8 then 2.700378
    else if HOUR == 9 then 3.667511
    else if HOUR == 10 then 2.401245
    else if HOUR == 11 then 0.930321
    else if HOUR == 12 then 1.465012
    else if HOUR == 13 then 1.549942
    else if HOUR == 14 then 2.152565
    else if HOUR == 15 then 1.622504
    else if HOUR == 16 then 0.510000
    else if HOUR == 17 then 0.650000
    else if HOUR == 18 then 1.300000
    else if HOUR == 19 then 2.290000
    else if HOUR == 20 then 0.000000
    else if HOUR == 21 then 0.000000
    else if HOUR == 22 then 0.000000
    else 0.000000;
def m2 =
    if HOUR == 0 then 0.000000
    else if HOUR == 1 then 0.000000
    else if HOUR == 2 then 0.000000
    else if HOUR == 3 then 0.000000
    else if HOUR == 4 then -0.485000
    else if HOUR == 5 then 0.340000
    else if HOUR == 6 then 0.290000
    else if HOUR == 7 then 0.220000
    else if HOUR == 8 then -0.063000
    else if HOUR == 9 then 0.580000
    else if HOUR == 10 then 0.270000
    else if HOUR == 11 then -0.583000
    else if HOUR == 12 then -0.123000
    else if HOUR == 13 then 0.047000
    else if HOUR == 14 then 0.489000
    else if HOUR == 15 then -0.002000
    else if HOUR == 16 then 0.035000
    else if HOUR == 17 then 0.069000
    else if HOUR == 18 then 0.005000
    else if HOUR == 19 then -0.360000
    else if HOUR == 20 then 0.000000
    else if HOUR == 21 then 0.000000
    else if HOUR == 22 then 0.000000
    else 0.000000;

def midline = hl2(period = AggregationPeriod.HOUR)[1];
def median = hl2(period = AggregationPeriod.HOUR)[1] + m2;
def upper = median + (q3);
def lower = median + (q1);

plot c = midline;
c.setStyle(CURve.SHORT_DASH);
c.setDefaultColor(getColor(7));

plot median_line = median[barsPerHour];
median_line.SetPaintingStrategy(PaintingStrategy.DASHES);
median_line.SetDefaultColor(getColor(3));
plot expected_upper = upper[barsPerHour];
expected_upper.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
expected_upper.SetDefaultColor(getColor(1));
plot expected_lower = lower[barsPerHour];
expected_lower.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
expected_lower.SetDefaultColor(getColor(5));

addcloud(expected_upper, expected_lower, color.white, color.white);
def upside = (expected_upper[(-1 * barsPerHour)] - close);
def downside = (close - expected_lower[(-1 * barsPerHour)]);
addLabel(yes, "current upside: " + upside + "  current downside: " + downside, if upside > downside then color.dark_green else if downside > upside then color.dark_red else color.dark_gray);

def dist_to_median = (median_line[(-1 * barsPerHour)] - close);
addLabel(yes, "Distance to Median: " + dist_to_median, 
if dist_to_median > 5 then color.green 
else if dist_to_median > 1 then color.dark_green 
else if dist_to_median < -5 then color.red
else if dist_to_median < -1 then color.dark_red 
else color.gray);
 
You're correct that many of the largest moves do happen over night. however this indicator really doesn't predict which way the market will move... it only shows the average moves from the last hour.

I did toy around with alternate versions of this that worked by finding typical ranges for the next hour when this hour was up or down, and one version where it looked at two hours (up-up, down-up, etc..) and one that looked at whether the up/down hour was "large" or "small" in magnitude. but I ran into a lack of data as the y-finance api will only return 60 days at 5 minutes.

If someone is sitting on years of 5m data, I'll be happy to run the script against it with a different set of conditions, but at some point, patterns are not patterns just random events because you don't have enough data to see the patterns.

-mashume
Do we need to updated it for ES, NQ Futures and Is there a way to use this for an overnight (Globex) strategy?
 
Last edited:
Do we need to updated it for ES, NQ Futures and Is there a way to use this for an overnight (Globex) strategy?
Not sure about how to make a strategy from this... but here are some updated versions:

ES and MES

Code:
#########################################################
#
#   Next Hour /ES Trading Range
#     Algorithmic Prediction
#
#   Values Derived on Data from
#          2022-09-18 to
#          2022-11-25
#
#   @mashume at usethinkscript.com
#
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#
#  This Version /ES and maybe /MES only
#  Functional on 1, 2, 3, 5, 10, 15, 20, 30, 60 minute charts
#
#########################################################

declare upper;

def Y = 31556926;
def M = 2629743;
def D = 86400;
def H = 3600;
def HalfHour = 1800;
def epoch = (getTime() / 1000);
def YMD = (epoch % Y) - (epoch / Y);
def month = (Floor(YMD / M) + 1);
def GMT = Floor((epoch % D) / H);
def HOUR = if GMT > 4 then GMT - 4 else if GMT < 4 then (GMT + 24) - 4 else 0;

def tf = getAggregationPeriod();

def barsPerHour = if tf == AggregationPeriod.HOUR then 1
    else if tf == AggregationPeriod.THIRTY_MIN then 2
    else if tf == AggregationPeriod.TWENTY_MIN then 3
    else if tf == AggregationPeriod.FIFTEEN_MIN then 4
    else if tf == AggregationPeriod.TEN_MIN then 6
    else if tf == AggregationPeriod.FIVE_MIN then 12
    else if tf == AggregationPeriod.THREE_MIN then 20
    else if tf == AggregationPeriod.TWO_MIN then 30
    else if tf == AggregationPeriod.MIN then 60
    else double.nan;
   

def q1 =
    if HOUR == 0 then -2.562000
    else if HOUR == 1 then -4.250000
    else if HOUR == 2 then -6.500000
    else if HOUR == 3 then -7.500000
    else if HOUR == 4 then -7.250000
    else if HOUR == 5 then -4.000000
    else if HOUR == 6 then -3.250000
    else if HOUR == 7 then -6.500000
    else if HOUR == 8 then -8.812000
    else if HOUR == 9 then -11.750000
    else if HOUR == 10 then -8.250000
    else if HOUR == 11 then -10.812000
    else if HOUR == 12 then -8.750000
    else if HOUR == 13 then -8.750000
    else if HOUR == 14 then -5.000000
    else if HOUR == 15 then -9.750000
    else if HOUR == 16 then -4.562000
    else if HOUR == 17 then -13.062000
    else if HOUR == 18 then -2.812000
    else if HOUR == 19 then -3.062000
    else if HOUR == 20 then -4.250000
    else if HOUR == 21 then -3.250000
    else if HOUR == 22 then -2.000000
    else -2.250000;
def q3 =
    if HOUR == 0 then 3.500000
    else if HOUR == 1 then 3.750000
    else if HOUR == 2 then 4.562500
    else if HOUR == 3 then 5.500000
    else if HOUR == 4 then 4.750000
    else if HOUR == 5 then 6.500000
    else if HOUR == 6 then 6.312500
    else if HOUR == 7 then 6.750000
    else if HOUR == 8 then 9.250000
    else if HOUR == 9 then 17.500000
    else if HOUR == 10 then 11.000000
    else if HOUR == 11 then 7.812500
    else if HOUR == 12 then 8.000000
    else if HOUR == 13 then 10.062500
    else if HOUR == 14 then 13.312500
    else if HOUR == 15 then 6.750000
    else if HOUR == 16 then 3.500000
    else if HOUR == 17 then -3.562500
    else if HOUR == 18 then 4.250000
    else if HOUR == 19 then 3.750000
    else if HOUR == 20 then 4.000000
    else if HOUR == 21 then 3.250000
    else if HOUR == 22 then 2.750000
    else 3.000000;
def m2 =
    if HOUR == 0 then 0.250000
    else if HOUR == 1 then 0.250000
    else if HOUR == 2 then -1.750000
    else if HOUR == 3 then -0.750000
    else if HOUR == 4 then -0.750000
    else if HOUR == 5 then 1.500000
    else if HOUR == 6 then 1.500000
    else if HOUR == 7 then 0.000000
    else if HOUR == 8 then -1.750000
    else if HOUR == 9 then 1.875000
    else if HOUR == 10 then 1.500000
    else if HOUR == 11 then -1.000000
    else if HOUR == 12 then -0.250000
    else if HOUR == 13 then 1.125000
    else if HOUR == 14 then 3.250000
    else if HOUR == 15 then -0.500000
    else if HOUR == 16 then 0.250000
    else if HOUR == 17 then -7.375000
    else if HOUR == 18 then 1.000000
    else if HOUR == 19 then 0.750000
    else if HOUR == 20 then -0.500000
    else if HOUR == 21 then 0.250000
    else if HOUR == 22 then 0.250000
    else 0.250000;

def midline = hl2(period = AggregationPeriod.HOUR)[1];
def median = hl2(period = AggregationPeriod.HOUR)[1] + m2;
def upper = median + (q3);
def lower = median + (q1);

plot c = midline;
c.setStyle(CURve.SHORT_DASH);
c.setDefaultColor(getColor(7));

plot median_line = median[barsPerHour];
median_line.SetPaintingStrategy(PaintingStrategy.DASHES);
median_line.SetDefaultColor(getColor(3));
plot expected_upper = upper[barsPerHour];
expected_upper.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
expected_upper.SetDefaultColor(getColor(1));
plot expected_lower = lower[barsPerHour];
expected_lower.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
expected_lower.SetDefaultColor(getColor(5));

addcloud(expected_upper, expected_lower, color.white, color.white);
def upside = (expected_upper[(-1 * barsPerHour)] - close);
def downside = (close - expected_lower[(-1 * barsPerHour)]);
addLabel(yes, "current upside: " + upside + "  current downside: " + downside, if upside > downside then color.dark_green else if downside > upside then color.dark_red else color.dark_gray);

def dist_to_median = (median_line[(-1 * barsPerHour)] - close);
addLabel(yes, "Distance to Median: " + dist_to_median,
if dist_to_median > 5 then color.green
else if dist_to_median > 1 then color.dark_green
else if dist_to_median < -5 then color.red
else if dist_to_median < -1 then color.dark_red
else color.gray);

NQ and MNQ

Code:
#########################################################
#
#   Next Hour /NQ Trading Range
#     Algorithmic Prediction
#
#   Values Derived on Data from
#          2022-09-18 to
#          2022-11-25
#
#   @mashume at usethinkscript.com
#
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#
#  This Version /NQ and maybe /MNQ only
#  Functional on 1, 2, 3, 5, 10, 15, 20, 30, 60 minute charts
#
#########################################################

declare upper;

def Y = 31556926;
def M = 2629743;
def D = 86400;
def H = 3600;
def HalfHour = 1800;
def epoch = (getTime() / 1000);
def YMD = (epoch % Y) - (epoch / Y);
def month = (Floor(YMD / M) + 1);
def GMT = Floor((epoch % D) / H);
def HOUR = if GMT > 4 then GMT - 4 else if GMT < 4 then (GMT + 24) - 4 else 0;

def tf = getAggregationPeriod();

def barsPerHour = if tf == AggregationPeriod.HOUR then 1
    else if tf == AggregationPeriod.THIRTY_MIN then 2
    else if tf == AggregationPeriod.TWENTY_MIN then 3
    else if tf == AggregationPeriod.FIFTEEN_MIN then 4
    else if tf == AggregationPeriod.TEN_MIN then 6
    else if tf == AggregationPeriod.FIVE_MIN then 12
    else if tf == AggregationPeriod.THREE_MIN then 20
    else if tf == AggregationPeriod.TWO_MIN then 30
    else if tf == AggregationPeriod.MIN then 60
    else double.nan;
   

def q1 =
    if HOUR == 0 then -9.812000
    else if HOUR == 1 then -16.062000
    else if HOUR == 2 then -22.812000
    else if HOUR == 3 then -28.312000
    else if HOUR == 4 then -25.250000
    else if HOUR == 5 then -14.500000
    else if HOUR == 6 then -13.812000
    else if HOUR == 7 then -20.812000
    else if HOUR == 8 then -41.688000
    else if HOUR == 9 then -56.312000
    else if HOUR == 10 then -32.750000
    else if HOUR == 11 then -40.812000
    else if HOUR == 12 then -32.500000
    else if HOUR == 13 then -30.625000
    else if HOUR == 14 then -19.500000
    else if HOUR == 15 then -34.625000
    else if HOUR == 16 then -18.125000
    else if HOUR == 17 then -94.250000
    else if HOUR == 18 then -10.562000
    else if HOUR == 19 then -12.750000
    else if HOUR == 20 then -16.000000
    else if HOUR == 21 then -13.750000
    else if HOUR == 22 then -8.250000
    else -9.750000;
def q3 =
    if HOUR == 0 then 11.375000
    else if HOUR == 1 then 14.750000
    else if HOUR == 2 then 16.750000
    else if HOUR == 3 then 17.125000
    else if HOUR == 4 then 17.125000
    else if HOUR == 5 then 22.250000
    else if HOUR == 6 then 21.500000
    else if HOUR == 7 then 25.312500
    else if HOUR == 8 then 38.062500
    else if HOUR == 9 then 69.000000
    else if HOUR == 10 then 46.812500
    else if HOUR == 11 then 30.500000
    else if HOUR == 12 then 29.500000
    else if HOUR == 13 then 36.312500
    else if HOUR == 14 then 50.125000
    else if HOUR == 15 then 24.250000
    else if HOUR == 16 then 10.812500
    else if HOUR == 17 then -59.250000
    else if HOUR == 18 then 15.750000
    else if HOUR == 19 then 13.500000
    else if HOUR == 20 then 15.312500
    else if HOUR == 21 then 10.500000
    else if HOUR == 22 then 9.000000
    else 12.062000;
def m2 =
    if HOUR == 0 then 0.250000
    else if HOUR == 1 then 0.375000
    else if HOUR == 2 then -5.750000
    else if HOUR == 3 then -5.375000
    else if HOUR == 4 then -5.250000
    else if HOUR == 5 then 3.250000
    else if HOUR == 6 then 4.250000
    else if HOUR == 7 then -0.500000
    else if HOUR == 8 then -7.750000
    else if HOUR == 9 then 0.000000
    else if HOUR == 10 then 5.375000
    else if HOUR == 11 then -7.625000
    else if HOUR == 12 then -1.000000
    else if HOUR == 13 then 1.750000
    else if HOUR == 14 then 8.625000
    else if HOUR == 15 then -2.875000
    else if HOUR == 16 then -0.750000
    else if HOUR == 17 then -76.750000
    else if HOUR == 18 then 2.625000
    else if HOUR == 19 then 2.875000
    else if HOUR == 20 then -1.875000
    else if HOUR == 21 then 0.250000
    else if HOUR == 22 then 0.500000
    else -0.250000;

def midline = hl2(period = AggregationPeriod.HOUR)[1];
def median = hl2(period = AggregationPeriod.HOUR)[1] + m2;
def upper = median + (q3);
def lower = median + (q1);

plot c = midline;
c.setStyle(CURve.SHORT_DASH);
c.setDefaultColor(getColor(7));

plot median_line = median[barsPerHour];
median_line.SetPaintingStrategy(PaintingStrategy.DASHES);
median_line.SetDefaultColor(getColor(3));
plot expected_upper = upper[barsPerHour];
expected_upper.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
expected_upper.SetDefaultColor(getColor(1));
plot expected_lower = lower[barsPerHour];
expected_lower.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
expected_lower.SetDefaultColor(getColor(5));

addcloud(expected_upper, expected_lower, color.white, color.white);
def upside = (expected_upper[(-1 * barsPerHour)] - close);
def downside = (close - expected_lower[(-1 * barsPerHour)]);
addLabel(yes, "current upside: " + upside + "  current downside: " + downside, if upside > downside then color.dark_green else if downside > upside then color.dark_red else color.dark_gray);

def dist_to_median = (median_line[(-1 * barsPerHour)] - close);
addLabel(yes, "Distance to Median: " + dist_to_median,
if dist_to_median > 5 then color.green
else if dist_to_median > 1 then color.dark_green
else if dist_to_median < -5 then color.red
else if dist_to_median < -1 then color.dark_red
else color.gray);
 
Not sure about how to make a strategy from this... but here are some updated versions:

ES and MES

Code:
#########################################################
#
#   Next Hour /ES Trading Range
#     Algorithmic Prediction
#
#   Values Derived on Data from
#          2022-09-18 to
#          2022-11-25
#
#   @mashume at usethinkscript.com
#
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#
#  This Version /ES and maybe /MES only
#  Functional on 1, 2, 3, 5, 10, 15, 20, 30, 60 minute charts
#
#########################################################

declare upper;

def Y = 31556926;
def M = 2629743;
def D = 86400;
def H = 3600;
def HalfHour = 1800;
def epoch = (getTime() / 1000);
def YMD = (epoch % Y) - (epoch / Y);
def month = (Floor(YMD / M) + 1);
def GMT = Floor((epoch % D) / H);
def HOUR = if GMT > 4 then GMT - 4 else if GMT < 4 then (GMT + 24) - 4 else 0;

def tf = getAggregationPeriod();

def barsPerHour = if tf == AggregationPeriod.HOUR then 1
    else if tf == AggregationPeriod.THIRTY_MIN then 2
    else if tf == AggregationPeriod.TWENTY_MIN then 3
    else if tf == AggregationPeriod.FIFTEEN_MIN then 4
    else if tf == AggregationPeriod.TEN_MIN then 6
    else if tf == AggregationPeriod.FIVE_MIN then 12
    else if tf == AggregationPeriod.THREE_MIN then 20
    else if tf == AggregationPeriod.TWO_MIN then 30
    else if tf == AggregationPeriod.MIN then 60
    else double.nan;
  

def q1 =
    if HOUR == 0 then -2.562000
    else if HOUR == 1 then -4.250000
    else if HOUR == 2 then -6.500000
    else if HOUR == 3 then -7.500000
    else if HOUR == 4 then -7.250000
    else if HOUR == 5 then -4.000000
    else if HOUR == 6 then -3.250000
    else if HOUR == 7 then -6.500000
    else if HOUR == 8 then -8.812000
    else if HOUR == 9 then -11.750000
    else if HOUR == 10 then -8.250000
    else if HOUR == 11 then -10.812000
    else if HOUR == 12 then -8.750000
    else if HOUR == 13 then -8.750000
    else if HOUR == 14 then -5.000000
    else if HOUR == 15 then -9.750000
    else if HOUR == 16 then -4.562000
    else if HOUR == 17 then -13.062000
    else if HOUR == 18 then -2.812000
    else if HOUR == 19 then -3.062000
    else if HOUR == 20 then -4.250000
    else if HOUR == 21 then -3.250000
    else if HOUR == 22 then -2.000000
    else -2.250000;
def q3 =
    if HOUR == 0 then 3.500000
    else if HOUR == 1 then 3.750000
    else if HOUR == 2 then 4.562500
    else if HOUR == 3 then 5.500000
    else if HOUR == 4 then 4.750000
    else if HOUR == 5 then 6.500000
    else if HOUR == 6 then 6.312500
    else if HOUR == 7 then 6.750000
    else if HOUR == 8 then 9.250000
    else if HOUR == 9 then 17.500000
    else if HOUR == 10 then 11.000000
    else if HOUR == 11 then 7.812500
    else if HOUR == 12 then 8.000000
    else if HOUR == 13 then 10.062500
    else if HOUR == 14 then 13.312500
    else if HOUR == 15 then 6.750000
    else if HOUR == 16 then 3.500000
    else if HOUR == 17 then -3.562500
    else if HOUR == 18 then 4.250000
    else if HOUR == 19 then 3.750000
    else if HOUR == 20 then 4.000000
    else if HOUR == 21 then 3.250000
    else if HOUR == 22 then 2.750000
    else 3.000000;
def m2 =
    if HOUR == 0 then 0.250000
    else if HOUR == 1 then 0.250000
    else if HOUR == 2 then -1.750000
    else if HOUR == 3 then -0.750000
    else if HOUR == 4 then -0.750000
    else if HOUR == 5 then 1.500000
    else if HOUR == 6 then 1.500000
    else if HOUR == 7 then 0.000000
    else if HOUR == 8 then -1.750000
    else if HOUR == 9 then 1.875000
    else if HOUR == 10 then 1.500000
    else if HOUR == 11 then -1.000000
    else if HOUR == 12 then -0.250000
    else if HOUR == 13 then 1.125000
    else if HOUR == 14 then 3.250000
    else if HOUR == 15 then -0.500000
    else if HOUR == 16 then 0.250000
    else if HOUR == 17 then -7.375000
    else if HOUR == 18 then 1.000000
    else if HOUR == 19 then 0.750000
    else if HOUR == 20 then -0.500000
    else if HOUR == 21 then 0.250000
    else if HOUR == 22 then 0.250000
    else 0.250000;

def midline = hl2(period = AggregationPeriod.HOUR)[1];
def median = hl2(period = AggregationPeriod.HOUR)[1] + m2;
def upper = median + (q3);
def lower = median + (q1);

plot c = midline;
c.setStyle(CURve.SHORT_DASH);
c.setDefaultColor(getColor(7));

plot median_line = median[barsPerHour];
median_line.SetPaintingStrategy(PaintingStrategy.DASHES);
median_line.SetDefaultColor(getColor(3));
plot expected_upper = upper[barsPerHour];
expected_upper.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
expected_upper.SetDefaultColor(getColor(1));
plot expected_lower = lower[barsPerHour];
expected_lower.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
expected_lower.SetDefaultColor(getColor(5));

addcloud(expected_upper, expected_lower, color.white, color.white);
def upside = (expected_upper[(-1 * barsPerHour)] - close);
def downside = (close - expected_lower[(-1 * barsPerHour)]);
addLabel(yes, "current upside: " + upside + "  current downside: " + downside, if upside > downside then color.dark_green else if downside > upside then color.dark_red else color.dark_gray);

def dist_to_median = (median_line[(-1 * barsPerHour)] - close);
addLabel(yes, "Distance to Median: " + dist_to_median,
if dist_to_median > 5 then color.green
else if dist_to_median > 1 then color.dark_green
else if dist_to_median < -5 then color.red
else if dist_to_median < -1 then color.dark_red
else color.gray);

NQ and MNQ

Code:
#########################################################
#
#   Next Hour /NQ Trading Range
#     Algorithmic Prediction
#
#   Values Derived on Data from
#          2022-09-18 to
#          2022-11-25
#
#   @mashume at usethinkscript.com
#
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#
#  This Version /NQ and maybe /MNQ only
#  Functional on 1, 2, 3, 5, 10, 15, 20, 30, 60 minute charts
#
#########################################################

declare upper;

def Y = 31556926;
def M = 2629743;
def D = 86400;
def H = 3600;
def HalfHour = 1800;
def epoch = (getTime() / 1000);
def YMD = (epoch % Y) - (epoch / Y);
def month = (Floor(YMD / M) + 1);
def GMT = Floor((epoch % D) / H);
def HOUR = if GMT > 4 then GMT - 4 else if GMT < 4 then (GMT + 24) - 4 else 0;

def tf = getAggregationPeriod();

def barsPerHour = if tf == AggregationPeriod.HOUR then 1
    else if tf == AggregationPeriod.THIRTY_MIN then 2
    else if tf == AggregationPeriod.TWENTY_MIN then 3
    else if tf == AggregationPeriod.FIFTEEN_MIN then 4
    else if tf == AggregationPeriod.TEN_MIN then 6
    else if tf == AggregationPeriod.FIVE_MIN then 12
    else if tf == AggregationPeriod.THREE_MIN then 20
    else if tf == AggregationPeriod.TWO_MIN then 30
    else if tf == AggregationPeriod.MIN then 60
    else double.nan;
  

def q1 =
    if HOUR == 0 then -9.812000
    else if HOUR == 1 then -16.062000
    else if HOUR == 2 then -22.812000
    else if HOUR == 3 then -28.312000
    else if HOUR == 4 then -25.250000
    else if HOUR == 5 then -14.500000
    else if HOUR == 6 then -13.812000
    else if HOUR == 7 then -20.812000
    else if HOUR == 8 then -41.688000
    else if HOUR == 9 then -56.312000
    else if HOUR == 10 then -32.750000
    else if HOUR == 11 then -40.812000
    else if HOUR == 12 then -32.500000
    else if HOUR == 13 then -30.625000
    else if HOUR == 14 then -19.500000
    else if HOUR == 15 then -34.625000
    else if HOUR == 16 then -18.125000
    else if HOUR == 17 then -94.250000
    else if HOUR == 18 then -10.562000
    else if HOUR == 19 then -12.750000
    else if HOUR == 20 then -16.000000
    else if HOUR == 21 then -13.750000
    else if HOUR == 22 then -8.250000
    else -9.750000;
def q3 =
    if HOUR == 0 then 11.375000
    else if HOUR == 1 then 14.750000
    else if HOUR == 2 then 16.750000
    else if HOUR == 3 then 17.125000
    else if HOUR == 4 then 17.125000
    else if HOUR == 5 then 22.250000
    else if HOUR == 6 then 21.500000
    else if HOUR == 7 then 25.312500
    else if HOUR == 8 then 38.062500
    else if HOUR == 9 then 69.000000
    else if HOUR == 10 then 46.812500
    else if HOUR == 11 then 30.500000
    else if HOUR == 12 then 29.500000
    else if HOUR == 13 then 36.312500
    else if HOUR == 14 then 50.125000
    else if HOUR == 15 then 24.250000
    else if HOUR == 16 then 10.812500
    else if HOUR == 17 then -59.250000
    else if HOUR == 18 then 15.750000
    else if HOUR == 19 then 13.500000
    else if HOUR == 20 then 15.312500
    else if HOUR == 21 then 10.500000
    else if HOUR == 22 then 9.000000
    else 12.062000;
def m2 =
    if HOUR == 0 then 0.250000
    else if HOUR == 1 then 0.375000
    else if HOUR == 2 then -5.750000
    else if HOUR == 3 then -5.375000
    else if HOUR == 4 then -5.250000
    else if HOUR == 5 then 3.250000
    else if HOUR == 6 then 4.250000
    else if HOUR == 7 then -0.500000
    else if HOUR == 8 then -7.750000
    else if HOUR == 9 then 0.000000
    else if HOUR == 10 then 5.375000
    else if HOUR == 11 then -7.625000
    else if HOUR == 12 then -1.000000
    else if HOUR == 13 then 1.750000
    else if HOUR == 14 then 8.625000
    else if HOUR == 15 then -2.875000
    else if HOUR == 16 then -0.750000
    else if HOUR == 17 then -76.750000
    else if HOUR == 18 then 2.625000
    else if HOUR == 19 then 2.875000
    else if HOUR == 20 then -1.875000
    else if HOUR == 21 then 0.250000
    else if HOUR == 22 then 0.500000
    else -0.250000;

def midline = hl2(period = AggregationPeriod.HOUR)[1];
def median = hl2(period = AggregationPeriod.HOUR)[1] + m2;
def upper = median + (q3);
def lower = median + (q1);

plot c = midline;
c.setStyle(CURve.SHORT_DASH);
c.setDefaultColor(getColor(7));

plot median_line = median[barsPerHour];
median_line.SetPaintingStrategy(PaintingStrategy.DASHES);
median_line.SetDefaultColor(getColor(3));
plot expected_upper = upper[barsPerHour];
expected_upper.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
expected_upper.SetDefaultColor(getColor(1));
plot expected_lower = lower[barsPerHour];
expected_lower.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
expected_lower.SetDefaultColor(getColor(5));

addcloud(expected_upper, expected_lower, color.white, color.white);
def upside = (expected_upper[(-1 * barsPerHour)] - close);
def downside = (close - expected_lower[(-1 * barsPerHour)]);
addLabel(yes, "current upside: " + upside + "  current downside: " + downside, if upside > downside then color.dark_green else if downside > upside then color.dark_red else color.dark_gray);

def dist_to_median = (median_line[(-1 * barsPerHour)] - close);
addLabel(yes, "Distance to Median: " + dist_to_median,
if dist_to_median > 5 then color.green
else if dist_to_median > 1 then color.dark_green
else if dist_to_median < -5 then color.red
else if dist_to_median < -1 then color.dark_red
else color.gray);
How often does this need to be Updated with new statistical data?
 

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
389 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