Volumes Custom

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

//|                                               Volumes Custom.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_separate_window

#property indicator_buffers 2

#property indicator_plots   1

//--- plot Histogram

#property indicator_label1  "Volumes"

#property indicator_type1   DRAW_COLOR_HISTOGRAM

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

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

//--- indicator buffers

double   iVolumesBuffer[];

double   iVolumesColors[];

//---

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,iVolumesBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,iVolumesColors,INDICATOR_COLOR_INDEX);

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

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

     }

//---

   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,iVolumesColors,handle_iVolumes,values_to_copy))

      return(0);

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

   bars_calculated=calculated;

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

                           double &color_buffer[],    // indicator buffer of colors

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

     }

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

   if(CopyBuffer(ind_handle,1,0,amount,color_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