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
- 2018.12.24
- www.mql5.com
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
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
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
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.
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.// 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 ... };
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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