MACD EA

 

I found somewhere this MACD EA, it looks pretty good in backtests but i have got one problem.

EA opens trade when MACD crosses 0 level then 

in point 1 it hits TP and it opens new trade (same in 2 ). How to code that  it opens new trade only if MACD crosses 0 line and then wait for next cross  (to not open trades in points

 1 and 2 )??

 

# i hope u understand me ;] sorry for my english 

#I gonna share my settings as soon as it's possible when i solve this problem 

  

 

Files:
 

As far as I can tell, you test for a cross of MACD and set SellValue / BuyValue to 1 appropriately. These values are part of your order trigger but they only get changed at the next cross. What I would do is change the appropriate variable back to 0 when you send the order (as you do with OpenSell and OpenBuy). That should prevent it opening again until the macd cross testing logic resets the condition to true again.

hth

V

PS. yes, settings would be nice to know

V

 
Thanks for interest in my topic. I tried to do as you said but it doesn't work or i'm doing something wrong (possible). Tomorrow I will try again.
 



#define OrderStr "MACD EA"

extern int    FastEMA = 12;
extern int    SlowEMA = 26;
extern int    MACD_SMA = 9;
extern double  Lots = 0.1;
extern int               mm = 0;
extern double         Risk = 10;
extern int              StopLoss = 0;
extern int              TakeProfit = 0;
bool    TradeAtCloseBar = false;
extern bool     Reverse = false;
extern int              TrailingStop = 0;
extern int              TrailingStep = 1;      //Trailing step
extern int      BreakEven = 0;
extern int              MagicNumber=0;
//For alerts:
extern int     Repeat=3;
extern int     Periods=5;
extern bool    UseAlert=false;
extern bool    SendEmail=false;
extern string
                        TradeLog="MACD EA";


extern int  Slippage = 3;


int                Crepeat=0;
int            AlertTime=0;
double         AheadTradeSec = 0;
double         AheadExitSec = 0;
int                             TradeBar = 0;
double         MaxTradeTime = 300;

extern string  
         Indicator_Setting = "---------- Indicator Setting";

int
                                        NumberOfTries           = 1, //Number of tries to set, close orders;
                                        RetryTime                       = 1; 



double  
                        Ilo                                     =       0;

int DotLoc=7;
static int TradeLast=0;

string sound="alert.wav";

double sig_cur=0, sig_prev=0;

int Spread=0;
string filename="";

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//---- 


Crepeat=Repeat;    
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//---- 
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
if (TradeAtCloseBar) TradeBar=1;
else TradeBar=0;

filename=Symbol() + TradeLog + "-" + Month() + "-" + Day() + ".log";


Spread=MarketInfo(Symbol(),MODE_SPREAD);


//---- 
int   i=0;

double   BuyValue=0, SellValue=0;
BuyValue=0; SellValue=0;


if (CntOrd(OP_BUY,MagicNumber)>0) TradeLast=1;
if (CntOrd(OP_SELL,MagicNumber)>0) TradeLast=-1;



bool Conflict=false;

int limit=100;

double Macd_1, Macd_2, Macd_3;
        
for (int shift=1; shift<=limit; shift++) {

Macd_1 = iMACD(NULL,0,FastEMA,SlowEMA,MACD_SMA,PRICE_CLOSE,MODE_MAIN,shift);
Macd_2 = iMACD(NULL,0,FastEMA,SlowEMA,MACD_SMA,PRICE_CLOSE,MODE_MAIN,shift+1);
Macd_3 = iMACD(NULL,0,FastEMA,SlowEMA,MACD_SMA,PRICE_CLOSE,MODE_MAIN,shift+2);

        if (Macd_1>0 && Macd_2<0 && Macd_3<0)
      { 
                if (Reverse) SellValue=1; else BuyValue=1;
                break;
        }
        if (Macd_1<0 && Macd_2>0 && Macd_3>0)
      { 
                if (Reverse) BuyValue=1; else SellValue=1;
                break;
        }
}



int  cnt=0,OpenPos=0,OpenSell=0,OpenBuy=0,CloseSell=0,CloseBuy=0;






double  mode=0,Stop=0,NewBarTime=0;

//Here we found if new bar has just opened
static int prevtime=0;  
int NewBar=0,FirstRun=1;

if (FirstRun==1) {
FirstRun=0;
prevtime=Time[0];
}
if ((prevtime == Time[0]) &&  (CurTime()-prevtime)>MaxTradeTime) {
NewBar=0;
}
else {
prevtime = Time[0];
NewBar=1;
}


int   AllowTrade=0,AllowExit=0;
//Trade before bar current bar closed
if (CurTime()>= Time[0]+Period()*60-AheadTradeSec) AllowTrade=1; else AllowTrade=0;
if (CurTime()>= Time[0]+Period()*60-AheadExitSec) AllowExit=1; else AllowExit=0;
if (AheadTradeSec==0) AllowTrade=1;
if (AheadExitSec==0) AllowExit=1;


Ilo=Lots;
if (mm<0) 
  {
        Ilo=MathCeil(AccountEquity()*Risk/10000)/10-0.1;
        if (Ilo<0.1) Ilo=0.1;           
  }
if (mm>0) 
  {
        Ilo=MathCeil(AccountEquity()*Risk/10000)/10-1;
        if (Ilo>1) Ilo=MathCeil(Ilo);
    if (Ilo<1) Ilo=1;           
  }
if (Ilo>100) Ilo=100;

OpenPos=0;
for(cnt=0; cnt<OrdersTotal(); cnt++) {
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if ((OrderType()==OP_SELL || OrderType()==OP_BUY) && OrderSymbol()==Symbol() && ((OrderMagicNumber () == MagicNumber) || MagicNumber==0)) OpenPos=OpenPos+1;
}

if (OpenPos>=1) {
                OpenSell=0;     OpenBuy=0;
}



OpenBuy=0; OpenSell=0;


//Conditions to open the position
//
if (SellValue>0) {
        OpenSell=1;
        OpenBuy=0;
}

if  (BuyValue>0) {
        OpenBuy=1;
        OpenSell=0;

}

//Print("OpenSell=",OpenSell," OpenBuy=",OpenBuy);


//Conditions to close the positions
if (SellValue>0) {
        CloseBuy=1;
}


if (BuyValue>0) {
        CloseSell=1;
}

subPrintDetails();


for(cnt=0; cnt<OrdersTotal(); cnt++) {
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

        if (OrderType()==OP_BUY && OrderSymbol()==Symbol() && ((OrderMagicNumber () == MagicNumber) || MagicNumber==0)) {
                if (CloseBuy==1 && AllowExit==1) {
                                if (NewBar==1 && TradeBar>0)
                                {
                                   SetText(Time[0],High[0]+1*DotLoc*Point,("CloseBuy"+DoubleToStr(Time[0],0)),CharToStr(251),Magenta);
                                   PlaySound("alert.wav");
                                   OrdClose(OrderTicket(),OrderLots(),Bid,Slippage,Red);  
                                        Alerts(0, 0, CloseBuy, CloseSell,Bid,0,0,OrderTicket());
                                        return(0);      
                                }
                                if (TradeBar==0) 
                                {  
                                   SetText(Time[0],High[0]+1*DotLoc*Point,("CloseBuy"+DoubleToStr(Time[0],0)),CharToStr(251),Magenta);
                                   PlaySound("alert.wav");
                                   OrdClose(OrderTicket(),OrderLots(),Bid,Slippage,Red);
                                        Alerts(0, 0, CloseBuy, CloseSell,Bid,0,0,OrderTicket());
                                        return(0);      
                                }
                        
                }       
        }

        if (OrderType()==OP_SELL && OrderSymbol()==Symbol() && ((OrderMagicNumber () == MagicNumber) || MagicNumber==0)) {
                if (CloseSell==1 && AllowExit==1) {
                        if (NewBar==1 && TradeBar>0)
                        {
                                   SetText(Time[0],High[0]-0.3*DotLoc*Point,("CloseSell"+DoubleToStr(Time[0],0)),CharToStr(251),Magenta);
                                   PlaySound("alert.wav");
                                   OrdClose(OrderTicket(),OrderLots(),Ask,Slippage,Red);
                                        Alerts(0, 0, CloseBuy, CloseSell,Ask,0,0,OrderTicket());
                                        return(0);      
                        }
                        if (TradeBar==0)
                        {
               SetText(Time[0],High[0]-0.3*DotLoc*Point,("CloseSell"+DoubleToStr(Time[0],0)),CharToStr(251),Magenta);                              
                                   PlaySound("alert.wav");
                                   OrdClose(OrderTicket(),OrderLots(),Ask,Slippage,Red);
                                        Alerts(0, 0, CloseBuy, CloseSell,Ask,0,0,OrderTicket());
                                        return(0);      
                        }


                }
        }

}

double MyStopLoss=0, MyTakeProfit=0;
int ticket=0;



//Should we open a position?
if (OpenPos==0) {
        if (OpenSell==1 && AllowTrade==1) {
                if (NewBar==1 && TradeBar>0) 
                {
      SetText(Time[0],High[0]+1*DotLoc*Point,("Sell"+DoubleToStr(Time[0],0)),CharToStr(234),Red);
      if (TakeProfit==0) MyTakeProfit=0; else MyTakeProfit=Bid-TakeProfit*Point;
                if (StopLoss==0) MyStopLoss=0; else MyStopLoss=Bid+StopLoss*Point;              
      PlaySound("alert.wav");
      ticket=OrdSend(Symbol(),OP_SELL,Ilo,Bid,Slippage,MyStopLoss,MyTakeProfit,OrderStr,MagicNumber,0,Red);
                Alerts(OpenBuy, OpenSell, 0, 0,Bid,MyStopLoss,MyTakeProfit,ticket);
                OpenSell=0;
                return(0);
                }
                if (TradeBar==0)
                {
                SetText(Time[0],High[0]+1*DotLoc*Point,("Sell"+DoubleToStr(Time[0],0)),CharToStr(234),Red);             
      if (TakeProfit==0) MyTakeProfit=0; else MyTakeProfit=Bid-TakeProfit*Point;
                if (StopLoss==0) MyStopLoss=0; else MyStopLoss=Bid+StopLoss*Point;              
      PlaySound("alert.wav");
      ticket=OrdSend(Symbol(),OP_SELL,Ilo,Bid,Slippage,MyStopLoss,MyTakeProfit,OrderStr,MagicNumber,0,Red);             
                Alerts(OpenBuy, OpenSell, 0, 0,Bid,MyStopLoss,MyTakeProfit,ticket);
                OpenSell=0;
                return(0);              
                }
        }       
        if (OpenBuy==1 && AllowTrade==1) {
                if (NewBar==1 && TradeBar>0)
                {
      SetText(Time[0],Low[0]-0.3*DotLoc*Point,("Buy"+DoubleToStr(Time[0],0)),CharToStr(233),Lime);
      if (TakeProfit==0) MyTakeProfit=0; else MyTakeProfit=Ask+TakeProfit*Point;
      if (StopLoss==0) MyStopLoss=0; else MyStopLoss=Ask-StopLoss*Point;                      
      PlaySound("alert.wav");
      ticket=OrdSend(Symbol(),OP_BUY,Ilo,Ask,Slippage,MyStopLoss,MyTakeProfit,OrderStr,MagicNumber,0,Lime);
                Alerts(OpenBuy, OpenSell, 0, 0,Ask,MyStopLoss,MyTakeProfit,ticket);
                OpenBuy=0;
                return(0);
                }
                if (TradeBar==0)
                {
      SetText(Time[0],Low[0]-0.3*DotLoc*Point,("Buy"+DoubleToStr(Time[0],0)),CharToStr(233),Lime);
      if (TakeProfit==0) MyTakeProfit=0; else MyTakeProfit=Ask+TakeProfit*Point;
      if (StopLoss==0) MyStopLoss=0; else MyStopLoss=Ask-StopLoss*Point;                      
      PlaySound("alert.wav");
      ticket=OrdSend(Symbol(),OP_BUY,Ilo,Ask,Slippage,MyStopLoss,MyTakeProfit,OrderStr,MagicNumber,0,Lime);
                Alerts(OpenBuy, OpenSell, 0, 0,Ask,MyStopLoss,MyTakeProfit,ticket);
                OpenBuy=0;
                return(0);
                }
                
        }

}



for (i=0; i<OrdersTotal(); i++) {
   if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
      if (OrderSymbol()==Symbol() && ((OrderMagicNumber () == MagicNumber) || MagicNumber==0)) {
         TrailingPositions();
      }
   }
}


Alerts(0, 0, 0, 0, 0, 0, 0, 0);


// the end
if anybody has any idea pls help :)
 

It's the same idea I'm afraid. This whole code is driven by the MACD Cross setting SellValue and BuyValue. While either of those are true it will try to trade (save for the new bar and not on market filter). With the logic, if you only want it to trade once per MACD signal, then you need to change that signal back to 0 when you take the first trade. The best place to do this is immediately after succesfully sending your order.

Hopefully, you are the author of the code and therefore will have no problem identifying the appropriate point to slot in the variable switch. If not, then I think it worth while trying to read the code and understand what it is doing. As far as I can tell, there are other bugs in this code that will need working out; in fact, does it even compile?

Hope that helps

V

 

I'm not author of this EA but I understand how it works. It compiles and even works but yesterday I saw few bugs like you said.  I need to put in it more work than I thought. But I already know what to do.

 Thanks for help :)

I will write  if it will be profitable.

Reason: