//+------------------------------------------------------------------+
//|                                                   TSR_Ranges.mq4 |
//|                                         Copyright © 2006, Ogeima |
//|                                             ph_bresson@yahoo.com |
//| made for FXiGoR for the TSR Trend Slope Retracement method       |
//| modified to the DYNAMIC Daily Range Breakout System             |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Ogeima"
#property link      "ph_bresson@yahoo.com"

#property indicator_chart_window
//---- input parameters
extern double  Risk_to_Reward_ratio =  3.0;
int nDigits;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
   if(Symbol()=="GBPJPY" || Symbol()=="EURJPY" || Symbol()=="USDJPY" || Symbol()=="GOLD")  nDigits = 2;
   else nDigits = 4;

   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   //----
   string PIPS="";
   string temprange="";
   int R1=0,MR25=0,WR25=0,R25=0,R40=0,RAvg=0;
   int HR25=0,LR25=0,HWR25=0,LWR25=0,HMR25=0,LMR25=0;
   int Prevday=0,Prevwk=0,Prevmthy=0;
   int Curday=0,Curwk=0,Curmthy=0;
   int temp1=0,temp2=0,counter1=0,counter2=0,temp3=0,temp4=0;
   int temp5=0,temp6=0,counter3=0,counter4=0,temp7=0,temp8=0;
   int temp9=0,temp10=0,counter5=0,counter6=0,temp11=0,temp12=0;
   int RoomUp=0,RoomDown=0,StopLoss_Long=0,StopLoss_Short=0,today_range=0;
   double   SL_Long=0,SL_Short=0,rangepercent=0;
   double   low0=0,high0=0;
   string   Text="";
   int i=0;
   double OPEN = iOpen(NULL,1440,0);
   double CLOSE = iClose(NULL,1440,0);
   PIPS =  DoubleToStr((CLOSE-OPEN)/Point,0);
      
   R1 =  (iHigh(NULL,PERIOD_D1,1)-iLow(NULL,PERIOD_D1,1))/Point;
   
   for(i=1;i<=40;i++)
      R25   =    R25 +  (iHigh(NULL,PERIOD_D1,i)-iLow(NULL,PERIOD_D1,i))/Point;
   for(i=1;i<=26;i++)
      WR25   =    WR25 +  (iHigh(NULL,PERIOD_W1,i)-iLow(NULL,PERIOD_W1,i))/Point;
   for(i=1;i<=24;i++)
      MR25   =    MR25 +  (iHigh(NULL,PERIOD_MN1,i)-iLow(NULL,PERIOD_MN1,i))/Point;

Prevday =  (iHigh(NULL,PERIOD_D1,1)-iLow(NULL,PERIOD_D1,1))/Point;
Prevwk =  (iHigh(NULL,PERIOD_W1,1)-iLow(NULL,PERIOD_W1,1))/Point;
Prevmthy =  (iHigh(NULL,PERIOD_MN1,1)-iLow(NULL,PERIOD_MN1,1))/Point;

Curday =  (iHigh(NULL,PERIOD_D1,0)-iLow(NULL,PERIOD_D1,0))/Point;
Curwk =  (iHigh(NULL,PERIOD_W1,0)-iLow(NULL,PERIOD_W1,0))/Point;
Curmthy = (iHigh(NULL,PERIOD_MN1,0)-iLow(NULL,PERIOD_MN1,0))/Point;

   
   R25 = R25/40;
   WR25 = WR25/26;
   MR25 = MR25/24;
   
   for(i=1;i<=40;i++)
   {
      if ((iHigh(NULL,PERIOD_D1,i)-iLow(NULL,PERIOD_D1,i))/Point > R25)
         { 
            temp3 = (iHigh(NULL,PERIOD_D1,i)-iLow(NULL,PERIOD_D1,i))/Point;
            temp1 = temp1 + (temp3 - R25);
            counter1 = counter1 + 1 ; 
         }
      if ((iHigh(NULL,PERIOD_D1,i)-iLow(NULL,PERIOD_D1,i))/Point < R25)
         { 
            temp4 = (iHigh(NULL,PERIOD_D1,i)-iLow(NULL,PERIOD_D1,i))/Point;
            temp2 = temp2 + (R25 - temp4);
            counter2 = counter2 + 1 ; 
         }
   }
   
   for(i=1;i<=26;i++)
   {
      if ((iHigh(NULL,PERIOD_W1,i)-iLow(NULL,PERIOD_W1,i))/Point > WR25)
         { 
            temp7 = (iHigh(NULL,PERIOD_W1,i)-iLow(NULL,PERIOD_W1,i))/Point;
            temp5 = temp5 + (temp7 - WR25);
            counter3 = counter3 + 1 ; 
         }
      if ((iHigh(NULL,PERIOD_W1,i)-iLow(NULL,PERIOD_W1,i))/Point < WR25)
         { 
            temp8 = (iHigh(NULL,PERIOD_W1,i)-iLow(NULL,PERIOD_W1,i))/Point;
            temp6 = temp6 + (WR25 - temp8);
            counter4 = counter4 + 1 ; 
         }
   }
   
   for(i=1;i<=24;i++)
   {
      if ((iHigh(NULL,PERIOD_MN1,i)-iLow(NULL,PERIOD_MN1,i))/Point > MR25)
         { 
            temp11 = (iHigh(NULL,PERIOD_MN1,i)-iLow(NULL,PERIOD_MN1,i))/Point;
            temp9 = temp9 + (temp11 - MR25);
            counter5 = counter5 + 1 ; 
         }
      if ((iHigh(NULL,PERIOD_MN1,i)-iLow(NULL,PERIOD_MN1,i))/Point < MR25)
         { 
            temp12 = (iHigh(NULL,PERIOD_MN1,i)-iLow(NULL,PERIOD_MN1,i))/Point;
            temp10 = temp10 + (MR25 - temp12);
            counter6 = counter6 + 1 ; 
         }
   }
      
   HR25 = R25 + (temp1/counter1);
   LR25 = R25 - (temp2/counter2);
   HWR25 = WR25 + (temp5/counter3);
   LWR25 = WR25 - (temp6/counter4);
   HMR25 = MR25 + (temp9/counter5);
   LMR25 = MR25 - (temp10/counter6);
   
   low0  =  iLow(NULL,PERIOD_D1,0);
   high0 =  iHigh(NULL,PERIOD_D1,0);
   RoomUp   =  RAvg - (Bid - low0)/Point;
   RoomDown =  RAvg - (high0 - Bid)/Point;
   StopLoss_Long  =  RoomUp/Risk_to_Reward_ratio;
   SL_Long        =  Bid - StopLoss_Long*Point;
   StopLoss_Short =  RoomDown/Risk_to_Reward_ratio;
   SL_Short       =  Bid + StopLoss_Short*Point;

   today_range = (iHigh(NULL,PERIOD_D1,0)) - (iLow(NULL,PERIOD_D1,0));
   
   rangepercent = (Curmthy/((MR25)/Point))*1000000 ;
   temprange = DoubleToStr((rangepercent),0);

   Text =   //"\n"+"Today Range: " +  today_range  + "" +
            "\n\n"+"Avg 40 Days Range: " +  R25  + "\n" +
            "Avg HIGH : " +  HR25  + "\n" +
            "Avg LOW : " +  LR25  + "\n\n" +
            "Prev Day Range: " +  Prevday  + "\n" +
            "Current Day Range: " +  Curday  + "\n\n" +
            "---------------------------------------\n\n" +
            "Avg 26 Weeks Range: " +  WR25  + "\n" +
            "Avg HIGH : " +  HWR25  + "\n" +
            "Avg LOW : " +  LWR25  + "\n\n" +
            "Prev Week Range: " +  Prevwk  + "\n" +
            "Current Week Range: " +  Curwk  + "\n\n" +
            "---------------------------------------\n\n" +
            "Avg Month Range (%): " +  temprange  + "\n\n" +
            "Avg 24 Months Range: " +  MR25  + "\n" +           
            "Avg HIGH : " +  HMR25  + "\n" +
            "Avg LOW : " +  LMR25  + "\n\n" +
            "Prev Month Range: " +  Prevmthy  + "\n" +
            "Current Month Range: " +  Curmthy  + "\n";
            //"Today Range: " +  today_range  + "\n";
  
   Comment(Text);

   return(0);
  }
//+------------------------------------------------------------------+