Minutes between one point and another Zig Zag

 
Hello everyone, I wrote this code to know how many minutes there are between one point and the other two of the ZigZag. The code is not of error but I do not have the desired result.
Could you kindly help me?

Thank you

//+------------------------------------------------------------------+
//| Indicatore: Mio_Test |
//+------------------------------------------------------------------+
#property copyright ""
#property link ""
#property version "1.0"
#property description ""

#include <stdlib.mqh>
#include <stderror.mqh>



//--- indicator settings
#property indicator_chart_window
#property indicator_buffers 2

#property indicator_type1 DRAW_ARROW
#property indicator_width1 1
#property indicator_color1 White
#property indicator_label1 "Buy"

#property indicator_type2 DRAW_ARROW
#property indicator_width2 1
#property indicator_color2 White
#property indicator_label2 "Sell"

//--- indicator buffers
double Buffer1[];
double Buffer2[];

extern double DistanzaIcona = 1;
extern bool Audible_Alerts = true;
datetime time_alert;
double myPoint;

void myAlert(string type, string message)
{
if(type == "print")
Print(message);
else if(type == "error")
{
Print(type+" | Test @ "+Symbol()+","+Period()+" | "+message);
}
else if(type == "order")
{
}
else if(type == "modify")
{
}
else if(type == "indicator")
{
if(Audible_Alerts) Alert(type+" | Test @ | "+Symbol()+","+Period()+" | "+message);
}
}

//-------ZigZag-----------

datetime ZigZag(int j)
{
int k=0;
double ZigZag0=0;
datetime time=0;

for(int T=0;T<500;T++) //Last 500 candles
{
ZigZag0=iCustom(Symbol(),0,"ZigZag",12,6,3,0,T); //I take the value of the Zig Zag

if(ZigZag0!=0.0&&k<j) {k++;continue;} //If the value of Zig Zag is not what interests me the difference

if(ZigZag0!=0.0&&time==0) {time=Time[T];break;} //If the value of Zig Zag is what interests me, except the time with the index.
}
return(time);
}
//------------------------
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
IndicatorBuffers(2);

SetIndexBuffer(0, Buffer1);
SetIndexBuffer(1, Buffer2);

SetIndexEmptyValue(0, 0);
SetIndexEmptyValue(1, 0);

SetIndexArrow(0, 159);
SetIndexArrow(1, 159);

//initialize myPoint
myPoint = Point();
if(Digits() == 5 || Digits() == 4 || Digits() == 3 || Digits() == 2)
{
myPoint *= 10;
}
return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime& time[],
const double& open[],
const double& high[],
const double& low[],
const double& close[],
const long& tick_volume[],
const long& volume[],
const int& spread[])
{
int limit = rates_total - prev_calculated;
//--- counting from 0 to rates_total
ArraySetAsSeries(Buffer1, true);
ArraySetAsSeries(Buffer2, true);
//--- initial zero
if(prev_calculated < 1)
{
ArrayInitialize(Buffer1, 0);
ArrayInitialize(Buffer2, 0);
}

Int i = 0;

int A = (ZigZag(1) - ZigZag(2)) / 60; //Difference in minutes between the first value (current) of the Zig Zag and the third.

//Indicator Buffer 1
if( A < 23 )
{
Buffer1[1+i] = High[1+i] + DistanzaIcona * myPoint;
if(i == 0 && Time[0] != time_alert) { myAlert("indicator", "Sell"); time_alert = Time[0]; }
}

//Indicator Buffer 2
if(false)
{
Buffer2[i] = Low[i] - DistanzaIcona * myPoint;
if(i == 0 && Time[0] != time_alert) { myAlert("indicator", "Buy"); time_alert = Time[0]; }
}
}
return(rates_total);
}
//+------------------------------------------------------------------+



 
fly7680:
Hello everyone, I wrote this code to know how many minutes there are between one point and the other two of the ZigZag. The code is not of error but I do not have the desired result.
Could you kindly help me?

Thank you

What is the desired result and what is the result that you are getting?

int A = (ZigZag(1) - ZigZag(2)) / 60; //Difference in minutes between the first value (current) of the Zig Zag and the third.

This code finds the difference between the 2nd bar with a zigzag value and the 3rd

 

with various tests, I forgot to change the values, the correct values are these:

int A = (ZigZag(1) - ZigZag(2)) / 60; //Difference in minutes between the first value of the Zig Zag and the second

They should see the dots of the buffer only in the candle 1, but appear in all candles

I enclose scrennshot!



 
specifying Buffer1 [1+i] should not just go out in the first candle?
 
