Having trouble with EA making an open Long position, Help Please

 

Having trouble making the EA perform an OrderSend() after checking multiple.  However the commented out section of my sell position works to makes orders of the sell.  I believe the largest part of my problem is in the MACD retracement requirement.  I have not seen any other example that checks a few parameters, waits on an action to happen, then checks more parameters before making an order.  Assistance would be appreciated.

Side note, I understand that I am use many, if not excessive, parenthesis. I use another program editor just to make sure those all line up.

extern int     StoplossPips = 10;
extern int     TakeprofitPips = 100;
int            MagicNumber = 0;
extern double  Lots = 1.0;
extern int     expert1ID = 1017;

int init()
{
   int PeriodID = 0;
   switch(Period())
   {
      case PERIOD_MN1: PeriodID = 0; break;
      case PERIOD_W1:  PeriodID = 1; break;
      case PERIOD_D1:  PeriodID = 2; break;
      case PERIOD_H4:  PeriodID = 3; break;
      case PERIOD_H1:  PeriodID = 4; break;
      case PERIOD_M30: PeriodID = 5; break;
      case PERIOD_M15: PeriodID = 6; break;
      case PERIOD_M5:  PeriodID = 7; break;
      case PERIOD_M1:  PeriodID = 0; break;
   }
   MagicNumber = (expert1ID*10) + PeriodID;
   return (0);
   //took from mql book example
}
int start()
{
  
   RefreshRates();
   bool adxValue1, adxSpread1, macdRetraced,macdValues,adxValue2, adxSpread2, MakePurchase;
   int orderOpen = OrdersTotal();
   //all this is checking before ordering a currency pair *purchasing*
   if(orderOpen < 1) { //only 1 order at a time for each currency
   // check if ADXmain > 25  for all time periods
   if( (iADX(NULL,PERIOD_M1,14,PRICE_CLOSE,MODE_MAIN,0)>=25) && (iADX(NULL,PERIOD_M5,14,PRICE_CLOSE,MODE_MAIN,0)>=25) &&    
       (iADX(NULL,PERIOD_M15,14,PRICE_CLOSE,MODE_MAIN,0)>=25)
     )
       adxValue1=true;
      //now check if (ADX DIP - ADX DIN > 5) for all time periods
   if( ((iADX(NULL,PERIOD_M5,14,PRICE_CLOSE,MODE_PLUSDI,0)) - (iADX(NULL,PERIOD_M15,14,PRICE_CLOSE,MODE_MINUSDI,0)) >= 5) &&
       ((iADX(NULL,PERIOD_M15,14,PRICE_CLOSE,MODE_PLUSDI,0)) - (iADX(NULL,PERIOD_M15,14,PRICE_CLOSE,MODE_MINUSDI,0)) >= 5)
     )
       adxSpread1=true;
     
     //check the MACD at M1 after ADX parameters checked, and tell it to wait until M1 MACD retraces into neg then back to positive again
   if( (iMACD(NULL,PERIOD_M1,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0) > 0) && (adxValue1 == true) && (adxSpread1 == true) )
      {
      datetime timeNow = TimeCurrent();
      datetime timeWait = timeNow + 3600; //3600 seconds (1hr) to wait up to 1 Hr
      while( TimeCurrent() != timeWait) //watch the 1M MACD go neg then retrace back to positive
         {bool MACDgoNEG, MACDbackPOS;
          RefreshRates();
          if (iMACD(NULL,PERIOD_M1,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0) < 0)//watch the MACD 1M go negative
            {MACDgoNEG = true;
             while(MACDgoNEG == true) //from here watch the MACD go from negaitve back to positive
               {
               if (iMACD(NULL,PERIOD_M1,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0) > 0 )//went back to positive
                 {macdRetraced = true;}
                 else
                 {macdRetraced = false;}
               }//while
            }//macd go neg
          
          return (macdRetraced);
         }//while timecurrent...
      
      }//
     
     //check the MACD on the 5 and 15 M chart
   if ( (iMACD(NULL,PERIOD_M5,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0) > 0) && (iMACD(NULL,PERIOD_M15,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0) > 0)
      )
      {macdValues = true;
      }
   
      //check the ADX parameters 1 more time after the retracement before purchasing an order.
   if( (macdRetraced == true) &&(iADX(NULL,PERIOD_M1,14,PRICE_CLOSE,MODE_MAIN,0)>=25) && (iADX(NULL,PERIOD_M5,14,PRICE_CLOSE,MODE_MAIN,0)>=25) &&    
      (iADX(NULL,PERIOD_M15,14,PRICE_CLOSE,MODE_MAIN,0)>=25) 
     )
     {adxValue2 = true;
     }
        //now check if (ADX DIP - ADX DIN > 5) again for all time periods
   if((macdRetraced == true) &&
       ((iADX(NULL,PERIOD_M5,14,PRICE_CLOSE,MODE_PLUSDI,0)) - (iADX(NULL,PERIOD_M5,14,PRICE_CLOSE,MODE_MINUSDI,0)) >= 5) &&
       ((iADX(NULL,PERIOD_M15,14,PRICE_CLOSE,MODE_PLUSDI,0)) - (iADX(NULL,PERIOD_M15,14,PRICE_CLOSE,MODE_MINUSDI,0)) >= 5) 
     )
     {adxSpread2 = true;}
  }//ordercount
    
   if ((macdValues == true) && (adxValue2 == true) && (adxSpread2 == true))
   {MakePurchase = true;
   }
  
     //now purchase the currency pair
     int ticket;
     double ask = MarketInfo(Symbol(),MODE_ASK);
     
   if (MakePurchase == true)  
     {
      ticket = OrderSend(Symbol(),OP_BUY,Lots,ask,30,0,0,NULL,MagicNumber,0,Green);
       if(ticket>=0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)== true) 
               Print("BUY order opened : ",OrderOpenPrice());
           }
            else Print("Error opening BUY order : ",GetLastError()); 
         return(0); 
     }//taken from MACD sample, but useful
     
//(sell)
//   if( (iADX(NULL,PERIOD_M15,14,PRICE_CLOSE,MODE_PLUSDI,0)) < (iADX(NULL,PERIOD_M15,14,PRICE_CLOSE,MODE_MINUSDI,0)) && orderOpen <0 )
//  {
//   OrderSend(Symbol(),OP_SELL,Lots,ask,30,0,0,NULL,MagicNumber,0,Red); 
   // close position
//   return(0); // exit
//  }

return (0);



}
 
asham:

Having trouble making the EA perform an OrderSend() after checking multiple.  However the commented out section of my sell position works to makes orders of the sell.  I believe the largest part of my problem is in the MACD retracement requirement.  I have not seen any other example that checks a few parameters, waits on an action to happen, then checks more parameters before making an order.  Assistance would be appreciated.

Side note, I understand that I am use many, if not excessive, parenthesis. I use another program editor just to make sure those all line up.

Maybe one of these is not true,  Print(), Comment() or Alert()  them and find out . . .

if ((macdValues == true) && (adxValue2 == true) && (adxSpread2 == true))

 By the way,  Buy at Ask,  Sell at Bid

 
  1. Add print statement before and inside your IFs so you find out why.
  2. Check your return values for FAILURE to find out why. What are Function return values ? How do I use them ? - MQL4 forum



 

RaptorUK and WHRoeder, thank you for your help. The Print() command has helped me figure out where most of my code was giving me trouble.  I have opening and closing of positions.

However I still have trouble making the MACD retracement become true. I have commented out (whatever it is called when I add the // to the code to essentially blank it out) everything except the ADX before the MACD retracement and after the MACD to ensure no other code would be in the way.  Here is my current attempt at the retracement. If this should be another thread inform me and it will be done. Assumed I could continue on the same thread since this is still the same EA.

So far I have gotten results as far as the Print() of "MACD went negative, wait for positive."  And the timeNow, timeWait, timeFinish variables have already been declared in the same area of my code as the bools of adxValue1, adxSpread1, etc.


if( (iMACD(NULL,PERIOD_M1,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0) > 0) && (adxValue1==true) && (adxSpread1==true))
     {macdValue=true;
     Print("MACD retracement watch beginning.");  
      timeNow = TimeCurrent();
      timeWait = timeNow + 3600; //1hr I believe
      if(iMACD(NULL,PERIOD_M1,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0) <= 0)
      { wentNeg=true;
       Print("MACD went negaitve, wait for positive.");
      }
     }
     //hang up at this point in the MACD retracement
      while(wentNeg==true)
       {if(iMACD(NULL,PERIOD_M1,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0) >=0)
        {
         backPos = true;
         Print("MACD went back to positive.");
         timeFinish = TimeCurrent();
         wentNeg=false;
        }
       }
      if((backPos==true) && (timeFinish<timeWait))
      {macdRetraced=true;
      Print("MACD retracement watch is FINISHED.");
      }
 
 while(MACDgoNEG == true) //from here watch the MACD go from negaitve back to positive
 {
   if (iMACD(NULL,PERIOD_M1,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0) > 0 )//went back to positive
      {macdRetraced = true;}
   else
      {macdRetraced = false;}
}//while
This is an infinite loop, nothing is changing. Return from start and wait for the next tick.