Using an array to determine a new bar in Metatrader
Recently I had a friend ask me why his array was not accurately portraying the last 30 bars of data. It is important when you create an Array that you are only changing its variable when the bar closes or opens. Otherwise your array would be getting updated during every single tick(or multiple times within one bar). Here I’m going to show an example of how you can configure your array to only record the changed variable when there is a new bar. The method I will use is on the change of time comparison.
This example is helpful if you are looking to optimize your code on close or open of bar. If you think about it your MT4 program runs during every single tick of data. If your code does not trade intrabar then you only need it to calculate if you should open or close a trade on the open of close of a bar. By inserting this code you can durastically cut down on processing time.
//——————————————————————–
// newbar.mq4
// The code should be used for educational purpose only.
//——————————————————————–
extern int Num_Bars=15; // Amount of bars this is where you define the number of bars you want to check externally
bool New_Bar=false; // Flag of a new bar – This is a global variable which will be true if the bar is new
//——————————————————————–
int start() // Special funct. start() – Here we are starting our function
{
double Minimum, // Minimal price – This is the lowest price of Num_Bars external variable
Maximum; // Maximal price – This is the highest price of Num_Bars external variable
//——————————————————————–
Fun_New_Bar(); // Function call – Here we jump down to the line void Fun_New_Bar to set the variable in the next line of code
if (New_Bar==false) // If bar is not new.. do below
return; // ..return or exit start()
//——————————————————————–
int Ind_max =ArrayMaximum(High,Num_Bars,1);// We land here if it is a new bar from function above. This sets Bar index of max. price
//it does so by calling ArrayMaximum() Which wants 3 inputs. An array to search, number of index to search in the array, and where in the
//index to start searching. Be cautious this returns the position of the element. Not the value of the element.
int Ind_min =ArrayMinimum(Low, Num_Bars,1);// Bar index of min. price
Maximum=High[Ind_max]; // Set variable Maximum to High of the bar index number from above.
Minimum=Low[Ind_min]; // Set variable minimum to Low of the bar index number from above
Alert(“For the last “,Num_Bars, // Show message alert so you can see values
” bars Min= “,Minimum,” Max= “,Maximum);
return; // Exit start() program is over.
}
//——————————————————————–
void Fun_New_Bar() // Funct. detecting .. Use the word void since we’re not returning a value
{ // .. a new bar
static datetime New_Time=0; // Create a new static datetime variable “new_time” this value will not be lost after the function is over
New_Bar=false; // No new bar – set the New_bar to false before the test
if(New_Time!=Time[0]) // Compare time if our new_time static variable is not = to the current bars time then proceed
{
New_Time=Time[0]; // If we got this far new_time did not equal the time of the current bar. So we’re now going to set it that way.
New_Bar=true; // Set our New_Bar global variable to true since our comparison was false previously. It will not be set to true again until the bar changes
}
}
//——————————————————————–


Leave a comment