BetterVolume

Author: Viktorov Alexey
0 Views
0 Downloads
0 Favorites
BetterVolume
ÿþ/********************************************************************\

|                                                   BetterVolume.mq5 |

|                                            © 2019, Alexey Viktorov |

|                       https://www.mql5.com/ru/users/alexeyvik/news |

\********************************************************************/

#property copyright "Viktorov Alexey"

#property link      "https://www.mql5.com/ru/users/alexeyvik/news"

#property version   "1.00"

#property strict

#property indicator_separate_window

#property indicator_buffers 3

#property indicator_plots   2

#property indicator_type1  DRAW_COLOR_HISTOGRAM

#property indicator_color1  clrOlive,clrDeepSkyBlue,clrMediumBlue,clrBlack,clrRed,clrMagenta

#property indicator_type2   DRAW_LINE

#property indicator_color2  clrBlack



#property indicator_width1 2

#property indicator_width2 1



input ENUM_APPLIED_VOLUME APPLIED_VOLUME  = VOLUME_TICK;

input int                 MAPeriod        = 100;

input int                 LookBack        = 20;



double Neutral[]

     , Color[]

     , Average[];

/*********************************************************************\

|               Custom indicator initialization function              |

\*********************************************************************/

int OnInit()

{

      SetIndexBuffer(0, Neutral, INDICATOR_DATA);

      SetIndexBuffer(1, Color, INDICATOR_COLOR_INDEX);

      PlotIndexSetString(0, PLOT_LABEL, EnumToString(APPLIED_VOLUME));

      PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, 0.0);

      SetIndexBuffer(2, Average, INDICATOR_DATA);

      PlotIndexSetString(1, PLOT_LABEL, "Average("+(string)MAPeriod+")");

      PlotIndexSetDouble(1, PLOT_EMPTY_VALUE, 0.0);

      IndicatorSetString(INDICATOR_SHORTNAME, "Better Volume");

//----

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

               )

{

   double VolLowest, Range, Value2, Value3, HiValue2, HiValue3, LoValue3, tempv2, tempv3, tempv;

   int limit = prev_calculated == 0 ? MAPeriod : rates_total-1;

//---

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

    {

      Neutral[i] = double(APPLIED_VOLUME == VOLUME_TICK ? tick_volume[i] : volume[i]);

      Color[i] = 1.0;             //  clrDeepSkyBlue

      Value2 = 0.0;

      Value3 = 0.0;

      HiValue2 = 0.0;

      HiValue3 = 0.0;

      LoValue3 = DBL_MAX;

      tempv2 = 0.0;

      tempv3 = 0.0;

      tempv = 0.0;

      VolLowest = double(APPLIED_VOLUME == VOLUME_TICK ? tick_volume[ArrayMinimum(tick_volume, LookBack, i)]

                                                       : volume[ArrayMinimum(volume, LookBack, i)]);

      double Volume = double(APPLIED_VOLUME == VOLUME_TICK ? tick_volume[i] : volume[i]);

      if(Volume == VolLowest)

       {

        Color[i] = 2.0;           //  clrMediumBlue

       }

      Range = (high[i]-low[i]);

      Value2 = APPLIED_VOLUME == VOLUME_TICK ? tick_volume[i]*Range : volume[i]*Range;

      if(Range != 0)

         Value3 = APPLIED_VOLUME == VOLUME_TICK ? tick_volume[i]/Range : volume[i]/Range;

//---

      for(int n = i; n > i-MAPeriod; n--)

       {

        tempv = APPLIED_VOLUME == VOLUME_TICK ? tick_volume[n]+tempv : volume[n]+tempv;

       } 

       Average[i] = (double)tempv/MAPeriod;

//---

       for(int n = i; n > i-fmin(LookBack, MAPeriod); n--)

        {

         tempv2 = APPLIED_VOLUME == VOLUME_TICK ? tick_volume[n]*(high[n]-low[n]) : volume[n]*(high[n]-low[n]);

          if(tempv2 >= HiValue2)

           HiValue2 = tempv2;

         if(tempv2 != 0)

          {           

           tempv3 = APPLIED_VOLUME == VOLUME_TICK ? tick_volume[n]/(high[n]-low[n]) : volume[n]/(high[n]-low[n]);

           if(tempv3 > HiValue3) 

            HiValue3 = tempv3; 

           if(tempv3 < LoValue3)

            LoValue3 = tempv3;

          } 

        }

       if(Value2 == HiValue2  && close[i] > (high[i] + low[i])/2)

        {

         Color[i] = 0.0;          //  clrOlive

        }   

       if(Value3 == HiValue3)

        {

         Color[i] = 3.0;          //  clrBlack

        }

       if(Value2 == HiValue2 && Value3 == HiValue3)

        {

         Color[i] = 5.0;          //  clrMagenta

        } 

      if(Value2 == HiValue2  && close[i] <= (high[i] + low[i])/2)

       {

        Color[i] = 4.0;           //  clrRed

       } 

    }

   return(rates_total);

}/********************************************************************/

Comments