Author: Copyright 2018, MetaQuotes Software Corp.
5 Views
0 Downloads
0 Favorites
Net_Volume
ÿþ//+------------------------------------------------------------------+

//|                                                   Net_Volume.mq5 |

//|                        Copyright 2018, MetaQuotes Software Corp. |

//|                                                 https://mql5.com |

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

#property copyright "Copyright 2018, MetaQuotes Software Corp."

#property link      "https://mql5.com"

#property version   "1.00"

#property description "Net volume indicator"

#property indicator_separate_window

#property indicator_buffers 4

#property indicator_plots   4

//--- plot Hist up

#property indicator_label1  "Volume Up"

#property indicator_type1   DRAW_HISTOGRAM

#property indicator_color1  clrGreen

#property indicator_style1  STYLE_SOLID

#property indicator_width1  8

//--- plot Hist dn

#property indicator_label2  "Volume Down"

#property indicator_type2   DRAW_HISTOGRAM

#property indicator_color2  clrRed

#property indicator_style2  STYLE_SOLID

#property indicator_width2  8

//--- plot Vol

#property indicator_label3  "Volume"

#property indicator_type3   DRAW_LINE

#property indicator_color3  clrBlue

#property indicator_style3  STYLE_SOLID

#property indicator_width3  1

//--- plot Cml

#property indicator_label4  "Cumulative"

#property indicator_type4   DRAW_ARROW

#property indicator_color4  clrDodgerBlue

#property indicator_style4  STYLE_SOLID

#property indicator_width4  1

//--- input parameters

input ENUM_TIMEFRAMES   InpTimeframe=PERIOD_M1;  // Timeframe

//--- indicator buffers

double         BufferUP[];

double         BufferDN[];

double         BufferVol[];

double         BufferCmlt[];

//--- global variables

ENUM_TIMEFRAMES   timeframe1;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- timer

   EventSetTimer(90);

//--- set global variables

   timeframe1=(InpTimeframe>Period() ? Period() : InpTimeframe);

//--- indicator buffers mapping

   SetIndexBuffer(0,BufferUP,INDICATOR_DATA);

   SetIndexBuffer(1,BufferDN,INDICATOR_DATA);

   SetIndexBuffer(2,BufferVol,INDICATOR_DATA);

   SetIndexBuffer(3,BufferCmlt,INDICATOR_DATA);

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

   PlotIndexSetInteger(3,PLOT_ARROW,159);

//--- setting indicator parameters

   IndicatorSetString(INDICATOR_SHORTNAME,"Net volume ("+TimeframeToString(timeframe1)+")");

   IndicatorSetInteger(INDICATOR_DIGITS,0);

//--- setting buffer arrays as timeseries

   ArraySetAsSeries(BufferUP,true);

   ArraySetAsSeries(BufferDN,true);

   ArraySetAsSeries(BufferVol,true);

   ArraySetAsSeries(BufferCmlt,true);

//--- get timeframe

   Time(NULL,timeframe1,1);

//---

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

  {

//--- #AB0=>2:0 <0AA82>2 1CD5@>2 :0: B09<A5@89

   ArraySetAsSeries(time,true);

//--- @>25@:0 :>;8G5AB20 4>ABC?=KE 10@>2

   if(rates_total<4 || Time(NULL,timeframe1,1)==0) return 0;

//--- @>25@:0 8 @0AGQB :>;8G5AB20 ?@>AG8BK205<KE 10@>2

   int limit=rates_total-prev_calculated;

   if(limit>1)

     {

      limit=rates_total-2;

      ArrayInitialize(BufferUP,0);

      ArrayInitialize(BufferDN,0);

      ArrayInitialize(BufferVol,0);

      ArrayInitialize(BufferCmlt,EMPTY_VALUE);

     }

   

//---  0AGQB 8=48:0B>@0

   for(int i=limit; i>=0 && !IsStopped(); i--)

     {

      int up=0,dn=0;

      if(!CalcV(i,up,dn,time))

         continue;

      BufferUP[i]=up;

      BufferDN[i]=dn;

      BufferCmlt[i]=up+dn;

      BufferVol[i]=up+fabs(dn);

     }



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

   return(rates_total);

  }

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

//| Custom indicator timer function                                  |

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

void OnTimer()

  {

   Time(NULL,timeframe1,1);

  }  

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

//|                                                                  |

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

bool CalcV(const int shift,int &value_up,int &value_dn,const datetime &time[])

  {

   int up=0,dn=0;

   int firstBar=WRONG_VALUE,lastBar=WRONG_VALUE;

   int lb=BarShift(NULL,InpTimeframe,time[shift],false);

   int fb=BarShift(NULL,InpTimeframe,time[shift+1],false);

   if(lb==WRONG_VALUE || fb==WRONG_VALUE)

      return false;

   lastBar=lb;

   firstBar=fb-1;

   for(int i=firstBar; i>=lastBar; i--)

     {

      double open=Open(NULL,InpTimeframe,i);

      double close=Close(NULL,InpTimeframe,i);

      if(open==0 || close==0) continue;

      if(close>open) up++;

      if(close<open) dn--;

     }

   value_up=up;

   value_dn=dn;

   return true;

  }

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

//| Timeframe to string                                              |

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

string TimeframeToString(const ENUM_TIMEFRAMES timeframe)

  {

   return StringSubstr(EnumToString(timeframe),7);

  }

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

//| >72@0I05B A<5I5=85 10@0 ?> 2@5<5=8                              |

//| https://www.mql5.com/ru/forum/743/page11#comment_7010041         |

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

int BarShift(const string symbol_name,const ENUM_TIMEFRAMES timeframe,const datetime time,bool exact=false)

  {

   int res=Bars(symbol_name,timeframe,time+1,UINT_MAX);

   if(exact) if((timeframe!=PERIOD_MN1 || time>TimeCurrent()) && res==Bars(symbol_name,timeframe,time-PeriodSeconds(timeframe)+1,UINT_MAX)) return(WRONG_VALUE);

   return res;

  }

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

//| >72@0I05B Time                                                  |

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

datetime Time(const string symbol_name,const ENUM_TIMEFRAMES timeframe,const int index)

  {

   datetime array[];

   return(CopyTime(symbol_name,timeframe,index,1,array)==1 ? array[0] : 0);

  }  

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

//| >72@0I05B F5=C Open                                             |

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

double Open(const string symbol_name,const ENUM_TIMEFRAMES timeframe,const int index)

  {

   double array[];

   return(CopyOpen(symbol_name,timeframe,index,1,array)==1 ? array[0] : 0);

  }  

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

//| >72@0I05B F5=C Close                                            |

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

double Close(const string symbol_name,const ENUM_TIMEFRAMES timeframe,const int index)

  {

   double array[];

   return(CopyClose(symbol_name,timeframe,index,1,array)==1 ? array[0] : 0);

  }  

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

Comments