You are on page 1of 3

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

//| DarvasBoxes.mq5 |
//| Copyright � 2004, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright � 2004, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"
//---- indicator version
#property version "1.00"
//---- drawing the indicator in the main window
#property indicator_chart_window
//---- number of indicator buffers
#property indicator_buffers 2
//---- 2 plots are used
#property indicator_plots 2
//+-----------------------------------+
//| Indicator drawing parameters |
//+-----------------------------------+
//---- drawing the indicator as a line
#property indicator_type1 DRAW_LINE
//---- red color is used as the color of the indicator line
#property indicator_color1 Blue
//---- the indicator line is a solid one
#property indicator_style1 STYLE_SOLID
//---- indicator line width is equal to 2
#property indicator_width1 2
//---- displaying the indicator label
#property indicator_label1 "Upper DarvasBoxes"

//---- drawing the indicator as a line


#property indicator_type2 DRAW_LINE
//---- red color is used as the color of the indicator line
#property indicator_color2 Magenta
//---- the indicator line is a solid one
#property indicator_style2 STYLE_SOLID
//---- indicator line width is equal to 2
#property indicator_width2 2
//---- displaying the indicator label
#property indicator_label2 "Lower DarvasBoxes"
//+-----------------------------------+
//| Indicator input parameters |
//+-----------------------------------+
input bool symmetry=false;
input int Shift=0; // Horizontal shift of the indicator in bars
//---+
//---- indicator buffers
double UpperBuffer[];
double LowerBuffer[];
//---- declaration of the integer variables for the start of data calculation
int start=2;
//+------------------------------------------------------------------+
//| DarvasBoxes initialization function |
//+------------------------------------------------------------------+
void OnInit()
{
//---- set dynamic array as an indicator buffer
SetIndexBuffer(0,UpperBuffer,INDICATOR_DATA);
//---- shifting the indicator 1 horizontally by AroonShift
PlotIndexSetInteger(0,PLOT_SHIFT,Shift);
//---- shifting the start of drawing the indicator 1
PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,start);
//---- create label to display in DataWindow
PlotIndexSetString(0,PLOT_LABEL,"Upper DarvasBoxes");
//---- setting values of the indicator that won't be visible on a chart
PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,EMPTY_VALUE);
//---- indexing the elements in the buffer as timeseries
ArraySetAsSeries(UpperBuffer,true);

//---- set dynamic array as an indicator buffer


SetIndexBuffer(1,LowerBuffer,INDICATOR_DATA);
//---- shifting the indicator 2 horizontally
PlotIndexSetInteger(1,PLOT_SHIFT,Shift);
//---- shifting the start of drawing the indicator 2
PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,start);
//---- create label to display in DataWindow
PlotIndexSetString(1,PLOT_LABEL,"Lower DarvasBoxes");
//---- setting values of the indicator that won't be visible on a chart
PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,EMPTY_VALUE);
//---- indexing the elements in the buffer as timeseries
ArraySetAsSeries(LowerBuffer,true);

//---- initializations of a variable for the indicator short name


string shortname="DarvasBoxes";
//---- creating a name for displaying in a separate sub-window and in a tooltip
IndicatorSetString(INDICATOR_SHORTNAME,shortname);
//---- determination of accuracy of displaying the indicator values
IndicatorSetInteger(INDICATOR_DIGITS,_Digits+1);
//---- initialization end
}
//+------------------------------------------------------------------+
//| DarvasBoxes iteration function |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total, // number of bars in history at the
current tick
const int prev_calculated,// number of bars calculated at previous
call
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(rates_total<start) return(0);

//---- indexing elements in arrays as timeseries


ArraySetAsSeries(high,true);
ArraySetAsSeries(low,true);

//---- declaration of integer variables


int limit,bar;
//---- declaration of static variables
static int state,STATE;
static double box_top,box_bottom,BOX_TOP,BOX_BUTTOM;
//---- calculations of the 'limit' starting index for the bars recalculation loop
if(prev_calculated>rates_total || prev_calculated<=0)// checking for the first
start of the indicator calculation
{
limit=rates_total-start; // starting index for calculation of all bars
BOX_TOP=high[limit+1];
BOX_BUTTOM=low[limit+1];
STATE=1;
}
else
{
limit=rates_total-prev_calculated; // starting index for calculation of new
bars
}

//---- restore values of the variables


state=STATE;
box_top=BOX_TOP;
box_bottom=BOX_BUTTOM;

//---- main indicator calculation loop


for(bar=limit; bar>=0; bar--)
{
//---- store values of the variables before running at the current bar
if(rates_total!=prev_calculated && bar==0)
{
STATE=state;
BOX_TOP=box_top;
BOX_BUTTOM=box_bottom;
}

switch(state)
{
case 1: box_top=high[bar]; if(symmetry)box_bottom=low[bar]; break;
case 2: if(box_top<=high[bar]) box_top=high[bar]; break;
case 3: if(box_top>high[bar]) box_bottom=low[bar]; else
box_top=high[bar]; break;
case 4: if(box_top > high[bar]) {if(box_bottom >= low[bar])
box_bottom=low[bar];} else box_top=high[bar]; break;
case 5: if(box_top > high[bar]) {if(box_bottom >= low[bar])
box_bottom=low[bar];} else box_top=high[bar]; state=0; break;
}

UpperBuffer[bar] = box_top;
LowerBuffer[bar] = box_bottom;
state++;
}
//----
return(rates_total);
}
//+------------------------------------------------------------------+

You might also like