Moving Average Crossover Strategy

F

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.
 
T

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
 
F

Fedegrosso

New member
Thank you very much.

I tried this code and it actually work:

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
 

Top