Author: Copyright � 2011, Nikolay Kositsin
Indicators Used
Indicator of the average true range
2 Views
0 Downloads
0 Favorites
RBVI
//+------------------------------------------------------------------+
//|                                                         RBVI.mq5 | 
//|                             Copyright © 2011,   Nikolay Kositsin | 
//|                              Khabarovsk,   farria@mail.redcom.ru | 
//+------------------------------------------------------------------+ 
#property copyright "Copyright © 2011, Nikolay Kositsin"
#property link "farria@mail.redcom.ru"
//---- indicator version number
#property version   "1.00"
//---- drawing indicator in a separate window
#property indicator_separate_window
//---- number of indicator buffers
#property indicator_buffers 1 
//---- only one plot is used
#property indicator_plots   1
//+----------------------------------------------+
//|  Indicator 1 drawing parameters              |
//+----------------------------------------------+
//---- drawing the indicator as a line
#property indicator_type1   DRAW_LINE
//---- blue color is used for the indicator line
#property indicator_color1 Blue
//---- the indicator line is a continuous curve
#property indicator_style1  STYLE_SOLID
//---- Indicator line width is equal to 1
#property indicator_width1  1
//---- displaying the indicator label
#property indicator_label1  "RBVI"
//+----------------------------------------------+
//| Parameters of displaying horizontal levels   |
//+----------------------------------------------+
#property indicator_level1 60
#property indicator_level2 40
#property indicator_levelcolor Magenta
#property indicator_levelstyle STYLE_DASHDOTDOT
//+----------------------------------------------+
//|  Indicator input parameters                  |
//+----------------------------------------------+
input int RBVIPeriod=10;                          // Indicator period
input ENUM_APPLIED_VOLUME VolumeType=VOLUME_TICK; // Volume
input int Shift=0;                                // Horizontal shift of the indicator in bars
//+-----------------------------------+
//---- declaration of dynamic arrays that
//---- will be used as indicator buffers
double RbviBuffer[];
//---- declaration of integer variables for storing indicator handles
int ATR_Handle;
//---- declaration of integer variables for the start of data calculation
int min_rates_total;
//+------------------------------------------------------------------+   
//| RBVI indicator initialization function                           | 
//+------------------------------------------------------------------+ 
int OnInit()
  {
//---- initialization of variables of the start of data calculation
   min_rates_total=2+RBVIPeriod;

//---- getting handle of the ATR indicator
   ATR_Handle=iATR(NULL,0,RBVIPeriod);
   if(ATR_Handle==INVALID_HANDLE)
     {
      Print(" Failed to get handle of the ATR indicator");
      return(1);
     }

//---- converting the RbviBuffer[] dynamic array to an indicator buffer
   SetIndexBuffer(0,RbviBuffer,INDICATOR_DATA);
//---- moving the indicator 1 horizontally
   PlotIndexSetInteger(0,PLOT_SHIFT,Shift);
//---- shifting the beginning of the indicator drawing
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,min_rates_total);
//---- setting the indicator values that won't be visible on a chart
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,EMPTY_VALUE);
//---- indexing the elements in buffers as in timeseries   
   ArraySetAsSeries(RbviBuffer,true);

//---- initializations of variable for a short name of the indicator
   string shortname;
   StringConcatenate(shortname,"RBVI(",string(RBVIPeriod),",",EnumToString(VolumeType),",",Shift,")");
//---- creating name for displaying in a separate sub-window and in tooltip
   IndicatorSetString(INDICATOR_SHORTNAME,shortname);

//--- determination of accuracy of displaying the indicator values
   IndicatorSetInteger(INDICATOR_DIGITS,1);
//---- initialization end
   return(0);
  }
//+------------------------------------------------------------------+ 
//| RBVI iteration function                                          | 
//+------------------------------------------------------------------+ 
int OnCalculate(const int rates_total,    // number of bars in history at the current tick
                const int prev_calculated,// amount of history in bars at the previous tick
                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[])
  {
//---- checking the number of bars to be enough for the calculation
   if(BarsCalculated(ATR_Handle)<rates_total
      || rates_total<min_rates_total)
      return(0);

//---- declaration of variables with a floating point  
   double vol;
//---- declaration of integer variables
   int to_copy,limit,bar;
   double Range[],vol0=0,vol1=0,sumn,sump,rel,positive,negative;
   static double positive_,negative_,sumn_,sump_;

//--- calculations of the necessary amount of data to be copied and
//---- the limit starting index for loop of bars recalculation
   if(prev_calculated>rates_total || prev_calculated<=0)// checking for the first start of the indicator calculation
     {
      limit=rates_total-min_rates_total-1; // starting index for calculation of all bars
      sumn_=0.0;
      sump_=0.0;
     }
   else
     {
      limit=rates_total-prev_calculated; // starting index for calculation of new bars
     }

   to_copy=limit+2;

//---- copy newly appeared data in the Range[] array
   if(CopyBuffer(ATR_Handle,0,0,to_copy,Range)<=0) return(0);

//---- indexation of elements in arrays as in timeseries  
   ArraySetAsSeries(Range,true);
   if(VolumeType==VOLUME_TICK) ArraySetAsSeries(tick_volume,true);
   else                        ArraySetAsSeries(volume,true);

//---- restore values of the variables
   positive=positive_;
   negative=negative_;
   sumn=sumn_;
   sump=sump_;

//---- main loop of the indicator calculation
   for(bar=limit; bar>=0 && !IsStopped(); bar--)
     {
      //---- memorize values of the variables before running at the current bar
      if(rates_total!=prev_calculated && bar==0)
        {
         positive_=positive;
         negative_=negative;
         sumn_=sumn;
         sump_=sump;
        }

      if(VolumeType==VOLUME_TICK)
        {
         vol0=double(tick_volume[bar]);
         vol1=double(tick_volume[bar+1]);
        }
      else
        {
         vol=double(volume[bar]);
         vol=double(volume[bar+1]);
        }

      rel=Range[bar]*vol0-Range[bar+1]*vol1;

      if(rel>0) sump= rel;
      else      sumn=-rel;

      positive=(positive*(RBVIPeriod-1)+sump)/RBVIPeriod;
      negative=(negative*(RBVIPeriod-1)+sumn)/RBVIPeriod;

      if(negative==0.0) RbviBuffer[bar]=0.0;
      else RbviBuffer[bar]=100.0*(1.0-1.0/(1.0+positive/negative));
     }
//----     
   return(rates_total);
  }
//+------------------------------------------------------------------+

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