//+------------------------------------------------------------------+
//|                                                 Range_Trader.mq4 |
//|                                         Copyright © 2008,JP      |
//|                                      maurosandrade@yahoo.com.br  |
//| made for BELEKAS and Igroks Thread                               |
//| modified for use with the Igrok Method             |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, Jupiter_Peak"
#property link      "maurosandrade@yahoo.com.br"

#property indicator_chart_window
//---- input parameters
extern double    Risk_to_Reward_ratio= 5.0;
extern int       Stop_Loss= 1;
int nDigits;
extern bool      SkipSundays = true;
//+------------------------------------------------------------------+
//| 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()
  {
//----
   Comment( "" );
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   //----
   string PIPS="";
   int R1=0,MR25=0,WR25=0,R25=0,R40=0,RAvg=0;
   int Prevday=0,Prevwk=0,Prevmthy=0;
   int Curday=0,Curwk=0,Curmthy=0,CurrentSpread=0;
   int EntryLong=0,EntryShort=0,StopLoss_Long=0,StopLoss_Short=0,today_range=0,RoomDown=0,TPLong=0,TPShort=0,TotalPips=0;
   double   SL_Long=0,SL_Short=0,ENT_Long=0,ENT_Short,TP_Long=0,TP_Short=0;
   double   low0=0,high0=0, low1=0,high1=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<=60;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;


   int dayshift = 1;
   if( SkipSundays && TimeDayOfWeek( iTime(NULL, PERIOD_D1, 0 ) ) == 1 ) dayshift = 2; //monday
   low1  =  iLow(NULL,PERIOD_D1,dayshift);
   high1 =  iHigh(NULL,PERIOD_D1,dayshift);
   
   int numbarsM1today = iBarShift( NULL, PERIOD_M1, iTime(NULL, PERIOD_D1, 0 ));
   bool mainMoveRange = true;
   bool mainMoveUp = false;
   for( int k=numbarsM1today;k>=0;k-- )
   if( iLow(NULL, PERIOD_M1, k ) < low1 ) { mainMoveRange = false; mainMoveUp = false; break; }
   else
   if( iHigh(NULL, PERIOD_M1, k ) > high1 ) { mainMoveRange = false; mainMoveUp = true; break; }
   string dayMove = "Main move of the day is ";
   if( mainMoveRange == true ) dayMove = dayMove + "RANGING"; else
   if( mainMoveUp == true ) dayMove = dayMove + "UP"; else dayMove = dayMove + "DOWN";   
      
   R25 = R25/60;
   WR25 = WR25/26;
   MR25 = MR25/24;
   
   low0  =  iLow(NULL,PERIOD_D1,0);
   high0 =  iHigh(NULL,PERIOD_D1,0);
   TotalPips = R25/Risk_to_Reward_ratio;
   EntryLong = TotalPips - Stop_Loss;
   ENT_Long = low0 + EntryLong*Point;
   SL_Long    =  low0 - Stop_Loss*Point;
   TPLong =  EntryLong + R25; 
   TP_Long = ENT_Long + TPLong*Point;
   EntryShort =  TotalPips - Stop_Loss;
   ENT_Short  =  high0 - EntryShort*Point;
   SL_Short   =  high0 + Stop_Loss*Point;
   TPShort = R25 + high0 - EntryShort;
   TP_Short = ENT_Short - TPShort*Point;
   
   CurrentSpread = (Ask/ Point)-(Bid/ Point);

   today_range = (iHigh(NULL,PERIOD_D1,0)) - (iLow(NULL,PERIOD_D1,0));

   Text =   "Avg 60 Days Range: " +  R25  + "\n" +
            "Prev Day Range: " +  Prevday  + "\n" +
            "Current Day Range: " +  Curday  + "\n\n" +
            "Avg 26 Weeks Range: " +  WR25  + "\n" +
            "Prev Week Range: " +  Prevwk  + "\n" +
            "Current Week Range: " +  Curwk  + "\n\n" +
            "Avg 24 Months Range: " +  MR25  + "\n" +
            "Prev Month Range: " +  Prevmthy  + "\n" +
            "Current Month Range: " +  Curmthy  + "\n" +
            "Currency Pair spread: " +  CurrentSpread  + "\n" +
            dayMove + "\n\n";
            //"Today Range: " +  today_range  + "\n";
            
   Text =   Text +
            "Long Entry: " + EntryLong + " Pips at " + DoubleToStr(ENT_Long,nDigits)  + "\n" +        
            "Short Entry: " + EntryShort + " Pips at " + DoubleToStr(ENT_Short,nDigits)  + "\n\n" +
            "StopLosses should be :"  + "\n" +
            "Long:  " + Stop_Loss  + " Pips at " + DoubleToStr(SL_Long,nDigits)  + "\n" +
            "Short: " + Stop_Loss + " Pips at " + DoubleToStr(SL_Short,nDigits) + "\n\n" +
            "Take Profits should be :"  + "\n" +
            "Long:  " + TPLong  + " Pips at " + DoubleToStr(TP_Long,nDigits)  + "\n" +
            "Short: " + TPShort + " Pips at " + DoubleToStr(TP_Short,nDigits) + "\n\n" +
            "** Always Consider Spread on Pair Before Entering **" + "\n\n"; 
            
  
   Comment(Text);

   return(0);
  }
//+------------------------------------------------------------------+