Stochastic Levels Histogram

Author: Copyright © 2021, Vladimir Karputov
Indicators Used
Stochastic oscillator
0 Views
0 Downloads
0 Favorites
Stochastic Levels Histogram
ÿþ//+------------------------------------------------------------------+

//|                                  Stochastic Levels Histogram.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.001"

#property indicator_separate_window

#property indicator_buffers 6

#property indicator_plots   4

//--- plot Stochastic

#property indicator_label1  "Stochastic"

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrLightSeaGreen

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot Signal

#property indicator_label2  "Signal"

#property indicator_type2   DRAW_LINE

#property indicator_color2  clrRed

#property indicator_style2  STYLE_DOT

#property indicator_width2  1

//--- plot Level2

#property indicator_label3  "Level2"

#property indicator_type3   DRAW_HISTOGRAM2

#property indicator_color3  C'255,150,150'

#property indicator_style3  STYLE_SOLID

#property indicator_width3  2

//--- plot Level1

#property indicator_label4  "Level1"

#property indicator_type4   DRAW_HISTOGRAM2

#property indicator_color4  C'150,150,255'

#property indicator_style4  STYLE_SOLID

#property indicator_width4  2

//--- input parameters

input group             "Stochastic"

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

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

//--- indicator buffers

double   StochasticBuffer[];

double   SignalBuffer[];

double   Level2Buffer1[];

double   Level2Buffer2[];

double   Level1Buffer1[];

double   Level1Buffer2[];

//---

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

bool     m_init_error      = false;             // error on InInit

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- indicator buffers mapping

   SetIndexBuffer(0,StochasticBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,SignalBuffer,INDICATOR_DATA);

   SetIndexBuffer(2,Level2Buffer1,INDICATOR_DATA);

   SetIndexBuffer(3,Level2Buffer2,INDICATOR_DATA);

   SetIndexBuffer(4,Level1Buffer1,INDICATOR_DATA);

   SetIndexBuffer(5,Level1Buffer2,INDICATOR_DATA);

//--- Set an empty value

   PlotIndexSetDouble(2,PLOT_EMPTY_VALUE,EMPTY_VALUE);

   PlotIndexSetDouble(3,PLOT_EMPTY_VALUE,EMPTY_VALUE);

//--- set accuracy

   IndicatorSetInteger(INDICATOR_DIGITS,2);

//--- set levels

   IndicatorSetInteger(INDICATOR_LEVELS,2);

   IndicatorSetDouble(INDICATOR_LEVELVALUE,0,Inp_STO_Level1);

   IndicatorSetDouble(INDICATOR_LEVELVALUE,1,Inp_STO_Level2);

//--- name for DataWindow and indicator subwindow label

   string short_name=StringFormat("Stoch(%d,%d,%d)",Inp_STO_Kperiod,Inp_STO_Dperiod,Inp_STO_slowing);

   IndicatorSetString(INDICATOR_SHORTNAME,short_name);

//--- sets first bar from what index will be drawn

   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,Inp_STO_Kperiod+Inp_STO_slowing-2);

   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,Inp_STO_Kperiod+Inp_STO_Dperiod);

   PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,Inp_STO_Kperiod+Inp_STO_slowing-2);

   PlotIndexSetInteger(3,PLOT_DRAW_BEGIN,Inp_STO_Kperiod+Inp_STO_slowing-2);

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

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

      return(0);

//--- number of values copied from the iStochastic indicator

   int values_to_copy;

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

   int calculated_sto=BarsCalculated(handle_iStochastic);

   if(calculated_sto<=0)

     {

      PrintFormat("BarsCalculated() returned %d, error code %d",calculated_sto,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_sto!=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_sto>rates_total)

         values_to_copy=rates_total;

      else

         values_to_copy=calculated_sto;

     }

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

//--- main loop

   int limit=prev_calculated-1;

   if(prev_calculated==0)

      limit=0;

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

     {

      Level2Buffer1[i]=EMPTY_VALUE;

      Level2Buffer2[i]=EMPTY_VALUE;

      Level1Buffer1[i]=EMPTY_VALUE;

      Level1Buffer2[i]=EMPTY_VALUE;

      if(StochasticBuffer[i]>Inp_STO_Level2)

        {

         Level2Buffer1[i]=Inp_STO_Level2;

         Level2Buffer2[i]=StochasticBuffer[i];

        }

      if(StochasticBuffer[i]<Inp_STO_Level1)

        {

         Level1Buffer1[i]=StochasticBuffer[i];

         Level1Buffer2[i]=Inp_STO_Level1;

        }

     }

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