Unexpected order - page 3

 

There are many ways a developer could find a solution, but this is how I would tackle the problem.

// Default tick event handler
   void OnTick()
   {
      // Check for new bar (compatible with both MQL4 and MQL5).
         static datetime dtBarCurrent  = WRONG_VALUE;
                datetime dtBarPrevious = dtBarCurrent;
                         dtBarCurrent  = iTime( _Symbol, _Period, 0 );
                bool     bNewBarEvent  = ( dtBarCurrent != dtBarPrevious );

      // Keep a running state of previous fractals
         static bool
            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 time
                     if( 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 fractals
                     bool
                        bUpperFractal = iFractals( _Symbol, _Period, MODE_UPPER, 3 ) > 0.0,
                        bLowerFractal = iFractals( _Symbol, _Period, MODE_LOWER, 3 ) > 0.0;
                  
                  // Check if signals are generated
                     bool
                        bBuySignal    = bLowerFractal && !bUpperFractal && !bLowerFractalPrevious,
                        bSellSignal   = bUpperFractal && !bLowerFractal && !bUpperFractalPrevious;
                     
                  // Save current fractal states for next time
                     bUpperFractalPrevious = bUpperFractal;
                     bLowerFractalPrevious = bLowerFractal;
                  
                  // React on generated signals
                     if( 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.
Reason: