Stochastic Custom and Label

Author: Copyright © 2021, Vladimir Karputov
0 Views
0 Downloads
0 Favorites
Stochastic Custom and Label
ÿþ//+------------------------------------------------------------------+

//|                                  Stochastic Custom and Label.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 description "Stochastic Custom and output the current value to the OBJ_LABEL graphic object"

//--- indicator settings

#property indicator_separate_window

#property indicator_buffers 4

#property indicator_plots   2

#property indicator_type1   DRAW_LINE

#property indicator_type2   DRAW_LINE

#property indicator_color1  clrLightSeaGreen

#property indicator_color2  clrRed

#property indicator_style2  STYLE_DOT

//--- input parameters

input group             "Stochastic"

input int                  Inp_STO_KPeriod         = 5;     // K period

input int                  Inp_STO_DPeriod         = 3;     // D period

input int                  Inp_STO_Slowing         = 3;     // Slowing

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

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

input group             "Label"

input ENUM_APPLIED_PRICE   InpLabelPrice           = PRICE_CLOSE;       // Label price

input string               InpName                 = "Stochastic Label";// Label name

input int                  InpX                    = 50;                // X-axis distance

input ENUM_BASE_CORNER     InpCorner               = CORNER_RIGHT_LOWER;// Chart corner for anchoring

input string               InpFont                 = "Lucida Console";  // Font

input int                  InpFontSize             = 14;                // Font size

input color                InpColor                = clrOrange;         // Color

input double               InpAngle                = 0.0;               // Slope angle in degrees

input bool                 InpBack                 = false;             // Background object

input bool                 InpSelection            = false;             // Highlight to move

input bool                 InpHidden               = true;              // Hidden in the object list

input long                 InpZOrder               = 0;                 // Priority for mouse click

//--- indicator buffers

double   ExtMainBuffer[];

double   ExtSignalBuffer[];

double   ExtHighesBuffer[];

double   ExtLowesBuffer[];

//---

bool     m_first=false;

int      m_event_id=5000;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

   m_first=true;

//--- indicator buffers mapping

   SetIndexBuffer(0,ExtMainBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,ExtSignalBuffer,INDICATOR_DATA);

   SetIndexBuffer(2,ExtHighesBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(3,ExtLowesBuffer,INDICATOR_CALCULATIONS);

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

//--- set maximum and minimum for subwindow

   IndicatorSetDouble(INDICATOR_MINIMUM,0);

   IndicatorSetDouble(INDICATOR_MAXIMUM,100);

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

   IndicatorSetString(INDICATOR_SHORTNAME,"Stoch("+

                      (string)Inp_STO_KPeriod+","+

                      (string)Inp_STO_DPeriod+","+

                      (string)Inp_STO_Slowing+")");

   PlotIndexSetString(0,PLOT_LABEL,"Main");

   PlotIndexSetString(1,PLOT_LABEL,"Signal");

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

//--- create a text label on the chart

   LabelCreate(0,InpName,ChartWindowFind(),InpX,0,InpCorner,"-//-",InpFont,InpFontSize,

               InpColor,InpAngle,ANCHOR_CENTER,InpBack,InpSelection,InpHidden,InpZOrder);

//--- create a timer with a 30 second period

   EventSetTimer(30);

//--- initialization done

   return(INIT_SUCCEEDED);

  }

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

//| Relative Strength Index                                          |

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

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

     {

      m_first=false;

      //--- form custom event ID

      ushort custom_event_id=(ushort)(m_event_id-CHARTEVENT_CUSTOM);

      //---  send a custom event with parameters filling

      EventChartCustom(ChartID(),custom_event_id,0,0.0,__FUNCTION__);

     }

   int i,k,start;

//--- check for bars count

   if(rates_total<=Inp_STO_KPeriod+Inp_STO_DPeriod+Inp_STO_Slowing)

      return(0);

//---

   start=Inp_STO_KPeriod-1;

   if(start+1<prev_calculated)

      start=prev_calculated-2;

   else

     {

      for(i=0; i<start; i++)

        {

         ExtLowesBuffer[i]=0.0;

         ExtHighesBuffer[i]=0.0;

        }

     }

//--- calculate HighesBuffer[] and ExtHighesBuffer[]

   for(i=start; i<rates_total && !IsStopped(); i++)

     {

      double dmin=1000000.0;

      double dmax=-1000000.0;

      for(k=i-Inp_STO_KPeriod+1; k<=i; k++)

        {

         if(dmin>low[k])

            dmin=low[k];

         if(dmax<high[k])

            dmax=high[k];

        }

      ExtLowesBuffer[i]=dmin;

      ExtHighesBuffer[i]=dmax;

     }

//--- %K

   start=Inp_STO_KPeriod-1+Inp_STO_Slowing-1;

   if(start+1<prev_calculated)

      start=prev_calculated-2;

   else

     {

      for(i=0; i<start; i++)

         ExtMainBuffer[i]=0.0;

     }

//--- main cycle

   for(i=start; i<rates_total && !IsStopped(); i++)

     {

      double sumlow=0.0;

      double sumhigh=0.0;

      for(k=(i-Inp_STO_Slowing+1); k<=i; k++)

        {

         sumlow +=(close[k]-ExtLowesBuffer[k]);

         sumhigh+=(ExtHighesBuffer[k]-ExtLowesBuffer[k]);

        }

      if(sumhigh==0.0)

         ExtMainBuffer[i]=100.0;

      else

         ExtMainBuffer[i]=sumlow/sumhigh*100;

     }

//--- signal

   start=Inp_STO_DPeriod-1;

   if(start+1<prev_calculated)

      start=prev_calculated-2;

   else

     {

      for(i=0; i<start; i++)

         ExtSignalBuffer[i]=0.0;

     }

   for(i=start; i<rates_total && !IsStopped(); i++)

     {

      double sum=0.0;

      for(k=0; k<Inp_STO_DPeriod; k++)

         sum+=ExtMainBuffer[i-k];

      ExtSignalBuffer[i]=sum/Inp_STO_DPeriod;

     }

   LabelTextChange(0,InpName,DoubleToString(ExtMainBuffer[rates_total-1],2));

//--- OnCalculate done. Return new prev_calculated.

   return(rates_total);

  }

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

//| Timer function                                                   |

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

void OnTimer()

  {

//--- create a text label on the chart

   if(ObjectFind(0,InpName)<0)

      LabelCreate(0,InpName,ChartWindowFind(),InpX,0,InpCorner,"-//-",InpFont,InpFontSize,

                  InpColor,InpAngle,ANCHOR_CENTER,InpBack,InpSelection,InpHidden,InpZOrder);

  }

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

//| ChartEvent function                                              |

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

void OnChartEvent(const int id,

                  const long &lparam,

                  const double &dparam,

                  const string &sparam)

  {

//---

   if(id==CHARTEVENT_CHART_CHANGE || id==m_event_id)

     {

      int height=ChartHeightInPixelsGet(ChartID(),ChartWindowFind());

      if(height==-1)

         return;

      LabelMove(ChartID(),InpName,InpX,height/2);

     }

  }

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

//| Indicator deinitialization function                              |

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

void OnDeinit(const int reason)

  {

   ObjectDelete(0,InpName);

//--- destroy the timer after completing the work

   EventKillTimer();

  }

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

//| Create a text label                                              |

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

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

                 const string            name="Label",             // label name

                 const int               sub_window=0,             // subwindow index

                 const int               x=0,                      // X coordinate

                 const int               y=0,                      // Y coordinate

                 const ENUM_BASE_CORNER  corner=CORNER_LEFT_UPPER, // chart corner for anchoring

                 const string            text="Label",             // text

                 const string            font="Arial",             // font

                 const int               font_size=10,             // font size

                 const color             clr=clrRed,               // color

                 const double            angle=0.0,                // text slope

                 const ENUM_ANCHOR_POINT anchor=ANCHOR_LEFT_UPPER, // anchor type

                 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

  {

//--- reset the error value

   ResetLastError();

//--- create a text label

   if(!ObjectCreate(chart_ID,name,OBJ_LABEL,sub_window,0,0))

     {

      Print(__FUNCTION__,

            ": failed to create text label! Error code = ",GetLastError());

      return(false);

     }

//--- set label coordinates

   ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,x);

   ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y);

//--- set the chart's corner, relative to which point coordinates are defined

   ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,corner);

//--- set the text

   ObjectSetString(chart_ID,name,OBJPROP_TEXT,text);

//--- set text font

   ObjectSetString(chart_ID,name,OBJPROP_FONT,font);

//--- set font size

   ObjectSetInteger(chart_ID,name,OBJPROP_FONTSIZE,font_size);

//--- set the slope angle of the text

   ObjectSetDouble(chart_ID,name,OBJPROP_ANGLE,angle);

//--- set anchor type

   ObjectSetInteger(chart_ID,name,OBJPROP_ANCHOR,anchor);

//--- set color

   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);

//--- 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 label by mouse

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

//--- successful execution

   return(true);

  }

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

//| Move the text label                                              |

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

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

               const string name="Label", // label name

               const int    x=0,          // X coordinate

               const int    y=0)          // Y coordinate

  {

//--- reset the error value

   ResetLastError();

//--- move the text label

   if(!ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,x))

     {

      Print(__FUNCTION__,

            ": failed to move X coordinate of the label! Error code = ",GetLastError());

      return(false);

     }

   if(!ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y))

     {

      Print(__FUNCTION__,

            ": failed to move Y coordinate of the label! Error code = ",GetLastError());

      return(false);

     }

//--- successful execution

   return(true);

  }

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

//| Change the label text                                            |

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

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

                     const string name="Label", // object name

                     const string text="Text")  // text

  {

//--- reset the error value

   ResetLastError();

//--- change object text

   if(!ObjectSetString(chart_ID,name,OBJPROP_TEXT,text))

     {

      Print(__FUNCTION__,

            ": failed to change the text! Error code = ",GetLastError());

      return(false);

     }

//--- successful execution

   return(true);

  }

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

//| Gets the height of chart (in pixels)                             |

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

int ChartHeightInPixelsGet(const long chart_ID=0,const int sub_window=0)

  {

//--- prepare the variable to get the property value

   long result=-1;

//--- reset the error value

   ResetLastError();

//--- receive the property value

   if(!ChartGetInteger(chart_ID,CHART_HEIGHT_IN_PIXELS,sub_window,result))

     {

      //--- display the error message in Experts journal

      Print(__FUNCTION__+", Error Code = ",GetLastError());

     }

//--- return the value of the chart property

   return((int)result);

  }

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

Comments