reference study in different timeframe

MFitz73

Member
Hi guys, I have a question about a strategy running on a 2 min timeframe. Is it possible for that strategy to be able to reference
a lower study from a 10 minute time frame. I know that I can do this with the open or close value but I am having issues trying to do it
with say the lower study macd crossover on a 10 minute time frame. Or maybe stochastic momentum or any lower study that has
a crossover.

I tried the following code and it's the only thing the script accepts but it doesn't seem to work.

def OpenMaster1 = StochasticMomentumIndex(aggregationPeriod.FIFTEEN_MIN) > StochasticMomentumIndex(aggregationPeriod.FIFTEEN_MIN).AvgSMI;

if anyone could confirm that it's not possible for the 2min chart to reference a lower study from a longer time frame
I'd appreciate it!
thanks
Mike
 
Last edited by a moderator:
Solution
Not sure if my questioin got lost in the shuffle or just no way to do it.
I tried the following code and it's the only thing the script accepts but it doesn't seem to work.

def OpenMaster1 = StochasticMomentumIndex(aggregationPeriod.FIFTEEN_MIN) > StochasticMomentumIndex(aggregationPeriod.FIFTEEN_MIN).AvgSMI;

if anyone could confirm that it's not possible for the 2min chart to reference a lower study from a longer time frame
I'd appreciate it!
thanks
Mike

You can make a higher timeframe version of a lower indicator and use it in your code, but it is often not straight forward.

In your example, the parts of the code you needed to change are not available by a refernce to the aggregationperiod as you did. It went into the...
Not sure if my questioin got lost in the shuffle or just no way to do it.
I tried the following code and it's the only thing the script accepts but it doesn't seem to work.

def OpenMaster1 = StochasticMomentumIndex(aggregationPeriod.FIFTEEN_MIN) > StochasticMomentumIndex(aggregationPeriod.FIFTEEN_MIN).AvgSMI;

if anyone could confirm that it's not possible for the 2min chart to reference a lower study from a longer time frame
I'd appreciate it!
thanks
Mike

You can make a higher timeframe version of a lower indicator and use it in your code, but it is often not straight forward.

In your example, the parts of the code you needed to change are not available by a refernce to the aggregationperiod as you did. It went into the overbought portion of the StochasticMomentumIndex
Capture.jpg


What you need to be able to do is change the price values in the Indicator directly if these are not available to change as noted above..

In this case, I made a copy of the Indicator, added an input for agg , defaulted it to your 15m timeframe, added (period=agg) to the prices and changed the plots in the indicator to def statements. Then I made your condition for OpenMaster1.

Ruby:
input agg = aggregationPeriod.FIFTEEN_MIN;
input over_bought = 40.0;
input over_sold = -40.0;
input percentDLength = 3;
input percentKLength = 5;

def min_low = lowest(low(period=agg), percentKLength);
def max_high = highest(high(period=agg), percentKLength);
def rel_diff = close(period=agg) - (max_high + min_low)/2;
def diff = max_high - min_low;

def avgrel = expaverage(expaverage(rel_diff, percentDLength), percentDLength);
def avgdiff = expaverage(expaverage(diff, percentDLength), percentDLength);

def SMI = if avgdiff != 0 then avgrel / (avgdiff / 2) * 100 else 0;
def AvgSMI = expaverage(smi, percentDLength);
def overbought = over_bought;
def oversold = over_sold;

plot OpenMaster1 = if SMI > AvgSMI then 1 else 0;
 
Last edited by a moderator:
Solution

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

You can make a higher timeframe version of a lower indicator and use it in your code, but it is often not straight forward.

In your example, the parts of the code you needed to change are not available by a refernce to the aggregationperiod as you did. It went into the overbought portion of the StochasticMomentumIndex
Capture.jpg


What you need to be able to do is change the price values in the Indicator directly if these are not available to change as noted above..

In this case, I made a copy of the Indicator, added an input for agg , defaulted it to your 15m timeframe, added (period=agg) to the prices and changed the plots in the indicator to def statements. Then I made your condition for OpenMaster1.
Hi @SleepyZ.

I just so happened to stumble on this post while looking to implement a couple custom labels on my workspace. Essentially, I am looking to create a custom labels called "Overbought" and "Oversold" - utilizing the native TOS SMI study (Stochastic Momentum Index) across the 1m/3m/5m timeframes.

The goal of what I am trying to do is that the SMI study counts a value of >40 being "Overbought" and a value of <-40 to be "Oversold". I was trying to create a custom label to add to my chart that when the SMI on each the 1 minute, 3 minute, and 5 minute is >40 it changes the "Overbought" label to RED. On the flipside, when the SMI on each the 1 minute, 3 minute, 5 minute is <-40 it changes the "Oversold" label to GREEN. When neither conditions are met, the labels are just WHITE.

Currently one of my workspaces has a grid I'm always having to manually check the SMI lower study across timeframes (1m/3m/5m) for confluence when they are all either Overbought of Oversold.

The goal is that these 2 labels will fire either RED or GREEN when across all 3 timeframes (1m/3/5m) are either >40 or <-40. Quick confluence point when I am looking to scalp option contracts for SPX.

