Help in my first EA just counting the new candles

 

Hi, I am new with MQ4 and I've tried to put this to work but there is not way to read a second NEW candle.  I've being testing in a  range of 2 weeks, and the script should look for a new candle every 5 minutes (candles of 5 minutes period).  I really don't know what I am missing.    Here you will see the code, please modify any part of the code and put it the Comment just to know what was wrong and learn from you.   I will really appreciate your help.

#property description "4 Candles Every 30 Minutes Expert Advisor"
#include <WinUser32.mqh>
#include <stdlib.mqh>

// Input Variables
input int HighLowBars = 8;
input int Shift = 0;
input bool TradeOnBarOpen = true;
input int MyPeriod = 5;  

// Global Variables
int hShift = iHighest(_Symbol, MyPeriod, MODE_HIGH, HighLowBars, Shift);
int lShift = iLowest(_Symbol, MyPeriod, MODE_LOW, HighLowBars, Shift);
double hHigh = High[hShift];
double lLow = Low[lShift];
double LastClose = Close[0];
bool IsHigh = false, IsLow = false;
datetime NewCandleTime = TimeCurrent();
int CeroQuince = 0;
int Resto = 0;
int Cien = 100;
int Dosciento = 200;
bool IsNewCandle=false;                   //Indicates if this is a new candle formed


//+------------------------------------------------------------------+
//| OnTick function                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {
    hShift = iHighest(_Symbol, MyPeriod, MODE_HIGH, HighLowBars, Shift);
    lShift = iLowest(_Symbol, MyPeriod, MODE_LOW, HighLowBars, Shift);
    hHigh = High[hShift];
    lLow = Low[lShift];
    LastClose = Close[0];
    IsHigh = false;
    IsLow = false;

  
    // Here is the problem... There is not a new candle every 5 minutes
    if(CheckNewBar())
     {
      // If the candle is new AND it started at minute 00 OR Minute 30
      // It means, it will check for a new candle every 30 minutes
      if(Minute()==00 || Minute()==30)
        {
          CeroQuince++;    // To store the times Minutes = "00"  or = "30"
        }
      else
        {
        Resto++;    // To store the times Minutes IS NOT = "00"  or IS NOT = "30"
        }

      // This is only to show me how many NEW candles (Period = M5) it counted 
      Alert("-- Candles On 00/30 ==> " + IntegerToString(CeroQuince) + " -- Differents Candles ==> " + IntegerToString(Resto));

      // Here I will look for a Down (Bullish) or Up (Bearish) Trend
      IsLow = F_DownTrendLow(1,4);
      IsHigh = F_UpTrendLow(1,4);
    }  // End if NewBar
    else
    {
      Alert("-- This is not a New Candle -- ");
    }
    return (0);
  }  // End of OnTick


//Check if it is a new bar
//-----------------------------------
datetime NewBarTime=TimeCurrent();
bool CheckNewBar(){
   //NewBarTime contains the open time of the last bar known
   //if that open time is the same as the current bar then we are still in the current bar, otherwise we are in a new bar
   if(NewBarTime==iTime(Symbol(),PERIOD_CURRENT,0)) IsNewCandle=false;
   else{
      NewBarTime=iTime(Symbol(),PERIOD_CURRENT,0);
      IsNewCandle=true;
   }
   return(IsNewCandle);
}


// DownTrendLow
//-----------------------------------
bool F_DownTrendLow(int from, int candles)
{
  bool lResult = false;
  for(int i=from; i<=candles; i++)
  {
    if(Low[i] > Low[i+1])
    {
      lResult = false;
      break;
    }
    else
    {
      lResult = true;
    }
  }
  return(lResult);
}


// UpTrendLow
//-----------------------------------
bool F_UpTrendLow(int from, int candles)
{
  bool lResult = false;
  for(int i=from; i<=candles; i++)
  {
    if(High[i] < High[i+1])
    {
      lResult = false;
      break;
    }
    else
    {
      lResult = true;
    }
  }
  return(lResult);
}

 

Forum on trading, automated trading systems and testing trading strategies

When you post code please use the CODE button (Alt-S)!

Use the CODE button


 
  1. 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

  2. // Global Variables
    
    int hShift = iHighest(_Symbol, MyPeriod, MODE_HIGH, HighLowBars, Shift);
    
    int lShift = iLowest(_Symbol, MyPeriod, MODE_LOW, HighLowBars, Shift);
    
    double hHigh = High[hShift];
    
    double lLow = Low[lShift];
    
    double LastClose = Close[0];
    
    datetime NewCandleTime = TimeCurrent();

    Those are not assignments; they are initialization of a common (globally declared), or static variable with a constant. They work exactly the same way in MT4/MT5/C/C++.

    1. They are initialized once on program load.

    2. They don't update unless you assign to them.

    3. In C/C++ you can only initialize them with constants, and they default to zero. In MTx you should only initialize them with constants. There is no default in MT5, or MT4 with strict (which you should always use).

      MT4/MT5 actually compiles with non-constants, but the order that they are initialized is unspecified and

      Don't try to use any price or server related functions in OnInit (or on load), as there may be no connection/chart yet:

      1. Terminal starts.
      2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
      3. OnInit is called.
      4. For indicators OnCalculate is called with any existing history.
      5. Human may have to enter password, connection to server begins.
      6. New history is received, OnCalculate called again.
      7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

    4. Unlike indicators, EAs are not reloaded on chart change so you must reinitialize them, if necessary.
                external static variable - MQL4 programming forum #2 2013.02.10

  3.    if(Minute()==00 || Minute()==30)

    What if there are no ticks during a specific candle period? There can be minutes between ticks during the Asian session, think M1 chart. Larger charts, think weekend, market holiday (country and broker specific), requires knowledge of when your broker stops and starts (not necessary the same as the market.)
              "Free-of-Holes" Charts - MQL4 Articles 20 June 2006
              No candle if open = close ? - MQL4 programming forum

 
Sergey Golubev

 Thank you... I didn't know it 

 
Topics concerning MT4 and MQL4 have their own section.
In future please post in the correct section.
I have moved your topic to the MQL4 and Metatrader 4 section.
 
How can I go to my topic in MQL4  ?
 
RBatista809: How can I go to my topic in MQL4  ?
This topic is in MQL4
Reason: