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

Is it current for ES?
I can't find my local copy of the indicator generator code at the moment (it's on a different machine), but this was generated from the google colab notebook here:
https://colab.research.google.com/drive/15AXGoDA9rtaapu-FIPea8xmXNgRlibfx?usp=sharing

Code:
#########################################################
#
#   Next Hour ES=f Trading Range
#     Algorithmic Prediction
#
#   Values Derived on Data from
#          2023-01-20 to
#          2023-03-31
#
#   mashume at usethinkscript.com
#
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#
#  This Version ES=f 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 -1.5
    else if HOUR == 1 then -2.75
    else if HOUR == 2 then -4.812
    else if HOUR == 3 then -4.5
    else if HOUR == 4 then -4.5
    else if HOUR == 5 then -3.5
    else if HOUR == 6 then -2.75
    else if HOUR == 7 then -6.062
    else if HOUR == 8 then -5.0
    else if HOUR == 9 then -7.188
    else if HOUR == 10 then -7.0
    else if HOUR == 11 then -6.75
    else if HOUR == 12 then -7.0
    else if HOUR == 13 then -6.25
    else if HOUR == 14 then -7.062
    else if HOUR == 15 then -2.75
    else if HOUR == 16 then -4.25
    else if HOUR == 17 then nan
    else if HOUR == 18 then -2.25
    else if HOUR == 19 then -2.5
    else if HOUR == 20 then -2.25
    else if HOUR == 21 then -1.5
    else if HOUR == 22 then -1.5
    else -2.0;
def q3 =
    if HOUR == 0 then 1.5
    else if HOUR == 1 then 2.5
    else if HOUR == 2 then 3.75
    else if HOUR == 3 then 3.75
    else if HOUR == 4 then 3.0
    else if HOUR == 5 then 3.75
    else if HOUR == 6 then 5.75
    else if HOUR == 7 then 4.0
    else if HOUR == 8 then 7.5
    else if HOUR == 9 then 12.688
    else if HOUR == 10 then 11.75
    else if HOUR == 11 then 6.25
    else if HOUR == 12 then 7.75
    else if HOUR == 13 then 8.312
    else if HOUR == 14 then 8.75
    else if HOUR == 15 then 8.0
    else if HOUR == 16 then 2.188
    else if HOUR == 17 then nan
    else if HOUR == 18 then 2.75
    else if HOUR == 19 then 3.0
    else if HOUR == 20 then 2.25
    else if HOUR == 21 then 2.75
    else if HOUR == 22 then 1.5
    else 2.0;
def m2 =
    if HOUR == 0 then 0.0
    else if HOUR == 1 then 0.0
    else if HOUR == 2 then -0.5
    else if HOUR == 3 then 0.0
    else if HOUR == 4 then -1.25
    else if HOUR == 5 then 0.375
    else if HOUR == 6 then 1.125
    else if HOUR == 7 then -1.25
    else if HOUR == 8 then 0.75
    else if HOUR == 9 then 2.5
    else if HOUR == 10 then 2.5
    else if HOUR == 11 then -0.25
    else if HOUR == 12 then 1.0
    else if HOUR == 13 then 1.375
    else if HOUR == 14 then 2.5
    else if HOUR == 15 then 3.25
    else if HOUR == 16 then -0.75
    else if HOUR == 17 then nan
    else if HOUR == 18 then 0.5
    else if HOUR == 19 then 0.75
    else if HOUR == 20 then 0.0
    else if HOUR == 21 then 0.5
    else if HOUR == 22 then 0.0
    else 0.0;

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
 
Hi @mashume. Can you help me understand how you would trade the following chart?

MAtEHd0.png
 
Hi @mashume. Can you help me understand how you would trade the following chart?

MAtEHd0.png
With tight stops.

The first full hour of the day, the price opened near the bottom of the normal range, tried to climb up to the median, and failed, falling back (and through) the normal low.
The last three hours of the day, the price started closing near the median, and then flirted with the high normal range, bounding off the median range the last full hour shown.

I don't know that I would take many trades, but when it broke above the nedian in the third full hour before the close, I might have gone long with a stop at the next hour median and a target at the high. Looks like a nice R:R setup.

-mashume
 
You ask, and tell me which instrument you'd like updated and I'll post the code here.
:)
-mashume
Hi Mashume, could you update for /TN? I searched the forum and did not find that python script, only one post of some single line code. Do you happen to know where I could find it?
 
With tight stops.

The first full hour of the day, the price opened near the bottom of the normal range, tried to climb up to the median, and failed, falling back (and through) the normal low.
The last three hours of the day, the price started closing near the median, and then flirted with the high normal range, bounding off the median range the last full hour shown.

I don't know that I would take many trades, but when it broke above the nedian in the third full hour before the close, I might have gone long with a stop at the next hour median and a target at the high. Looks like a nice R:R setup.

-mashume
Sounds good. I’ll watch it tomorrow and report back.
 
I can't find my local copy of the indicator generator code at the moment (it's on a different machine), but this was generated from the google colab notebook here:
https://colab.research.google.com/drive/15AXGoDA9rtaapu-FIPea8xmXNgRlibfx?usp=sharing

Code:
#########################################################
#
#   Next Hour ES=f Trading Range
#     Algorithmic Prediction
#
#   Values Derived on Data from
#          2023-01-20 to
#          2023-03-31
#
#   mashume at usethinkscript.com
#
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#
#  This Version ES=f 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 -1.5
    else if HOUR == 1 then -2.75
    else if HOUR == 2 then -4.812
    else if HOUR == 3 then -4.5
    else if HOUR == 4 then -4.5
    else if HOUR == 5 then -3.5
    else if HOUR == 6 then -2.75
    else if HOUR == 7 then -6.062
    else if HOUR == 8 then -5.0
    else if HOUR == 9 then -7.188
    else if HOUR == 10 then -7.0
    else if HOUR == 11 then -6.75
    else if HOUR == 12 then -7.0
    else if HOUR == 13 then -6.25
    else if HOUR == 14 then -7.062
    else if HOUR == 15 then -2.75
    else if HOUR == 16 then -4.25
    else if HOUR == 17 then nan
    else if HOUR == 18 then -2.25
    else if HOUR == 19 then -2.5
    else if HOUR == 20 then -2.25
    else if HOUR == 21 then -1.5
    else if HOUR == 22 then -1.5
    else -2.0;
def q3 =
    if HOUR == 0 then 1.5
    else if HOUR == 1 then 2.5
    else if HOUR == 2 then 3.75
    else if HOUR == 3 then 3.75
    else if HOUR == 4 then 3.0
    else if HOUR == 5 then 3.75
    else if HOUR == 6 then 5.75
    else if HOUR == 7 then 4.0
    else if HOUR == 8 then 7.5
    else if HOUR == 9 then 12.688
    else if HOUR == 10 then 11.75
    else if HOUR == 11 then 6.25
    else if HOUR == 12 then 7.75
    else if HOUR == 13 then 8.312
    else if HOUR == 14 then 8.75
    else if HOUR == 15 then 8.0
    else if HOUR == 16 then 2.188
    else if HOUR == 17 then nan
    else if HOUR == 18 then 2.75
    else if HOUR == 19 then 3.0
    else if HOUR == 20 then 2.25
    else if HOUR == 21 then 2.75
    else if HOUR == 22 then 1.5
    else 2.0;
def m2 =
    if HOUR == 0 then 0.0
    else if HOUR == 1 then 0.0
    else if HOUR == 2 then -0.5
    else if HOUR == 3 then 0.0
    else if HOUR == 4 then -1.25
    else if HOUR == 5 then 0.375
    else if HOUR == 6 then 1.125
    else if HOUR == 7 then -1.25
    else if HOUR == 8 then 0.75
    else if HOUR == 9 then 2.5
    else if HOUR == 10 then 2.5
    else if HOUR == 11 then -0.25
    else if HOUR == 12 then 1.0
    else if HOUR == 13 then 1.375
    else if HOUR == 14 then 2.5
    else if HOUR == 15 then 3.25
    else if HOUR == 16 then -0.75
    else if HOUR == 17 then nan
    else if HOUR == 18 then 0.5
    else if HOUR == 19 then 0.75
    else if HOUR == 20 then 0.0
    else if HOUR == 21 then 0.5
    else if HOUR == 22 then 0.0
    else 0.0;

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
The code is missing value for HOUR 17 is nan
 
Last edited by a moderator:
Hi Mashume, could you update for /TN? I searched the forum and did not find that python script, only one post of some single line code. Do you happen to know where I could find it?
The code is here:
https://colab.research.google.com/drive/15AXGoDA9rtaapu-FIPea8xmXNgRlibfx?usp=sharing

and here's the /TN script:
Code:
#########################################################
#
#   Next Hour /TN Trading Range
#     Algorithmic Prediction
#
#   Values Derived on Data from
#          2023-02-03 to
#          2023-04-14
#
#   mashume at usethinkscript.com
#
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#
#  This Version /TN 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.031
    else if HOUR == 1 then -0.062
    else if HOUR == 2 then -0.098
    else if HOUR == 3 then -0.125
    else if HOUR == 4 then -0.094
    else if HOUR == 5 then -0.07
    else if HOUR == 6 then -0.094
    else if HOUR == 7 then -0.141
    else if HOUR == 8 then -0.203
    else if HOUR == 9 then -0.176
    else if HOUR == 10 then -0.109
    else if HOUR == 11 then -0.109
    else if HOUR == 12 then -0.109
    else if HOUR == 13 then -0.102
    else if HOUR == 14 then -0.062
    else if HOUR == 15 then -0.062
    else if HOUR == 16 then -0.078
    else if HOUR == 17 then 0.0
    else if HOUR == 18 then -0.031
    else if HOUR == 19 then -0.062
    else if HOUR == 20 then -0.047
    else if HOUR == 21 then -0.031
    else if HOUR == 22 then -0.031
    else -0.031;
def q3 =
    if HOUR == 0 then 0.047
    else if HOUR == 1 then 0.062
    else if HOUR == 2 then 0.047
    else if HOUR == 3 then 0.078
    else if HOUR == 4 then 0.062
    else if HOUR == 5 then 0.063
    else if HOUR == 6 then 0.094
    else if HOUR == 7 then 0.141
    else if HOUR == 8 then 0.156
    else if HOUR == 9 then 0.156
    else if HOUR == 10 then 0.125
    else if HOUR == 11 then 0.078
    else if HOUR == 12 then 0.094
    else if HOUR == 13 then 0.078
    else if HOUR == 14 then 0.062
    else if HOUR == 15 then 0.078
    else if HOUR == 16 then 0.031
    else if HOUR == 17 then 0.0
    else if HOUR == 18 then 0.062
    else if HOUR == 19 then 0.062
    else if HOUR == 20 then 0.047
    else if HOUR == 21 then 0.062
    else if HOUR == 22 then 0.047
    else 0.031;
def m2 =
    if HOUR == 0 then 0.0
    else if HOUR == 1 then 0.0
    else if HOUR == 2 then -0.031
    else if HOUR == 3 then 0.0
    else if HOUR == 4 then -0.016
    else if HOUR == 5 then 0.0
    else if HOUR == 6 then -0.016
    else if HOUR == 7 then -0.016
    else if HOUR == 8 then -0.016
    else if HOUR == 9 then -0.016
    else if HOUR == 10 then 0.0
    else if HOUR == 11 then -0.016
    else if HOUR == 12 then 0.0
    else if HOUR == 13 then -0.016
    else if HOUR == 14 then 0.0
    else if HOUR == 15 then 0.016
    else if HOUR == 16 then -0.016
    else if HOUR == 17 then 0.0
    else if HOUR == 18 then 0.0
    else if HOUR == 19 then 0.016
    else if HOUR == 20 then 0.0
    else if HOUR == 21 then 0.016
    else if HOUR == 22 then 0.016
    else 0.0;

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);
 
