High and low of current bar tracker...

 
Hi guys! 

I am trying to track the high and low of the current 15min bar on a give chart. I have this script so far:

//+------------------------------------------------------------------+
//|                                            TrackHighLow_EA.mq5 |
//|                                                  |
//+------------------------------------------------------------------+
#property copyright "Your Name"
#property link      "https://www.yourwebsite.com"
#property version   "1.00"
#property strict

input bool TrackHighLow = true; // Set to false if you want to stop tracking

//--- Global variables to store the high and low values of the current bar
double currentHigh = 0.0;
double currentLow = 0.0;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
   //--- Initialize high and low with current bar values
   currentHigh = High[0];
   currentLow = Low[0];

   return INIT_SUCCEEDED;
}

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
   //--- Update high and low with the current bar values
   if (TrackHighLow)
   {
       double highPrice = iHigh(_Symbol, _Period, 0);
       double lowPrice = iLow(_Symbol, _Period, 0);

       if (highPrice > currentHigh)
           currentHigh = highPrice;

       if (lowPrice < currentLow)
           currentLow = lowPrice;
   }
}

//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
   //--- Do any cleanup here if needed
}
However when I compile it I get these errors:

' HL_tracker.mq5' HL_tracker.mq5 1 1
'High' - undeclared identifier HL_tracker.mq5 22 18
'[' - array required HL_tracker.mq5 22 22
'Low' - undeclared identifier HL_tracker.mq5 23 17
'[' - array required HL_tracker.mq5 23 20
4 errors, 0 warnings 5 1

By the way I am very new to scripting in MT5, so I would really appreciate your help! 

Thank you in advance! 


 
Kiril Kirilov:
int OnInit() {    //--- Initialize high and low with current bar values    currentHigh = High[0];    currentLow = Low[0];    return INIT_SUCCEEDED; }


int OnInit()
{
   //--- Initialize high and low with current bar values
   currentHigh = iHigh(_Symbol, _Period, 0);
   currentLow = iLow(_Symbol, _Period, 0);

   return INIT_SUCCEEDED;
}
 
int OnInit()
{
   //--- Initialize high and low with current bar values
   currentHigh = High[0];
   currentLow = Low[0];
  1. There are no predefine arrays in MT5 unless you define them. Using global arrays like Open[], Close[], Low[], High[] in custom indicator for MetaTrader 5 - MQL5 programming forum (2023)

  2. Don't try to use any price (or indicator) or server related functions in OnInit (or on load or in OnTimer before you've received a tick), 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. A new tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

  3. Simplify your code.
    int OnInit(){
       //--- Initialize high and low with current bar values
       currentHigh = DBL_MIN;
       currentLow  = DBL_MAX;
    
       return INIT_SUCCEEDED;
    }
    void OnTick(){
       ⋮
           currentHigh = MathMax( currentHigh, iHigh(_Symbol, _Period, 0) );
           currentLow  = MathMin( currentLow,   iLow(_Symbol, _Period, 0) );
    

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