Fractals Corridor

Author: Copyright © 2020-2021, Vladimir Karputov
Indicators Used
Fractals
0 Views
0 Downloads
0 Favorites
Fractals Corridor
ÿþ//+------------------------------------------------------------------+

//|                                           Fractals  Corridor.mq5 |

//|                         Copyright © 2020-2021, Vladimir Karputov |

//|                      https://www.mql5.com/en/users/barabashkakvn |

//+------------------------------------------------------------------+

#property copyright "Copyright © 2020-2021, Vladimir Karputov"

#property link      "https://www.mql5.com/en/users/barabashkakvn"

#property version   "1.003"

#property indicator_chart_window

#property indicator_buffers 4

#property indicator_plots   2

//--- plot Prev Up

#property indicator_label1  "Prev Up"

#property indicator_type1   DRAW_ARROW

#property indicator_color1  clrRed

#property indicator_width1  1

//--- plot Prev Down

#property indicator_label2  "Prev Down"

#property indicator_type2   DRAW_ARROW

#property indicator_color2  clrBlue

#property indicator_width2  1

//--- input parameters

input ushort   InpDayMaxCode  = 119;   // "Prev Up" symbol code from the Wingdings font

input ushort   InpDayMinCode  = 119;   // "Prev Down" symbol code from the Wingdings font

input ushort   InpShift       = 15;    // Vertical shift of arrows in pixels

//--- an indicator buffers for the plots

double   PrevUpBuffer[];

double   PrevDownBuffer[];

double   FractalUpBuffer[];

double   FractalDownBuffer[];

//---

int      handle_iFractals;                // variable for storing the handle of the iFractals indicator

int      bars_calculated   = 0;           // we will keep the number of values in the Fractals indicator

double   m_price_day_max   = 0.0;

double   m_price_day_min   = 0.0;

bool     m_init_error      = false;       // error on InInit

//+------------------------------------------------------------------+

//| Custom indicator initialization function                         |

//+------------------------------------------------------------------+

int OnInit()

  {

//--- indicator buffers mapping

   SetIndexBuffer(0,PrevUpBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,PrevDownBuffer,INDICATOR_DATA);

   SetIndexBuffer(2,FractalUpBuffer,INDICATOR_DATA);

   SetIndexBuffer(3,FractalDownBuffer,INDICATOR_DATA);

//--- define the symbol code for drawing in PLOT_ARROW

   PlotIndexSetInteger(0,PLOT_ARROW,InpDayMaxCode);

   PlotIndexSetInteger(1,PLOT_ARROW,InpDayMinCode);

//--- set the vertical shift of arrows in pixels

   PlotIndexSetInteger(0,PLOT_ARROW_SHIFT,-(int)(InpShift));

   PlotIndexSetInteger(1,PLOT_ARROW_SHIFT,(int)(InpShift));

//--- set as an empty value 0.0

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);

   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,0.0);

//--- 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

      m_init_error=true;

      return(INIT_SUCCEEDED);

     }

//---

   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[])

  {

   if(rates_total<10)

      return(0);

   if(m_init_error)

      return(0);

//--- number of values copied from the iFractals indicator

   int values_to_copy;

//--- determine the number of values calculated in the indicator

   int calculated=BarsCalculated(handle_iFractals);

   if(calculated<=0)

     {

      PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError());

      return(0);

     }

//--- if it is the first start of calculation of the indicator or if the number of values in the iFractals indicator changed

//---or if it is necessary to calculated the indicator for two or more bars (it means something has changed in the price history)

   if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1)

     {

      //--- if the FractalUpBuffer array is greater than the number of values in the iFractals indicator for symbol/period, then we don't copy everything

      //--- otherwise, we copy less than the size of indicator buffers

      if(calculated>rates_total)

         values_to_copy=rates_total;

      else

         values_to_copy=calculated;

     }

   else

     {

      //--- it means that it's not the first time of the indicator calculation, and since the last call of OnCalculate()

      //--- for calculation not more than one bar is added

      values_to_copy=(rates_total-prev_calculated)+1;

     }

//--- fill the FractalUpBuffer and FractalDownBuffer arrays with values from the Fractals indicator

//--- if FillArrayFromBuffer returns false, it means the information is nor ready yet, quit operation

   if(!FillArraysFromBuffers(FractalUpBuffer,FractalDownBuffer,handle_iFractals,values_to_copy))

      return(0);

//--- memorize the number of values in the Fractals indicator

   bars_calculated=calculated;

//---

   int limit=prev_calculated-4;

   if(prev_calculated==0)

     {

      limit=3;

      for(int i=0; i<=limit; i++)

        {

         PrevUpBuffer[i]=0.0;

         PrevUpBuffer[i]=0.0;

         m_price_day_max=0.0;

         m_price_day_min=0.0;

        }

     }

   for(int i=limit; i<rates_total; i++)

     {

      if(i>=rates_total-3)

        {

         if(m_price_day_max!=0.0)

            PrevUpBuffer[i]=m_price_day_max;

         else

            PrevUpBuffer[i]=0.0;

         if(m_price_day_min!=0.0)

            PrevDownBuffer[i]=m_price_day_min;

         else

            PrevDownBuffer[i]=0.0;

        }

      else

        {

         int d=0;

         if(FractalUpBuffer[i]!=EMPTY_VALUE && FractalUpBuffer[i]!=0.0)

            m_price_day_max=FractalUpBuffer[i];

         if(FractalDownBuffer[i]!=EMPTY_VALUE && FractalDownBuffer[i]!=0.0)

            m_price_day_min=FractalDownBuffer[i];

         PrevUpBuffer[i]=m_price_day_max;

         PrevDownBuffer[i]=m_price_day_min;

        }

     }

//--- return value of prev_calculated for next call

   return(rates_total);

  }

//+------------------------------------------------------------------+

//| Filling indicator buffers from the iFractals indicator           |

//+------------------------------------------------------------------+

bool FillArraysFromBuffers(double &up_arrows[],        // indicator buffer for up arrows

                           double &down_arrows[],      // indicator buffer for down arrows

                           int ind_handle,             // handle of the iFractals indicator

                           int amount                  // number of copied values

                          )

  {

//--- reset error code

   ResetLastError();

//--- fill a part of the FractalUpBuffer array with values from the indicator buffer that has 0 index

   if(CopyBuffer(ind_handle,0,0,amount,up_arrows)<0)

     {

      //--- if the copying fails, tell the error code

      PrintFormat("Failed to copy data from the iFractals indicator to the FractalUpBuffer array, error code %d",

                  GetLastError());

      //--- quit with zero result - it means that the indicator is considered as not calculated

      return(false);

     }

//--- fill a part of the FractalDownBuffer array with values from the indicator buffer that has index 1

   if(CopyBuffer(ind_handle,1,0,amount,down_arrows)<0)

     {

      //--- if the copying fails, tell the error code

      PrintFormat("Failed to copy data from the iFractals indicator to the FractalDownBuffer array, error code %d",

                  GetLastError());

      //--- quit with zero result - it means that the indicator is considered as not calculated

      return(false);

     }

//--- everything is fine

   return(true);

  }

//+------------------------------------------------------------------+

//| Indicator deinitialization function                              |

//+------------------------------------------------------------------+

void OnDeinit(const int reason)

  {

   if(handle_iFractals!=INVALID_HANDLE)

      IndicatorRelease(handle_iFractals);

//--- clear the chart after deleting the indicator

   Comment("");

  }

//+------------------------------------------------------------------

Comments