Stochastic Intersection Arrow 2 Strict

Author: Copyright © 2020, Vladimir Karputov
Indicators Used
Stochastic oscillator
Miscellaneous
It issuies visual alerts to the screen
0 Views
0 Downloads
0 Favorites
Stochastic Intersection Arrow 2 Strict
ÿþ//+------------------------------------------------------------------+

//|                       Stochastic Intersection Arrow 2 Strict.mq5 |

//|                              Copyright © 2020, Vladimir Karputov |

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

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

#property copyright "Copyright © 2020, Vladimir Karputov"

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

#property version   "2.000"

#property description "Add Alert and Push"

#property indicator_chart_window

#property indicator_buffers 4

#property indicator_plots   2

//--- plot Upwards

#property indicator_label1  "Upwards"

#property indicator_type1   DRAW_ARROW

#property indicator_color1  clrBlue

#property indicator_style1  STYLE_SOLID

#property indicator_width1  2

//--- plot Topdown

#property indicator_label2  "Topdown"

#property indicator_type2   DRAW_ARROW

#property indicator_color2  clrRed

#property indicator_style2  STYLE_SOLID

#property indicator_width2  2

//--- input parameters

input int               Inp_STO_Kperiod      = 5;           // Stochastic: K-period (number of bars for calculations)

input int               Inp_STO_Dperiod      = 3;           // Stochastic: D-period (period of first smoothing)

input int               Inp_STO_slowing      = 3;           // Stochastic: final smoothing

input ENUM_MA_METHOD    Inp_STO_ma_method    = MODE_SMA;    // Stochastic: type of smoothing

input ENUM_STO_PRICE    Inp_STO_price_field  = STO_LOWHIGH; // Stochastic: stochastic calculation method

input double            Inp_STO_Level1       = 25.0;        // Stochastic: Value Level #1 (25)

input double            Inp_STO_Level2       = 75.0;        // Stochastic: Value Level #2 (75)

input bool              InpStrict            = true;        // Strict

//---

input bool              InpAlert             = true;        // Alert (once per bar)

input bool              InpNotification      = true;        // Push notifications (once per bar)

//---

input ushort            InpUpwardsCode       = 217;         // Upwards: code from the Wingdings charset

input ushort            InpTopdownCode       = 218;         // Topdown: code from the Wingdings charset

input int               InpVerticalShift     = 10;          // Upwards and Topdown: vertical shift of arrows in pixels

//--- indicator buffers

double   UpwardsBuffer[];

double   TopdownBuffer[];

double   StochasticBuffer[];

double   SignalBuffer[];

//---

int      handle_iStochastic;        // variable for storing the handle of the iStochastic indicator

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

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

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- indicator buffers mapping

   SetIndexBuffer(0,UpwardsBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,TopdownBuffer,INDICATOR_DATA);

   SetIndexBuffer(2,StochasticBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(3,SignalBuffer,INDICATOR_CALCULATIONS);

//--- setting a code from the Wingdings charset as the property of PLOT_ARROW

   PlotIndexSetInteger(0,PLOT_ARROW,InpUpwardsCode);

   PlotIndexSetInteger(1,PLOT_ARROW,InpTopdownCode);

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

   PlotIndexSetInteger(0,PLOT_ARROW_SHIFT,InpVerticalShift);

   PlotIndexSetInteger(1,PLOT_ARROW_SHIFT,-InpVerticalShift);

//--- Set as an empty value 0

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);

   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,0.0);

//---

//--- create handle of the indicator iStochastic

   handle_iStochastic=iStochastic(Symbol(),Period(),

                                  Inp_STO_Kperiod,Inp_STO_Dperiod,Inp_STO_slowing,

                                  Inp_STO_ma_method,Inp_STO_price_field);

//--- if the handle is not created

   if(handle_iStochastic==INVALID_HANDLE)

     {

      //--- tell about the failure and output the error code

      PrintFormat("Failed to create handle of the iStochastic 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<Inp_STO_Kperiod+Inp_STO_Dperiod+3)

      return(0);

//---

   int values_to_copy;

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

   int calculated=BarsCalculated(handle_iStochastic);

   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 iStochastic 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 StochasticBuffer array is greater than the number of values in the iStochastic 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 arrays with values of the iStochastic indicator

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

   if(!FillArraysFromBuffers(StochasticBuffer,SignalBuffer,handle_iStochastic,values_to_copy))

      return(0);

//--- memorize the number of values in the Stochastic Oscillator indicator

   bars_calculated=calculated;

//--- main loop

   int limit=prev_calculated-1;

   if(prev_calculated==0)

     {

      limit=Inp_STO_Kperiod+Inp_STO_Dperiod+1;

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

        {

         UpwardsBuffer[i]=0.0;

         TopdownBuffer[i]=0.0;

        }

     }

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

     {

      UpwardsBuffer[i]=0.0;

      TopdownBuffer[i]=0.0;

      if(StochasticBuffer[i-1]<SignalBuffer[i-1] && StochasticBuffer[i]>SignalBuffer[i])

         if((!InpStrict) || (InpStrict && StochasticBuffer[i]<Inp_STO_Level1))

            UpwardsBuffer[i]=low[i];

      if(StochasticBuffer[i-1]>SignalBuffer[i-1] && StochasticBuffer[i]<SignalBuffer[i])

         if((!InpStrict) || (InpStrict && StochasticBuffer[i]>Inp_STO_Level2))

            TopdownBuffer[i]=high[i];

     }

//--- notifications

   if(m_last_notifications<time[rates_total-1])

      if(InpNotification || InpAlert)

        {

         if(UpwardsBuffer[rates_total-1]>0.0)

           {

            if(InpNotification)

               SendNotification(Symbol()+","+StringSubstr(EnumToString(Period()),7,-1)+" Upwards signal");

            if(InpAlert)

               Alert(Symbol()+","+StringSubstr(EnumToString(Period()),7,-1)+" Upwards signal");

            m_last_notifications=time[rates_total-1];

           }

         if(TopdownBuffer[rates_total-1]>0.0)

           {

            if(InpNotification)

               SendNotification(Symbol()+","+StringSubstr(EnumToString(Period()),7,-1)+" Topdown signal");

            if(InpAlert)

               Alert(Symbol()+","+StringSubstr(EnumToString(Period()),7,-1)+" Topdown signal");

            m_last_notifications=time[rates_total-1];

           }

        }

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

   return(rates_total);

  }

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

//| Filling indicator buffers from the iStochastic indicator         |

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

bool FillArraysFromBuffers(double &main_buffer[],    // indicator buffer of Stochastic Oscillator values

                           double &signal_buffer[],  // indicator buffer of the signal line

                           int ind_handle,           // handle of the iStochastic indicator

                           int amount                // number of copied values

                          )

  {

//--- reset error code

   ResetLastError();

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

   if(CopyBuffer(ind_handle,MAIN_LINE,0,amount,main_buffer)<0)

     {

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

      PrintFormat("Failed to copy data from the iStochastic indicator, 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 SignalBuffer array with values from the indicator buffer that has index 1

   if(CopyBuffer(ind_handle,SIGNAL_LINE,0,amount,signal_buffer)<0)

     {

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

      PrintFormat("Failed to copy data from the iStochastic indicator, 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_iStochastic!=INVALID_HANDLE)

      IndicatorRelease(handle_iStochastic);

  }

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

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