How to make an mql5 code that identifies peaks and draws accurate trendlines

 
Hello guys please I need help in fixing a bug i've been having a problem with for some time now. So basically i'm trying to build a portion of code that helps me to identify peaks in the market and store them in a 2D array so that I can access them later to use them in drawing trendlines and support and resistance zones.


double HighPeaks[][2];
   double LowPeaks[][2];
   double High[];
   double Low[];
   double Open[];
   double Close[];
   datetime Time[];
   ArraySetAsSeries(High, true);
   ArraySetAsSeries(Low, true);
   ArraySetAsSeries(Open, true);
   ArraySetAsSeries(Close, true);
   ArraySetAsSeries(Time, true);
   int Range = 300;
   int Tolerance = 10;  // minimum amount of candles before trend reversal before being considerd as a peak
   int MinPeaks = 2;
   for (int i = 0; i <= Range - 1; i++)
   {
      
      if (i - Tolerance >= 0 && i + Tolerance < Range)
      {
         CopyHigh(_Symbol, PERIOD_CURRENT, i, Range, High);
         CopyLow(_Symbol, PERIOD_CURRENT, i, Range, Low); 
         CopyOpen(_Symbol, PERIOD_CURRENT, i, Range, Open);
         CopyClose(_Symbol, PERIOD_CURRENT, i, Range, Close);
         CopyTime(_Symbol, PERIOD_CURRENT, i, Range, Time);
         bool HighPeakCon1 = true;
         bool HighPeakCon2 = true;
         bool LowPeakCon1 = true;
         bool LowPeakCon2 = true;
         for (int j = i; j <= i + Tolerance; j++)
         {
            if (High[i] < High[j])
            {
               HighPeakCon1 = false;
            }
            if (Low[i] > Low[j])
            {
               LowPeakCon1 = false;
            }
         }
         for (int j = i; j >= i - Tolerance; j--)
         {
            if (High[i] < High[j])
            {
               HighPeakCon2 = false;
            }
            if (Low[i] > Low[j])
            {
               LowPeakCon2 = false;
            }
         }  
  
         if (HighPeakCon1 && HighPeakCon2)
         {
            ArrayResize(HighPeaks, ArrayRange(HighPeaks, 0) + 1, 0);
            HighPeaks[ArrayRange(HighPeaks, 0) - 1][0] = Time[i];
            HighPeaks[ArrayRange(HighPeaks, 0) - 1][1] = High[i];
            
         }
         if (LowPeakCon1 && LowPeakCon2)
         {
            ArrayResize(LowPeaks, ArrayRange(LowPeaks, 0) + 1, 0);
            LowPeaks[ArrayRange(LowPeaks, 0) - 1][0] = Time[i];
            LowPeaks[ArrayRange(LowPeaks, 0) - 1][1] = Low[i];
            
         }
      }
   }
   ArraySort(HighPeaks);
   ArraySort(LowPeaks);
   ArrayReverse(HighPeaks);
   ArrayReverse(LowPeaks);
   ArrayPrint(HighPeaks, _Digits, ", ", 0, WHOLE_ARRAY, ARRAYPRINT_HEADER|ARRAYPRINT_INDEX|ARRAYPRINT_LIMIT|ARRAYPRINT_ALIGN);
   ArrayPrint(LowPeaks, _Digits, ", ", 0, WHOLE_ARRAY, ARRAYPRINT_HEADER|ARRAYPRINT_INDEX|ARRAYPRINT_LIMIT|ARRAYPRINT_ALIGN);

in the above portion of code the algorithm seemed to work well and I was having valid peaks being stored in the array


for (int i = 0; i < ArrayRange(HighPeaks, 0); i++)
   {
      string Name = "High Peak At " + DoubleToString(HighPeaks[i][1], 16);
      double TempTime = HighPeaks[i][0];
      double TempTime1 = TempTime - 900;
      double TempTime2 = TempTime + 900;
      datetime Time1 = TempTime1;
      datetime Time2 = TempTime2;
      ObjectCreate(0, Name, OBJ_TREND, 0, Time1, HighPeaks[i][1], Time2, HighPeaks[i][1]); // high peak identifier
      ObjectSetInteger(0, Name, OBJPROP_COLOR, clrLightCyan);
   }
   
   for (int i = 0; i < ArrayRange(LowPeaks, 0); i++)
   {
      string Name = "Low Peak At " + DoubleToString(LowPeaks[i][1], 16);
      double TempTime = LowPeaks[i][0];
      double TempTime1 = TempTime - 900;
      double TempTime2 = TempTime + 900;
      datetime Time1 = TempTime1;
      datetime Time2 = TempTime2;
      ObjectCreate(0, Name, OBJ_TREND, 0, Time1, LowPeaks[i][1], Time2, LowPeaks[i][1]); // low peak identifier
      ObjectSetInteger(0, Name, OBJPROP_COLOR, clrLightCyan);
   }
   
   if (HighPeaks[0][1] > HighPeaks[1][1])
   {
      string Name = "High Trendline At " + DoubleToString(HighPeaks[0][1]);
      datetime Time1 = HighPeaks[0][0];
      datetime Time2 = HighPeaks[1][0];
      double Price1 = HighPeaks[0][1];
      double Price2 = HighPeaks[1][1];
      ObjectCreate(0, Name, OBJ_TREND, 0, Time1, Price1, Time2, Price2);
   } 


However in the above code when i tried to access the arrays to draw small lines that identified peaks in the market I noticed two things:

1. Some of the peaks being drawn on the screen where not stored in some of the iterations where the array was printed this lead me to conclude that for some odd reason some iterations of the code failed to identify and store some valid peaks and because of this,

2. When the algorithm attempts to draw trendlines, sometimes it draws accurate lines while sometimes it fails to do so but draws invalid trendlines using a pare of peak points that do not match. 


Iv'e been trying to find the cause of this problem for months now but I don't seem to see any problem in the logic or syntax of my code. Please I could use some help in solving this problem and if there is some other way to write this code i'll be happy to hear it.

 
clyde50:
Hello guys please I need help in fixing a bug i've been having a problem with for some time now. So basically i'm trying to build a portion of code that helps me to identify peaks in the market and store them in a 2D array so that I can access them later to use them in drawing trendlines and support and resistance zones.


in the above portion of code the algorithm seemed to work well and I was having valid peaks being stored in the array



However in the above code when i tried to access the arrays to draw small lines that identified peaks in the market I noticed two things:

1. Some of the peaks being drawn on the screen where not stored in some of the iterations where the array was printed this lead me to conclude that for some odd reason some iterations of the code failed to identify and store some valid peaks and because of this,

2. When the algorithm attempts to draw trendlines, sometimes it draws accurate lines while sometimes it fails to do so but draws invalid trendlines using a pare of peak points that do not match. 


Iv'e been trying to find the cause of this problem for months now but I don't seem to see any problem in the logic or syntax of my code. Please I could use some help in solving this problem and if there is some other way to write this code i'll be happy to hear it.

Could you mark out a chart screenshot? So that it is visible what is correct and what is false?

It would help me to understand.
 
Your topic has been moved to the section: Technical Indicators
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 
The ZigZag indicator is the best option to identify trends.

 
Dominik Egert #:
Could you mark out a chart screenshot? So that it is visible what is correct and what is false?

It would help me to understand.

Sorry for the late response

In that image the last high trendline(the one marked blue) my EA drew  wasn't supposed to be that way it was supposed to span between the peaks I marked as blue but instead it started at a point far away from it. The same happens at the trendline I marked as red.


The part of the code I initially posted is written in the onTick function so I came to the conclusion that the reason why this error happens is because on some ticks and the ones where the trendlines are drawn the code fails to identify those peaks(The ones the trendline where supposed to start from)
 
Check the code in the link for Fast Zigzag.
 
amrali #:
Check the code in the link for Fast Zigzag.
which Link do you mean?
 
clyde50:
Hello guys please I need help in fixing a bug i've been having a problem with for some time now. So basically i'm trying to build a portion of code that helps me to identify peaks in the market and store them in a 2D array so that I can access them later to use them in drawing trendlines and support and resistance zones.


in the above portion of code the algorithm seemed to work well and I was having valid peaks being stored in the array



However in the above code when i tried to access the arrays to draw small lines that identified peaks in the market I noticed two things:

1. Some of the peaks being drawn on the screen where not stored in some of the iterations where the array was printed this lead me to conclude that for some odd reason some iterations of the code failed to identify and store some valid peaks and because of this,

2. When the algorithm attempts to draw trendlines, sometimes it draws accurate lines while sometimes it fails to do so but draws invalid trendlines using a pare of peak points that do not match. 


Iv'e been trying to find the cause of this problem for months now but I don't seem to see any problem in the logic or syntax of my code. Please I could use some help in solving this problem and if there is some other way to write this code i'll be happy to hear it.




You must set start index to zero in CopyHigh and CopyLow functions