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

EDIT 2021-December

There are many updated versions of this indicator using more recent data. Please start your search at the END of this thread and work your way toward page 1 to find the most recent for whichever instrument you want. If you don't see it, post a request and I'll do my best to fill it in reasonable order.

So, from the data described here:
https://usethinkscript.com/threads/es-futures-probability-at-time-market-timing-differently.6250/
I decided to build an indicator to see what would happen. This indicator is the result of that.

It may or may not be mathematically correct. If you'd like to audit my statistics, shifts, and so forth, please do.

Remember, the bounding boxes are being drawn a minimum of 1 hour before the candles are drawn. They are real predictions, based on probable movements of the market as described in the link above. These use 1st and 3rd quartile deviances from median for the range shown (for those who care).

View attachment 10334
Overnight projections.

View attachment 10335
Showing regular trading day projections.

View attachment 10336
This one shows the predictions in the expansion area on the right.

These are not overly accurate, but an interesting direction in indicators.

I have written a python script to "regenerate" the indicator code so I can run it weekly to update the coefficients if there is interest.

Here's the code for the indicator:
Code:
#########################################################
#
#   Next Hour ES Trading Range
#     Algorithmic Prediction
#   Values Derived on Data from
#          2021-01-12 to
#          2021-04-08
#
#   @mashume at usethinkscript.com
#
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#
#  This Version /ES (and maybe /MES) only
#  Functional on 1, 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.MIN then 60
    else double.nan;

def q1 =
    if HOUR == 0 then -2.25
    else if HOUR == 1 then -3.5
    else if HOUR == 2 then -3.25
    else if HOUR == 3 then -3.5
    else if HOUR == 4 then -2.0
    else if HOUR == 5 then -2.5
    else if HOUR == 6 then -2.0
    else if HOUR == 7 then -4.0
    else if HOUR == 8 then -3.75
    else if HOUR == 9 then -6.25
    else if HOUR == 10 then -4.5
    else if HOUR == 11 then -3.0
    else if HOUR == 12 then -2.812
    else if HOUR == 13 then -3.75
    else if HOUR == 14 then -6.75
    else if HOUR == 15 then -5.75
    else if HOUR == 16 then -1.75
    else if HOUR == 17 then 1.0
    else if HOUR == 18 then -3.5
    else if HOUR == 19 then -3.25
    else if HOUR == 20 then -3.5
    else if HOUR == 21 then -3.5
    else if HOUR == 22 then -2.0
    else -1.25;
def q3 =
    if HOUR == 0 then 2.25
    else if HOUR == 1 then 3.0
    else if HOUR == 2 then 3.75
    else if HOUR == 3 then 4.25
    else if HOUR == 4 then 2.75
    else if HOUR == 5 then 3.25
    else if HOUR == 6 then 3.75
    else if HOUR == 7 then 3.0
    else if HOUR == 8 then 5.312
    else if HOUR == 9 then 8.75
    else if HOUR == 10 then 7.75
    else if HOUR == 11 then 7.0
    else if HOUR == 12 then 5.5
    else if HOUR == 13 then 5.25
    else if HOUR == 14 then 6.562
    else if HOUR == 15 then 6.75
    else if HOUR == 16 then 4.0
    else if HOUR == 17 then 6.25
    else if HOUR == 18 then 3.25
    else if HOUR == 19 then 3.5
    else if HOUR == 20 then 3.75
    else if HOUR == 21 then 2.5
    else if HOUR == 22 then 2.25
    else 2.5;
def m2 =
    if HOUR == 0 then 0.0
    else if HOUR == 1 then -0.5
    else if HOUR == 2 then 0.25
    else if HOUR == 3 then 0.25
    else if HOUR == 4 then 0.25
    else if HOUR == 5 then 0.5
    else if HOUR == 6 then 0.5
    else if HOUR == 7 then -0.25
    else if HOUR == 8 then 0.5
    else if HOUR == 9 then 2.0
    else if HOUR == 10 then 2.75
    else if HOUR == 11 then 2.5
    else if HOUR == 12 then 1.5
    else if HOUR == 13 then 1.0
    else if HOUR == 14 then -0.5
    else if HOUR == 15 then 0.75
    else if HOUR == 16 then 1.0
    else if HOUR == 17 then 2.5
    else if HOUR == 18 then -0.25
    else if HOUR == 19 then 0.5
    else if HOUR == 20 then 0.25
    else if HOUR == 21 then -0.75
    else if HOUR == 22 then 0.5
    else 0.25;

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);

If you find this useful, let me know. This could, potentially, be applied to any ticker, though it would need to be for a single ticker. If you trade exclusively one symbol, it could be an interesting exercise.

REMEMBER This is just probabilities applied to prices. It's not magic, and it's not always going to be correct.

Happy Trading,
mashume
@mashume - is there anyway you can create a scan when the price crosses above median? I am looking to create a scan for SPY and AMZN on 2m timeframe. I am not able to create one for a 2m time frame since the Aggregation Period is set to 1 hour.
 
Last edited:
@mashume - is there anyway you can create a scan when the price crosses above median? I am looking to create a scan for SPY and AMZN on 2m timeframe. I am not able to create one for a 2m time frame since the Aggregation Period is set to 1 hour.

Yes, that is correct.
You cannot create a 2m scan.
You must set your scan timeframe to be the same as the aggregation period in your script.
If the secondary aggregation is set at 1hr then the scanner must be set to 1hr.
There is no workaround to this limitation as the ToS platform does not support the use of differing secondary aggregations in any of the ToS widgets (scans, watchlists, conditional orders, etc).
 

EDIT 2021-December

There are many updated versions of this indicator using more recent data. Please start your search at the END of this thread and work your way toward page 1 to find the most recent for whichever instrument you want. If you don't see it, post a request and I'll do my best to fill it in reasonable order.

So, from the data described here:
https://usethinkscript.com/threads/es-futures-probability-at-time-market-timing-differently.6250/
I decided to build an indicator to see what would happen. This indicator is the result of that.

It may or may not be mathematically correct. If you'd like to audit my statistics, shifts, and so forth, please do.

Remember, the bounding boxes are being drawn a minimum of 1 hour before the candles are drawn. They are real predictions, based on probable movements of the market as described in the link above. These use 1st and 3rd quartile deviances from median for the range shown (for those who care).

View attachment 10334
Overnight projections.

View attachment 10335
Showing regular trading day projections.

View attachment 10336
This one shows the predictions in the expansion area on the right.

These are not overly accurate, but an interesting direction in indicators.

I have written a python script to "regenerate" the indicator code so I can run it weekly to update the coefficients if there is interest.

Here's the code for the indicator:
Code:
#########################################################
#
#   Next Hour ES Trading Range
#     Algorithmic Prediction
#   Values Derived on Data from
#          2021-01-12 to
#          2021-04-08
#
#   @mashume at usethinkscript.com
#
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#
#  This Version /ES (and maybe /MES) only
#  Functional on 1, 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.MIN then 60
    else double.nan;

def q1 =
    if HOUR == 0 then -2.25
    else if HOUR == 1 then -3.5
    else if HOUR == 2 then -3.25
    else if HOUR == 3 then -3.5
    else if HOUR == 4 then -2.0
    else if HOUR == 5 then -2.5
    else if HOUR == 6 then -2.0
    else if HOUR == 7 then -4.0
    else if HOUR == 8 then -3.75
    else if HOUR == 9 then -6.25
    else if HOUR == 10 then -4.5
    else if HOUR == 11 then -3.0
    else if HOUR == 12 then -2.812
    else if HOUR == 13 then -3.75
    else if HOUR == 14 then -6.75
    else if HOUR == 15 then -5.75
    else if HOUR == 16 then -1.75
    else if HOUR == 17 then 1.0
    else if HOUR == 18 then -3.5
    else if HOUR == 19 then -3.25
    else if HOUR == 20 then -3.5
    else if HOUR == 21 then -3.5
    else if HOUR == 22 then -2.0
    else -1.25;
def q3 =
    if HOUR == 0 then 2.25
    else if HOUR == 1 then 3.0
    else if HOUR == 2 then 3.75
    else if HOUR == 3 then 4.25
    else if HOUR == 4 then 2.75
    else if HOUR == 5 then 3.25
    else if HOUR == 6 then 3.75
    else if HOUR == 7 then 3.0
    else if HOUR == 8 then 5.312
    else if HOUR == 9 then 8.75
    else if HOUR == 10 then 7.75
    else if HOUR == 11 then 7.0
    else if HOUR == 12 then 5.5
    else if HOUR == 13 then 5.25
    else if HOUR == 14 then 6.562
    else if HOUR == 15 then 6.75
    else if HOUR == 16 then 4.0
    else if HOUR == 17 then 6.25
    else if HOUR == 18 then 3.25
    else if HOUR == 19 then 3.5
    else if HOUR == 20 then 3.75
    else if HOUR == 21 then 2.5
    else if HOUR == 22 then 2.25
    else 2.5;
def m2 =
    if HOUR == 0 then 0.0
    else if HOUR == 1 then -0.5
    else if HOUR == 2 then 0.25
    else if HOUR == 3 then 0.25
    else if HOUR == 4 then 0.25
    else if HOUR == 5 then 0.5
    else if HOUR == 6 then 0.5
    else if HOUR == 7 then -0.25
    else if HOUR == 8 then 0.5
    else if HOUR == 9 then 2.0
    else if HOUR == 10 then 2.75
    else if HOUR == 11 then 2.5
    else if HOUR == 12 then 1.5
    else if HOUR == 13 then 1.0
    else if HOUR == 14 then -0.5
    else if HOUR == 15 then 0.75
    else if HOUR == 16 then 1.0
    else if HOUR == 17 then 2.5
    else if HOUR == 18 then -0.25
    else if HOUR == 19 then 0.5
    else if HOUR == 20 then 0.25
    else if HOUR == 21 then -0.75
    else if HOUR == 22 then 0.5
    else 0.25;

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);

If you find this useful, let me know. This could, potentially, be applied to any ticker, though it would need to be for a single ticker. If you trade exclusively one symbol, it could be an interesting exercise.

REMEMBER This is just probabilities applied to prices. It's not magic, and it's not always going to be correct.

Happy Trading,
mashume
This is excellent! Thank you...I noticed that on lower time frames where I usually trade (5 minute chart), it can exceed the price prediction which is fine...not meant as a criticism. Do you have any suggestions on how to add how far it has exceeded that price (lower and higher) in percentage?
If the predicted price (top line) was $100 and the underlying went to $110 then it would be 10% over your top line..vice if the predicted price was $100 (bottom line) and the underlying went to $90 then it would be 10% below the predicted price.
This is a great indicator and thank you again.
 

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