//+------------------------------------------------------------------+
//|                              smMilliped VolaBreakoutLines_v1.mq4 |
//+------------------------------------------------------------------+
#property  copyright "Copyright © 2010.08.21, SwingMan"
#property  link      ""

#property indicator_chart_window

#property indicator_buffers 2

#property indicator_color1 YellowGreen  //highs 
#property indicator_color2 DarkOrange   //lows

#property indicator_width1 0
#property indicator_width2 0

//---- extern input parameters
//+------------------------------------------------------------------+
extern bool StrongCondition_OpenClose = false;
extern int Number_BarsBack = 2;
extern int Offset_LineDraw = 0;
//+------------------------------------------------------------------+

//---- buffers
double highs[], lows[];

//---- constants

//---- variables
int nDigits;
double dPoint;
double offsetLine;
int iBigbar;
bool bBigbarAvailable;
double valHighs, valLows;
datetime thisTime, oldTime;
bool newBarOK;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
   Comment("SwingMan VolaBreakoutLines");
   Get_MarketInfos();
//---- indicators   
   int iArrow = 59;
   SetIndexBuffer(0,highs); SetIndexStyle(0,DRAW_ARROW); SetIndexArrow(0,iArrow);
   SetIndexLabel(0,"Breakout HIGH");   
   
   SetIndexBuffer(1,lows);  SetIndexStyle(1,DRAW_ARROW); SetIndexArrow(1,iArrow);
   SetIndexLabel(1,"Breakout LOW");
//---- initialisations
   offsetLine = Offset_LineDraw*dPoint;
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
   Comment("");
   return(0);
}


//###################################################################
//-------------------------------------------------------------------
int start()
{
   if (nDigits == 0) {
      Get_MarketInfos();   
      offsetLine = Offset_LineDraw*dPoint;
   }

   int counted_bars=IndicatorCounted();
   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
   int limit=Bars-counted_bars - Number_BarsBack;
     
   //---- main loop
   for(int i=limit; i>=0; i--)
   {
      thisTime = Time[i];       
      if (thisTime > oldTime) {
         newBarOK = true;
         oldTime = thisTime;
      }
      else 
         newBarOK = false;
      
      //-- BigBar not available .....................................
      if (bBigbarAvailable == false && newBarOK)
      {
         bool conditionBigbar = Get_ConditionBigbar(i);
         if (conditionBigbar == true) 
         {
            bBigbarAvailable = true;
            iBigbar = i + Number_BarsBack;         
            valHighs = High[iBigbar];
            valLows  = Low[iBigbar];
            for (int j=1; j<=Number_BarsBack; j++) {
               highs[i+j] = valHighs + offsetLine;
               lows[i+j]  = valLows  - offsetLine;
            }
         }
      }
      else
      
      // BigBar is available ........................................
      if (bBigbarAvailable == true && newBarOK)
      {
         int i1 = i+1;
         bool conditionBreakout = Get_ConditionBreakout(i1);
         if (conditionBreakout == true) {
            bBigbarAvailable = false;
            highs[i] = EMPTY_VALUE;
            lows[i]  = EMPTY_VALUE;  
         }
         else
         if (conditionBreakout == false) {
            highs[i1] = valHighs + offsetLine;
            lows[i1]  = valLows  - offsetLine;            
         }
      }
   }
   return(0);
}
//+------------------------------------------------------------------+
//###################################################################

//___________________________________________________________________
bool Get_ConditionBreakout(int iBar)
{
   bool condOpen  = (Open[iBar]  > valHighs) || (Open[iBar]  < valLows);
   bool condClose = (Close[iBar] > valHighs) || (Close[iBar] < valLows);
   
   if (condOpen || condClose) return(true); else return(false);
}//Get_ConditionBreakout

//___________________________________________________________________
bool Get_ConditionBigbar(int iBar)
{
   int firstBar = iBar + Number_BarsBack;
   int nBars = 0;
   
   for (int i=1; i<Number_BarsBack; i++)
   {      
      bool condOpen  = (Open[iBar+i]  <= High[firstBar]) && (Open[iBar+i]  >= Low[firstBar]);
      bool condClose = (Close[iBar+i] <= High[firstBar]) && (Close[iBar+i] >= Low[firstBar]);
      
      if (StrongCondition_OpenClose==true)
      {
         if (condOpen && condClose) 
           nBars++;
         else break;
      }
      else
      
      if (StrongCondition_OpenClose==false)
      {
         if (condClose) 
           nBars++;
         else break;
      }      
   }

   if (nBars == Number_BarsBack-1)
        return(true);
   else return(false);
}//Get_ConditionBigbar

//____________________________________________________________________
void Get_MarketInfos()
{
   int iDigits = MarketInfo(Symbol(),MODE_DIGITS);
   nDigits = MarketInfo(Symbol(),MODE_DIGITS);
   dPoint  = MarketInfo(Symbol(),MODE_POINT);
   if (nDigits == 3 || nDigits == 5) {
      nDigits--;
      dPoint = dPoint * 10;      
   }
   return;   
}