Net_Volume_Delta

Author: Copyright 2018, MetaQuotes Software Corp.
Price Data Components
Series array that contains open time of each barSeries array that contains open prices of each barSeries array that contains close prices for each bar
2 Views
0 Downloads
0 Favorites
Net_Volume_Delta
ÿþ//+------------------------------------------------------------------+

//|                                             Net_Volume_Delta.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 Delta oscillator"

#property indicator_separate_window

#property indicator_buffers 2

#property indicator_plots   1

//--- plot Volume

#property indicator_label1  "Net Volume Delta"

#property indicator_type1   DRAW_COLOR_HISTOGRAM

#property indicator_color1  clrGreen,clrRed,clrDarkGray

#property indicator_style1  STYLE_SOLID

#property indicator_width1  8

//--- input parameters

input ENUM_TIMEFRAMES   InpTimeframe   =  PERIOD_M1;  // Timeframe

//--- indicator buffers

double         BufferVolume[];

double         BufferColors[];

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

   SetIndexBuffer(1,BufferColors,INDICATOR_COLOR_INDEX);

//--- setting indicator parameters

   IndicatorSetString(INDICATOR_SHORTNAME,"Net Volume Delta ("+TimeframeToString(timeframe1)+")");

   IndicatorSetInteger(INDICATOR_DIGITS,0);

//--- setting buffer arrays as timeseries

   ArraySetAsSeries(BufferVolume,true);

   ArraySetAsSeries(BufferColors,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(BufferVolume,0);

      ArrayInitialize(BufferColors,2);

     }



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

      BufferVolume[i]=double(up+dn);

      BufferColors[i]=(BufferVolume[i]>0 ? 0 : BufferVolume[i]<0 ? 1 : 2);

     }

   

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

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---