Why did EA not close ? `

 

I was running this EA on demo for testing.

See the image showing a Buy signal and when the 1M and 5M MACD crosses down it was suppose to close trade but it didn't.
I don't see why but the code seems ok to me. 

Thanks

trade didn't close


//+------------------------------------------------------------------+
//|                                                 1min5minmacd.mq4 |
//|                        Copyright 2020, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property strict
//---- input parameters
extern double    TakeProfit=20.0;
extern double    Lots=1.0;
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()
  {
//---////This EA does not produce errors that I can tell GetLastError() at any point in the code seems to produce "0"
//However, the EA in strategy tester seems to trade ONLY the MACD that is on that particular chart and does not care about the other time.
//IN other words MACD 1M trades regardless of MACD 5M or MACD4 it just keeps trading up or down doesn't matter what any other indictor or timeframe comparison is. 
   int ticket,i,total; 
     
   double   faster_1 = iMACD(Symbol(),PERIOD_M5,12,26,9,PRICE_CLOSE,MODE_MAIN,1), //MODE_MAIN
            slower_1 = iMACD(Symbol(),PERIOD_M5,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1), //MODE_SIGNAL
            faster_2 = iMACD(Symbol(),PERIOD_M15,12,26,9,PRICE_CLOSE,MODE_MAIN,1), //MODE_MAIN
            slower_2 = iMACD(Symbol(),PERIOD_M15,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1), //MODE_SIGNAL
            faster_3 = iMACD(Symbol(),PERIOD_H1,12,26,9,PRICE_CLOSE,MODE_MAIN,1), //MODE_MAIN
            slower_3 = iMACD(Symbol(),PERIOD_H1,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1), //MODE_SIGNAL
            faster_4 = iMACD(Symbol(),PERIOD_H4,12,26,9,PRICE_CLOSE,MODE_MAIN,1), //MODE_MAIN
            slower_4 = iMACD(Symbol(),PERIOD_H4,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1); //MODE_SIGNAL
   
   int tradecount=0;
   int d=TimeDay(TimeCurrent());         
   int h=TimeHour(TimeCurrent());

Comment(StringFormat("Show prices\nfaster_1 = %G\nslower_1 = %G\nfaster_2 = %G\nslower_2 = %G\nfaster_3 = %G\nslower_3 = %d",faster_1,slower_1,faster_2,slower_2,faster_3,slower_2));



   total  = OrdersTotal();
   
   
   if(total < 1)
      {
      if(faster_1 > slower_1 && faster_2 > slower_2 && faster_3 > slower_3 && h >= 15 && h < 17)
         {
         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 && faster_3 < slower_3 && h >= 15 && h < 17)
      {
      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 && faster_2 < slower_2)
            {
            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 && faster_2 > slower_2)
         {
            if(!OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(),3, White))
            {
               Print("Order", OrderTicket()," failed to close Error ", GetLastError());
               return;
            }   
         }    
      }
        
    }              
   return;
   }    
    
//+------------------------------------------------------------------+

Thanks

 
Agent86: 1M and 5M MACD crosses down it was suppose to close trade but it didn't.

The histogram is up (above zero) no cross has occurred.

 
William Roeder:

The histogram is up (above zero) no cross has occurred.

Did I code this wrong or something ?  

I was intending for the faster line"historgram" to be above the slower line"red line" for up trades, and faster line "histogram" below the slower line "red line" for selling or closing signal but nothing related to the 0 line
Is that not what I coded ? Or is this only the 0 line trading ? 

From the MQL4 reference manual in the editor I'm reading this

Note

In some systems it is called MACD Histogram and plotted as two lines. In MetaTrader 4 client terminal MACD is plotted as histogram.

Example:



  if(iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0)>iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0)) return(0);
 



Lines cross up or down, but when plotted as histogram the manual seems to suggest it's the same except it's plotted as a histogram. Is this wrong ? 

https://www.babypips.com/learn/forex/macd



Did I code this incorrectly or understand the MT4 indicator incorrectly somehow ?

Please advise
Thanks


How to Use the MACD Indicator
How to Use the MACD Indicator
  • www.babypips.com
MACD is an acronym for M oving A verage C onvergence D ivergence. This technical indicator is a tool that’s used to identify moving averages that are indicating a new trend, whether it’s bullish or bearish. After all, a top priority in trading is being able to find a trend, because that is where the most money is made. With a MACD chart, you...
 
Agent86: Did I code this wrong or something ?  

What part of "The histogram is up (above zero) no cross has occurred" is unclear.  What part of that has to do with code?

Do your own research and find out what the histogram means.

 
William Roeder:

What part of "The histogram is up (above zero) no cross has occurred" is unclear.  What part of that has to do with code?

Do your own research and find out what the histogram means.

I understand what a histogram means. Are all histograms the same ? 
I want to use the MACD crosses not the divergence part for certain things.
https://forex-indicators.net/macd


So I guess this answers why it didn't close position.

MACD is suppose to have 3 elements but this one seems to only have 2  ? 
So how to make use of MACD and trigger line if this one doesn't have a trigger line ? 

The histogram in the MT4 MACD does move very much like the MACD line and Trigger Lines, but I guess there is no way to make any code use of it ? 
macd