Download MetaTrader 5

Minutes between one point and another Zig Zag

To add comments, please log in or register
fly7680
543
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

//+------------------------------------------------------------------+
//| 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);
}
//+------------------------------------------------------------------+



Keith Watford
Moderator
9644
Keith Watford  
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

fly7680
543
fly7680  

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!



fly7680
543
fly7680  
specifying Buffer1 [1+i] should not just go out in the first candle?
whroeder1
14786
whroeder1  
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...
fly7680
543
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 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);
  }
//+------------------------------------------------------------------+
Keith Watford
Moderator
9644
Keith Watford  

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.

To add comments, please log in or register