D1 Fractal Levels

Author: Copyright © 2021, Vladimir Karputov
Indicators Used
Fractals
2 Views
0 Downloads
0 Favorites
D1 Fractal Levels
ÿþ//+------------------------------------------------------------------+

//|                                            D1 Fractal Levels.mq5 |

//|                              Copyright © 2021, Vladimir Karputov |

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

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

#property copyright "Copyright © 2021, Vladimir Karputov"

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

#property version   "1.000"

#property indicator_chart_window

#property indicator_buffers 0

#property indicator_plots   0

//--- input parameters

input group             "Upper Line"

input string               InpUpperName   = "Upper Line";   // Line name

input color                InpUpperColor  = clrRed;         // Line color

input ENUM_LINE_STYLE      InpUpperStyle  = STYLE_DASH;     // Line style

input int                  InpUpperWidth  = 3;              // Line width

input group             "Lower Line"

input string               InpLowerName   = "Lower Line";   // Line name

input color                InpLowerColor  = clrBlue;        // Line color

input ENUM_LINE_STYLE      InpLowerStyle  = STYLE_DASH;     // Line style

input int                  InpLowerWidth  = 3;              // Line width

//---

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

datetime m_prev_bars       = 0;        // "0" -> D'1970.01.01 00:00';

string   m_prefix          = "D1FL_";  // prefix

bool     m_init_error      = false;    // error on InInit

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

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

      m_init_error=true;

      return(INIT_SUCCEEDED);

     }

//---

   return(INIT_SUCCEEDED);

  }

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

//| Indicator deinitialization function                              |

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

void OnDeinit(const int reason)

  {

//---

   ObjectsDeleteAll(ChartID(),m_prefix,0,OBJ_HLINE);

  }

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

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

      return(0);

//--- we work only at the time of the birth of new bar

   datetime time_d1[];

   ArraySetAsSeries(time_d1,true);

   int start_pos=0,count=3;

   if(CopyTime(Symbol(),PERIOD_D1,start_pos,count,time_d1)!=count)

      return(0);

   if(time_d1[0]==m_prev_bars)

      return(rates_total);

   m_prev_bars=time_d1[0];

//---

   double upper[],lower[];

   ArraySetAsSeries(upper,true);

   ArraySetAsSeries(lower,true);

   if(!iGetArray(handle_iFractals,UPPER_LINE,time_d1[1],time_d1[0],upper) || !iGetArray(handle_iFractals,LOWER_LINE,time_d1[1],time_d1[0],lower))

      return(0);

//--- upper

   double price_max=DBL_MIN;

   int size_upper=ArraySize(upper);

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

     {

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

         if(upper[i]>price_max)

            price_max=upper[i];

     }

   if(ObjectFind(ChartID(),m_prefix+"Upper")<0)

      HLineCreate(ChartID(),m_prefix+"Upper",0,price_max,InpUpperColor,InpUpperStyle,InpUpperWidth);

   else

      HLineMove(ChartID(),m_prefix+"Upper",price_max);

//--- lower

   double price_min=DBL_MAX;

   int size_lower=ArraySize(lower);

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

     {

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

         if(lower[i]<price_min)

            price_min=lower[i];

     }

   if(ObjectFind(ChartID(),m_prefix+"Lower")<0)

      HLineCreate(ChartID(),m_prefix+"Lower",0,price_min,InpLowerColor,InpLowerStyle,InpLowerWidth);

   else

      HLineMove(ChartID(),m_prefix+"Lower",price_min);

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

   return(rates_total);

  }

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

//| Get value of buffers                                             |

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

bool iGetArray(const int handle,const int buffer,const datetime start_time,

               const datetime stop_time,double &arr_buffer[])

  {

   bool result=true;

   if(!ArrayIsDynamic(arr_buffer))

     {

      PrintFormat("ERROR! EA: %s, FUNCTION: %s, this a no dynamic array!",__FILE__,__FUNCTION__);

      return(false);

     }

   ArrayFree(arr_buffer);

//--- reset error code

   ResetLastError();

//--- fill a part of the iBands array with values from the indicator buffer

   int copied=CopyBuffer(handle,buffer,start_time,stop_time,arr_buffer);

   if(copied<0)

     {

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

      PrintFormat("ERROR! EA: %s, FUNCTION: %s, copied: %d, error code %d",

                  __FILE__,__FUNCTION__,copied,GetLastError());

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

      return(false);

     }

   return(result);

  }

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

//| Create the horizontal line                                       |

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

bool HLineCreate(const long            chart_ID=0,          // chart's ID

                 const string          name="HLine",        // line name

                 const int             sub_window=0,        // subwindow index

                 double                price=0,             // line price

                 const color           clr=clrRed,          // line color

                 const ENUM_LINE_STYLE style=STYLE_SOLID,   // line style

                 const int             width=1,             // line width

                 const bool            back=false,          // in the background

                 const bool            selection=false,     // highlight to move

                 const bool            hidden=true,         // hidden in the object list

                 const long            z_order=0)           // priority for mouse click

  {

//--- if the price is not set, set it at the current Bid price level

   if(!price)

      price=SymbolInfoDouble(Symbol(),SYMBOL_BID);

//--- reset the error value

   ResetLastError();

//--- create a horizontal line

   if(!ObjectCreate(chart_ID,name,OBJ_HLINE,sub_window,0,price))

     {

      Print(__FUNCTION__,

            ": failed to create a horizontal line! Error code = ",GetLastError());

      return(false);

     }

//--- set line color

   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);

//--- set line display style

   ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style);

//--- set line width

   ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,width);

//--- display in the foreground (false) or background (true)

   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);

//--- enable (true) or disable (false) the mode of moving the line by mouse

//--- when creating a graphical object using ObjectCreate function, the object cannot be

//--- highlighted and moved by default. Inside this method, selection parameter

//--- is true by default making it possible to highlight and move the object

   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);

   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);

//--- hide (true) or display (false) graphical object name in the object list

   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);

//--- set the priority for receiving the event of a mouse click in the chart

   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);

//--- define text for object Label

   ObjectSetString(chart_ID,name,OBJPROP_TEXT,name);

//--- successful execution

   return(true);

  }

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

//| Move horizontal line                                             |

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

bool HLineMove(const long   chart_ID=0,   // chart's ID

               const string name="HLine", // line name

               double       price=0)      // line price

  {

//--- if the line price is not set, move it to the current Bid price level

   if(!price)

      price=SymbolInfoDouble(Symbol(),SYMBOL_BID);

//--- reset the error value

   ResetLastError();

//--- move a horizontal line

   if(!ObjectMove(chart_ID,name,0,0,price))

     {

      Print(__FUNCTION__,

            ": failed to move the horizontal line! Error code = ",GetLastError());

      return(false);

     }

//--- successful execution

   return(true);

  }

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

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