I've been messing with some of the codes, but as you mentioned I'm having issues with the aggregation period for each specific timeframe referencing the original SMI script. Then need to write the label to only fire the color change when the conditions are met across each of the three timeframes.

I've added the default TOS SMI study code below and would truly appreciate any insight or assistance you can provide.

Code:
#
# TD Ameritrade IP Company, Inc. (c) 2008-2022
#

declare lower;

input over_bought = 40.0;
input over_sold = -40.0;
input percentDLength = 3;
input percentKLength = 5;

def min_low = lowest(low, percentKLength);
def max_high = highest(high, percentKLength);
def rel_diff = close - (max_high + min_low)/2;
def diff = max_high - min_low;

def avgrel = expaverage(expaverage(rel_diff, percentDLength), percentDLength);
def avgdiff = expaverage(expaverage(diff, percentDLength), percentDLength);

plot SMI = if avgdiff != 0 then avgrel / (avgdiff / 2) * 100 else 0;
smi.setDefaultColor(getColor(1));

plot AvgSMI = expaverage(smi, percentDLength);
avgsmi.setDefaultColor(getcolor(5));

plot overbought = over_bought;
overbought.setDefaultColor(getcolor(5));

plot oversold = over_sold;
oversold.setDefaultColor(getcolor(5));
 
Last edited:
Hi @SleepyZ.

I just so happened to stumble on this post while looking to implement a couple custom labels on my workspace. Essentially, I am looking to create a custom labels called "Overbought" and "Oversold" - utilizing the native TOS SMI study (Stochastic Momentum Index) across the 1m/3m/5m timeframes.

The goal of what I am trying to do is that the SMI study counts a value of >40 being "Overbought" and a value of <-40 to be "Oversold". I was trying to create a custom label to add to my chart that when the SMI on each the 1 minute, 3 minute, and 5 minute is >40 it changes the "Overbought" label to RED. On the flipside, when the SMI on each the 1 minute, 3 minute, 5 minute is <-40 it changes the "Oversold" label to GREEN. When neither conditions are met, the labels are just WHITE.

Currently one of my workspaces has a grid I'm always having to manually check the SMI lower study across timeframes (1m/3m/5m) for confluence when they are all either Overbought of Oversold.

The goal is that these 2 labels will fire either RED or GREEN when across all 3 timeframes (1m/3/5m) are either >40 or <-40. Quick confluence point when I am looking to scalp option contracts for SPX.

I've been messing with some of the codes, but as you mentioned I'm having issues with the aggregation period for each specific timeframe referencing the original SMI script. Then need to write the label to only fire the color change when the conditions are met across each of the three timeframes.

I've added the default TOS SMI study code below and would truly appreciate any insight or assistance you can provide.

Ruby:
script smi{
input agg = aggregationPeriod.MIN;
input over_bought = 40.0;
input over_sold = -40.0;
input percentDLength = 3;
input percentKLength = 5;

def min_low = lowest(low(period=agg), percentKLength);
def max_high = highest(high(period=agg), percentKLength);
def rel_diff = close(period=agg) - (max_high + min_low)/2;
def diff = max_high - min_low;

def avgrel = expaverage(expaverage(rel_diff, percentDLength), percentDLength);
def avgdiff = expaverage(expaverage(diff, percentDLength), percentDLength);

plot SMI = if avgdiff != 0 then avgrel / (avgdiff / 2) * 100 else 0;
smi.setDefaultColor(getColor(1));

plot AvgSMI = expaverage(smi, percentDLength);
avgsmi.setDefaultColor(getcolor(5));

plot overbought = over_bought;
overbought.setDefaultColor(getcolor(5));

plot oversold = over_sold;
oversold.setDefaultColor(getcolor(5));
}

def smi1 = smi(agg = "MIN");
def smi2 = smi(agg = "THREE_MIN");
def smi3 = smi(agg = "FIVE_MIN");

addlabel(1, "SMI " + if smi1>40 and smi2>40 and smi3>40
                     then "OverBought"
                     else if smi1<-40 and smi2<-40 and smi3<-40     
                     then "OverSold"
                     else "Neutral",                     
                     if smi1>40 and smi2>40 and smi3>40
                     then color.red
                     else if smi1<-40 and smi2<-40 and smi3<-40     
                     then color.green
                     else color.white);

This code uses the script function to create the smi for each timeframe.

Capture.jpg
Ruby:
script smi{
input agg = aggregationPeriod.MIN;
input over_bought = 40.0;
input over_sold = -40.0;
input percentDLength = 3;
input percentKLength = 5;

def min_low = lowest(low, percentKLength);
def max_high = highest(high, percentKLength);
def rel_diff = close - (max_high + min_low)/2;
def diff = max_high - min_low;

def avgrel = expaverage(expaverage(rel_diff, percentDLength), percentDLength);
def avgdiff = expaverage(expaverage(diff, percentDLength), percentDLength);

plot SMI = if avgdiff != 0 then avgrel / (avgdiff / 2) * 100 else 0;
smi.setDefaultColor(getColor(1));

plot AvgSMI = expaverage(smi, percentDLength);
avgsmi.setDefaultColor(getcolor(5));

plot overbought = over_bought;
overbought.setDefaultColor(getcolor(5));

plot oversold = over_sold;
oversold.setDefaultColor(getcolor(5));
}