I can't find my local copy of the indicator generator code at the moment (it's on a different machine), but this was generated from the google colab notebook here:
https://colab.research.google.com/drive/15AXGoDA9rtaapu-FIPea8xmXNgRlibfx?usp=sharing

Code:
#########################################################
#
#   Next Hour ES=f Trading Range
#     Algorithmic Prediction
#
#   Values Derived on Data from
#          2023-01-20 to
#          2023-03-31
#
#   mashume at usethinkscript.com
#
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#
#  This Version ES=f 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 -1.5
    else if HOUR == 1 then -2.75
    else if HOUR == 2 then -4.812
    else if HOUR == 3 then -4.5
    else if HOUR == 4 then -4.5
    else if HOUR == 5 then -3.5
    else if HOUR == 6 then -2.75
    else if HOUR == 7 then -6.062
    else if HOUR == 8 then -5.0
    else if HOUR == 9 then -7.188
    else if HOUR == 10 then -7.0
    else if HOUR == 11 then -6.75
    else if HOUR == 12 then -7.0
    else if HOUR == 13 then -6.25
    else if HOUR == 14 then -7.062
    else if HOUR == 15 then -2.75
    else if HOUR == 16 then -4.25
    else if HOUR == 17 then nan
    else if HOUR == 18 then -2.25
    else if HOUR == 19 then -2.5
    else if HOUR == 20 then -2.25
    else if HOUR == 21 then -1.5
    else if HOUR == 22 then -1.5
    else -2.0;
def q3 =
    if HOUR == 0 then 1.5
    else if HOUR == 1 then 2.5
    else if HOUR == 2 then 3.75
    else if HOUR == 3 then 3.75
    else if HOUR == 4 then 3.0
    else if HOUR == 5 then 3.75
    else if HOUR == 6 then 5.75
    else if HOUR == 7 then 4.0
    else if HOUR == 8 then 7.5
    else if HOUR == 9 then 12.688
    else if HOUR == 10 then 11.75
    else if HOUR == 11 then 6.25
    else if HOUR == 12 then 7.75
    else if HOUR == 13 then 8.312
    else if HOUR == 14 then 8.75
    else if HOUR == 15 then 8.0
    else if HOUR == 16 then 2.188
    else if HOUR == 17 then nan
    else if HOUR == 18 then 2.75
    else if HOUR == 19 then 3.0
    else if HOUR == 20 then 2.25
    else if HOUR == 21 then 2.75
    else if HOUR == 22 then 1.5
    else 2.0;
def m2 =
    if HOUR == 0 then 0.0
    else if HOUR == 1 then 0.0
    else if HOUR == 2 then -0.5
    else if HOUR == 3 then 0.0
    else if HOUR == 4 then -1.25
    else if HOUR == 5 then 0.375
    else if HOUR == 6 then 1.125
    else if HOUR == 7 then -1.25
    else if HOUR == 8 then 0.75
    else if HOUR == 9 then 2.5
    else if HOUR == 10 then 2.5
    else if HOUR == 11 then -0.25
    else if HOUR == 12 then 1.0
    else if HOUR == 13 then 1.375
    else if HOUR == 14 then 2.5
    else if HOUR == 15 then 3.25
    else if HOUR == 16 then -0.75
    else if HOUR == 17 then nan
    else if HOUR == 18 then 0.5
    else if HOUR == 19 then 0.75
    else if HOUR == 20 then 0.0
    else if HOUR == 21 then 0.5
    else if HOUR == 22 then 0.0
    else 0.0;

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
thx @mashume. can you please fix the errors for some of the variables marked :nan
 
Last edited by a moderator:
Watching it more @mashume. Some elementary questions. What are these plots mainly meant for? I don't want to assume.

QzOccMe.png
1 is the 1st quartile
2 is the median
3 is the 3rd quartile
4 is the median from the previous hour shifted and used for the calculation of 1, 2, and 3.

By way of explanation for 1, 2, and 3, you can look at them as the most likely movement for the next hour (2), and the maximum margins up and down that the instrument moves 75% of the time. (gross oversimplification, but in the right direction -- you can do some reading on quartiles on wikipedia here: https://en.wikipedia.org/wiki/Quartile if you're really interested)

I usually turn #4 off on my screen, it is there as a sanity check more than a useful plot.

-mashume

P.S. For the day you're looking at there, the hour you have hi-lighted along with the one before it shows (since the price is already much much higher than the expected movement) that the market is making a very unusual move -- somewhere there was a catalyst that was a statistical outlier and the market reacted in an unusual way. Overnight, the proceeding night, the market behaved largely as expected. The last two full hours of the day also behaved much more normally than those two or three hours of possibly irrational exuberance.
 
Can you post latest for /ES?
Code:
#########################################################
#
#   Next Hour /ES Trading Range
#     Algorithmic Prediction
#
#   Values Derived on Data from
#          2023-03-01 to
#          2023-05-10
#
#   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 -1.5
    else if HOUR == 1 then -2.5
    else if HOUR == 2 then -3.5
    else if HOUR == 3 then -4.75
    else if HOUR == 4 then -2.5
    else if HOUR == 5 then -2.125
    else if HOUR == 6 then -3.25
    else if HOUR == 7 then -3.812
    else if HOUR == 8 then -4.75
    else if HOUR == 9 then -7.75
    else if HOUR == 10 then -6.25
    else if HOUR == 11 then -6.0
    else if HOUR == 12 then -5.0
    else if HOUR == 13 then -5.625
    else if HOUR == 14 then -4.812
    else if HOUR == 15 then -2.125
    else if HOUR == 16 then -3.75
    else if HOUR == 17 then -3.75
    else if HOUR == 18 then -1.5
    else if HOUR == 19 then -2.25
    else if HOUR == 20 then -1.75
    else if HOUR == 21 then -1.0
    else if HOUR == 22 then -1.25
    else -1.5;
def q3 =
    if HOUR == 0 then 1.312
    else if HOUR == 1 then 2.25
    else if HOUR == 2 then 3.5
    else if HOUR == 3 then 3.0
    else if HOUR == 4 then 3.75
    else if HOUR == 5 then 3.875
    else if HOUR == 6 then 3.75
    else if HOUR == 7 then 2.75
    else if HOUR == 8 then 7.25
    else if HOUR == 9 then 9.5
    else if HOUR == 10 then 6.5
    else if HOUR == 11 then 6.688
    else if HOUR == 12 then 6.5
    else if HOUR == 13 then 7.0
    else if HOUR == 14 then 7.25
    else if HOUR == 15 then 8.0
    else if HOUR == 16 then 1.0
    else if HOUR == 17 then -0.25
    else if HOUR == 18 then 2.0
    else if HOUR == 19 then 2.062
    else if HOUR == 20 then 2.25
    else if HOUR == 21 then 2.75
    else if HOUR == 22 then 1.5
    else 1.75;
def m2 =
    if HOUR == 0 then 0.0
    else if HOUR == 1 then 0.0
    else if HOUR == 2 then 0.25
    else if HOUR == 3 then -0.75
    else if HOUR == 4 then 0.75
    else if HOUR == 5 then 0.75
    else if HOUR == 6 then 0.5
    else if HOUR == 7 then -0.5
    else if HOUR == 8 then 1.0
    else if HOUR == 9 then 0.875
    else if HOUR == 10 then 0.0
    else if HOUR == 11 then 0.75
    else if HOUR == 12 then 2.625
    else if HOUR == 13 then 1.25
    else if HOUR == 14 then 1.25
    else if HOUR == 15 then 2.75
    else if HOUR == 16 then -1.5
    else if HOUR == 17 then -2.0
    else if HOUR == 18 then 0.5
    else if HOUR == 19 then -0.5
    else if HOUR == 20 then 0.25
    else if HOUR == 21 then 0.5
    else if HOUR == 22 then 0.0
    else 0.0;

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 @mashume for updating the /ES. I use your study on my 15 min /ES chart and find it very helpful. If you have the time to do so, could you update the SPX study also? For my trading style, I always watch the 15min, 30 min, and 60 min SPX charts. (when trading the /ES).
 

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