fly7680: specifying Buffer1 [1+i] should not just go out in the first candle?
  1. Write English, that is unintelligible.
  2. I==0, so it puts a dot on bar 1. When a new bar forms, it puts a dot on the new bar 1. When a new bar forms, it puts a dot on the new bar 1...
 

 Another example to be able to understand and find a solution. According to the time of the Zig Zag, I want to determine whether the first two points if there is a rising or falling, and so I wrote the code, I do not receive errors but the MT4 crashes!



#include <stdlib.mqh>
#include <stderror.mqh>

//--- indicator settings
#property indicator_chart_window
#property indicator_buffers 2

#property indicator_type1 DRAW_ARROW
#property indicator_width1 1
#property indicator_color1 0xFFAA00
#property indicator_label1 "Sell"

#property indicator_type2 DRAW_ARROW
#property indicator_width2 1
#property indicator_color2 0x0000FF
#property indicator_label2 "Buy"

//--- indicator buffers
double Buffer1[];
double Buffer2[];

extern double DistanzaIcone = 1;
datetime time_alert; //used when sending alert
extern bool Audible_Alerts = true;
double myPoint; //initialized in OnInit

void myAlert(string type, string message)
  {
   if(type == "print")
      Print(message);
   else if(type == "error")
     {
      Print(type+" | Test @ "+Symbol()+","+Period()+" | "+message);
     }
   else if(type == "order")
     {
     }
   else if(type == "modify")
     {
     }
   else if(type == "indicator")
     {
      if(Audible_Alerts) Alert(type+" | Test2 @ "+Symbol()+","+Period()+" | "+message);
     }
  }

//+------------------------------------------------------------------+
//| ZigZag()                                                         |
//+------------------------------------------------------------------+
datetime ZigZag(int y,int j)
  {
   int k=0;
   double ZigZag0=0;
   datetime time=0;
  
   for(int i=y;i<y+50;i++) //Passo in rassegna le ultime 500 candele...
     {
      ZigZag0=iCustom(Symbol(),0,"ZigNeutro",480,4,i); //Prendo il valore dello ZigZag...
      
      if(ZigZag0!=0.0&&k<j) {k++;continue;} //Se il valore dello ZigZag non è quello che mi interessa lo scarto...
      
      if(ZigZag0!=0.0&&time==0) {time=Time[i];break;} //Se il valore dello ZigZag è quello che mi interessa, salvo l'orario con l'indice.
     }
   return(time);
  }
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {   
   IndicatorBuffers(2);
   
   SetIndexBuffer    (0, Buffer1);
   SetIndexEmptyValue(0, 0);
   SetIndexArrow     (0, 242);
   
   SetIndexBuffer    (1, Buffer2);
   SetIndexEmptyValue(1, 0);
   SetIndexArrow     (1, 241);
   
   //initialize myPoint
   myPoint = Point();
   if(Digits() == 5 || Digits() == 3)
     {
      myPoint *= 10;
     }
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime& time[],
                const double& open[],
                const double& high[],
                const double& low[],
                const double& close[],
                const long& tick_volume[],
                const long& volume[],
                const int& spread[])
  {
   int limit = rates_total - prev_calculated;
   //--- counting from 0 to rates_total
   ArraySetAsSeries(Buffer1, true);
   ArraySetAsSeries(Buffer2, true);
   //--- initial zero
   if(prev_calculated < 1)
     {
      ArrayInitialize(Buffer1, 0);
      ArrayInitialize(Buffer2, 0);
     }
   else
      limit++;
   
   //--- main loop
   for(int i = limit-1; i >= 0; i--)
     {
      if (i >= MathMin(5000-1, rates_total-1-50)) continue;
      
         
      //Indicator Buffer 1
      if(ZigZag(i,0) < ZigZag(i,1) // downhill
      && iRSI(NULL, PERIOD_CURRENT, 2, PRICE_CLOSE, 2+i) < 50
      && iRSI(NULL, PERIOD_CURRENT, 2, PRICE_CLOSE, 1+i) > 50 
      )
        {
         Buffer1[i] = High[i] + DistanzaIcone * myPoint;; 
         if(i == 0 && Time[0] != time_alert) { myAlert("indicator", "Sell"); time_alert = Time[0]; } 
        }
      
      //Indicator Buffer 2
      if(false 
      )
        {
         Buffer2[i] = Low[i] - DistanzaIcone * myPoint; 
         if(i == 0 && Time[0] != time_alert) { myAlert("indicator", "Buy"); time_alert = Time[0]; } 
        }
      
     }
   return(rates_total);
  }
//+------------------------------------------------------------------+
 

I  cannot see any reason for MT4 to crash

      if(ZigZag(i,0) < ZigZag(i,1) // downhill

you are comparing 2 datetimes so it doesn't indicate direction.

as long as there are 2 zigzag points within 50 bars, this will return true.

 

Hello everyone! I'm having trouble plotting the histogram backwards. The value I already found, but I can't plot the previous ones with the same value. Follow the code for that.


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime & time[],
                const double & open[],
                const double & high[],
                const double & low[],
                const double & close[],
                const long & tick_volume[],
                const long & volume[],
                const int &spread[]) {
   int last_zz_pos = -1;
   if (prev_calculated == 0)
      ArrayInitialize(zzUp, EMPTY_VALUE);
   else {
      for (int pos = prev_calculated - 1; pos >= 0; --pos) {
         if (zz[pos] != EMPTY_VALUE) {
            last_zz_pos = pos;
            break;
         }
      }
   }
   zigzag_handle = iCustom(_Symbol, _Period, "Examples\\ZigZag", ExtDepth, ExtDeviation, ExtBackstep);
   if(zigzag_handle == INVALID_HANDLE) {
      PrintFormat("Falha ao criar o manipulador do indicador ZigZag para o símbolo %s/%s, código de erro %d", _Symbol, GetLastError());
      PrintFormat("tentando carregar o indicador ZigZag para o símbolo %s/%s, código de erro %d", _Symbol, GetLastError());
      zigzag_handle = iCustom(_Symbol, _Period, "ZigZags\\ZigZag", ExtDepth, ExtDeviation, ExtBackstep);
      if(zigzag_handle == INVALID_HANDLE) {
         Alert("Instale o indicador 'ZigZag'");
         return(INIT_FAILED);
      }
   }


   for (int pos = prev_calculated - 1; pos < rates_total; ++pos) {
      //LastBar  = (datetime) SeriesInfoInteger(_Symbol, _Period, SERIES_BARS_COUNT);
      //if(ZigZag(pos, 0) < ZigZag(pos, 1) // downhill

      if (CopyBuffer(zigzag_handle, 0, rates_total - pos - 1, 1, buffer) == 1) {
         if (buffer[0] > 0 || pos == 0) {
            zz[pos] = buffer[0];

            if (last_zz_pos != -1) {
               zzDown[pos] = EMPTY_VALUE;
               zzUp[pos] = EMPTY_VALUE;
               if (zz[last_zz_pos] < zz[pos]) {
                  //zzDown[pos] = NormalizeDouble((zz[pos] - zz[last_zz_pos]) * digits, 1);
                  prc_h[pos] = NormalizeDouble((zz[pos] - zz[last_zz_pos]) * digits, 1);
                  zzUp[pos - 1] = prc_h[pos];

                  //LastBar = ZigZag(pos, 5, zigzag_handle);
                  //Print(iTime(_Symbol, PERIOD_CURRENT, pos));

                  //value = ZigZag_(1, zigzag_handle) - ZigZag_(2, zigzag_handle) / 60;
                  //Print(value);

               } else {
                  //zzUp[pos] = NormalizeDouble((zz[last_zz_pos] - zz[pos]) * digits, 1);
                  prc_l[pos] = NormalizeDouble((zz[last_zz_pos] - zz[pos]) * digits, 1);
                  zzDown[pos - 1] = prc_l[pos];

               }
            }
            last_zz_pos = pos;
         } else {
            zzDown[pos] = EMPTY_VALUE;
            zzUp[pos] = EMPTY_VALUE;
            if (zzUp[pos - 1] != EMPTY_VALUE) {
               zzUp[pos] = zzUp[pos - 1];
               //zzUp[pos - 2] = zzUp[pos];
               //zzUp[pos - 3] = zzUp[pos];
               //zzUp[pos - 4] = zzUp[pos];
            } else {
               zzDown[pos] = zzDown[pos - 1];
            }
         }
      }
   }
   return rates_total;
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
datetime ZigZag(int y, int j, int handle) {
   int k = 0;
   datetime time = 0;

   for(int i = y + 0; i < y + 1440; i++) {
      if(handle != 0 && k < j) {
         k++;
         continue;
      }
      if(handle != 0 && time == 0) {
         time  = iTime(_Symbol, _Period, i);
         break;
      }
   }
   return(time);
}
 
fly7680:

 Another example to be able to understand and find a solution. According to the time of the Zig Zag, I want to determine whether the firsttwo points if there is a rising or falling, and so I wrote the code, I do not receive errors but theMT4crashes!



I was not particularly able to use the code, could you explain what it does better?

I need to do a bar count between one zigzag and the other.


//+------------------------------------------------------------------+
//| ZigZag()                                                         |
//+------------------------------------------------------------------+
datetime ZigZag(int y,int j)
  {
   int k=0;
   double ZigZag0=0;
   datetime time=0;
  
   for(int i=y;i<y+50;i++) //Passo in rassegna le ultime 500 candele...
     {
      ZigZag0=iCustom(Symbol(),0,"ZigNeutro",480,4,i); //Prendo il valore dello ZigZag...
      
      if(ZigZag0!=0.0&&k<j) {k++;continue;} //Se il valore dello ZigZag non è quello che mi interessa lo scarto...
      
      if(ZigZag0!=0.0&&time==0) {time=Time[i];break;} //Se il valore dello ZigZag è quello che mi interessa, salvo l'orario con l'indice.
     }
   return(time);
  }
 

And I also noticed that my zigzag and the volume histogram is not updating correctly.



//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime & time[],
                const double & open[],
                const double & high[],
                const double & low[],
                const double & close[],
                const long & tick_volume[],
                const long & volume[],
                const int &spread[]) {
   int last_zz_pos = -1;
   if (prev_calculated == 0)
      ArrayInitialize(zzUp, EMPTY_VALUE);
   else {
      for (int pos = prev_calculated - 1; pos >= 0; --pos) {
         if (zz[pos] != EMPTY_VALUE) {
            last_zz_pos = pos;
            break;
         }
      }
   }
   zigzag_handle = iCustom(_Symbol, _Period, "Examples\\ZigZag", ExtDepth, ExtDeviation, ExtBackstep);
   if(zigzag_handle == INVALID_HANDLE) {
      PrintFormat("Falha ao criar o manipulador do indicador ZigZag para o símbolo %s/%s, código de erro %d", _Symbol, GetLastError());
      PrintFormat("tentando carregar o indicador ZigZag para o símbolo %s/%s, código de erro %d", _Symbol, GetLastError());
      zigzag_handle = iCustom(_Symbol, _Period, "ZigZags\\ZigZag", ExtDepth, ExtDeviation, ExtBackstep);
      if(zigzag_handle == INVALID_HANDLE) {
         Alert("Instale o indicador 'ZigZag'");
         return(INIT_FAILED);
      }
   }


   for (int pos = prev_calculated - 1; pos < rates_total; ++pos) {
      //LastBar  = (datetime) SeriesInfoInteger(_Symbol, _Period, SERIES_BARS_COUNT);
      //if(ZigZag(pos, 0) < ZigZag(pos, 1) // downhill

      if (CopyBuffer(zigzag_handle, 0, rates_total - pos - 1, 1, buffer) == 1) {
         if (buffer[0] > 0 || pos == 0) {
            zz[pos] = buffer[0];

            if (last_zz_pos != -1) {
               zzDown[pos] = EMPTY_VALUE;
               zzUp[pos] = EMPTY_VALUE;
               if (zz[last_zz_pos] < zz[pos]) {
                  //zzDown[pos] = NormalizeDouble((zz[pos] - zz[last_zz_pos]) * digits, 1);
                  prc_h[pos] = NormalizeDouble((zz[pos] - zz[last_zz_pos]) * digits, 1);
                  zzUp[pos - 1] = prc_h[pos];

                  //LastBar = ZigZag(pos, 5, zigzag_handle);
                  //Print(iTime(_Symbol, PERIOD_CURRENT, pos));

                  //value = ZigZag_(1, zigzag_handle) - ZigZag_(2, zigzag_handle) / 60;
                  //Print(value);

               } else {
                  //zzUp[pos] = NormalizeDouble((zz[last_zz_pos] - zz[pos]) * digits, 1);
                  prc_l[pos] = NormalizeDouble((zz[last_zz_pos] - zz[pos]) * digits, 1);
                  zzDown[pos - 1] = prc_l[pos];

               }
            }
            last_zz_pos = pos;
         } else {
            zzDown[pos] = EMPTY_VALUE;
            zzUp[pos] = EMPTY_VALUE;
            if (zzUp[pos - 1] != EMPTY_VALUE) {
               zzUp[pos] = zzUp[pos - 1];
               //zzUp[pos - 2] = zzUp[pos];
               //zzUp[pos - 3] = zzUp[pos];
               //zzUp[pos - 4] = zzUp[pos];
            } else {
               zzDown[pos] = zzDown[pos - 1];
            }
         }
      }
   }
   return rates_total;
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
datetime ZigZag(int y, int j, int handle) {
   int k = 0;
   datetime time = 0;

   for(int i = y + 0; i < y + 1440; i++) {
      if(handle != 0 && k < j) {
         k++;
         continue;
      }
      if(handle != 0 && time == 0) {
         time  = iTime(_Symbol, _Period, i);
         break;
      }
   }
   return(time);
}

I thank the help of all you!


Only update when I recompile the code, as shown below



Reason: