Moving Average Crossover Strategy

Fedegrosso

New member
Hi Everyone,

I'm very new to ThinkScript and coding in general

Have any of you ever made a very basic Moving average crossover strategy?
Eg. Buy/Sell When MA9 crosses MA20. Something very basic like that

If yes would you be so kind to share the code in order for me to start learning TS getting my hand dirty trying to tweak the code?

Thank you very much

PS: Exercise caution and be safe out there in these very uncertain days.
 

tradebyday

Active member
Here is a code for a moving average crossover. To make it a "strategy" you just have to add the buy/sell order requirements. I am not familiar with the coding for that so I would suggest searching through previously posted works to find a strategy built in and just copy those lines of code over and put into the one I provided, then just edit to make sure the conditions are stated properly
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 = 34;
input slowLength = 50;
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
 

Fedegrosso

New member
Thank you very much.

I tried this code and it actually work:

Code:
def sma10 = reference simplemovingavg(length=10);
def sma30 = reference simplemovingavg(length=30);
addOrder(OrderType.BUY_AUTO, sma10 > sma30, tickColor = GetColor(6), arrowColor = GetColor(6));
addOrder(OrderType.SELL_AUTO, sma10 < sma30, tickColor = GetColor(5), arrowColor = GetColor(5));

I'll try your code today
 

LGLyon

New member
I am looking for assistance in writing the following strategy. I am using Think or Swim.

I want a buy strategy for the uptrend when the 8ema crosses above the 21 ema and the exit strategy to be the 8th light blue bar of the Squeeze Pro indicator or the 1st dark blue bar of the Squeeze Pro indicator

For this strategy I want a signal displayed on the chart as well as a sms alert

I want a buy strategy for the downtrend when the 8ema crosses below the 21 ema and the exit strategy to be the 8th red bar of the Squeeze Pro indicator or the 1st yellow bar of the Squeeze Pro indicator

For these strategies I want a signal displayed on the chart as well as a sms alert

Writing this as a strategy over a study is it will allow me to backtest with a report.
 

MerryDay

Administrative
Staff member
Staff
VIP
Lifetime
@LGLyon if you search the TOS strategies for 'cross', you will find TOS GoldenCross strategy. Change the moving average lengths and you will have your buy order. Merge your Squeeze Pro Indicator code and change the sell order to equal your definition.
Even if you are not a coder, you should have enough to get you started. Come back here w/ your code, if you run into trouble.
 

LGLyon

New member
thank you I will try that
@LGLyon if you search the TOS strategies for 'cross', you will find TOS GoldenCross strategy. Change the moving average lengths and you will have your buy order. Merge your Squeeze Pro Indicator code and change the sell order to equal your definition.
Even if you are not a coder, you should have enough to get you started. Come back here w/ your code, if you run into trouble.
 

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