Function examples:
Example Purpose: Returns the larger of two values.
Syntax: MathMax(value1, value2)
double a = 5.0;
double b = 10.0;
double max_value = MathMax(a, b); // max_value will be 10.0
MathMin
Purpose: Returns the smaller of two values.
Syntax: MathMin(value1, value2)
Example:
mql
double a = 5.0;
double b = 10.0;
double min_value = MathMin(a, b); // min_value will be 5.0
No Functions work Why. ALSO loops don't work why.
Here's an example in MQL5 to calculate the largest body bar between bar 1 and bar 5. In this context, the "body" of a bar is the absolute difference between the opening and closing prices of that bar.
Example Code:
void OnStart()
{
// Arrays to hold the open and close prices of bars
double openPrices[5], closePrices[5];
// Assign example prices for bars 1 to 5
openPrices[0] = 1.1234; closePrices[0] = 1.1244; // Bar 1
openPrices[1] = 1.1245; closePrices[1] = 1.1210; // Bar 2
openPrices[2] = 1.1205; closePrices[2] = 1.1230; // Bar 3
openPrices[3] = 1.1225; closePrices[3] = 1.1215; // Bar 4
openPrices[4] = 1.1250; closePrices[4] = 1.1280; // Bar 5
// Variable to hold the largest body size
double largestBodySize = 0;
int largestBodyIndex = -1;
// Calculate the body size for each bar and find the largest
for (int i = 0; i < 5; i++)
{
double bodySize = MathAbs(closePrices[i] - openPrices[i]);
if (bodySize > largestBodySize)
{
largestBodySize = bodySize;
largestBodyIndex = i;
}
}
// Print the result
Print("The largest body is at bar ", largestBodyIndex + 1, " with a size of ", largestBodySize);
}
WHy! or how to get functions and loops to work?




