There are many ways a developer could find a solution, but this is how I would tackle the problem.
// Default tick event handlervoidOnTick()
{
// Check for new bar (compatible with both MQL4 and MQL5).staticdatetime dtBarCurrent = WRONG_VALUE;
datetime dtBarPrevious = dtBarCurrent;
dtBarCurrent = iTime( _Symbol, _Period, 0 );
bool bNewBarEvent = ( dtBarCurrent != dtBarPrevious );
// Keep a running state of previous fractalsstaticbool
bFirstFractalCheck = true,
bUpperFractalPrevious = false,
bLowerFractalPrevious = false;
// React to a new bar event and handle it.if( bNewBarEvent )
{
// Detect if this is the first tick received and handle it./* For example, when it is first attached to a chart and
the bar is somewhere in the middle of its progress and
it's not actually the start of a new bar. */if( dtBarPrevious == WRONG_VALUE )
{
// Do something on first tick or middle of bar ...
}
else
{
// Check for previous confirmed fractals on first timeif( bFirstFractalCheck )
{
bFirstFractalCheck = false;
int nBars = iBars( _Symbol, _Period );
for( int i = 4;
( i < nBars ) && !bUpperFractalPrevious && !bLowerFractalPrevious;
i++ )
{
bUpperFractalPrevious = iFractals( _Symbol, _Period, MODE_UPPER, i ) > 0.0;
bLowerFractalPrevious = iFractals( _Symbol, _Period, MODE_LOWER, i ) > 0.0;
};
};
// Get state of currently confirmed fractalsbool
bUpperFractal = iFractals( _Symbol, _Period, MODE_UPPER, 3 ) > 0.0,
bLowerFractal = iFractals( _Symbol, _Period, MODE_LOWER, 3 ) > 0.0;
// Check if signals are generatedbool
bBuySignal = bLowerFractal && !bUpperFractal && !bLowerFractalPrevious,
bSellSignal = bUpperFractal && !bLowerFractal && !bUpperFractalPrevious;
// Save current fractal states for next time
bUpperFractalPrevious = bUpperFractal;
bLowerFractalPrevious = bLowerFractal;
// React on generated signalsif( bBuySignal )
{
// Place a buy order
};
if( bSellSignal )
{
// Place a sell order
};
// Do something else when a normal bar starts ...
};
// Do something irrespective of the above condition ...
}
else
{
// Do something else ...
};
// Do other things ...
};
EDIT: Please note that the above code was not tested. It is sample code only. It's also only valid for MQL4. MQL5 handles indicators differently.
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
There are many ways a developer could find a solution, but this is how I would tackle the problem.
EDIT: Please note that the above code was not tested. It is sample code only. It's also only valid for MQL4. MQL5 handles indicators differently.