//+------------------------------------------------------------------+
//|                                         LevelsOfScalp_8_12v2.mq4 |
//|                                          Copyright © 2006, Magex |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Magex"
#property link      "http://www.metaquotes.net"

#property indicator_chart_window
#property indicator_buffers 8
#property indicator_color1 White
#property indicator_color2 White
#property indicator_color3 White
#property indicator_color4 White
#property indicator_color5 White
#property indicator_color6 White
#property indicator_color7 Red
#property indicator_color8 Red


//---- input parameters
extern int       Hours2Check=     8;   //количество часов для измерения пробиваемого диапазона     
extern int       CloseHour=       9;    //время закрытия всех позиций    
extern int       EarliestOpenHour=14;   //раньше этого часа не открываемся
extern int       Days2Check=      10;    //количество суток для измерения среднего суточного диапазона
extern double    ChannelK=        1.5;  //коэффициент рассчёта канала   

extern int       CheckHour=     8;//12    //время измерения пробиваемого диапазона   
extern double    ProfitK=       2;//2    //коэффициент рассчёта профита
extern double    LossK=         2;//1.5    //коэффициент рассчёта лосса  
extern double    OffsetK=       2;//2.5  //коэффициент рассчёта расстояния пробоя     

//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
double ExtMapBuffer5[];
double ExtMapBuffer6[];
double ExtMapBuffer7[];
double ExtMapBuffer8[];
double ExtMapBuffer9[];
double ExtMapBuffer10[];

int BarsPerDay;//свечй в сутки
int LatestOpenHour=22;//после этого часа не открываемся

//массивы 
int Bars2Check;
int profit;
int loss;
int offset;
int avrange;


int OpenDay;
bool Opened;
bool buysell; 
double sellprice;
double buyprice;
double daj;
double hh;
double ll;
double totalrange;
double channel;

double profl;
double lossl;



//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,ExtMapBuffer1);
   
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,ExtMapBuffer2);
   
    SetIndexStyle(2,DRAW_ARROW,EMPTY, 1);
   SetIndexArrow(2,108);
   SetIndexBuffer(2,ExtMapBuffer3);
   SetIndexEmptyValue(2,0.0);
   
    SetIndexStyle(3,DRAW_ARROW,EMPTY, 1);
   SetIndexArrow(3,233);
   SetIndexBuffer(3,ExtMapBuffer4);
   SetIndexEmptyValue(3,0.0);
   
   SetIndexStyle(4,DRAW_ARROW,EMPTY, 1);
   SetIndexArrow(4,234);
   SetIndexBuffer(4,ExtMapBuffer5);
   SetIndexEmptyValue(2,0.0);
   
   SetIndexStyle(5,DRAW_ARROW,EMPTY, 1);
   SetIndexArrow(5,221);
   SetIndexBuffer(5,ExtMapBuffer6);
   SetIndexEmptyValue(2,0.0);
   
   SetIndexStyle(6,DRAW_ARROW,EMPTY, 1);
   SetIndexArrow(6,170);
   SetIndexBuffer(6,ExtMapBuffer7);
   SetIndexEmptyValue(2,0.0);
   
   SetIndexStyle(7,DRAW_ARROW,EMPTY, 1);
   SetIndexArrow(7,4);
   SetIndexBuffer(7,ExtMapBuffer8);
   SetIndexEmptyValue(2,0.0);
   


//----
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
   int    counted_bars=IndicatorCounted();
   int limit;
   if (counted_bars>0) limit=Bars-counted_bars;
   if (counted_bars<0) return(0);
   if (counted_bars==0) 
      limit=Bars-Days2Check*24*60/Period()-1;
//----

   
   int i,j,n;
   
   for(i=limit;i>=0;i--)
   {
              
         if( CheckHour == TimeHour(Time[i]) && TimeMinute(Time[i]) == 0 )
         {
            
            Bars2Check = ((Hours2Check*60)/Period()); 
           
    
            hh=High[Highest(NULL,0,MODE_HIGH,Bars2Check,i+1)];
            ll=Low[Lowest(NULL,0,MODE_LOW,Bars2Check,i+1)];
            BarsPerDay=((24*60)/Period());
            totalrange=0; 
            channel=hh-ll;

            for(j=1;j<=Days2Check;j++)
            {
               daj=(High[Highest(NULL,0,MODE_HIGH,BarsPerDay,BarsPerDay*(j-1)+(i+1))]
                      -Low[Lowest(NULL,0,MODE_LOW,BarsPerDay,BarsPerDay*(j-1)+(i+1))]);
               totalrange=totalrange+daj;  
            }
            
            avrange = MathRound((totalrange/Days2Check)/Point);
            offset = MathRound(avrange/OffsetK);
            sellprice = NormalizeDouble(ll-offset*Point,Digits);
            buyprice = NormalizeDouble(hh+offset*Point,Digits);
            profit=MathRound(avrange/ProfitK);
            loss=MathRound(avrange/LossK); 
            
         }      
      
      ExtMapBuffer1[i] = buyprice;  
      ExtMapBuffer2[i] = sellprice;
 
      //
      if(TimeHour(Time[i])<LatestOpenHour && TimeHour(Time[i])>EarliestOpenHour && OpenDay != TimeDay(Time[i])) 
      {
      
         if( channel<(avrange/ChannelK)*Point && !Opened)
         {
            if(Close[i+1] > buyprice)
            {
               ExtMapBuffer4[i] = Close[i+1];
               Opened = true;
               buysell = true;
            
               profl = Close[i+1] + profit*Point;
               lossl = Close[i+1] - loss*Point;
                ExtMapBuffer7[i] = profl;
                ExtMapBuffer8[i] = lossl;
                OpenDay = TimeDay(Time[i]);
            }
            if(Close[i+1] < sellprice)
            {
               ExtMapBuffer5[i] = Close[i+1];
               Opened = true; 
               buysell = false;
            
               profl = Close[i+1] - profit*Point;
               lossl = Close[i+1] + loss*Point;  
               ExtMapBuffer7[i] = profl;
               ExtMapBuffer8[i] = lossl;
               OpenDay = TimeDay(Time[i]);
            }
         
         } 
     }
     
     if(Opened)
     {
        if(buysell)
        {
            if(Close[i+1] > profl || Close[i+1] < lossl)
            {
               Opened = false;
               ExtMapBuffer3[i] = Close[i+1];
            }
        }
        else
        {
            if(Close[i+1] < profl || Close[i+1] > lossl)
            {
               Opened = false;
               ExtMapBuffer3[i] = Close[i+1];
            }
        }
        if(CloseHour == TimeHour(Time[i]))
        {
            Opened = false;
            ExtMapBuffer3[i] = Close[i+1]; 
        }
       
     }
     
     
   }


   
      

      
//----
   return(0);
}
//+------------------------------------------------------------------+