Volume Extremes

Author: Copyright © 2022, Vladimir Karputov
0 Views
0 Downloads
0 Favorites
Volume Extremes
ÿþ//+------------------------------------------------------------------+

//|                                              Volume Extremes.mq5 |

//|                              Copyright © 2022, Vladimir Karputov |

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

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

#property copyright "Copyright © 2022, Vladimir Karputov"

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

#property version   "1.00"

#property indicator_separate_window

#property indicator_buffers 5

#property indicator_plots   3

//--- plot Volumes

#property indicator_label1  "Volumes"

#property indicator_type1   DRAW_COLOR_HISTOGRAM

#property indicator_color1  clrLimeGreen,clrDodgerBlue

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot UP

#property indicator_label2  "UP"

#property indicator_type2   DRAW_ARROW

#property indicator_color2  clrRed

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- plot DOWN

#property indicator_label3  "DOWN"

#property indicator_type3   DRAW_ARROW

#property indicator_color3  clrBlue

#property indicator_style3  STYLE_SOLID

#property indicator_width3  1

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

//| Enum Plot Line Width                                             |

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

enum ENUM_PLOT_LINE_WIDTH

  {

   width_1=1,  //

   width_2=2,  //

   width_3=3,  //

   width_4=4,  //

   width_5=5,  //

  };

//--- input parameters

input group             "Volumes"

input ENUM_APPLIED_VOLUME  Inp_Volumes_applied_volume = VOLUME_TICK;    // Volumes: volume type for calculation

input color                Inp_Volumes_up_color       = clrLimeGreen;   // Volumes: Up Color

input color                Inp_Volumes_down_color     = clrDodgerBlue;  // Volumes: Down Color

input ENUM_LINE_STYLE      Inp_Volumes_line_style     = STYLE_SOLID;    // Volumes: histogram style

input ENUM_PLOT_LINE_WIDTH Inp_Volumes_line_width     = width_3;        // Volumes: histogram width

input group             "Arrow"

input uchar                InpUpArrowCode             = 241;            // Arrow Up: code (font Wingdings)

input int                  InpUpArrowShift            = 5;              // Arrow Up: vertical shift in pixel

input uchar                InpDownArrowCode           = 242;            // Arrow Down: code (font Wingdings)

input int                  InpDownArrowShift          = 5;              // Arrow Down: vertical shift in pixel

//--- indicator buffers

double   VolumesBuffer[];

double   VolumesColors[];

double   UPBuffer[];

double   DOWNBuffer[];

double   iVolumesBuffer[];

//---

int      handle_iVolumes;                    // variable for storing the handle of the iVolumes indicator

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

bool     m_init_error               = false; // error on InInit

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- indicator buffers mapping

   SetIndexBuffer(0,VolumesBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,VolumesColors,INDICATOR_COLOR_INDEX);

   SetIndexBuffer(2,UPBuffer,INDICATOR_DATA);

   SetIndexBuffer(3,DOWNBuffer,INDICATOR_DATA);

   SetIndexBuffer(4,iVolumesBuffer,INDICATOR_CALCULATIONS);

//--- set the style line

   PlotIndexSetInteger(0,PLOT_LINE_STYLE,Inp_Volumes_line_style);

//--- set number of colors in color buffer

   PlotIndexSetInteger(0,PLOT_COLOR_INDEXES,2);

//--- set colors for color buffer

   PlotIndexSetInteger(0,PLOT_LINE_COLOR,0,Inp_Volumes_up_color);

   PlotIndexSetInteger(0,PLOT_LINE_COLOR,1,Inp_Volumes_down_color);

//--- set line thickness

   if(Inp_Volumes_line_style!=STYLE_SOLID)

      PlotIndexSetInteger(0,PLOT_LINE_WIDTH,1);

   else

      PlotIndexSetInteger(0,PLOT_LINE_WIDTH,Inp_Volumes_line_width);

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

   PlotIndexSetInteger(1,PLOT_ARROW,InpUpArrowCode);

   PlotIndexSetInteger(2,PLOT_ARROW,InpDownArrowCode);

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

   PlotIndexSetInteger(1,PLOT_ARROW_SHIFT,-InpUpArrowShift);

   PlotIndexSetInteger(2,PLOT_ARROW_SHIFT,InpDownArrowShift);

//--- an empty value for plotting, for which there is no drawing

   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,0.0);

   PlotIndexSetDouble(2,PLOT_EMPTY_VALUE,0.0);

//--- indicator digits

   IndicatorSetInteger(INDICATOR_DIGITS,0);

//--- create handle of the indicator

   handle_iVolumes=iVolumes(Symbol(),Period(),Inp_Volumes_applied_volume);

//--- if the handle is not created

   if(handle_iVolumes==INVALID_HANDLE)

     {

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

      PrintFormat("Failed to create handle of the iVolumes 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);

     }

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

   PlotIndexSetInteger(1,PLOT_ARROW,159);

   PlotIndexSetInteger(2,PLOT_ARROW,159);

//---

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

   int values_to_copy;

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

   int calculated=BarsCalculated(handle_iVolumes);

   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 iVolumes 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 iVolumesBuffer array is greater than the number of values in the iVolumes 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 iVolumes indicator

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

   if(!FillArraysFromBuffers(iVolumesBuffer,handle_iVolumes,values_to_copy))

      return(0);

//--- memorize the number of values in the Volumes indicator

   bars_calculated=calculated;

//--- main loop

   int limit=rates_total-values_to_copy;

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

     {

      //--- volumes

      if(close[i]>open[i])

        {

         VolumesBuffer[i]=iVolumesBuffer[i];

         VolumesColors[i]=0.0;

        }

      else

        {

         VolumesBuffer[i]=-iVolumesBuffer[i];

         VolumesColors[i]=1.0;

        }

      //---extremes

      UPBuffer[i]=0.0;

      DOWNBuffer[i]=0.0;

      if(i>1)

        {

         if(VolumesBuffer[i-2]<VolumesBuffer[i-1] && VolumesBuffer[i-1]>VolumesBuffer[i] && VolumesBuffer[i-1]>0.0)

            UPBuffer[i-1]=VolumesBuffer[i-1];

         if(VolumesBuffer[i-2]>VolumesBuffer[i-1] && VolumesBuffer[i-1]<VolumesBuffer[i] && VolumesBuffer[i-1]<0.0)

            DOWNBuffer[i-1]=VolumesBuffer[i-1];

        }

     }

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

   return(rates_total);

  }

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

//| Filling indicator buffers from the iVolumes indicator            |

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

bool FillArraysFromBuffers(double &volume_buffer[],   // indicator buffer of Volumes values

                           int ind_handle,            // handle of the iVolumes indicator

                           int amount                 // number of copied values

                          )

  {

//--- reset error code

   ResetLastError();

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

   if(CopyBuffer(ind_handle,0,0,amount,volume_buffer)<0)

     {

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

      PrintFormat("Failed to copy data from the iVolumes 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_iVolumes!=INVALID_HANDLE)

      IndicatorRelease(handle_iVolumes);

  }

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

Comments