def smi1 = smi(agg = "MIN");
def smi2 = smi(agg = "THREE_MIN");
def smi3 = smi(agg = "FIVE_MIN");

addlabel(1, "SMI " + if smi1>40 and smi2>40 and smi3>40
                     then "OverBought"
                     else if smi1<-40 and smi2<-40 and smi3<-40    
                     then "OverSold"
                     else "Neutral",                    
                     if smi1>40 and smi2>40 and smi3>40
                     then color.red
                     else if smi1<-40 and smi2<-40 and smi3<-40    
                     then color.green
                     else color.white);
 
Last edited by a moderator:
Thank you so much!!! Is there a way I could please donate for your time?

Sorry one last thing, is there a way to add an alert ping anytime the Overbought or Oversold flag fire?

You're welcome! I do this because I like to.

Here is revised script with alert option

Ruby:
script smi {
    input agg = AggregationPeriod.MIN;
    input over_bought = 40.0;
    input over_sold = -40.0;
    input percentDLength = 3;
    input percentKLength = 5;

    def min_low = Lowest(low(period = agg), percentKLength);
    def max_high = Highest(high(period = agg), percentKLength);
    def rel_diff = close(period = agg) - (max_high + min_low) / 2;
    def diff = max_high - min_low;

    def avgrel = ExpAverage(ExpAverage(rel_diff, percentDLength), percentDLength);
    def avgdiff = ExpAverage(ExpAverage(diff, percentDLength), percentDLength);

    plot SMI = if avgdiff != 0 then avgrel / (avgdiff / 2) * 100 else 0;
    SMI.SetDefaultColor(GetColor(1));

    plot AvgSMI = ExpAverage(SMI, percentDLength);
    AvgSMI.SetDefaultColor(GetColor(5));

    plot overbought = over_bought;
    overbought.SetDefaultColor(GetColor(5));

    plot oversold = over_sold;
    oversold.SetDefaultColor(GetColor(5));
}

def smi1 = smi(agg = "MIN");
def smi2 = smi(agg = "THREE_MIN");
def smi3 = smi(agg = "FIVE_MIN");

def ob = smi1 > 40 and smi2 > 40 and smi3 > 40;
def os = smi1 < -40 and smi2 < -40 and smi3 < -40;

AddLabel(1, "SMI " + if ob
                     then "OverBought"
                     else if os   
                     then "OverSold"
                     else "Neutral",                     
                     if ob
                     then Color.RED
                     else if os     
                     then Color.GREEN
                     else Color.WHITE);


input alerts = yes;
Alert(!os[1] and os or !ob[1] and ob, "SMI" , Alert.BAR, Sound.Ding);
#
 
This code uses the script function to create the smi for each timeframe.

View attachment 3492
Ruby:
script smi{
input agg = aggregationPeriod.MIN;
input over_bought = 40.0;
input over_sold = -40.0;
input percentDLength = 3;
input percentKLength = 5;

def min_low = lowest(low, percentKLength);
def max_high = highest(high, percentKLength);
def rel_diff = close - (max_high + min_low)/2;
def diff = max_high - min_low;

def avgrel = expaverage(expaverage(rel_diff, percentDLength), percentDLength);
def avgdiff = expaverage(expaverage(diff, percentDLength), percentDLength);

plot SMI = if avgdiff != 0 then avgrel / (avgdiff / 2) * 100 else 0;
smi.setDefaultColor(getColor(1));

plot AvgSMI = expaverage(smi, percentDLength);
avgsmi.setDefaultColor(getcolor(5));

plot overbought = over_bought;
overbought.setDefaultColor(getcolor(5));

plot oversold = over_sold;
oversold.setDefaultColor(getcolor(5));
}

def smi1 = smi(agg = "MIN");
def smi2 = smi(agg = "THREE_MIN");
def smi3 = smi(agg = "FIVE_MIN");

addlabel(1, "SMI " + if smi1>40 and smi2>40 and smi3>40
                     then "OverBought"
                     else if smi1<-40 and smi2<-40 and smi3<-40   
                     then "OverSold"
                     else "Neutral",                   
                     if smi1>40 and smi2>40 and smi3>40
                     then color.red
                     else if smi1<-40 and smi2<-40 and smi3<-40   
                     then color.green
                     else color.white);
Thank you so much! The label is working as intended, much appreciated!!
Hi, the code is great. Seems like you don't have a problem, but one quick note is that you can't reference a lower timeframe in a study on a chart with a higher timeframe. Eg, if the chart timeframe is 15 minutes, the minimum aggregation period you could use in the study would be 15 minutes, while, say, a 5 min, 3 min or 1 min agg period would produce NA. To use 5,3 and 1 min agg periods in a study, then the chart timeframe should be 1 min. (You can reference higher timeframes than the chart in a study, eg, you can use a 5 min agg period in a study on a 1 min chart) Hope this is helpful.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
435 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