EA that trades only on MA Crossover

 
Hi everyone! I am new to making EA but not new to trading. I currently have an EA that buys when the price closes over my MA, with a 50pip TP and a 30pip SL, and vice versa. But my problem is that it opens positions EVERY TIME a candle closes in those conditions. Which can be very inconvenient. If anyone can help me out on how I would code for the position ONLY to open when it crosses the MA, rather than EVERY TIME it closes under or over the MA. Thank you!
 
edwardfabian91 opens positions EVERY TIME a candle closes in those conditions.

You are testing fast vs. slow. Test for a cross.

double aPrev = ..., aCurr = ...,
       bPrev = ..., bCurr = ...;
bool   wasUp = aPrev > bPrev,
        isUp = aCurr > bCurr,
       isCross = isUp != wasUp;
 
whroeder1:

You are testing fast vs. slow. Test for a cross.

This is my code:


extern int MagicNumber=10001;
extern double Lots =1;
extern double StopLoss=25;
extern double TakeProfit=35;
extern int TrailingStop=50;
extern int Slippage=3;
//+------------------------------------------------------------------+
//    expert start function
//+------------------------------------------------------------------+
int start()
{
  double MyPoint=Point;
  if(Digits==3 || Digits==5) MyPoint=Point*10;
  
  double TheStopLoss=0;
  double TheTakeProfit=0;
  if( TotalOrdersCount()==0 ) 
  {
     int result=0;
     if((Close[1]>iMA(NULL,PERIOD_H1,10,1,MODE_EMA,PRICE_CLOSE,0))) // Here is your open buy rule
     {
        result=OrderSend(Symbol(),OP_BUY,AdvancedMM(),Ask,Slippage,0,0,"EA Generator www.ForexEAdvisor.com",MagicNumber,0,Blue);
        if(result>0)
        {
         TheStopLoss=0;
         TheTakeProfit=0;
         if(TakeProfit>0) TheTakeProfit=Ask+TakeProfit*MyPoint;
         if(StopLoss>0) TheStopLoss=Ask-StopLoss*MyPoint;
         OrderSelect(result,SELECT_BY_TICKET);
         OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green);
        }
        return(0);
     }
     if((Close[1]<iMA(NULL,PERIOD_H1,10,1,MODE_EMA,PRICE_CLOSE,0))) // Here is your open Sell rule
     {
        result=OrderSend(Symbol(),OP_SELL,AdvancedMM(),Bid,Slippage,0,0,"EA Generator www.ForexEAdvisor.com",MagicNumber,0,Red);
        if(result>0)
        {
         TheStopLoss=0;
         TheTakeProfit=0;
         if(TakeProfit>0) TheTakeProfit=Bid-TakeProfit*MyPoint;
         if(StopLoss>0) TheStopLoss=Bid+StopLoss*MyPoint;
         OrderSelect(result,SELECT_BY_TICKET);
         OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green);
        }
        return(0);
     }
  }
  
  for(int cnt=0;cnt<OrdersTotal();cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL &&   
         OrderSymbol()==Symbol() &&
         OrderMagicNumber()==MagicNumber 
         )  
        {
         if(OrderType()==OP_BUY)  
           {
            if(TrailingStop>0)  
              {                 
               if(Bid-OrderOpenPrice()>MyPoint*TrailingStop)
                 {
                  if(OrderStopLoss()<Bid-MyPoint*TrailingStop)
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailingStop*MyPoint,OrderTakeProfit(),0,Green);
                     return(0);
                    }
                 }
              }
           }
         else 
           {
            if(TrailingStop>0)  
              {                 
               if((OrderOpenPrice()-Ask)>(MyPoint*TrailingStop))
                 {
                  if((OrderStopLoss()>(Ask+MyPoint*TrailingStop)) || (OrderStopLoss()==0))
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Ask+MyPoint*TrailingStop,OrderTakeProfit(),0,Red);
                     return(0);
                    }
                 }
              }
           }
        }
     }
   return(0);
}

int TotalOrdersCount()
{
  int result=0;
  for(int i=0;i<OrdersTotal();i++)
  {
     OrderSelect(i,SELECT_BY_POS ,MODE_TRADES);
     if (OrderMagicNumber()==MagicNumber) result++;

   }
  return (result);
}
double AdvancedMM()
{
 int i;
 double AdvancedMMLots = 0;
 bool profit1=false;
 int SystemHistoryOrders=0;
  for( i=0;i<OrdersHistoryTotal();i++)
  {  OrderSelect(i,SELECT_BY_POS ,MODE_HISTORY);
     if (OrderMagicNumber()==MagicNumber) SystemHistoryOrders++;
  }
 bool profit2=false;
 int LO=0;
 if(SystemHistoryOrders<2) return(Lots);
 for( i=OrdersHistoryTotal()-1;i>=0;i--)
  {
     if(OrderSelect(i,SELECT_BY_POS ,MODE_HISTORY))
     if (OrderMagicNumber()==MagicNumber) 
     {
        if(OrderProfit()>=0 && profit1) return(Lots);
        if( LO==0)
        {  if(OrderProfit()>=0) profit1=true;
           if(OrderProfit()<0)  return(OrderLots());
           LO=1;
        }
        if(OrderProfit()>=0 && profit2) return(AdvancedMMLots);
        if(OrderProfit()>=0) profit2=true;
        if(OrderProfit()<0 ) 
        {   profit1=false;
            profit2=false;
            AdvancedMMLots+=OrderLots();
        }
     }
  }
 return(AdvancedMMLots);
}
 
  1. When you post code please use the CODE button (Alt-S)! (For large amounts of code, attach it.) Please edit your (original) post.
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

  2.  if((Close[1]>iMA(NULL,PERIOD_H1,10,1,MODE_EMA,PRICE_CLOSE,0))) 
    You are testing a level. Test for a cross.
Reason: