//+------------------------------------------------------------------+
//|                                          Volatility Breakout.mq4 |
//|                                      Copyright © 2010, Taiyakixz |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, Taiyakixz"

#property indicator_chart_window
#property indicator_buffers 8
#property indicator_color1 Blue
#property indicator_color2 Red
#property indicator_color3 DeepSkyBlue
#property indicator_color4 HotPink
#property indicator_color5 SteelBlue
#property indicator_color6 Peru
#property indicator_color7 Aqua
#property indicator_color8 Pink
#property indicator_width7 2
#property indicator_width8 2

extern int Pips=5;
extern int HighLowLines1=5;
extern int HighLowLines2=12;
extern bool ShowLine=false;
extern bool AlertOn=true;

double PipsHighBuffer[];
double PipsLowBuffer[];
double H1Buffer[];
double L1Buffer[];
double H2Buffer[];
double L2Buffer[];
double H3Buffer[];
double L3Buffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
   string shortName;
   int beginLines;

   SetIndexStyle(0,DRAW_LINE);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexStyle(2,DRAW_LINE);
   SetIndexStyle(3,DRAW_LINE);
   SetIndexStyle(4,DRAW_LINE);
   SetIndexStyle(5,DRAW_LINE);
   SetIndexStyle(6,DRAW_ARROW);
   SetIndexArrow(6,159);
   SetIndexStyle(7,DRAW_ARROW);
   SetIndexArrow(7,159);

   SetIndexBuffer(0,PipsHighBuffer);
   SetIndexBuffer(1,PipsLowBuffer);
   SetIndexBuffer(2,H1Buffer);
   SetIndexBuffer(3,L1Buffer);
   SetIndexBuffer(4,H2Buffer);
   SetIndexBuffer(5,L2Buffer);
   SetIndexBuffer(6,H3Buffer);
   SetIndexBuffer(7,L3Buffer);

   SetIndexEmptyValue(0,0);
   SetIndexEmptyValue(1,0);
   SetIndexEmptyValue(2,0);
   SetIndexEmptyValue(3,0);
   SetIndexEmptyValue(4,0);
   SetIndexEmptyValue(5,0);
   SetIndexEmptyValue(6,0);
   SetIndexEmptyValue(7,0);

   shortName="Volatility Breakout";
   IndicatorShortName(shortName);
   SetIndexLabel(0,shortName);

   beginLines = HighLowLines2 + 1;

   SetIndexDrawBegin(0,beginLines);
   SetIndexDrawBegin(1,beginLines);
   SetIndexDrawBegin(2,beginLines);
   SetIndexDrawBegin(3,beginLines);
   SetIndexDrawBegin(4,beginLines);
   SetIndexDrawBegin(5,beginLines);
   SetIndexDrawBegin(6,beginLines);
   SetIndexDrawBegin(7,beginLines);


  return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
//----
   
//----
  return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
  int counted_bars=IndicatorCounted(), i, shift, tmpShift;
  double tmpH1, tmpL1, tmpH2, tmpL2;
  double tph,tpl,th1,tl1,th2,tl2;

  if (counted_bars==0) counted_bars=HighLowLines2+1;
  i=(Bars-counted_bars);

  for (shift=i; shift>=0;shift--)
  {
    tmpShift = shift + 1;
    tph = High[tmpShift] + Pips * Point;
    tpl = Low[tmpShift] - Pips * Point;
    th1 = High[Highest(NULL,0,MODE_HIGH,HighLowLines1,tmpShift)];
    tl1 = Low[Lowest(NULL,0,MODE_LOW,HighLowLines1,tmpShift)];
    th2 = High[shift+HighLowLines2];
    tl2 = Low[shift+HighLowLines2];
    if(ShowLine)
    {
      PipsHighBuffer[shift] = tph;
      PipsLowBuffer[shift] = tpl;
      H1Buffer[shift] = th1;
      L1Buffer[shift] = tl1;
      H2Buffer[shift] = th2;
      L2Buffer[shift] = tl2;
    }
    tmpH1 = MathMax( tph, th1);
    tmpH2 = MathMax( tmpH1, th2);
    tmpL1 = MathMin( tpl, tl1);
    tmpL2 = MathMin( tmpL1, tl2);
    H3Buffer[shift] = tmpH2;
    L3Buffer[shift] = tmpL2;
  }

  if(Volume[0] == 1)
  {
    if(AlertOn == true && Bid > H3Buffer[1])
      Alert(Symbol()+" Volatility Breakout BUY at "+Ask);
    if(AlertOn == true && Bid < L3Buffer[1])
      Alert(Symbol()+" Volatility Breakout SELL at "+Bid);
  }

  return(0);
}
//+------------------------------------------------------------------+