MTF calculate the SMAs of SPX and then add the fair value differential of ES

Glefdar

Active member
I'm not sure why, is there a simple reason and fix that I'm missing? (The study is deliberately written to only plot in the expansion area, to clarify.)

Code:
input price = FundamentalType.CLOSE;
input aggregationPeriodhour = AggregationPeriod.HOUR;
input aggregationPeriodFourhour = aggregationPeriod.FOUR_HOURS;
input lengthtwenty = 20;
input lengthfifty  = 50;
input lengthtwohundred = 200;
input offset = 1;
input length2 = 25;
input displace=0;

def SMAtwenty_hour_pre = Average(fundamental(price, period = aggregationPeriodhour)[-displace], lengthtwenty)  ;

def SMAtwenty_hour_pre_line = SimpleMovingAvg(SMAtwenty_hour_pre, 1, length2);
rec SMAtwenty_hour_pre_line_2 = if IsNaN(SMAtwenty_hour_pre_line) then SMAtwenty_hour_pre_line_2[1] else SMAtwenty_hour_pre_line[offset];
plot SMAtwenty_hour= if !isNan(close) then double.nan else if isnan(SMAtwenty_hour_pre_line) then SMAtwenty_hour_pre_line_2 else double.nan;

SMAtwenty_hour.SetDefaultColor(CreateColor(143,239,191));
SMAtwenty_hour.SetPaintingStrategy(PaintingStrategy.line_vs_points);
SMAtwenty_hour.SetLineWeight(1);

def SMAfifty_hour_pre = Average(fundamental(price, period = aggregationPeriodhour)[-displace], lengthfifty);

def SMAfifty_hour_pre_line = SimpleMovingAvg(SMAfifty_hour_pre, 1, length2);
rec SMAfifty_hour_pre_line_2 = if IsNaN(SMAfifty_hour_pre_line) then SMAfifty_hour_pre_line_2[1] else SMAfifty_hour_pre_line[offset];
plot SMAfifty_hour= if !isNan(close) then double.nan else  if isnan(SMAfifty_hour_pre_line) then SMAfifty_hour_pre_line_2 else double.nan;

SMAfifty_hour.SetDefaultColor(CreateColor(0,0,255));
SMAfifty_hour.SetPaintingStrategy(PaintingStrategy.line_vs_points);
SMAfifty_hour.SetLineWeight(1);

def SMAtwohundred_hour_pre = Average(fundamental(price, period = aggregationPeriodhour)[-displace], lengthtwohundred);

def SMAtwohundred_hour_pre_line = SimpleMovingAvg(SMAtwohundred_hour_pre, 1, length2);
rec SMAtwohundred_hour_pre_line_2 = if IsNaN(SMAtwohundred_hour_pre_line) then SMAtwohundred_hour_pre_line_2[1] else SMAtwohundred_hour_pre_line[offset];
plot SMAtwohundred_hour= if !isNan(close) then double.nan else  if isnan(SMAtwohundred_hour_pre_line) then SMAtwohundred_hour_pre_line_2 else double.nan;

SMAtwohundred_hour.SetDefaultColor(CreateColor(255,102,255));
SMAtwohundred_hour.SetPaintingStrategy(PaintingStrategy.line_vs_points);
SMAtwohundred_hour.SetLineWeight(1);

def SMAtwenty_fourhour_pre =Average(fundamental(price, period = aggregationPeriodfourhour)[-displace], lengthtwenty) ;

def SMAtwenty_fourhour_pre_line = SimpleMovingAvg(SMAtwenty_fourhour_pre, 1, length2);
rec SMAtwenty_fourhour_pre_line_2 = if IsNaN(SMAtwenty_fourhour_pre_line) then SMAtwenty_fourhour_pre_line_2[1] else SMAtwenty_fourhour_pre_line[offset];
plot SMAtwenty_fourhour= if !isNan(close) then double.nan else  if isnan(SMAtwenty_fourhour_pre_line) then SMAtwenty_fourhour_pre_line_2 else double.nan;

SMAtwenty_fourhour.SetDefaultColor(CreateColor(143,239,191));
SMAtwenty_fourhour.SetPaintingStrategy(PaintingStrategy.line_vs_points);
SMAtwenty_fourhour.SetLineWeight(2);

def SMAfifty_fourhour_pre =Average(fundamental(price, period = aggregationPeriodfourhour)[-displace], lengthfifty) ;

