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

@petey150 Start by going to the Scan tab > Add new study filter. Start with the first condition. Then add another condition.
 
Since I daytrade, I am really fond of this indicator. I have added labels to display the ema/ma's on my chart and also added VWAP label which turns green if price > vwap and red if price is < vwap. All the ema/ma labels are of the same color as the plot line respectively. Also I added 50/200d ma which I make sure intersects with the 3-ema crossover to generate a slightly better buy / sell signal. This works works out fine for me. Here is a slightly modified version of the original code which I use.

Code:
# 3-in-1 moving avg crossover

declare upper;
input short_average = 5;
input medium_average = 8;
input long_average = 13;
input fifty_day = 50;
input two_hundred_day = 200;
input average_type = {default "EMA", "SMA"};
input show_vertical_line = no;
input show_bubble_labels = no;
input show_arrows = yes;

def MA1;
def MA2;
def MA3;
def MA4;
def MA5;
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);
}

#50 & 200d ma
MA4 = Average(close, fifty_day);
MA5 = Average(close, two_hundred_day);

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

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

#Current Price
def current_price = close;

#Vwap Label
def price_close = close(period = AggregationPeriod.DAY);
def vwap = vwap(period = AggregationPeriod.DAY);
AddLabel(yes, "VWAP" + ":" + Round(vwap,2),if price_close > vwap then Color.UPTICK else if price_close < vwap then Color.RED else CreateColor(255, 255, 255));

# 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(0, 204, 204));
ln1.SetLineWeight(2);
AddLabel(yes, short_average + " EMA" + ":" + Round(MA1,2),CreateColor(0, 204, 204));
plot ln2 = MA2;
ln2.SetDefaultColor(CreateColor(255, 102, 0));
ln2.SetLineWeight(2);
AddLabel(yes, medium_average + " EMA" + ":" + Round(MA2,2),CreateColor(255, 102, 0));
plot ln3 = MA3;
ln3.SetDefaultColor(CreateColor(231, 190, 0));
ln3.SetLineWeight(2);
AddLabel(yes, long_average + " EMA" + ":" + Round(MA3,2),CreateColor(231, 190, 0));
plot ln4 = MA4;
ln4.SetDefaultColor(CreateColor(143, 239, 191));
ln4.SetLineWeight(2);
AddLabel(yes, fifty_day + " MA" + ":" + Round(MA4,2),CreateColor(143, 239, 191));
plot ln5 = MA5;
ln5.SetDefaultColor(CreateColor(234, 156, 255));
ln5.SetLineWeight(2);
AddLabel(yes, two_hundred_day + " MA" + ":" + Round(MA5,2),CreateColor(234, 156, 255));

# 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);
  
# Plot Up / Down Arrow
plot up_signal = SignalUp;
up_signal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
up_signal.SetDefaultColor(Color.YELLOW);
up_signal.SetLineWeight(5);
up_signal.SetHiding(!show_arrows);
plot down_signal = SignalDn;
down_signal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
down_signal.SetDefaultColor(Color.MAGENTA);
down_signal.SetLineWeight(5);
down_signal.SetHiding(!show_arrows);

sVvVA0f.png
 
New here! never thought this site existed. Ive been digging for a while and i cant seem to find the right code im looking for. Its pretty close to the posts here. Im looking for something like this, youtube link lol , the conditions seems like when the 5 EMAs (8, 21, 34, 55, 89) are stacked the "Stacked EM" turns green (top left of chart), if its not, its grey.
 

thanks for that Ben! I actually found one but my whole screen turned green lol.

Code:
def EMA8 = ExpAverage(close, 8);
def EMA13 = ExpAverage(close, 13);
def EMA21 = ExpAverage(close, 21);
def EMA34 = ExpAverage(close, 34);

def bullish = EMA8 > EMA13 and EMA13 > EMA21 and EMA21 > EMA34;
def bearish = EMA8 < EMA13 and EMA13 < EMA21 and EMA21 < EMA34;

AddLabel(bullish, "Bullish Stacked MAs", color.black);
AddLabel(bearish, "Bearish StackedMAs", color.black);
AddLabel(!bullish and !bearish, " ", color.black);
 
Last edited:
I was wondering if there was a way to make just one script with ema 3/8/15/34 and vwap all on the same script so that I can use it on mobile and instead of adding all 5 scripts on my laptop. Is that even possible any help would be appreciated
 
I tried this @sobiswas for the same ticker SNOW on 1Y1D but the indicator disappears, whereas the original 3-in-1 MvgAvg works properly.
Can you please check once if this Indicator can work on 1Y1D time frame ?
 
Hello, I was wondering if someone can help me with this code. It seems that if I switch to daily(yearly) and I look at a company like PLTR, it wont show me the moving averages but if I switch to like MSFT or AAPL, it works just fine. Any advice on how to fix this? Thank you.

Code:
input AvgType = averageType.SIMPLE;
input Length1 = 9;
input Length2 = 21;
input Length3 = 50;
input Length4 = 90;
input Length5 = 200;
Input Length6 = 500;
input price = close;

plot Avg1 = MovingAverage(AvgType, price, Length1);
Avg1.SetDefaultColor(GetColor(1));

plot Avg2 = MovingAverage(AvgType, price, Length2);
Avg2.SetDefaultColor(GetColor(1));

plot Avg3 = MovingAverage(AvgType, price, Length3);
Avg3.SetDefaultColor(GetColor(1));

plot Avg4 = MovingAverage(AvgType, price, Length4);
Avg4.SetDefaultColor(GetColor(1));

plot Avg5 = MovingAverage(AvgType, price, Length5);
Avg5.SetDefaultColor(GetColor(1));

plot Avg6 = MovingAverage(AvgType, price, Length6);
Avg6.SetDefaultColor(GetColor(1));

