Problem with 5EMA / 34EMA Xover EA

Hi!

First post here. I’m coding a 5EMA / 34EMA crossover strategy in MQL4. I have been back testing it for a week trying different things to make it profitable but I always end up with poor profit factors.

Looking at the trades on the chart I’ve came to realize they were not entered properly along with my strategy.

The strategy looks for last hour closing prices EMAs and use it to trigger the crossover.

bool isEmaCrossed()
  {
   hourlyBigEma=ND(iMA(NULL,PERIOD_H1,bigEmaPeriod,0,MODE_EMA,PRICE_CLOSE,0));
   hourlySmallEma=ND(iMA(NULL,PERIOD_H1,smallEmaPeriod,0,MODE_EMA,PRICE_CLOSE,0));
   if(isSmallAboveBigEma)
     {
      if(hourlyBigEma>hourlySmallEma)
        {
         isSmallAboveBigEma=false;
         return true;
        }
      return false;
     }
   else
     {
      if(hourlySmallEma>hourlyBigEma)
        {
         isSmallAboveBigEma=true;
         return true;
        }
      return false;
     }

  }

And main method :

void OnTick()
  {
   if(OrdersTotal()==0)
     {
      if(isBullMarket())
        {
         if(isEmaCrossed() && isSmallAboveBigEma)
           {
            //OP BUY
            double price=ND(Ask);
            double sl=ND(Ask-200*Point);
            double tp= ND(price+takeProfit*Point);
            ticketNumber=OrderSend(NULL,OP_BUY,0.01,price,5,sl,tp,NULL,0,TimeCurrent()+expirationTime;
           }
        }
      else
        {
         if(isEmaCrossed() && !isSmallAboveBigEma)
           {
            //OP SELL
            double price=ND(Bid);
            double sl = ND(Bid+200*Point);
            double tp = ND(price-takeProfit*Point);
            ticketNumber=OrderSend(NULL,OP_SELL,0.01,price,5,sl,tp,NULL,0,TimeCurrent()+expirationTime);
           }
        }
     }
}

Unfortunately I can’t put pictures in the post but imagine the 5EMA crossing 34EMA from the top and then on the next candle (hourly) it triggers a buy order. Obviously it sees that 5EMA is still above 34EMA but why is it triggering a crossover then?

Different timings?

I expect someone to tell me an evidence and then to feel completely dumb. :stuck_out_tongue:

I hope i’m clear. Big thanks for the help.

I use this code , try it :slight_smile:

extern string OM = “------------------Order Management”;
extern double Lots=0.01;
extern double TakeProfit = 30;
extern double StopLose = 50;
input int MagicNo=2017;
extern string MA1 = “------------------Fast Moving Average Sitting”;
extern int PERIOD1 = 9;
extern ENUM_APPLIED_PRICE Apllied_Price1 = 0;
extern ENUM_MA_METHOD Apllied_Method1 = 0;
extern int MA1_SHIFT = 0;
extern string MA2 = “------------------Slow Moving Average Sitting”;
extern int PERIOD2 = 18;
extern ENUM_APPLIED_PRICE Apllied_Price2 = 0;
extern ENUM_MA_METHOD Apllied_Method2 = 0;
extern int MA2_SHIFT = 0;

double MinRisk=0.01;
double STEP,PF,RX,TP,SL;
double MaxRisk=1000;
double NewLots;
double point;
int digits;
datetime BarTime;

//±-----------------------------------------------------------------+
//| Expert initialization function |
//±-----------------------------------------------------------------+
int OnInit()
{
//—
if(Digits<4) { point=0.01;digits=2;}else {point=0.0001;digits=4;}
//—
return(INIT_SUCCEEDED);
}
//±-----------------------------------------------------------------+
//| Expert deinitialization function |
//±-----------------------------------------------------------------+
void OnDeinit(const int reason)
{
//—

}
//±-----------------------------------------------------------------+
//| Expert tick function |
//±-----------------------------------------------------------------+
void OnTick()
{

double MA1_1 = iMA(Symbol(),0,PERIOD1,MA1_SHIFT,Apllied_Method1,Apllied_Price1,1);
double MA1_2 = iMA(Symbol(),0,PERIOD1,MA1_SHIFT,Apllied_Method1,Apllied_Price1,2);

double MA2_1 = iMA(Symbol(),0,PERIOD2,MA2_SHIFT,Apllied_Method2,Apllied_Price2,1);
double MA2_2 = iMA(Symbol(),0,PERIOD2,MA2_SHIFT,Apllied_Method2,Apllied_Price2,2);

if (MA2_2 > MA1_2 && MA2_1 < MA1_1 && Orderscnt()==0)
{

if (TakeProfit == 0) TP =0;
if (TakeProfit!= 0) TP = Ask+TakeProfitpoint;
if (StopLose ==0) SL =0;
if (StopLose !=0) SL = Ask-StopLose
point;
int B1 = OrderSend(Symbol(),OP_BUY,Lots,Ask,3,SL,TP,“w”,MagicNo,0,Blue);
}

if (MA2_2 < MA1_2 && MA2_1 > MA1_1 && Orderscnt()==0)
{

if (TakeProfit == 0) TP =0;
if (TakeProfit!= 0) TP = Bid-TakeProfitpoint;
if (StopLose ==0) SL =0;
if (StopLose !=0) SL = Bid+StopLose
point;
int B1 = OrderSend(Symbol(),OP_SELL,Lots,Bid,3,SL,TP,“w”,MagicNo,0,Red);
}

}
//±-----------------------------------------------------------------+
int Orderscnt()// total order
{
int cnt=0;
for(int i=0;i<OrdersTotal();i++)//
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol()&&MagicNo==OrderMagicNumber())
{
cnt++;
}
}
}
return(cnt);
}

1 Like

Nice try bro, unfortunately it offers no explanation to the OP question.

@yan.lamothe I believe the problems lies within the fact your ea is running the code through on every tick. Therefor if during the hour the EMA’s cross it triggers your bools to true.

You only need run your code on the start of a new bar, try this

//— Static Variables
static datetime current_bar;

//—Function variables
datetime this_bar=iTime(NULL,0,0);

//—Algorithm Logic------------------------------------------------+

//—Check for new bar
if(current_bar!=this_bar)
{
current_bar=this_bar;

  RunYourFunctionsHere

}

1 Like

Hey _bob!

Thanks for the help. iTime looks like a good method to use to avoid hard coding things. At first I was using Hour() method to run my OnTick() every hour.

int hour;

OnInit()
{
hour = Hour();
}

OnTick()
{
if(Hour() != hour)
{
hour = Hour();
//Code here
}
}

I think this code is similar to yours but is more precise on the time-frame. Let’s say I would like to use H4 later… I would have to rewrite my code, but with your method I can just change a param.

That being said, I tried your code and I still have some problems with how the trades are entered. I wish I could send a picture or a file.

Maybe I could use a lower-time frame just to check which EMA is on top?

I’m a young programmer learning the world of Forex so tell me if i’m completely in the field. :stuck_out_tongue:

I’ll try to send you my whole code in some ways.

Thanks

Hey WAT!

Thanks for the help. Looks like a template of yours. Is it how your work? Having bunch of templates to variate strategies? It’s not the main subject of my topic but could you explain your methodology when it comes to develop a new EA based on a strategy. I’ve been coding for a month and i’m curious to know how long and how much work it can take to achieve a profitable EA.

Thanks

Ok so I can upload things now!

34EMA Xover.zip (2.0 KB)