Fractals Corridor Breakdown

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

//|                                  Fractals Corridor Breakdown.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.00"

#property indicator_chart_window

#property indicator_buffers 9

#property indicator_plots   3

//--- plot Up

#property indicator_label1  "Up"

#property indicator_type1   DRAW_ARROW

#property indicator_color1  clrRed

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot Down

#property indicator_label2  "Down"

#property indicator_type2   DRAW_ARROW

#property indicator_color2  clrBlue

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- plot Breakdown

#property indicator_label3  "Breakdown"

#property indicator_type3   DRAW_COLOR_CANDLES

#property indicator_color3  clrRed,clrBlue

#property indicator_style3  STYLE_SOLID

#property indicator_width3  1

//--- input parameters

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

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

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

//--- indicator buffers

double   UpBuffer[];

double   DownBuffer[];

double   BreakdownOpenBuffer[];

double   BreakdownHighBuffer[];

double   BreakdownLowBuffer[];

double   BreakdownCloseBuffer[];

double   BreakdownColors[];

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_up=0.0;

double   m_price_down=0.0;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- indicator buffers mapping

   SetIndexBuffer(0,UpBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,DownBuffer,INDICATOR_DATA);

   SetIndexBuffer(2,BreakdownOpenBuffer,INDICATOR_DATA);

   SetIndexBuffer(3,BreakdownHighBuffer,INDICATOR_DATA);

   SetIndexBuffer(4,BreakdownLowBuffer,INDICATOR_DATA);

   SetIndexBuffer(5,BreakdownCloseBuffer,INDICATOR_DATA);

   SetIndexBuffer(6,BreakdownColors,INDICATOR_COLOR_INDEX);

   SetIndexBuffer(7,FractalUpBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(8,FractalDownBuffer,INDICATOR_CALCULATIONS);

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

   PlotIndexSetDouble(2,PLOT_EMPTY_VALUE,0.0);

   //--- set the display of the symbol 

   PlotIndexSetString(2,PLOT_LABEL,"Open;"+"High;"+"Low;"+"Close"); 

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

        {

         UpBuffer[i]=0.0;

         UpBuffer[i]=0.0;

         m_price_up=0.0;

         m_price_down=0.0;

         BreakdownOpenBuffer[i]=0.0;

         BreakdownHighBuffer[i]=0.0;

         BreakdownLowBuffer[i]=0.0;

         BreakdownCloseBuffer[i]=0.0;

         BreakdownColors[i]=0.0;

        }

     }

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

     {

      if(i>=rates_total-3)

        {

         if(m_price_up!=0.0)

            UpBuffer[i]=m_price_up;

         else

            UpBuffer[i]=0.0;

         if(m_price_down!=0.0)

            DownBuffer[i]=m_price_down;

         else

            DownBuffer[i]=0.0;

        }

      else

        {

         int d=0;

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

            m_price_up=FractalUpBuffer[i];

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

            m_price_down=FractalDownBuffer[i];

         UpBuffer[i]=m_price_up;

         DownBuffer[i]=m_price_down;

        }

      //--- COLOR_CANDLES

      BreakdownOpenBuffer[i]=0.0;

      BreakdownHighBuffer[i]=0.0;

      BreakdownLowBuffer[i]=0.0;

      BreakdownCloseBuffer[i]=0.0;

      BreakdownColors[i]=0.0;

      if(low[i]<UpBuffer[i] && high[i]>UpBuffer[i])

        {

         BreakdownOpenBuffer[i]=open[i];

         BreakdownHighBuffer[i]=high[i];

         BreakdownLowBuffer[i]=low[i];

         BreakdownCloseBuffer[i]=close[i];

         BreakdownColors[i]=0.0;

        }

      else

         if(low[i]<DownBuffer[i] && high[i]>DownBuffer[i])

           {

            BreakdownOpenBuffer[i]=open[i];

            BreakdownHighBuffer[i]=high[i];

            BreakdownLowBuffer[i]=low[i];

            BreakdownCloseBuffer[i]=close[i];

            BreakdownColors[i]=1.0;

           }

     }

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