def MA_1_above = if price > Avg1 then 1 else 0;
def MA_1_below = if price <= Avg1 then 1 else 0;
AddLabel(MA_1_above, Length1 + " SMA: " + Avg1, COLOR.Green);
AddLabel(MA_1_below, Length1 + " SMA: " + Avg1, COLOR.RED);

def MA_2_above = if price > Avg2 then 1 else 0;
def MA_2_below = if price <= Avg2 then 1 else 0;
AddLabel(MA_1_above, Length2+ " SMA: " + Avg2, COLOR.Green);
AddLabel(MA_1_below, Length2 + " SMA: " + Avg2, COLOR.RED);

def MA_3_above = if price > Avg3 then 1 else 0;
def MA_3_below = if price <= Avg3 then 1 else 0;
AddLabel(MA_3_above, Length3 + " SMA: " + Avg3, COLOR.Green);
AddLabel(MA_3_below, Length3 + " SMA: " + Avg3, COLOR.RED);

def MA_4_above = if price > Avg4 then 1 else 0;
def MA_4_below = if price <= Avg4 then 1 else 0;
AddLabel(MA_4_above,Length4+ " SMA: " + Avg4, COLOR.Green);
AddLabel(MA_4_below, Length4 + " SMA: " + Avg4, COLOR.RED);

def MA_5_above = if price > Avg5 then 1 else 0;
def MA_5_below = if price <= Avg5 then 1 else 0;
AddLabel(MA_5_above, Length5 + " SMA: " + Avg5, COLOR.Green);
AddLabel(MA_5_below, Length5 + " SMA: " + Avg5, COLOR.RED);

def MA_6_above = if price > Avg6 then 1 else 0;
def MA_6_below = if price <= Avg6 then 1 else 0;
AddLabel(MA_5_above, Length6 + " SMA: " + Avg6, COLOR.Green);
AddLabel(MA_5_below, Length6 + " SMA: " + Avg6, COLOR.RED);
 
@chernandez88 The study needs as many bars on the chart as the longest moving average. PLTR is a recent IPO, and has been trading for less than 500 days. The only way to show the shorter moving averages is to plot them separately.
 
I was wondering if there was a way to make just one script with ema 3/8/15/34 and vwap all on the same script so that I can use it on mobile and instead of adding all 5 scripts on my laptop. Is that even possible any help would be appreciated
did you ever find the solution to the MA and VWAP script?
 
I also tried to make a watchlist column out of it, similar to the MACD one. I modified the original code and added the signal lines to show background color. Does this look right? I used it on the futures symbols, looks okay

Code:
# 3-in-1 moving avg crossover watchlist column

declare upper;
input lookback = 5;
input short_average = 5;
input medium_average = 8;
input long_average = 13;
input average_type = {default "EMA", "SMA"};
input show_arrows = 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 0;
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 0;

def bull_lookback = highest(SignalUp, lookback);
def bear_lookback = highest(SignalDn, lookback);

plot signal = if bull_lookback then 2 else if bear_lookback then 1 else 0;
signal.AssignValueColor(if signal == 2 then Color.Dark_Green else if signal == 1 then Color.Dark_Red else Color.WHITE);
AssignBackgroundCOlor(if signal == 2 then Color.Dark_Green else if signal == 1 then Color.Dark_Red else Color.WHITE);
Can you explain better what the difference between the red and green backgrounds are representing please. And if there is a updated version of this that goes with the updated code you posted later in the thread?
 
This is a 4 in 1 EMA Indicator for TOS

I learned how to code from a failed college course and YouTube videos. i can mostly get it to do what i need it to but still have a lot to learn. So here's my code, how do i have my signal (VLine) not repeat its self. i just want it to display once at the beginning. Other than that open for the suggestions for the code its self

input price = close;
input length1 = 5;
input length2 = 13;
input length3 = 50;
input length4 = 200;
input displace = 0;
input showBreakoutSignals = no;

plot A1 = ExpAverage(price[-displace], length1);
plot A2 = ExpAverage(price[-displace], length2);
plot A3 = ExpAverage(price[-displace], length3);
plot A4 = ExpAverage(price[-displace], length4);

AddVerticalLine (if A1<A2 and a2<a3 and a3<a4
then 1 else 0, "--- Bearish Full ---", Color.White, Curve.LONG_DASH);

AddVerticalLine (if A1>A2 and a2>a3 and a3>a4
then 1 else 0, "--- Bullish Full ---", Color.Black, Curve.LONG_DASH);

AddVerticalLine (if A1>A2 and a2<a3 and a3<a4
then 1 else 0, "--- Bearish 3/4 ---", Color.White, Curve.LONG_DASH);

AddVerticalLine (if A1<A2 and a2>a3 and a3>a4
then 1 else 0, "--- Bullish 3/4 ---", Color.Black, Curve.LONG_DASH);

AddVerticalLine (if A1>A2 and a1>a3 and a2<a3 and a3<a4
then 1 else 0, "--- Bearish 1/4 ---", Color.White, Curve.LONG_DASH);

AddVerticalLine (if A1<A2 and a1<a3 and a2>a3 and a3>a4
then 1 else 0, "--- Bullish 1/4 ---", Color.Black, Curve.LONG_DASH);

AddVerticalLine (if A1<a4 and a2>a4 and a3>a4 and a3>a4
then 1 else 0, "--- Bearish Reversal ---", Color.White, Curve.LONG_DASH);

AddVerticalLine (if A1>A4 and a2<a4 and a3<a4 and a3<a4
then 1 else 0, "--- Bullish Reversal ---", Color.Black, Curve.LONG_DASH);

if you mean the conditions in the addverticallines, change one condition to be x crosses above y ( or crosses below)
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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