Momentum strategy using two moving averages

BigBen

New member
Hi,

I'm new (just created my account), and was wondering how I can get this indicator/strat to populate buy and sell signals.

This is a long only strategy which should generate a long entry when Price closes above the 200 EMA & generate a close signal when price closes past the 50 EMA

any help would be greatly appreciated. Thanks!

Ben

Code:
input price = close;
input length1 = 200;
input length2 = 50;
input averageType = AverageType.Exponential;
input mode = {default "Trend Following", "Reversal"};

plot Avg = MovingAverage(averageType, price, length1);
plot Avg2 = MovingAverage (averageType, price, length2);
Avg.SetDefaultColor(GetColor(0));
Avg2.SetDefaultColor (GetColor(1));

def crossAbove = price crosses above Avg;
def crossBelow = price crosses below Avg2;
def buy;
def sell;
switch (mode) {
case  "Trend Following":
    buy = crossAbove;
    sell = crossBelow;
case  "Reversal":
    buy = crossBelow;
    sell = crossAbove;
}

AddOrder(OrderType.BUY_AUTO, buy, tickcolor = GetColor(2), arrowcolor = GetColor(2), name = "MovAvgStratLE");
AddOrder(OrderType.SELL_AUTO, sell, tickcolor = GetColor(1), arrowcolor = GetColor(1), name = "MovAvgStratSE");
 

pal_yc

New member
VIP
Hi,

I'm new (just created my account), and was wondering how I can get this indicator/strat to populate buy and sell signals.

This is a long only strategy which should generate a long entry when Price closes above the 200 EMA & generate a close signal when price closes past the 50 EMA

any help would be greatly appreciated. Thanks!

Ben

Code:
input price = close;
input length1 = 200;
input length2 = 50;
input averageType = AverageType.Exponential;
input mode = {default "Trend Following", "Reversal"};

plot Avg = MovingAverage(averageType, price, length1);
plot Avg2 = MovingAverage (averageType, price, length2);
Avg.SetDefaultColor(GetColor(0));
Avg2.SetDefaultColor (GetColor(1));

def crossAbove = price crosses above Avg;
def crossBelow = price crosses below Avg2;
def buy;
def sell;
switch (mode) {
case  "Trend Following":
    buy = crossAbove;
    sell = crossBelow;
case  "Reversal":
    buy = crossBelow;
    sell = crossAbove;
}

AddOrder(OrderType.BUY_AUTO, buy, tickcolor = GetColor(2), arrowcolor = GetColor(2), name = "MovAvgStratLE");
AddOrder(OrderType.SELL_AUTO, sell, tickcolor = GetColor(1), arrowcolor = GetColor(1), name = "MovAvgStratSE");
@BigBen - In TOS, the MovAvgTwoLinesStrat strategy (under Studies/Strategies tab) can be used for this without you trying to code. You can change the MA period to any two length (50, 200 or 20, 50, etc.,) you want. Use the MovAvgTwoLinesStratLE and MovAvgTwoLinesStratSE to set up Buy to Open and Sell to Close. Cheers!
 

Similar threads

Top