Help Needed With mql4 Signal EA - Close Candle Only

 

Hi everyone,

I would appreciate any help you can offer with an EA I am writing. The EA I wrote gives me a signal while a candle is open while I only want the signal to be provided after the candle closes and can't seem to figure out how to code for this.

Intent:  Using CCI levels.  Give sell signal when CCI value crosses from above +100 to below +100, buy signal when CCI value crosses from below -100 to above -100.

I thought the using shift position 1 and 2 for candles 1 and 2 would resolved this but it did not.  

I then added the IsNewCandle script but did not change anything.  

Here is the code I have so far.

Thank you for your assistance.

Serge

void OnTick()
  {
// We create a string variable for the signal
string signal ="";
int level=90;

// define the ICCI

double D1 = iCCI(_Symbol,_Period,35,PRICE_CLOSE,1);
double D2 = iCCI(_Symbol,_Period,35,PRICE_CLOSE,2);


// IF IT IS ABOVE +100 and crossing +100 downwards
           if ((D1<level)&&(D2>level))
{
        signal="sell";
}
// IF IT IS BELOW -100 & crossing -100 upwards
           if ((D1>(-level))&&(D2<(-level)))
{
        signal="buy";
}

// chart output for the signal
   
//   PlaySound("alert");

   if (signal=="sell"&&IsNewCandle())
      {
      Alert("The current CCI signal is: ",signal," for ",Symbol());
      }

   if (signal=="buy"&&IsNewCandle())
      {
      Alert("The current CCI signal is: ",signal," for ",Symbol());
      }
  
  }

//--- to have alarm sound only one time

bool IsNewCandle()
{
   static datetime saved_candle_time;
   if(Time[0]==saved_candle_time)
   return false;
   else
   saved_candle_time=Time[0];
   return true;

   
}
Documentation on MQL5: Constants, Enumerations and Structures / Objects Constants / Object Types
Documentation on MQL5: Constants, Enumerations and Structures / Objects Constants / Object Types
  • www.mql5.com
When a graphical object is created using the ObjectCreate() function, it's necessary to specify the type of object being created, which can be one of the values of the ENUM_OBJECT enumeration. Further specifications of object properties are possible using functions for working with graphical objects.
 
bool IsNewCandle()
  {
   static datetime lastbar;
   datetime curbar = Time[0];
   if(lastbar!=curbar)
     {
      lastbar=curbar;
      return (true);
     }
   else
     {
      return(false);
     }
  }
 
Serge:

Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
          General rules and best pratices of the Forum. - General - MQL5 programming forum 2019.05.06
          Messages Editor

 
William Roeder:

Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
          General rules and best pratices of the Forum. - General - MQL5 programming forum 2019.05.06
          Messages Editor

Thanks William, I will review those practices. I am new here and learning.

Regards, Serge

 
Mehmet Bastem:

Thank you Mehmet.  I will give it a try and let you know. Serge

Reason: