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

BenTen

BenTen

Administrative
Staff
VIP
Warehouse
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.



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
http://tos.mx/XxwhcP

All credit goes to Robert.

 
Last edited:
BenTen

BenTen

Administrative
Staff
VIP
Warehouse
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
http://tos.mx/wW73tM

 
Last edited:
L

Likos

Member
VIP
@BenTen Will this ideally work via day trading?

 
Last edited:
BenTen

BenTen

Administrative
Staff
VIP
Warehouse
@Likos Thats up for interpretation. Depending on your selected MAs and how you like to use it.

 
Last edited:
M

mrstair

New member
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:
BenTen

BenTen

Administrative
Staff
VIP
Warehouse
@mrstair Unfortunately it does not. The developer just named it e-signal. Had nothing to do with SMS alerts.

 
Last edited:
BenTen

BenTen

Administrative
Staff
VIP
Warehouse
@tenacity11 By stacked do you mean just generally above the EMAs or the crossover?
 
tenacity11

tenacity11

Member
VIP
I'm looking at the crossover where the 5 and 10 are together and cross above the 20. thanks
 
markos

markos

Well-known member
VIP
Warehouse
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
 
tenacity11

tenacity11

Member
VIP
Well actually I've been going through that whole area beginning last night. I will go through what you suggested and thanks again.
 
BenTen

BenTen

Administrative
Staff
VIP
Warehouse
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 :)
 
tenacity11

tenacity11

Member
VIP
That's what happens when you over think. A big fat DUH to me. Thanks Ben for pointing that out.
 
BenTen

BenTen

Administrative
Staff
VIP
Warehouse
@john3 I think because the fastest EMA didn't crossover the other two MAs within 1-2 bars.
 

Top