Fractals Alert

Author: Copyright © 2021, Vladimir Karputov
Indicators Used
Fractals
Miscellaneous
It plays sound alertsIt issuies visual alerts to the screen
0 Views
0 Downloads
0 Favorites
Fractals Alert
ÿþ//+------------------------------------------------------------------+

//|                                               Fractals Alert.mq5 |

//|                              Copyright © 2021, Vladimir Karputov |

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

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

#property copyright "Copyright © 2021, Vladimir Karputov"

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

#property version   "1.000"

#property indicator_chart_window

#property indicator_buffers 2

#property indicator_plots   2

//--- plot Fractal Up

#property indicator_label1  "Fractal Up"

#property indicator_type1   DRAW_ARROW

#property indicator_color1  clrRed

#property indicator_width1  1

//--- plot Fractal Down

#property indicator_label2  "Fractal Down"

#property indicator_type2   DRAW_ARROW

#property indicator_color2  clrBlue

#property indicator_width2  1

//--- input parameters

input group             "Fractals"

input ushort               InpFractalUpCode        = 217;            // "Fractal Up" symbol code from the Wingdings font

input ushort               InpFractalDownCode      = 218;            // "Fractal Down" symbol code from the Wingdings font

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

input group             "Alerts"

input string               InpSoundName            = "alert.wav";    // Sound Name

input uchar                InpSoundRepetitions     = 3;              // Repetitions

input uchar                InpSoundPause           = 3;              // Pause, in seconds

input bool                 InpMain_and_Signal      = true;           // Main AND Signal

input bool                 InpMain_and_Zero        = true;           // Main AND '0'

input bool                 InpSignal_and_Zero      = true;           // Signal AND '0'

//--- an indicator buffers for the plots

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

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

uchar    m_repetitions     = 0;        //

string   m_text            = "";       //

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

bool     m_init_error      = false;    // error on InInit

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- indicator buffers mapping

   SetIndexBuffer(0,FractalUpBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,FractalDownBuffer,INDICATOR_DATA);

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

   PlotIndexSetInteger(0,PLOT_ARROW,InpFractalUpCode);

   PlotIndexSetInteger(1,PLOT_ARROW,InpFractalDownCode);

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

   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,EMPTY_VALUE);

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

//--- main loop

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

   datetime time_0=time[rates_total-1];

   if(time_0!=m_prev_bars)

     {

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

      m_repetitions  = 0;  //

      m_text         = "";

      m_prev_bars    = time_0;

     }

//---

   int i=rates_total-1;

   if(FractalUpBuffer[i-3]!=EMPTY_VALUE || FractalDownBuffer[i-3]!=EMPTY_VALUE)

     {

      if(m_repetitions>=InpSoundRepetitions)

         return(rates_total);

      //---

      datetime time_current=TimeCurrent();

      if(time_current-m_last_sound>InpSoundPause)

        {

         //--- Fractal Up

         if(FractalUpBuffer[i-3]!=EMPTY_VALUE)

           {

            PlaySound(InpSoundName);

            Alert("Fractal Up, ",TimeToString(time[i-3]));

            m_last_sound=time_current;

            m_repetitions++;

            return(rates_total);

           }

         //--- Fractal Down

         if(FractalDownBuffer[i-3]!=EMPTY_VALUE)

           {

            PlaySound(InpSoundName);

            Alert("Fractal Down, ",TimeToString(time[i-3]));

            m_last_sound=time_current;

            m_repetitions++;

            return(rates_total);

           }

        }

     }

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