def SMAfifty_fourhour_pre_line = SimpleMovingAvg(SMAfifty_fourhour_pre, 1, length2);
rec SMAfifty_fourhour_pre_line_2 = if IsNaN(SMAfifty_fourhour_pre_line) then SMAfifty_fourhour_pre_line_2[1] else SMAfifty_fourhour_pre_line[offset];
plot SMAfifty_fourhour=  if !isNan(close) then double.nan else if isnan(SMAfifty_fourhour_pre_line) then SMAfifty_fourhour_pre_line_2 else double.nan;

SMAfifty_fourhour.SetDefaultColor(CreateColor(0,0,255));
SMAfifty_fourhour.SetPaintingStrategy(PaintingStrategy.line_vs_points);
SMAfifty_fourhour.SetLineWeight(2);

def SMAtwohundred_fourhour_pre = Average(fundamental(price, period = aggregationPeriodfourhour)[-displace], lengthtwohundred) ;

def SMAtwohundred_fourhour_pre_line = SimpleMovingAvg(SMAtwohundred_fourhour_pre, 1, length2);
rec SMAtwohundred_fourhour_pre_line_2 = if IsNaN(SMAtwohundred_fourhour_pre_line) then SMAtwohundred_fourhour_pre_line_2[1] else SMAtwohundred_fourhour_pre_line[offset];
plot SMAtwohundred_fourhour= if !isNan(close) then double.nan else  if isnan(SMAtwohundred_fourhour_pre_line) then SMAtwohundred_fourhour_pre_line_2 else double.nan;

SMAtwohundred_fourhour.SetDefaultColor(CreateColor(255,102,255));
SMAtwohundred_fourhour.SetPaintingStrategy(PaintingStrategy.line_vs_points);
SMAtwohundred_fourhour.SetLineWeight(2);
 
I'm not sure why, is there a simple reason and fix that I'm missing? (The study is deliberately written to only plot in the expansion area, to clarify.)
Higher Timeframs can't access lower aggregations, this is the limitation of ThinkorSwim, nothing wrong with your script. The script won't work or plot anything above 1H, say 2H and above, chart as the lowest aggregation used is 1H.

Another limitation of TOS, if one of the plot statements is invalid the remaining plot in the script will stop plotting too.

-S
 
Higher Timeframs can't access lower aggregations, this is the limitation of ThinkorSwim, nothing wrong with your script. The script won't work or plot anything above 1H, say 2H and above, chart as the lowest aggregation used is 1H.

Another limitation of TOS, if one of the plot statements is invalid the remaining plot in the script will stop plotting too.

-S

Sorry, I didn't explain well. That is not the issue here because I'm using the study on a chart with a lower time frame than the periods I'm selecting. So I use it on a 5min chart, for example, and can see the 4H SMAs just fine. But when I change the aggregation to be 1 Day, 1 Week, etc. then it won't plot.
 
Sorry, I didn't explain well. That is not the issue here because I'm using the study on a chart with a lower time frame than the periods I'm selecting. So I use it on a 5min chart, for example, and can see the 4H SMAs just fine. But when I change the aggregation to be 1 Day, 1 Week, etc. then it won't plot.
Yes, they do plot.
You need to change your chart settings in order to see them:
TTPeVO0.jpg

SnitJ4c.png
 
Yes, they do plot.
You need to change your chart settings in order to see them:
TTPeVO0.jpg

SnitJ4c.png
Thank you 🙏 I realize you're right now that I'm checking it on a SPY chart, but there was a different issue that I'll mention in case someone else has the same confusion. They won't plot on an /ES chart (which is what I was trying to do). I am not sure why but I am guessing this is because of something to do with the way the contracts roll not allowing enough bars of any individual expiration for the SMAs to be calculated.
 
Last edited:
Thank you 🙏 I realize you're right now that I'm checking it on a SPY chart, but there was a different issue that I'll mention in case someone else has the same confusion. They won't plot on an /ES chart (which is what I was trying to do). I am not sure why but I am guessing this is because of something to do with the way the contracts roll not allowing enough bars of any individual expiration for the SMAs to be calculated.

@MerryDay I thought I had a solution to whatever is preventing the SMAs from plotting for /ES, but I guess not: I tried to just calculate the SMAs of SPX and then add the fair value differential of ES to that, to plot the day and week SMAs for /ES. It doesn't work, I'm not sure if it's because the moving average function can't accept another symbol:

Code:
input aggregationPeriodhour = AggregationPeriod.day;
input aggregationPeriodFourhour = aggregationPeriod.week;
def price1 = CLOSE(symbol="SPX",period=aggregationPeriodhour);
def price2 = CLOSE(symbol="SPX",period=aggregationPeriodFourhour);
input lengthtwenty = 20;
input lengthfifty  = 50;
input lengthtwohundred = 200;
input offset = 1;
input length2 = 25;
input displace=0;

def fairvalueES_offset = close-(close(symbol="SPX"));

def SMAtwenty_hour_pre = SimpleMovingAvg(price1, lengthtwenty)  ;

def SMAtwenty_hour_pre_line = SimpleMovingAvg(SMAtwenty_hour_pre, 1, length2);
rec SMAtwenty_hour_pre_line_2 = if IsNaN(SMAtwenty_hour_pre_line) then SMAtwenty_hour_pre_line_2[1] else SMAtwenty_hour_pre_line[offset];
plot SMAtwenty_hour= if !isNan(close) then double.nan else if isnan(SMAtwenty_hour_pre_line) then SMAtwenty_hour_pre_line_2+fairvalueES_offset else double.nan;

SMAtwenty_hour.SetDefaultColor(CreateColor(143,239,191));
SMAtwenty_hour.SetPaintingStrategy(PaintingStrategy.line_vs_points);
SMAtwenty_hour.SetLineWeight(1);

def SMAfifty_hour_pre = SimpleMovingAvg(price1, lengthfifty);

def SMAfifty_hour_pre_line = SimpleMovingAvg(SMAfifty_hour_pre, 1, length2);
rec SMAfifty_hour_pre_line_2 = if IsNaN(SMAfifty_hour_pre_line) then SMAfifty_hour_pre_line_2[1] else SMAfifty_hour_pre_line[offset];
plot SMAfifty_hour= if !isNan(close) then double.nan else  if isnan(SMAfifty_hour_pre_line) then SMAfifty_hour_pre_line_2+fairvalueES_offset else double.nan;

SMAfifty_hour.SetDefaultColor(CreateColor(0,0,255));
SMAfifty_hour.SetPaintingStrategy(PaintingStrategy.line_vs_points);
SMAfifty_hour.SetLineWeight(1);

def SMAtwohundred_hour_pre = SimpleMovingAvg(price1, lengthtwohundred);

def SMAtwohundred_hour_pre_line = SimpleMovingAvg(SMAtwohundred_hour_pre, 1, length2);
rec SMAtwohundred_hour_pre_line_2 = if IsNaN(SMAtwohundred_hour_pre_line) then SMAtwohundred_hour_pre_line_2[1] else SMAtwohundred_hour_pre_line[offset];
plot SMAtwohundred_hour= if !isNan(close) then double.nan else  if isnan(SMAtwohundred_hour_pre_line) then SMAtwohundred_hour_pre_line_2+fairvalueES_offset else double.nan;

SMAtwohundred_hour.SetDefaultColor(CreateColor(255,102,255));
SMAtwohundred_hour.SetPaintingStrategy(PaintingStrategy.line_vs_points);
SMAtwohundred_hour.SetLineWeight(1);

def SMAtwenty_fourhour_pre =SimpleMovingAvg(price2, lengthtwenty) ;

def SMAtwenty_fourhour_pre_line = SimpleMovingAvg(SMAtwenty_fourhour_pre, 1, length2);
rec SMAtwenty_fourhour_pre_line_2 = if IsNaN(SMAtwenty_fourhour_pre_line) then SMAtwenty_fourhour_pre_line_2[1] else SMAtwenty_fourhour_pre_line[offset];
plot SMAtwenty_fourhour= if !isNan(close) then double.nan else  if isnan(SMAtwenty_fourhour_pre_line) then SMAtwenty_fourhour_pre_line_2+fairvalueES_offset else double.nan;

SMAtwenty_fourhour.SetDefaultColor(CreateColor(143,239,191));
SMAtwenty_fourhour.SetPaintingStrategy(PaintingStrategy.line_vs_points);
SMAtwenty_fourhour.SetLineWeight(2);

def SMAfifty_fourhour_pre =SimpleMovingAvg(price2, lengthfifty) ;

def SMAfifty_fourhour_pre_line = SimpleMovingAvg(SMAfifty_fourhour_pre, 1, length2);
rec SMAfifty_fourhour_pre_line_2 = if IsNaN(SMAfifty_fourhour_pre_line) then SMAfifty_fourhour_pre_line_2[1] else SMAfifty_fourhour_pre_line[offset];
plot SMAfifty_fourhour=  if !isNan(close) then double.nan else if isnan(SMAfifty_fourhour_pre_line) then SMAfifty_fourhour_pre_line_2+fairvalueES_offset else double.nan;

SMAfifty_fourhour.SetDefaultColor(CreateColor(0,0,255));
SMAfifty_fourhour.SetPaintingStrategy(PaintingStrategy.line_vs_points);
SMAfifty_fourhour.SetLineWeight(2);

def SMAtwohundred_fourhour_pre = SimpleMovingAvg(price2, lengthtwohundred) ;

def SMAtwohundred_fourhour_pre_line = SimpleMovingAvg(SMAtwohundred_fourhour_pre, 1, length2);
rec SMAtwohundred_fourhour_pre_line_2 = if IsNaN(SMAtwohundred_fourhour_pre_line) then SMAtwohundred_fourhour_pre_line_2[1] else SMAtwohundred_fourhour_pre_line[offset];
plot SMAtwohundred_fourhour= if !isNan(close) then double.nan else  if isnan(SMAtwohundred_fourhour_pre_line) then SMAtwohundred_fourhour_pre_line_2+fairvalueES_offset else double.nan;

SMAtwohundred_fourhour.SetDefaultColor(CreateColor(255,102,255));
SMAtwohundred_fourhour.SetPaintingStrategy(PaintingStrategy.line_vs_points);
SMAtwohundred_fourhour.SetLineWeight(2);
 
Last edited:
@imk99 ES and SPX values is not the same, hence the SMA Values will change too, can't be a solution for your problem.

here is an idea, Split the Script, don't bother with MTF, Instead just use higher frame and the higher frame as an input. add 2 instance of the script, each set to a different timeframe, ex: 1st instance to 1H and the 2nd instance to 4H.

-S
 
Last edited:
@imk99 ES and SPX values is not the same, hence the SMA Values will change too, can't be a solution for your problem.

here is an idea, Split the Script, don't bother with MTF, Instead just use higher frame and the higher frame as an input. add 2 instance of the script, each set to a different timeframe, ex: 1st instance to 1H and the 2nd instance to 4H.

-S

For the purpose of finding support and resistance, it is just as well to add the fair value of ES to whatever is calculated for SPX. It's a similar idea to plotting the highest OI strikes for SPX on an ES chart.

(The SPX key price levels are much more important than the ES price levels, in terms of affecting how ES trades, but most of the time they'll be the same. In this case however it seems to be necessary to calculate the SMAs separately and add the fair value differential to that in order to make those key levels plot on an ES chart.)

I have my charts set up in a particular way to make it easy to identify confluences among various levels so that is why I'm hoping there is a solution for making the MTF study plot.
 
For the purpose of finding support and resistance, it is just as well to add the fair value of ES to whatever is calculated for SPX. It's a similar idea to plotting the highest OI strikes for SPX on an ES chart.

(The SPX key price levels are much more important than the ES price levels, in terms of affecting how ES trades, but most of the time they'll be the same. In this case however it seems to be necessary to calculate the SMAs separately and add the fair value differential to that in order to make those key levels plot on an ES chart.)

I have my charts set up in a particular way to make it easy to identify confluences among various levels so that is why I'm hoping there is a solution for making the MTF study plot.
ES and SPX have been about 18 - 28 points off for far too long, that /ES == SPX and /ES == SPX * 10 is broken.

I know coz, I had to write an indicator to get the offset for some mad math, the sad thing about this is, that TOS SecondsTillTime() will still not be able to grab the data for actual second, when you are on any chart above 1m.

Happy Hunting, with your script issue, AFA MAs, Concerned, I verified 1D 1M charts can plot MA 1H and MA 4H just fine on their own. Don't want to invest time on something that is a non issue. But perhaps when I got some time will take a stab at this, purely in the pursuit of knowledge.

-S
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Thread starter Similar threads Forum Replies Date
T Ehlers Stochastic and AMA into a MTF? Questions 0
B MTF Stochastic Questions 1
E MTF Stacked Moving Averages Questions 3
M MTF AMA Questions 2
C MTF EMA cloud Questions 1

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
354 Online
Create Post

Similar threads

Similar threads

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