add a time variable to the SMI script

themainman

New member
I currently use the below SMI lower study across my ES charts. Right now, the study just plots the SMI and arrows when the crosses occur according to whatever timeframe the lower study is set to.

I typically am using this study on ES 1m, 3m, 5m charts. I was looking to see if you knew a way to add a time variable to the SMI script below so I could stack lower studies of it that would display the SMI of ES (or whatever ticker the chart is on) for 1m, 3m, 5m timeframes.

The reason so is right now I'm using a flexible grid with different timeframes, but if I could have the SMI plotted and stacked on top of each other, for example, 1m ES chart, but lower study will have 3 SMI's. One for 1m, 3m, and 5m.

Is it possible to tweak the script below and have it display for a specific timeframe rather than the one selected on the chart the lower study is added to? Your insight is much appreciated!

Code:
# Stochastic Momentum Index
# Base code by TD Ameritrade IP Company, Inc. (c) 2008-2020
# Added arrows and alerts when SMI/AvgSMI cross above/below the oversold/overbought lines

declare lower;

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

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 signalUp = AvgSMI[1] < oversold and SMI[1] < AvgSMI[1] and SMI > AvgSMI;
def signalDn = AvgSMI[1] > overbought and SMI[1] > AvgSMI[1] and SMI < AvgSMI;

plot arrowUp = if signalUp then AvgSMI else double.nan;
     arrowUp.SetPaintingStrategy(PaintingStrategy.Arrow_Up);
     arrowUp.SetDefaultColor(color.green);

plot arrowDn = if signalDn then AvgSMI else double.nan;
     arrowDn.SetPaintingStrategy(PaintingStrategy.Arrow_Down);
     arrowDn.SetDefaultColor(color.red);



#end code
 
Last edited by a moderator:

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
213 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.
Top