Detet the trend using ZigZag

 

In this code "https://c.mql5.com/3/260/ZigZag_Example.mq5" i learn how to detet ZigZag extremum. How to know if the last detected extremum is high or low? How to code: if last extremum was high then it's downtrend and if it's low then it's downtrend? Thanks

 
Tiofelo Teles: In this code "https://c.mql5.com/3/260/ZigZag_Example.mq5" i learn how to detet ZigZag extremum. How to know if the last detected extremum is high or low? How to code: if last extremum was high then it's downtrend and if it's low then it's downtrend? Thanks
By keeping track of the ZigZag point before that. However, by definition of the ZigZag, each swing point is just that — a swing point, which is the opposite extreme of the previous one.
 
Tiofelo Teles:

In this code "https://c.mql5.com/3/260/ZigZag_Example.mq5" i learn how to detet ZigZag extremum. How to know if the last detected extremum is high or low? How to code: if last extremum was high then it's downtrend and if it's low then it's downtrend? Thanks

in the attached link Examples/ZigZag is being used, You can replace it with Emamples/ZigZagColor indicator which has a buffer for color index, so you can know if its uptrend or downtrend 

 
How to start with MQL5
How to start with MQL5
  • 2018.12.24
  • www.mql5.com
This thread discusses MQL5 code examples. There will be examples of how to get data from indicators, how to program advisors...
 

That only works with strong trend, ***

And with strong trend you need wide stoploss lol.

 
Arpit T #:

in the attached link Examples/ZigZag is being used, You can replace it with Emamples/ZigZagColor indicator which has a buffer for color index, so you can know if its uptrend or downtrend 

Thanks for the answer! My goal is to use "normal ZigZag", without coloring.

 
Jan4654 #:

That only works with strong trend, ***

And with strong trend you need wide stoploss lol.

Thanks for the answer! I don't care if it only works on strong trend, I just want to know how I can detect the trend using it. I would be very grateful if you help me

 

Dear Vladimir Karputov, thanks for the reply! I will have a look at the code provided in detail. Thanks

 
Fernando Carreiro #:
By keeping track of the ZigZag point before that. However, by definition of the ZigZag, each swing point is just that — a swing point, which is the opposite extreme of the previous one.

Dear Fernando Carreiro,thanks for the reply! I will try to implement your explanation.

 

Here's my code! Sometimes it takes time to show the direction of the "trend" even though it has already found new tops and bottoms in the chart. Any improvement will be welcome. Thanks

Files:
 
Tiofelo Teles #:

Dear Fernando Carreiro,thanks for the reply! I will try to implement your explanation.

The following is sample code for Fractals, but it can easily be adapted for zigzag. Instead of using the upper and lower fractals, just look for swing points and detect if it is an upper or lower by simply comparing to the high or low price of that bar. If the swing point is the same as the high, then it is the upper, and if it is the same as the low, it is the lower.

EDIT: Please note that it is MQL4 code and not MQL5, but it just serves as a sample for you to adapt the logic for the zigzag.

Forum on trading, automated trading systems and testing trading strategies

Unexpected order

Fernando Carreiro, 2022.06.29 15:14

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: