iFractals

 
Igor Knyazkov:
Здравствуйте. Покажите, пожалуйста, пример работы с iFractals. А именно: нужна проверка, есть ли фрактал на третьей свече.

Сначала нужно заглянуть в код индикатора Fractals - чтобы понять, что именно он записывает в индикаторные буферы:

   for(i=limit;i<rates_total-3 && !IsStopped();i++)
     {
      //---- Upper Fractal
      if(high[i]>high[i+1] && high[i]>high[i+2] && high[i]>=high[i-1] && high[i]>=high[i-2])
         ExtUpperBuffer[i]=high[i];
      else ExtUpperBuffer[i]=EMPTY_VALUE;

      //---- Lower Fractal
      if(low[i]<low[i+1] && low[i]<low[i+2] && low[i]<=low[i-1] && low[i]<=low[i-2])
         ExtLowerBuffer[i]=low[i];
      else ExtLowerBuffer[i]=EMPTY_VALUE;
     }

Как видите, если нет фрактала, то в индикаторный буфер записывается значение

EMPTY_VALUE

Пустое значение в индикаторном буфере

DBL_MAX 

 

Значит в советнике нужно проверять такое условие: если в индикаторном буфере храниться не DBL_MAX - значит это фрактал.

Код советника:

//+------------------------------------------------------------------+
//|                                                    iFractals.mq5 |
//|                              Copyright © 2016, Vladimir Karputov |
//|                                           http://wmua.ru/slesar/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2016, Vladimir Karputov"
#property link      "http://wmua.ru/slesar/"
#property description "Search of a fractal on the set bar"
#property version   "1.000"
//--- input parameters
input int   bar_numder=3;                 // bar on which you want to find fractal
//---
int         handle_iFractals;             // variable for storing the handle of the iFractals indicator
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create handle of the indicator iFractals
   handle_iFractals=iFractals(Symbol(),Period());
//--- if the handle is not created
   if(handle_iFractals==INVALID_HANDLE)
     {
      //--- tell about the failure and output the error code
      PrintFormat("Failed to create handle of the iFractals indicator for the symbol %s/%s, error code %d",
                  Symbol(),
                  EnumToString(Period()),
                  GetLastError());
      //--- the indicator is stopped early
      return(INIT_FAILED);
     }
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   Comment("");
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//--- find fractal in UPPER
   double upper=iFractalsGet(UPPER_LINE,bar_numder);
//--- find fractal in LOWER
   double lower=iFractalsGet(LOWER_LINE,bar_numder);
//---
   string text="";
   if(upper!=DBL_MAX)
      text+="On bar № "+IntegerToString(bar_numder)+" there is the UPPER fractal"+"\n";
   else
      text+="On bar № "+IntegerToString(bar_numder)+" there is no UPPER fractal"+"\n";

   if(lower!=DBL_MAX)
      text+="On bar № "+IntegerToString(bar_numder)+" there is the LOWER fractal"+"\n";
   else
      text+="On bar № "+IntegerToString(bar_numder)+" there is no LOWER fractal"+"\n";

   Comment(text);
  }
//+------------------------------------------------------------------+
//| Get value of buffers for the iFractals                           |
//|  the buffer numbers are the following:                           |
//|   0 - UPPER_LINE, 1 - LOWER_LINE                                 |
//+------------------------------------------------------------------+
double iFractalsGet(const int buffer,const int index)
  {
   double Fractals[1];
//--- reset error code
   ResetLastError();
//--- fill a part of the iFractalsBuffer array with values from the indicator buffer that has 0 index
   if(CopyBuffer(handle_iFractals,buffer,index,1,Fractals)<0)
     {
      //--- if the copying fails, tell the error code
      PrintFormat("Failed to copy data from the iFractals indicator, error code %d",GetLastError());
      //--- quit with zero result - it means that the indicator is considered as not calculated
      return(0.0);
     }
   return(Fractals[0]);
  }
//+------------------------------------------------------------------+

 

Результат работы советника:

Поиск фрактала 

На баре номер "3" есть верхний фрактал, о чём есть сообщение (выделено цветом на графике). 

Файлы:
iFractals.mq5  8 kb
 
Vladimir Karputov:

Сначала нужно заглянуть в код индикатора Fractals - чтобы понять, что именно он записывает в индикаторные буферы:

   for(i=limit;i<rates_total-3 && !IsStopped();i++)
     {
      //---- Upper Fractal
      if(high[i]>high[i+1] && high[i]>high[i+2] && high[i]>=high[i-1] && high[i]>=high[i-2])
         ExtUpperBuffer[i]=high[i];
      else ExtUpperBuffer[i]=EMPTY_VALUE;

      //---- Lower Fractal
      if(low[i]<low[i+1] && low[i]<low[i+2] && low[i]<=low[i-1] && low[i]<=low[i-2])
         ExtLowerBuffer[i]=low[i];
      else ExtLowerBuffer[i]=EMPTY_VALUE;
     }

Как видите, если нет фрактала, то в индикаторный буфер записывается значение

EMPTY_VALUE

Пустое значение в индикаторном буфере

DBL_MAX 

 

Значит в советнике нужно проверять такое условие: если в индикаторном буфере храниться не DBL_MAX - значит это фрактал.

Код советника:

//+------------------------------------------------------------------+
//|                                                    iFractals.mq5 |
//|                              Copyright © 2016, Vladimir Karputov |
//|                                           http://wmua.ru/slesar/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2016, Vladimir Karputov"
#property link      "http://wmua.ru/slesar/"
#property description "Search of a fractal on the set bar"
#property version   "1.000"
//--- input parameters
input int   bar_numder=3;                 // bar on which you want to find fractal
//---
int         handle_iFractals;             // variable for storing the handle of the iFractals indicator
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create handle of the indicator iFractals
   handle_iFractals=iFractals(Symbol(),Period());
//--- if the handle is not created
   if(handle_iFractals==INVALID_HANDLE)
     {
      //--- tell about the failure and output the error code
      PrintFormat("Failed to create handle of the iFractals indicator for the symbol %s/%s, error code %d",
                  Symbol(),
                  EnumToString(Period()),
                  GetLastError());
      //--- the indicator is stopped early
      return(INIT_FAILED);
     }
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   Comment("");
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//--- find fractal in UPPER
   double upper=iFractalsGet(UPPER_LINE,bar_numder);
//--- find fractal in LOWER
   double lower=iFractalsGet(LOWER_LINE,bar_numder);
//---
   string text="";
   if(upper!=DBL_MAX)
      text+="On bar № "+IntegerToString(bar_numder)+" there is the UPPER fractal"+"\n";
   else
      text+="On bar № "+IntegerToString(bar_numder)+" there is no UPPER fractal"+"\n";

   if(lower!=DBL_MAX)
      text+="On bar № "+IntegerToString(bar_numder)+" there is the LOWER fractal"+"\n";
   else
      text+="On bar № "+IntegerToString(bar_numder)+" there is no LOWER fractal"+"\n";

   Comment(text);
  }
//+------------------------------------------------------------------+
//| Get value of buffers for the iFractals                           |
//|  the buffer numbers are the following:                           |
//|   0 - UPPER_LINE, 1 - LOWER_LINE                                 |
//+------------------------------------------------------------------+
double iFractalsGet(const int buffer,const int index)
  {
   double Fractals[1];
   ArraySetAsSeries(Fractals,true);
//--- reset error code
   ResetLastError();
//--- fill a part of the iFractalsBuffer array with values from the indicator buffer that has 0 index
   if(CopyBuffer(handle_iFractals,buffer,index,1,Fractals)<0)
     {
      //--- if the copying fails, tell the error code
      PrintFormat("Failed to copy data from the iFractals indicator, error code %d",GetLastError());
      //--- quit with zero result - it means that the indicator is considered as not calculated
      return(0.0);
     }
   return(Fractals[0]);
  }
//+------------------------------------------------------------------+


Результат работы советника:

 

На баре номер "3" есть верхний фрактал, о чём есть сообщение (выделено цветом на графике). 

Спасибо!
 

Обратите внимание - была ошибка, я перевставил код в предыдущем сообщении:

//+------------------------------------------------------------------+
//| Get value of buffers for the iFractals                           |
//|  the buffer numbers are the following:                           |
//|   0 - UPPER_LINE, 1 - LOWER_LINE                                 |
//+------------------------------------------------------------------+
double iFractalsGet(const int buffer,const int index)
  {
   double Fractals[1];
//--- reset error code
   ResetLastError();
//--- fill a part of the iFractalsBuffer array with values from the indicator buffer that has 0 index
   if(CopyBuffer(handle_iFractals,buffer,index,1,Fractals)<0)
     {
      //--- if the copying fails, tell the error code
      PrintFormat("Failed to copy data from the iFractals indicator, error code %d",GetLastError());
      //--- quit with zero result - it means that the indicator is considered as not calculated
      return(0.0);
     }
   return(Fractals[0]);
  }
//+------------------------------------------------------------------+

Так расчёты выполняются быстрее и экономичнее.

Причина обращения: