3-in-1 Simple or Exponential Moving Average for ThinkorSwim

BenTen

Administrative
Staff member
Staff
VIP
Lifetime
Here is a multi-moving averages indicator for ThinkorSwim so that you can easily add more than one simple or exponential moving averages without adding multiple indicators. Normally if you want to add a 5 and 10 SMA into your chart, you would need to include 2 different inputs and 2 copies of the same indicator. Now you don't have to. With this indicator, you can include 3 SMA or EMA into your chart in a single indicator.

The indicator is fully customizable and includes crossover signals (Bullish and Bearish) as well.

Jtqv3KK.png


thinkScript Code

Rich (BB code):
# E-Charts v2

declare upper;
input short_average = 5;
input medium_average = 10;
input long_average = 20;
input average_type = {default "SMA", "EMA"};
input show_vertical_line = no;
input show_bubble_labels = yes;

def MA1;
def MA2;
def MA3;
switch (average_type) {
case "SMA":
    MA1 = Average(close, short_average);
    MA2 = Average(close, medium_average);
    MA3 = Average(close, long_average);
case "EMA":
    MA1 = ExpAverage(close, short_average);
    MA2 = ExpAverage(close, medium_average);
    MA3 = ExpAverage(close, long_average);
}

# define e-signal and crossover point
def Eup = MA1 > MA2 && MA2 > MA3;
def Edn = MA1 < MA2 && MA2 < MA3;

def CrossUp = close > MA1 && Eup && !Eup[1];
def CrossDn = close < MA1 && Edn && !Edn[1];

# Define up and down signals
def higherHigh = close > Highest(max(open,close), 3)[1];
def lowerLow = close < Lowest(min(open,close), 3)[1];
def SignalUp = if (CrossUp && higherHigh)
    then 1
        else if    (CrossUp[1] && higherHigh && !higherHigh[1])
    then 1
        else if    (CrossUp[2] && higherHigh && !higherHigh[1] && !higherHigh[2])
    then 1
        else Double.NaN;
def SignalDn = if (CrossDn && lowerLow)
    then 1
        else if (CrossDn[1] && lowerLow && !lowerLow[1])
    then 1
        else if (CrossDn[2] && lowerLow && !lowerLow[1] && !lowerLow[2])
    then 1
        else Double.NaN;
       
# Plot the moving average lines
plot ln1 = MA1;
ln1.SetDefaultColor(CreateColor(145, 210, 144));
ln1.SetLineWeight(2);
plot ln2 = MA2;
ln2.SetDefaultColor(CreateColor(111, 183, 214));
ln2.SetLineWeight(2);
plot ln3 = MA3;
ln3.SetDefaultColor(CreateColor(249, 140, 182));
ln3.SetLineWeight(2);
   
# Draw vertical line to indicate call and put signals
AddVerticalLine(SignalUp && show_vertical_line, "Up", Color.UPTICK);
AddVerticalLine(SignalDn && show_vertical_line, "Down", Color.LIGHT_RED);

# Show Call / Put Signal in a Chart Bubble
AddChartBubble(SignalUp && show_bubble_labels, low - 0.3, "Up", Color.UPTICK, no);
AddChartBubble(SignalDn && show_bubble_labels, high + 0.3, "Dn", Color.LIGHT_RED);

# Add label for Eup or Edn
AddLabel(Eup, "E Up", Color.GREEN);
AddLabel(Edn, "E Dn", Color.RED);

Shareable Link

https://tos.mx/XxwhcP

All credit goes to Robert.
 
Last edited:

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

If you only use 2 moving averages, here is the 2-in-1 version. Same functionality.

thinkScript Code

Rich (BB code):
# E-Charts v3

declare upper;
input short_average = 14;
input medium_average = 30;
input average_type = {default "SMA", "EMA"};
input showArrows = yes;

def MA1;
def MA2;
switch (average_type) {
case "SMA":
    MA1 = Average(close, short_average);
    MA2 = Average(close, medium_average);
case "EMA":
    MA1 = ExpAverage(close, short_average);
    MA2 = ExpAverage(close, medium_average);
}

# define e-signal and crossover point
def Eup = MA1 > MA2 && MA2;
def Edn = MA1 < MA2 && MA2;

def CrossUp = close > MA1 && Eup && !Eup[1];
def CrossDn = close < MA1 && Edn && !Edn[1];

# Define up and down signals
def higherHigh = close > Highest(Max(open, close), 3)[1];
def lowerLow = close < Lowest(Min(open, close), 3)[1];
def SignalUp = if (CrossUp && higherHigh)
    then 1
        else if    (CrossUp[1] && higherHigh && !higherHigh[1])
    then 1
        else if    (CrossUp[2] && higherHigh && !higherHigh[1] && !higherHigh[2])
    then 1
        else Double.NaN;
def SignalDn = if (CrossDn && lowerLow)
    then 1
        else if (CrossDn[1] && lowerLow && !lowerLow[1])
    then 1
        else if (CrossDn[2] && lowerLow && !lowerLow[1] && !lowerLow[2])
    then 1
        else Double.NaN;

# Plot the moving average lines
plot ln1 = MA1;
     ln1.SetDefaultColor(CreateColor(145, 210, 144));
     ln1.SetLineWeight(2);
plot ln2 = MA2;
     ln2.SetDefaultColor(CreateColor(111, 183, 214));
     ln2.SetLineWeight(2);
   
# Show an arrow
plot ArrowUp = if SignalUp and showArrows then low * 0.997 else Double.NaN;
     ArrowUp.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
     ArrowUp.SetLineWeight(4);
     ArrowUp.SetDefaultColor(Color.YELLOW);
plot ArrowDn = if SignalDn and showArrows then high * 1.003 else Double.NaN;
     ArrowDn.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
     ArrowDn.SetLineWeight(4);
     ArrowDn.SetDefaultColor(Color.CYAN);

# Calculate Meter Values
def sl1 = ma1 > ma1[1];
def sl2 = ma2 > ma2[1];
def p1 = close > ma1;
def p2 = close > ma2;
def u1 = 2 * (close > open);
def u2 = 2 * (close > high[1]);
def d1 = 2 * (close < open);
def d2 = 2 * (close < low[1]);

def UPstr = sl1 + sl2 + p1 + p2 + u1 + u2;
def DNstr = !sl1 + !sl2 + !p1 +!p2 + d1 + d2;

Shareable Link

https://tos.mx/wW73tM
 
Last edited:
@Likos Thats up for interpretation. Depending on your selected MAs and how you like to use it.

 
Last edited:
The "3-in-1 Simple or Exponential Moving Average" indicator has a section "define e-signal and crossover point". Does this issue an esignal (SMS) and alert at the crossover point to email and text?

 
Last edited:
@mrstair Unfortunately it does not. The developer just named it e-signal. Had nothing to do with SMS alerts.

 
Last edited:
I'm wondering if a scan can be done when price H is moving above the 3moving averages(ema) which are stacked. Thanks so much.
 

Attachments

  • 3 ma2019-06-26-TOS_CHARTS.png
    3 ma2019-06-26-TOS_CHARTS.png
    9.8 KB · Views: 1,400
I'm looking at the crossover where the 5 and 10 are together and cross above the 20. thanks
 
Thanks for the reply but I could not find anything
What do you mean? No one can help you if your not specific. Just for fun, I clicked on the link above and it works. There is no way you could go through that onenote section in 8 minutes. It took me that long to find the right section for you. What you are looking for is something like plot_scan, plot scan, or something in the moving ave. section of the scans in one note to copy-paste into your script to make it scan. There is a section in there called "How to drive the scanner"., read that. The second tab from the top in the OneNote says the Manual. Look for a template there. Good Trading, Markos
 
Well actually I've been going through that whole area beginning last night. I will go through what you suggested and thanks again.
 
I'm looking at the crossover where the 5 and 10 are together and cross above the 20. thanks

You can just simply scan for 5/20 crossover. If you think about it the 10 MA is faster than the 20 MA. If 5 MA crossing the 20 that means it also already crossed the 10 :)
 
@BenTen This is an useful indicator as well as time saving. Can you help to put on an alert script for the "up" and "down" signals? Thanks !
 
@Jenny Add this to the bottom of your script:

Code:
# Alerts
Alert(CrossUp, " ", Alert.Bar, Sound.Chimes);
Alert(CrossDn, " ", Alert.Bar, Sound.Bell);
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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