Create backtesting strategy based on EMA CrossOver?

C

chaba03

New member
VIP
Hi,
Below is EMA Corss over indicator. A line and arrow is displayed every time a cross over occures.
Can someone create a strategy and autotrade in such a way -
1. When the strategy starts 1 contract is taken as per the Crossover.
2. Every time the cross over Occures, two contracts are taken in opposite direction (that is also equal to - closing the old contract and opening the new contract)
3. Theoretically, the only loss will be the commission if there is no trend. If there is a trend this its always a winner.
This works in any time frame and any thick value.

Code:
#  Plots the 8 and 34 EMA 
#  Displays WHITE Candle with Directional Arrow           
#  Displays a label for TREND DIRECTION (8 VS 34)
#  Displays a label for PRICE VS 8MA      

# +---------------------------------------------------+
# |                   PLOT EMA'S                       |
# +---------------------------------------------------+

plot MA8 = ExpAverage(close, 8);
MA8.SetDefaultColor(Color.BLUE);
MA8.SetLineWeight(2);

plot MA34 = ExpAverage(close, 34);
MA34.SetDefaultColor(Color.YELLOW);
MA34.SetLineWeight(2);

plot MA8CrossUp = if MA8 crosses above MA34 then low else 0;
MA8CrossUp.SetDefaultColor(Color.WHITE);
MA8CrossUp.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
MA8CrossUp.SetLineWeight(2);
AddVerticalLine(MA8CrossUp, "8above34", Color.Green, Curve.FIRM);

plot MA8CrossDown = if MA8 crosses below MA34 then high else 0;
MA8CrossDown.SetDefaultColor(Color.WHITE);
MA8CrossDown.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
MA8CrossDown.SetLineWeight(2);
AddVerticalLine(MA8CrossDown, "8below34", Color.RED, Curve.FIRM);
AssignPriceColor(if MA8CrossUp or MA8CrossDown then Color.WHITE else Color.CURRENT);

Alert(MA8CrossUp, "8above34", Alert.BAR, Sound.Ding);
Alert(MA8CrossDown, "8below34", Alert.BAR, Sound.Ding);

# +---------------------------------------------------+
#  ADD LABEL FOR TRADE DIRECTION
# +---------------------------------------------------+

def BULLDIRECTION = if MA8 > MA34 then 1 else 0;
def BEARDIRECTION = if MA8 < MA34 then 1 else 0;

AddLabel(BULLDIRECTION, "8 ABOVE 34", Color.DARK_GREEN);
AddLabel(BEARDIRECTION, "8 BELOW 34", Color.DARK_RED);

# +---------------------------------------------------+
#      ADD LABEL FOR PRICE VS MA8 
# +---------------------------------------------------+

def BULLPRICE = if close > MA8 then 1 else 0;
def BEARPRICE = if close < MA8 then 1 else 0;

AddLabel(BULLPRICE, "PRICE: ABOVE MA8", Color.DARK_GREEN);
AddLabel(BEARPRICE, "PRICE: BELOW MA8", Color.DARK_RED);
 
Last edited by a moderator:
A

adii800

Member
There is a strat like this already on TOS called GoldenCrossBreakouts but I had to modify it a bit.
Here it is:
Code:
# TD Ameritrade IP Company, Inc. (c) 2017-2020
#
# edited by adii800

input fastLength = 8;
input slowLength = 34;
input averageType = AverageType.EXPONENTIAL;

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

AddOrder(OrderType.BUY_AUTO, FastMA crosses above SlowMA, tickColor = GetColor(1), arrowColor = GetColor(1), name = "EMACossover", tradeSize = 1);
AddOrder(OrderType.SELL_AUTO, FastMA crosses below SlowMA, tickColor = GetColor(2), arrowColor = GetColor(2), name = "EMACrossover", tradeSize = 1);

I highly recommend using some type of trend filter such as MACD or Aroon Indicator or something along those lines to avoid some losses.
 
Last edited:

Similar threads

Top