Fractals Channel

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

//|                                             Fractals Channel.mq5 |

//|                              Copyright © 2020, Vladimir Karputov |

//|                     https://www.mql5.com/ru/market/product/43516 |

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

#property copyright "Copyright © 2020, Vladimir Karputov"

#property link      "https://www.mql5.com/ru/market/product/43516"

#property version   "1.000"

#property indicator_chart_window

#property indicator_buffers 2

#property indicator_plots   2

//--- plot Arrows

#property indicator_label1  "Upper"

#property indicator_type1   DRAW_SECTION

#property indicator_color1  clrRed

#property indicator_style1  STYLE_DASH

#property indicator_width1  2



#property indicator_label2  "Lower"

#property indicator_type2   DRAW_SECTION

#property indicator_color2  clrBlue

#property indicator_style2  STYLE_DASH

#property indicator_width2  2

//--- input parameters

input ushort   InpDayMaxCode  = 83;    // "Day max" symbol code from the Wingdings font

input ushort   InpDayMinCode  = 83;    // "Day min" symbol code from the Wingdings font

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

//--- an indicator buffers for the plots

double   UpperBuffer[];

double   LowerBuffer[];

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

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- indicator buffers mapping

   SetIndexBuffer(0,UpperBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,LowerBuffer,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

      return(INIT_FAILED);

     }

//---

   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);

//--- 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(UpperBuffer,LowerBuffer,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++)

         {

          UpperBuffer[i]=0.0;

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

             UpperBuffer[i]=m_price_day_max;

          else

             UpperBuffer[i]=0.0;

          if(m_price_day_min!=0.0)

             LowerBuffer[i]=m_price_day_min;

          else

             LowerBuffer[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];



          UpperBuffer[i]=m_price_day_max;

          LowerBuffer[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

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---