Multi time frame MACD trading when condition is false

 
Hi, 

I'm experimenting with multi time frame indicators. 
I want to trade when M5 and M1 agree

IE these are both MACD M1 and M5:  
if(faster_1 > slower_1 && faster_2 > slower_2)

For some reason the M1 seems to just trade regardless of M5 agreement

See code

//+------------------------------------------------------------------+
//|                                                 1min5minmacd.mq4 |
//|                        Copyright 2020, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property strict
//---- input parameters
extern double    TakeProfit=20.0;
extern double    Lots=0.1;
extern double    StopLoss=10.0;
//++++ These are adjusted for 5 digit brokers.


int     pips2points;    // slippage  3 pips    3=points    30=points
double  pips2dbl;       // Stoploss 15 pips    0.0015      0.00150
//int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)

    // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl


//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
    if (Digits == 5 || Digits == 3)
   {    // Adjust for five (5) digit brokers.
      pips2dbl    = Point*10; pips2points = 10;   //Digits = 1;
   } 
   else 
    {    
      pips2dbl    = Point;    pips2points =  1;   //Digits = 0; 
    }
    // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
//---- 

//----
   return(0);
        
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//--- 
   int ticket,i,total; 
     
   double   faster_1 = iMACD("EURUSD",PERIOD_M1,12,26,9,PRICE_CLOSE,MODE_MAIN,1), //MODE_MAIN
            slower_1 = iMACD("EURUSD",PERIOD_M1,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1), //MODE_SIGNAL
            faster_2 = iMACD("EURUSD",PERIOD_M5,12,26,9,PRICE_CLOSE,MODE_MAIN,1), //MODE_MAIN
            slower_2 = iMACD("EURUSD",PERIOD_M5,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1), //MODE_SIGNAL
            faster_3 = iMACD("EURUSD",PERIOD_H4,12,26,9,PRICE_CLOSE,MODE_MAIN,1), //MODE_MAIN
            slower_3 = iMACD("EURUSD",PERIOD_H4,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1); //MODE_SIGNAL

   total  = OrdersTotal(); 
   
   if(total < 1)
      {
      if(faster_1 > slower_1 && faster_2 > slower_2)
         {
         ticket = OrderSend(Symbol(),OP_BUY,Lots,Ask,3*pips2points,Bid-StopLoss*pips2dbl,Bid+TakeProfit*pips2dbl,"My EA",12345,0,Green);
         if(ticket > 0)
            {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))Print("BUY order opened : ",OrderOpenPrice());
            }
      else Print("Error opening BUY order : ",GetLastError());
      return;
      }
         
      if(faster_1 < slower_1 && faster_2 > slower_2)
      {
      ticket = OrderSend(Symbol(),OP_SELL,Lots,Bid,3*pips2points,Ask+StopLoss*pips2dbl,Ask-TakeProfit*pips2dbl,"My EA",12345,0,Red);  
      if(ticket > 0)
         {
         if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))Print("SELL order opened : ",OrderOpenPrice());
         }
      else Print("Error opening SELL order : ",GetLastError());
      return;        
      }
     } 
          

for (i = total - 1; i >= 0; i--)
      { 
      if(!OrderSelect(i, SELECT_BY_POS, MODE_TRADES))break;
      
      if( OrderType() == OP_BUY && Symbol() == OrderSymbol())
      {
         if(faster_1 < slower_1)
            {
            if (!OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(),3, White))
               {
                Print("Order", OrderTicket()," failed to close Error ",GetLastError());
                return;
               }
            }
      }         
       
      if(OrderType() == OP_SELL && Symbol() == OrderSymbol())
      {   
         if(faster_1 > slower_1)
         {
            if(!OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(),3, White))
            {
               Print("Order", OrderTicket()," failed to close Error ", GetLastError());
               return;
            }   
         }    
      }
        
    }              
   return;
   }    
    
//+------------------------------------------------------------------+

Thanks
Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • www.mql5.com
Dozens of new automated trading applications appear in the MQL5 Market every day. Choose the right app among 10,000 products and forget about unnecessary routine operations of manual trading. Sell your algorithmic trading programs through the largest store of trading applications! One Click Close The script allows users to easily close...
 
Asked and answered #1.2 and #5
 
William Roeder:
Asked and answered #1.2 and #5

Ok, I'll read that response, thanks