Download MetaTrader 5

Check not enough history

To add comments, please log in or register
Teck Hua Chew
931
Teck Hua Chew  

Hi,

Need some help on history logic.

How do i check if i have enough history to test?

say if my history starts from 01.01.2009, but EA needs at least 31.12.2008 to go without error. i know i can always download the history for 31.12.2008 and start testing from 01.01.2009, but i think it should be test proof for other users.

I wanna print a message when history is not enough. Sorry if i didn't put up any codes here. I simply have no idea how to go about starting it and i can't find any leads from the forum.

i vividly remember seeing something like

if(Bars < 30){ do something something }

Is this the one?

Snelle Moda
613
Snelle Moda  

Hi

You can use these functions to solve your problem:


Use the function Bars() to check the number of available bars in your history.

https://www.mql5.com/en/docs/series/bars

Use the function StringToTime() to convert a stringdate into the MQL date/time format.

https://www.mql5.com/en/docs/convert/stringtotime

Use the function CopyTime() to copy the first date in your history to a time/date array.

https://www.mql5.com/en/docs/series/copytime


See this example to check if the first date in your history is before the minimum required history.

// https://www.mql5.com/en/docs/convert/stringtotime
// Variable for minimum test date.
         datetime My_Minimum_Required_Date = StringToTime("2008.12.31 00:00"); // Convert stringdate to MQL date.

// Array for storing the first date of your history.         
         datetime First_Date_InHistory[1];
         CopyTime(Symbol(), Period(), Bars(Symbol(), Period()), 1, First_Date_InHistory); 

         Alert("Minimum required start date =  ", My_Minimum_Required_Date, "  Startdate in my history  ", First_Date_InHistory[0]);

// Check if minimum required date is earlier then the date available in your history.
         if(My_Minimum_Required_Date < First_Date_InHistory[0])
               {
               Alert("History date is not available");
               return 0;                 
               }


If you know the actual number of required bars your EA needs then you can also use this function to count the number of bars between your date periods.

Then use something like this to check if you have enough bars:

// Simpel method:
//---------------
// Check the number of bars in the total history.
         if(Bars(Symbol(), Period()) < 30) // if the function returns 0, the current history is to short for your minimum required ammount of BARS.
               {
               Alert("History date is not available");
               return 0;                
               }


// Different approach:
//--------------------
// Minimum amount of bars needed for indicator calculation.
         int nMinimum_Required_Bars = Bars(Symbol(), Period(), My_Minimum_Required_Date, TimeCurrent());           

         Alert("Total amount of BARS on the Chart = ", Bars(Symbol(), Period()), " Bars needed = ", nMinimum_Required_Bars);

// Check the total ammount of BARS on the chart is lower then the minimum amount.      
         if(Bars(Symbol(), Period()) < nMinimum_Required_Bars)
               {
               
               Alert("Not enough bars available! Number available = ", Bars(Symbol(), Period()), " Needed = ", nMinimum_Required_Bars);
               return 0;               
               }
Documentation on MQL5: Timeseries and Indicators Access / Bars
Documentation on MQL5: Timeseries and Indicators Access / Bars
  • www.mql5.com
Timeseries and Indicators Access / Bars - Reference on algorithmic/automated trading language for MetaTrader 5
Teck Hua Chew
931
Teck Hua Chew  
Thanks Snelle, these are great leads and help
To add comments, please log in or register