36 trades in 1 min with MACD EA (4Hr timeframe)

 

I think I have messed up something very badly since my ea that I coded yesterday traded 36 trades GBPUSD trades in less than 1 min which would have resulted in huge losses in a real account. I've also tried it with AUDUSD where I got a pretty sweet result.

After that it didn't trade at all. The time line I used was 1 year, this made me question my coding skills big time. I checked the code and didn't find anything unusual..but still it got me wonder like big time so now I need you guys, can you spot the mistake and if so please let me know and I'll recode it.

Here is the code:

extern int MagicNumber=10001;

extern double Lots =0.1;

extern double StopLoss=20;

extern double TakeProfit=20;

extern int TrailingStop=15;

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((iMA(NULL,PERIOD_H4,15,0,MODE_EMA,PRICE_HIGH,0)<iMACD(NULL,PERIOD_H4,23,94,9,PRICE_CLOSE,MODE_SIGNAL,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((iMA(NULL,PERIOD_H4,15,0,MODE_EMA,PRICE_HIGH,0)>iMACD(NULL,PERIOD_H4,23,94,9,PRICE_CLOSE,MODE_MAIN,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((iMA(NULL,PERIOD_H4,15,0,MODE_EMA,PRICE_HIGH,0)<iMACD(NULL,PERIOD_H4,23,94,9,PRICE_CLOSE,MODE_MAIN,0))) //here is your close buy rule

              {

                   OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Red);

              }

            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((iMA(NULL,PERIOD_H4,15,0,MODE_EMA,PRICE_HIGH,0)>iMACD(NULL,PERIOD_H4,23,94,9,PRICE_CLOSE,MODE_MAIN,0))) // here is your close sell rule

                {

                   OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Red);

                }

            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);

}
 

Forum on trading, automated trading systems and testing trading strategies


Hello,

Please use the SRC button when you post code. Thank you.


This time, I edited it for you.


 
Alain Verleyen:

Thanks for the tip xD
 
Philip Ollas:

I think I have messed up something very badly since my ea that I coded yesterday traded 36 trades GBPUSD trades in less than 1 min which would have resulted in huge losses in a real account. I've also tried it with AUDUSD where I got a pretty sweet result.

After that it didn't trade at all. The time line I used was 1 year, this made me question my coding skills big time. I checked the code and didn't find anything unusual..but still it got me wonder like big time so now I need you guys, can you spot the mistake and if so please let me know and I'll recode it.


The EA was created by an EA generator and I guess that you have tried to modify it.

              if((iMA(NULL,PERIOD_H4,15,0,MODE_EMA,PRICE_HIGH,0)<iMACD(NULL,PERIOD_H4,23,94,9,PRICE_CLOSE,MODE_SIGNAL,0))) // Here is your open buy rule


              if((iMA(NULL,PERIOD_H4,15,0,MODE_EMA,PRICE_HIGH,0)<iMACD(NULL,PERIOD_H4,23,94,9,PRICE_CLOSE,MODE_MAIN,0))) //here is your close buy rule


Why are you comparing a MA's value with a MACD's value? that doesn't make any sense at all.

The rule to close a trade is likely to be true at the same time as the rule to close it so a trade may be opened and immediately closed

 
Keith Watford:

The EA was created by an EA generator and I guess that you have tried to modify it.

              if((iMA(NULL,PERIOD_H4,15,0,MODE_EMA,PRICE_HIGH,0)<iMACD(NULL,PERIOD_H4,23,94,9,PRICE_CLOSE,MODE_SIGNAL,0))) // Here is your open buy rule


              if((iMA(NULL,PERIOD_H4,15,0,MODE_EMA,PRICE_HIGH,0)<iMACD(NULL,PERIOD_H4,23,94,9,PRICE_CLOSE,MODE_MAIN,0))) //here is your close buy rule


Why are you comparing a MA's value with a MACD's value? that doesn't make any sense at all.

The rule to close a trade is likely to be true at the same time as the rule to close it so a trade may be opened and immediately closed

Hi Keith, 

Yes that's true. Did to speed things up a bit since quite honest I'm not good at coding, though that will change in the future.

I've completely overlooked that...thank you that's a very good point. Looking  at it now it really doesn't make any sense at all. My goal was to create an EA that start to buy when EMA 15 is bellow the candle and MACD is rising. And sells when EMA 15 is above the candle and MACD is falling. For further info please check the link https://www.forexstart.com/education/professional/54. 

To get that strategy right would it be recommended to  change buy and sell rule so that EMA is equal to MACD ? Or what would you recommend.

Many thanks in advance. 

 
Philip Ollas:

To get that strategy right would it be recommended to  change buy and sell rule so that EMA is equal to MACD ? Or what would you recommend.


No, you need 2 separate conditions and both need to be true, you cannot compare an MA to MACD

1st condition price compared to the MA

2nd condition MACD

 
Keith Watford: The EA was created by an EA generator and I guess that you have tried to modify it.
  • We hate EA builder
  • You couldn't be bothered to learn mql4, therefor there is no common language for us to communicate.
  • There are only two choices: learn to code it, or pay someone. We're not going to code it FOR you. We are willing to HELP you when you post your attempt (using SRC) and the nature of your problem, but we are not going to debug your hundreds lines of code.
  • EA builder makes bad code counting up while closing multiple orders.
  • EA builder makes bad code Bars is unreliable (max bars on chart) volume is unreliable (miss ticks) Always use time. New candle - MQL4 forum
  • EA builder makes bad code Not adjusting for 4/5 digit brokers, TP/SL and slippage:
    double   pip        = StringFind(_Symbol,"JPY") < 0 ? 0.01 : 0.0001;
    int      slippage   = 3 * int(pip / _Point);
  • EA builder makes bad code not adjusting for ECN brokers.
  • EA builder makes bad code not checking return codes.
  • EATree uses objects on chart to save values - not persistent storage (files or GV+Flush.) No recovery (crash/reboot.)
 
Keith Watford:

No, you need 2 separate conditions and both need to be true, you cannot compare an MA to MACD

1st condition price compared to the MA

2nd condition MACD

Great I'll get to work with it, didn't take that in account.
 
whroeder1:
  • We hate EA builder
  • You couldn't be bothered to learn mql4, therefor there is no common language for us to communicate.
  • There are only two choices: learn to code it, or pay someone. We're not going to code it FOR you. We are willing to HELP you when you post your attempt (using SRC) and the nature of your problem, but we are not going to debug your hundreds lines of code.
  • EA builder makes bad code counting up while closing multiple orders.
  • EA builder makes bad code Bars is unreliable (max bars on chart) volume is unreliable (miss ticks) Always use time. New candle - MQL4 forum
  • EA builder makes bad code Not adjusting for 4/5 digit brokers, TP/SL and slippage:
    double   pip        = StringFind(_Symbol,"JPY") < 0 ? 0.01 : 0.0001;
    int      slippage   = 3 * int(pip / _Point);
  • EA builder makes bad code not adjusting for ECN brokers.
  • EA builder makes bad code not checking return codes.
  • EATree uses objects on chart to save values - not persistent storage (files or GV+Flush.) No recovery (crash/reboot.)
Yes, I can clearly see why you dislike them it's better to do it yourself. The only reason why I tested it out was to speed up things a bit as I outlined in the previous writing. Have I asked you to debug the code ?....in my view no since I will do it myself from now on. And if I don't know the answer I'll ask.
 
Keith Watford:

The EA was created by an EA generator and I guess that you have tried to modify it.

              if((iMA(NULL,PERIOD_H4,15,0,MODE_EMA,PRICE_HIGH,0)<iMACD(NULL,PERIOD_H4,23,94,9,PRICE_CLOSE,MODE_SIGNAL,0))) // Here is your open buy rule


              if((iMA(NULL,PERIOD_H4,15,0,MODE_EMA,PRICE_HIGH,0)<iMACD(NULL,PERIOD_H4,23,94,9,PRICE_CLOSE,MODE_MAIN,0))) //here is your close buy rule


Why are you comparing a MA's value with a MACD's value? that doesn't make any sense at all.

The rule to close a trade is likely to be true at the same time as the rule to close it so a trade may be opened and immediately closed

Okay Keith here is the modification, I'm not 100% sure that I've done it correctly since it doesn't open any trades on strategy tester. Never the less re-coded the buy and sell rules according to your instructions. Hope these are okay.
double MACD (int shift) { return(iMACD(NULL,240,23,94,9,0,MODE_SIGNAL,shift));}

double MA (int shift){ return(iMA(NULL,240,15,0,MODE_EMA,PRICE_HIGH,shift));}    ///CCI change this to qqEA
//HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH//  
//+------------------------------------------------------------------+
//| Open Trade Positions                                             |
//+------------------------------------------------------------------+
void Entry()
{  
   Buy = MACD(0) < iClose(NULL,0,0) && MA(15) > (0);
   Sell = MACD(0) > iClose(NULL,0,0) && MA(15) < (0);  
 

   Buy = MACD(0) < iClose(NULL,0,0) && MA(15) > (0);
   Sell = MACD(0) > iClose(NULL,0,0) && MA(15) < (0);  

Have you any experience of using the MACD or an MA on a chart and using them to make trading decisions?

If you are trading manually, you would never compare the MACD value with a close price and you would never compare the MA to zero


   Buy = (MACD(0) > (0) && iClose(NULL,0,0) > MA(15));

   Sell = (MACD(0) < (0) > iClose(NULL,0,0) < MA(15)) ; 


would make more sense.

Also, I would avdise to only work with closed bars as signals can change many times intra-bar sometimes