#########################################################
#
# Next Hour ES Trading Range
# Algorithmic Prediction
#
# Values Derived on Data from
# 2022-04-08 to
# 2022-06-17
#
# @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 -3.500000
else if HOUR == 1 then -6.250000
else if HOUR == 2 then -9.562000
else if HOUR == 3 then -5.500000
else if HOUR == 4 then -7.250000
else if HOUR == 5 then -5.250000
else if HOUR == 6 then -6.250000
else if HOUR == 7 then -7.500000
else if HOUR == 8 then -13.375000
else if HOUR == 9 then -22.812000
else if HOUR == 10 then -16.875000
else if HOUR == 11 then -12.250000
else if HOUR == 12 then -11.000000
else if HOUR == 13 then -13.812000
else if HOUR == 14 then -14.312000
else if HOUR == 15 then -12.250000
else if HOUR == 16 then -2.500000
else if HOUR == 17 then -20.875000
else if HOUR == 18 then -2.750000
else if HOUR == 19 then -5.500000
else if HOUR == 20 then -5.062000
else if HOUR == 21 then -3.000000
else if HOUR == 22 then -2.500000
else -2.250000;
def q3 =
if HOUR == 0 then 3.500000
else if HOUR == 1 then 5.250000
else if HOUR == 2 then 6.000000
else if HOUR == 3 then 9.500000
else if HOUR == 4 then 4.000000
else if HOUR == 5 then 5.000000
else if HOUR == 6 then 6.000000
else if HOUR == 7 then 6.250000
else if HOUR == 8 then 5.875000
else if HOUR == 9 then 12.000000
else if HOUR == 10 then 10.250000
else if HOUR == 11 then 10.000000
else if HOUR == 12 then 9.500000
else if HOUR == 13 then 9.500000
else if HOUR == 14 then 13.250000
else if HOUR == 15 then 10.000000
else if HOUR == 16 then 6.500000
else if HOUR == 17 then 5.125000
else if HOUR == 18 then 3.500000
else if HOUR == 19 then 5.500000
else if HOUR == 20 then 4.812500
else if HOUR == 21 then 5.000000
else if HOUR == 22 then 5.250000
else 3.312000;
def m2 =
if HOUR == 0 then 0.000000
else if HOUR == 1 then 0.500000
else if HOUR == 2 then -1.750000
else if HOUR == 3 then 3.000000
else if HOUR == 4 then -1.750000
else if HOUR == 5 then 0.250000
else if HOUR == 6 then 0.250000
else if HOUR == 7 then -1.000000
else if HOUR == 8 then -3.750000
else if HOUR == 9 then -7.000000
else if HOUR == 10 then -2.250000
else if HOUR == 11 then 0.500000
else if HOUR == 12 then 0.000000
else if HOUR == 13 then -2.125000
else if HOUR == 14 then -1.500000
else if HOUR == 15 then -2.000000
else if HOUR == 16 then 2.250000
else if HOUR == 17 then -0.250000
else if HOUR == 18 then 0.500000
else if HOUR == 19 then -0.250000
else if HOUR == 20 then 0.750000
else if HOUR == 21 then 1.000000
else if HOUR == 22 then 1.500000
else 0.750000;
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);
#########################################################
#
# Next Hour NQ Trading Range
# Algorithmic Prediction
#
# Values Derived on Data from
# 2022-04-08 to
# 2022-06-17
#
# @mashume at usethinkscript.com
#
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#
# This Version /NQ 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 -14.562000
else if HOUR == 1 then -21.000000
else if HOUR == 2 then -37.750000
else if HOUR == 3 then -21.812000
else if HOUR == 4 then -28.312000
else if HOUR == 5 then -20.000000
else if HOUR == 6 then -24.812000
else if HOUR == 7 then -31.000000
else if HOUR == 8 then -57.000000
else if HOUR == 9 then -102.188000
else if HOUR == 10 then -68.938000
else if HOUR == 11 then -50.562000
else if HOUR == 12 then -45.562000
else if HOUR == 13 then -54.375000
else if HOUR == 14 then -55.375000
else if HOUR == 15 then -45.250000
else if HOUR == 16 then -9.250000
else if HOUR == 17 then 9.125000
else if HOUR == 18 then -12.750000
else if HOUR == 19 then -20.562000
else if HOUR == 20 then -20.812000
else if HOUR == 21 then -12.750000
else if HOUR == 22 then -8.750000
else -9.750000;
def q3 =
if HOUR == 0 then 13.625000
else if HOUR == 1 then 20.750000
else if HOUR == 2 then 21.250000
else if HOUR == 3 then 38.500000
else if HOUR == 4 then 14.000000
else if HOUR == 5 then 18.937500
else if HOUR == 6 then 24.750000
else if HOUR == 7 then 27.312500
else if HOUR == 8 then 29.250000
else if HOUR == 9 then 70.062500
else if HOUR == 10 then 41.500000
else if HOUR == 11 then 45.812500
else if HOUR == 12 then 37.562500
else if HOUR == 13 then 37.750000
else if HOUR == 14 then 42.562500
else if HOUR == 15 then 40.562500
else if HOUR == 16 then 27.250000
else if HOUR == 17 then 50.375000
else if HOUR == 18 then 16.062500
else if HOUR == 19 then 20.500000
else if HOUR == 20 then 19.562500
else if HOUR == 21 then 21.062500
else if HOUR == 22 then 21.312500
else 14.312000;
def m2 =
if HOUR == 0 then -1.625000
else if HOUR == 1 then 1.750000
else if HOUR == 2 then -9.500000
else if HOUR == 3 then 10.125000
else if HOUR == 4 then -5.875000
else if HOUR == 5 then -0.250000
else if HOUR == 6 then 0.250000
else if HOUR == 7 then -4.500000
else if HOUR == 8 then -16.750000
else if HOUR == 9 then -23.250000
else if HOUR == 10 then -8.125000
else if HOUR == 11 then 2.750000
else if HOUR == 12 then -2.625000
else if HOUR == 13 then -9.125000
else if HOUR == 14 then -10.000000
else if HOUR == 15 then -3.375000
else if HOUR == 16 then 10.000000
else if HOUR == 17 then 9.500000
else if HOUR == 18 then 3.000000
else if HOUR == 19 then 1.000000
else if HOUR == 20 then 3.250000
else if HOUR == 21 then 2.875000
else if HOUR == 22 then 6.000000
else 3.750000;
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);
#########################################################
#
# Next Hour TSLA Trading Range
# Algorithmic Prediction
#
# Values Derived on Data from
# 2022-03-24 to
# 2022-06-17
#
# @mashume at usethinkscript.com
#
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#
# This Version /TSLA 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 -3.285000
else if HOUR == 5 then -2.593000
else if HOUR == 6 then -4.777000
else if HOUR == 7 then -3.765000
else if HOUR == 8 then -8.792000
else if HOUR == 9 then -13.224000
else if HOUR == 10 then -8.848000
else if HOUR == 11 then -6.939000
else if HOUR == 12 then -5.660000
else if HOUR == 13 then -5.239000
else if HOUR == 14 then -6.410000
else if HOUR == 15 then -3.349000
else if HOUR == 16 then -1.300000
else if HOUR == 17 then -0.756000
else if HOUR == 18 then -1.420000
else if HOUR == 19 then -6.267000
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.860000
else if HOUR == 5 then 2.132500
else if HOUR == 6 then 2.672500
else if HOUR == 7 then 2.992500
else if HOUR == 8 then 6.406247
else if HOUR == 9 then 12.220125
else if HOUR == 10 then 8.290009
else if HOUR == 11 then 3.532196
else if HOUR == 12 then 3.799881
else if HOUR == 13 then 3.787537
else if HOUR == 14 then 3.503479
else if HOUR == 15 then 4.752119
else if HOUR == 16 then 1.500000
else if HOUR == 17 then 1.905000
else if HOUR == 18 then 1.340000
else if HOUR == 19 then 11.182500
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.455000
else if HOUR == 5 then -0.115000
else if HOUR == 6 then -1.010000
else if HOUR == 7 then -0.795000
else if HOUR == 8 then -1.720000
else if HOUR == 9 then -0.535000
else if HOUR == 10 then -0.276000
else if HOUR == 11 then -1.619000
else if HOUR == 12 then -0.295000
else if HOUR == 13 then -0.287000
else if HOUR == 14 then -1.628000
else if HOUR == 15 then 0.367000
else if HOUR == 16 then 0.035000
else if HOUR == 17 then 0.440000
else if HOUR == 18 then -0.065000
else if HOUR == 19 then 1.640000
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);
There is no real validation... the values are based on hourly data over the proceeding 60 days at 5m. The values for each hour are aggregated, and basic statistics (q1, q3, and median) are calculated. Those are then adjusted as movement vs the previous hour's HL2 candle and plotted for the hour. I think there is more on it here somewhere. The only validation you could really run is against the computed values, and you're welcome to look through here for the source code. I think I published it -- perhaps as a google collab notebook.Has anyone done a study on validating the time prediction? This is really interesting
Look at this post and the preceding few posts. Will give you an idea of what to expect....Has anyone done a study on validating the time prediction? This is really interesting
No, this one does not work on tick charts due to the use of offset bars. Tick charts do not have a predictable number of bars for a given timeframe, and so there is no way to get the predicted values to show up in the right place.@mashume would it be possible to get it to work on a 1000 tick chart?
I have experimented with various versions of this script myself:Hi there, I was looking for something similar to this. The idea was to have data from the last X amount of days (the bigger the sample the better), and then fetch the Hi and Low for each hour in regards to the open of each day. And this data will be used to predict the movement of the next two hours. BUT..
..BUT the prediction, to be more accurate, should compare the same hours from the days before and they should have a similar pattern in order to throw a prediction..
For example we are at 11:00 am
From 9:00 to 10:00am the price Hi reached 2% above the open and the Low reached -1% below the open. (we will call this (A)
From 10:00 to 11:00am the hi reached 3% above the open and the low 1% above the open. (we will call this (B)
(And we'll call them both together AB).
The prediction would grab AB, and it will show the different possible outcomes for the 11:00 to 12:00 and the 12:00 to 13:00 periods.
Ideally there would be more than 1 outcome so it would rank them according to the amount of times that that outcome has appeared in the past. There might be old patterns similar to A and others similar to B but for the indicator to show a prediction, there must be patterns similar to AB, if there are non, then the indicator will show no prediction, if the indicator shows too many predictions then we should consider adding one more hour: C, so it would have to fetch the ones that coincide with ABC, reducing the possible outcomes.
Some notes:
The range from the open could be separated in areas ex: from 0 to 2% from 2% to 4% and so on in order to give a little bit more of freedom to the indicator. (I know that those percentages are exaggerated but you get my point..)
Ideally the amount of days and hours to look back could be modified.
Ideally the different outcomes can be displayed with a number that indicates how many times it happened.
I trade the ES so I thought this for futures, though I usually use the 9:30 open as a guide.
I'm open for different ideas and If someone is willing to give it a try and code this, that would be great, because my programing skills are similar to the ones an intelligent horse could have, and I don't see this indicator as something easy to achieve.
Take a look at post #27 in this thread:@mashume hi Sir. I am interested in learning to update by myself the data for this indicator.
Is there any tutorial or indications about how to do it??
Thanks.
many thanks. I will have a look.Take a look at post #27 in this thread:
https://usethinkscript.com/threads/...ut-prediction-for-thinkorswim.6282/post-61282
it has a link to a Google Collab Notebook with a [slightly early] version of the python code that generates this indicator. If you go through that, it should be somewhat clearer how this works, and I'll be happy to answer questions about the code here any time.
There isn't really a tutorial, but the notebook should run and generate an updated script. If it doesn't, please write back here and we'll try to sort out your problems with it.
Good luck,
mashume
Hmmm mm... I feel a GitHub repo coming on...@mashume is it possible to have as a single script instead of Jupiter?. Not familiar with Jupiter , only Spyder.
These are the way that the YFinance API handles futures. ToS says "/ES", YFin says "ES=F". Potato, potato; tomato, tomato.Amazing script! Thank you for this... btw can it work for Crude Oil (CL) futures? I'm still trying to figure it out how to edit the script and run it...
tickers = ['ES=F', 'CL=F', '^VIX', 'GC=F', 'ZN=F']
es_data = yf.Ticker('ES=F')
Like here why does F equals different symbols?? Can you help me with the CL script? while I try to understand this...
The script fetches data for the last 60 days, since that's all the YFinance API will serve up at 5m resolution.maybe I'm dull but does the script auto-generate the dates for the last 60 days @mashume ?
Few questions.The script fetches data for the last 60 days, since that's all the YFinance API will serve up at 5m resolution.
-mashume
Join useThinkScript to post your question to a community of 21,000+ developers and traders.
Start a new thread and receive assistance from our community.
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.
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.