Moving Average Crossovers For ThinkOrSwim

Sorry about that! Here is the link to the original code http://bit.ly/TrendFollowingRLT. Attached is a photo of what the study looks like on a chart! This study gives an entry signal when the 8 and 21 period EMAs cross. It also fills in the space between the EMAs as either green when bullish or red when bearish. It also plots a green or red dot under each candle to show momentum. As far as I know there is no study like this available in TOS currently. This study is great because it gives very clear visual indication of entry. I would love for someone to do the coding but would be willing to learn also!
NwALV3rN
 
@LRTFOREX
You will find a plethora of scripts that do what you are requesting in this thread. Plug&play with them (they won't break :))
 
This is just a simple indicator for moving average crossover but added scanner, cloud, and alerts for additional visual effect and enhancement.

For example, if 5/10 EMA crossover is your strategy, then this indicator plot an up arrow on the golden cross and down arrow on the death cross. You can also use the scanner to scan for stocks with EMA crossover and the built-in alerts to let you know as it happens.

1XlzIJA.png


thinkScript Code

Rich (BB code):
# Moving Average Crossover With Arrows, Alerts, Crossing Count and Bubble at Cross
# Mobius
# Chat Room Request 01.25.2017
# Modified a bit by BenTen

input price = close;
input fastLength = 8;
input slowLength = 21;
input averageType = AverageType.EXPONENTIAL;

plot FastMA = MovingAverage(averageType, price, fastLength);
plot SlowMA = MovingAverage(averageType, price, slowLength);
FastMA.SetDefaultColor(GetColor(1));
SlowMA.SetDefaultColor(GetColor(2));

plot ArrowUp = if FastMA crosses above SlowMA
               then low
               else double.nan;
     ArrowUP.SetPaintingStrategy(PaintingStrategy.Arrow_UP);
     ArrowUP.SetLineWeight(3);
     ArrowUP.SetDefaultColor(Color.Green);
plot ArrowDN = if FastMA crosses below SlowMA
               then high
               else double.nan;
     ArrowDN.SetPaintingStrategy(PaintingStrategy.Arrow_DOWN);
     ArrowDN.SetLineWeight(3);
     ArrowDN.SetDefaultColor(Color.Red);
Alert(ArrowUp, " ", Alert.Bar, Sound.Chimes);
Alert(ArrowDN, " ", Alert.Bar, Sound.Bell);

AddCloud(FastMA, SlowMA, Color.GREEN, Color.RED);
# End Code

Shareable Link

https://tos.mx/2ZED6i
Is there a way to add "Buy" and "Sell" text in addition to arrows? i am having trouble finding syntax on adding text in thinkscript.
 
Hi MerryDay and all, i have this code and need some help. would like to get an alert when price crosses above the 200 EMA with an arrow up and below 200EMA with down arrow.

Ideally looking to add this to a watchlist too. I understand there is sometimes a short delay, but something to keep an eye out for a trend change. Not a cross between 2 MA's but just price crossing the 200EMA. If it's possible for TOS to do even allow for green candle close above 200EMA, or red below that would be even better.

CODE:

input price = close;
input length = 200;
input displace = 0;

plot = Average(price[-displace], length);
AssignBackgroundColor(if price > Average then color.light_green else color.light_red);
 
@tradebook Why not use the standard MovAvgExponential study that comes with Thinkorswim...??? Just change the settings in the Study Settings Panel to 200... If you want to make modifications/additions to the code just copy it to a new Study and go from there... You can also reference it from a Custom Watchlist Column or create one using the code... Your example is using a Simple Moving Average by using Average()...
 
hi, i have the 200 MA on the chart. But is there a way to create the alerts with arrows. Thats the part I am not familiar with, and not too savy with coding (although learning). the code i pasted is the one i tried to create on watch list, but not working.
 
hi, i have the 200 MA on the chart. But is there a way to create the alerts with arrows. Thats the part I am not familiar with, and not too savy with coding (although learning). the code i pasted is the one i tried to create on watch list, but not working.

Did you look at the Study I recommended...??? It has the arrows already... You would use the same logic that the arrows use to create alerts using the Alert() function... Just add an Alert() for Up and Down... The quickest way to learn is to re-use existing code and modify it to your needs...
 
yes i see the one for EMA now. I realized i was looking at hull and it was not showing. Thought it was a custom code I needed to use. Thanks for the direction. Would you happen to know if I wanted to test this on hull what i would need to change. EMA and SMA has a script already in place. Figured MA's simple, Ex or Hull would all be under same umbrella if I wanted to test it out. Thanks again.

CODE:

input price = close;
input length = 8;
input displace = 0;
input showBreakoutSignals = no;

plot AvgExp = ExpAverage(price[-displace], length);
plot UpSignal = price crosses above AvgExp;
plot DownSignal = price crosses below AvgExp;

UpSignal.SetHiding(!showBreakoutSignals);
DownSignal.SetHiding(!showBreakoutSignals);

AvgExp.SetDefaultColor(GetColor(1));
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
 
Disregard, I got someone to help me with the alerts for hullma. Here it is for easy access. Thanks!

CODE:

input price = close;
input length = 8;
input displace = 0;
input showBreakoutSignals = yes;

plot HMA = MovingAverage(AverageType.HULL, price, length)[-displace];
plot UpSignal = price crosses above HMA;
plot DownSignal = price crosses below HMA;

UpSignal.SetHiding(!showBreakoutSignals);
DownSignal.SetHiding(!showBreakoutSignals);

HMA.SetDefaultColor(GetColor(1));
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
 
@tradebook I was just posting that there is also a HullMovingAvg that you could tweak... Another option now would be to add one line and edit one line and that code will work for most MA's... I have included the modified code below... Not tested but should work... It should allow for Exponential, Hull, Simple, Weighted, and Wilders averages...

Ruby:
input price = close;
input length = 8;
input displace = 0;
input showBreakoutSignals = yes;
input avgType = AverageType.HULL;

plot HMA = MovingAverage(avgType, price, length)[-displace];
plot UpSignal = price crosses above HMA;
plot DownSignal = price crosses below HMA;

UpSignal.SetHiding(!showBreakoutSignals);
DownSignal.SetHiding(!showBreakoutSignals);

HMA.SetDefaultColor(GetColor(1));
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
 
Hi everyone,
I am trying to find a code to generate an alert for a specific stock to be triggered when
price cross below SMA(20), the time frame is daily, and I want to get the alert whenever during the trading hours the price goes below 20 daily moving average,

Thanks
 
Hi everyone,
I am trying to find a code to generate an alert for a specific stock to be triggered when
price cross below SMA(20), the time frame is daily, and I want to get the alert whenever during the trading hours the price goes below 20 daily moving average,

Thanks

Are you looking for a Scan Alert or for a Study Alert on a Chart...??? The code logic is essentially close crosses below ExpAverage(close, 20)... While candles are still open Close works as Last...
 
Hi,

I needed help from all the wonderful people here. I am having a hard time creating a script where the EMA would cross above the Previous day high and the EMA cross below the Previous day low. The crossover would be signaled by an up arrow if the ema crosses above the previous day high and down arrow if the ema crosses below the previous day low. Thanks.

Code:
#moving average

input price = close;
input length = 10;
input displace = 0;
input showBreakoutSignals = no;

plot AvgExp = ExpAverage(price[-displace], length);

#Prevday High Low

input aggregationPeriod = AggregationPeriod.DAY;
input length1 = 1;
input displace1 = -1;
input showOnlyLastPeriod = yes;
plot PrevDayHigh;
plot PrevDayLow;


if showOnlyLastPeriod and !IsNaN(high(period = aggregationPeriod)[-1]) and !IsNaN(low(period = aggregationPeriod)[-1]) and !IsNaN(close(period = aggregationPeriod)[-1])
{
    PrevDayHigh = Double.NaN;
    PrevDayLow = Double.NaN;
}
else
{
    PrevDayHigh = Highest(high(period = aggregationPeriod)[-displace], length);
    PrevDayLow = Highest(low(period = aggregationPeriod)[-displace], length);
    
}

plot crossabove = ?

crossabove.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);



plot crossbelow = ?
crossbelow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
 
Hi everyone,

I am trying to setup a multi prong scan for the 30 min time frame. Looking to search for the 89 moving average crossing the 233 and the 233 moving average crossing the 377. So far I have not been able to accomplish this. Anyone have any pointers?

Currently tried this in the scan section:
1. Study MovingAvgCrossover 89 simple above 233 simple close
2. Study MovingAvgCrossover 233 simple above 377 simple close
 
@Cloves
Ruby:
Average(close, 89) crossing above Average(close, 233) and
Average(close, 233) crossing above Average(close, 377);

FYI, the possibility of two crossovers happening on the exact candle at the exact same time will return few if any results.
 
Last edited:
@Cloves
Ruby:
Average(close, 89) crossing above Average(close, 233) and
Average(close, 233) crossing above Average(close, 377);

FYI, the possibility of two crossovers happening on the exact candle at the exact same time will return few if any results.
Is there anyway to confirm the first portion and then have it execute the second check. Sort of like an if then operator?
 
Is there anyway to confirm the first portion and then have it execute the second check. Sort of like an if then operator?

That code in the previous post more or less does that... "IF" the first MA comparison is true "THEN" do the second MA comparison, other wise fail out... if the second MA comparison doesn't prove true then it also fails out... It is just a shorthand method of performing an If this is true then if that is true then true else false type of clause... Both comparisons have to be true otherwise the entire clause is false... It is merely shortened by using and... The end result is the same, true or false...
 
Thanks for the replies! looks like the thread was merged here.

I actually made a minor mistake and I am looking for this cross to happen on the 4HR chart. So I would like the scanner to return tickers in which the 89 is above the 233 sma and the 377 sma on a 4HR time frame. I know you can setup the scanner for a golden cross. That is just the 50 over the 200 ma, this requires an extra validation and time frame if that is even possible.

@MerryDay so the tos scanner only scans for these conditions in overlapping candles?

@rad14733 So it sounds like each step would have to be true in order to move to the next step. Can time frames be applied to this as well?

Here is an example on the $APPL chart.
233-377-Long-Position-Screenshot-2021-08-27-232500.png
 
I was looking for a scanner for SMA 8,21,50.
Candle drops below 21 SMA with in 9 bars and crosses above 21 SMA and crosses above 8 SMA.

looking like, drops and then moves up.
 

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
310 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