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

Moderator
Staff member
VIP
@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

Top