//+------------------------------------------------------------------+
//|                             #(T_S_R)-Weekly Range Calculator.mq4 |
//|                      Copyright © 2006, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property link      "Data window & extra Periods by cja"
//+------------------------------------------------------------------+
//|                                                   TSR_Ranges.mq4 |
//|                                         Copyright © 2006, Ogeima |
//|                                             ph_bresson@yahoo.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Ogeima"
#property link      "ph_bresson@yahoo.com"

#property indicator_separate_window
//---- input parameters
extern double  Risk_to_Reward_ratio =  3.0;
extern int First_AV = 5;
extern int Second_AV = 10;
extern int Third_AV = 20;
int nDigits;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
IndicatorShortName("TSR_Weekly");
   if(Symbol()=="GBPJPY" || Symbol()=="EURJPY" || Symbol()=="USDJPY" || Symbol()=="CHFJPY" || Symbol()=="AUDJPY") nDigits = 2;
   if(Symbol()=="GBPUSD" || Symbol()=="EURUSD" || Symbol()=="NZDUSD" || Symbol()=="USDCHF"  ||
   Symbol()=="USDCAD" || Symbol()=="AUDUSD" || Symbol()=="AUDCAD" || Symbol()=="EURCHF"  || Symbol()=="EURGBP"
   || Symbol()=="EURCAD" || Symbol()=="EURAUD" ||Symbol()=="AUDNZD"|| Symbol()=="GBPCHF" )nDigits = 4;
   
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
  // Comment(""); 
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   //----
   int R1=0,R5=0,R10=0,R20=0,RAvg=0;
   int RoomUp=0,RoomDown=0,StopLoss_Long=0,StopLoss_Short=0;
   double   SL_Long=0,SL_Short=0;
   double   low0=0,high0=0;
   string   Text="";
   int i=0;

   R1 =  (iHigh(NULL,PERIOD_W1,1)-iLow(NULL,PERIOD_W1,1))/Point;
   for(i=1;i<=First_AV;i++)
      R5    =    R5  +  (iHigh(NULL,PERIOD_W1,i)-iLow(NULL,PERIOD_W1,i))/Point;
   for(i=1;i<=Second_AV;i++)
      R10   =    R10 +  (iHigh(NULL,PERIOD_W1,i)-iLow(NULL,PERIOD_W1,i))/Point;
   for(i=1;i<=Third_AV;i++)
      R20   =    R20 +  (iHigh(NULL,PERIOD_W1,i)-iLow(NULL,PERIOD_W1,i))/Point;

   R5 = R5/First_AV;
   R10 = R10/Second_AV;
   R20 = R20/Third_AV;
   RAvg  =  (R1+R5+R10+R20)/4; //RAvg  =  (R5+R10+R20)/3;new setting    

   low0  =  iLow(NULL,PERIOD_W1,0);
   high0 =  iHigh(NULL,PERIOD_W1,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;


   Text =   "Average Weeks Range: " +  RAvg + "\n"  + 
            "Prev 01  Weeks  Range: " +  R1   + "\n" + 
            "Prev 05  Weeks Range: " +  R5   + "\n" + 
            "Prev 10  Weeks Range: " +  R10  + "\n" +
            "Prev 20  Weeks Range: " +  R20  + "\n";
   Text =   Text +
            "Room Up:     " + RoomUp              + "\n" +
            "Room Down: " + RoomDown            + "\n" +
            "Maximum StopLosses :"  + "\n" +
            "Long:  " + StopLoss_Long  + " Pips at " + DoubleToStr(SL_Long,nDigits)  + "\n" +
            "Short: " + StopLoss_Short + " Pips at " + DoubleToStr(SL_Short,nDigits) + "\n" ;

   //Comment(Text);
  
   string P=Period();
  
   
        ObjectCreate("WTSR", OBJ_LABEL, WindowFind("TSR_Weekly"), 0, 0);
        ObjectSetText("WTSR",StringSubstr(Symbol(),0),12, "Arial Bold", CadetBlue);
        ObjectSet("WTSR", OBJPROP_CORNER, 0);
        ObjectSet("WTSR", OBJPROP_XDISTANCE, 25);
        ObjectSet("WTSR", OBJPROP_YDISTANCE, 2);
        ObjectCreate("WTSR1", OBJ_LABEL, WindowFind("TSR_Weekly"), 0, 0);
        ObjectSetText("WTSR1",StringSubstr(P,0),12, "Arial Bold", CadetBlue);
        ObjectSet("WTSR1", OBJPROP_CORNER, 0);
        ObjectSet("WTSR1", OBJPROP_XDISTANCE, 100);
        ObjectSet("WTSR1", OBJPROP_YDISTANCE, 2);
        
        ObjectCreate("WTSR2", OBJ_LABEL, WindowFind("TSR_Weekly"), 0, 0);
        ObjectSetText("WTSR2","Average Weekly Range:", 10, "Arial Bold", CadetBlue);
        ObjectSet("WTSR2", OBJPROP_CORNER, 0);
        ObjectSet("WTSR2", OBJPROP_XDISTANCE, 150);
        ObjectSet("WTSR2", OBJPROP_YDISTANCE, 2);
        ObjectCreate("WTSR3", OBJ_LABEL, WindowFind("TSR_Weekly"), 0, 0);
        ObjectSetText("WTSR3",DoubleToStr(RAvg ,0),12, "Arial Bold", Orange);
        ObjectSet("WTSR3", OBJPROP_CORNER, 0);
        ObjectSet("WTSR3", OBJPROP_XDISTANCE, 310);
        ObjectSet("WTSR3", OBJPROP_YDISTANCE, 2);
        
        ObjectCreate("WTSR4", OBJ_LABEL, WindowFind("TSR_Weekly"), 0, 0);
        ObjectSetText("WTSR4","Prev 1 Week Range:", 10, "Arial ", LightSteelBlue);
        ObjectSet("WTSR4", OBJPROP_CORNER, 0);
        ObjectSet("WTSR4", OBJPROP_XDISTANCE, 25);
        ObjectSet("WTSR4", OBJPROP_YDISTANCE, 20);
        ObjectCreate("WTSR5", OBJ_LABEL, WindowFind("TSR_Weekly"), 0, 0);
        ObjectSetText("WTSR5",DoubleToStr(R1,0),12, "Arial Bold", Orange);
        ObjectSet("WTSR5", OBJPROP_CORNER, 0);
        ObjectSet("WTSR5", OBJPROP_XDISTANCE, 170);
        ObjectSet("WTSR5", OBJPROP_YDISTANCE, 20);
        
        ObjectCreate("WTSR6", OBJ_LABEL, WindowFind("TSR_Weekly"), 0, 0);
        ObjectSetText("WTSR6","Prev "+First_AV+" Weeks Range:", 10, "Arial ", LightSteelBlue);
        ObjectSet("WTSR6", OBJPROP_CORNER, 0);
        ObjectSet("WTSR6", OBJPROP_XDISTANCE, 25);
        ObjectSet("WTSR6", OBJPROP_YDISTANCE, 35);
        ObjectCreate("WTSR7", OBJ_LABEL, WindowFind("TSR_Weekly"), 0, 0);
        ObjectSetText("WTSR7",DoubleToStr(R5,0),12, "Arial Bold", Orange);
        ObjectSet("WTSR7", OBJPROP_CORNER, 0);
        ObjectSet("WTSR7", OBJPROP_XDISTANCE, 170);
        ObjectSet("WTSR7", OBJPROP_YDISTANCE, 35);
        
        ObjectCreate("WTSR8", OBJ_LABEL, WindowFind("TSR_Weekly"), 0, 0);
        ObjectSetText("WTSR8","Prev "+Second_AV+" Weeks Range:", 10, "Arial ", LightSteelBlue);
        ObjectSet("WTSR8", OBJPROP_CORNER, 0);
        ObjectSet("WTSR8", OBJPROP_XDISTANCE, 220);
        ObjectSet("WTSR8", OBJPROP_YDISTANCE, 20);
        ObjectCreate("WTSR9", OBJ_LABEL, WindowFind("TSR_Weekly"), 0, 0);
        ObjectSetText("WTSR9",DoubleToStr(R10,0),12, "Arial Bold", Orange);
        ObjectSet("WTSR9", OBJPROP_CORNER, 0);
        ObjectSet("WTSR9", OBJPROP_XDISTANCE, 370);
        ObjectSet("WTSR9", OBJPROP_YDISTANCE, 20);
        
        ObjectCreate("WTSR10", OBJ_LABEL, WindowFind("TSR_Weekly"), 0, 0);
        ObjectSetText("WTSR10","Prev "+Third_AV+" Weeks Range:", 10, "Arial ", LightSteelBlue);
        ObjectSet("WTSR10", OBJPROP_CORNER, 0);
        ObjectSet("WTSR10", OBJPROP_XDISTANCE, 220);
        ObjectSet("WTSR10", OBJPROP_YDISTANCE, 35);
        ObjectCreate("WTSR11", OBJ_LABEL, WindowFind("TSR_Weekly"), 0, 0);
        ObjectSetText("WTSR11",DoubleToStr(R20,0),12, "Arial Bold", Orange);
        ObjectSet("WTSR11", OBJPROP_CORNER, 0);
        ObjectSet("WTSR11", OBJPROP_XDISTANCE, 370);
        ObjectSet("WTSR11", OBJPROP_YDISTANCE, 35);
        
        ObjectCreate("WTSR12", OBJ_LABEL, WindowFind("TSR_Weekly"), 0, 0);
        ObjectSetText("WTSR12","Room UP:", 10, "Arial ", LightSteelBlue);
        ObjectSet("WTSR12", OBJPROP_CORNER, 0);
        ObjectSet("WTSR12", OBJPROP_XDISTANCE, 420);
        ObjectSet("WTSR12", OBJPROP_YDISTANCE, 20);
        ObjectCreate("WTSR13", OBJ_LABEL, WindowFind("TSR_Weekly"), 0, 0);
        ObjectSetText("WTSR13",DoubleToStr(RoomUp,0),12, "Arial Bold", Orange);
        ObjectSet("WTSR13", OBJPROP_CORNER, 0);
        ObjectSet("WTSR13", OBJPROP_XDISTANCE, 490);
        ObjectSet("WTSR13", OBJPROP_YDISTANCE, 20);
        
        ObjectCreate("WTSR14", OBJ_LABEL, WindowFind("TSR_Weekly"), 0, 0);
        ObjectSetText("WTSR14","Room DN:", 10, "Arial ", LightSteelBlue);
        ObjectSet("WTSR14", OBJPROP_CORNER, 0);
        ObjectSet("WTSR14", OBJPROP_XDISTANCE, 420);
        ObjectSet("WTSR14", OBJPROP_YDISTANCE, 35);
        ObjectCreate("WTSR15", OBJ_LABEL, WindowFind("TSR_Weekly"), 0, 0);
        ObjectSetText("WTSR15",DoubleToStr(RoomDown,0),12, "Arial Bold", Orange);
        ObjectSet("WTSR15", OBJPROP_CORNER, 0);
        ObjectSet("WTSR15", OBJPROP_XDISTANCE, 490);
        ObjectSet("WTSR15", OBJPROP_YDISTANCE, 35);
        
        ObjectCreate("WTSR16", OBJ_LABEL, WindowFind("TSR_Weekly"), 0, 0);
        ObjectSetText("WTSR16","Maximum StopLosses;",10, "Arial Bold", CadetBlue);
        ObjectSet("WTSR16", OBJPROP_CORNER, 0);
        ObjectSet("WTSR16", OBJPROP_XDISTANCE, 560);
        ObjectSet("WTSR16", OBJPROP_YDISTANCE, 2);
        
        ObjectCreate("WTSR17", OBJ_LABEL, WindowFind("TSR_Weekly"), 0, 0);
        ObjectSetText("WTSR17","Long:             Pips at", 10, "Arial ", LightSteelBlue);
        ObjectSet("WTSR17", OBJPROP_CORNER, 0);
        ObjectSet("WTSR17", OBJPROP_XDISTANCE, 560);
        ObjectSet("WTSR17", OBJPROP_YDISTANCE, 20);
        ObjectCreate("WTSR18", OBJ_LABEL, WindowFind("TSR_Weekly"), 0, 0);
        ObjectSetText("WTSR18",DoubleToStr(StopLoss_Long,0),14, "Arial Bold", LimeGreen);
        ObjectSet("WTSR18", OBJPROP_CORNER, 0);
        ObjectSet("WTSR18", OBJPROP_XDISTANCE, 600);
        ObjectSet("WTSR18", OBJPROP_YDISTANCE, 17);
        
        ObjectCreate("WTSR19", OBJ_LABEL, WindowFind("TSR_Weekly"), 0, 0);
        ObjectSetText("WTSR19","Short:             Pips at", 10, "Arial ", LightSteelBlue);
        ObjectSet("WTSR19", OBJPROP_CORNER, 0);
        ObjectSet("WTSR19", OBJPROP_XDISTANCE, 560);
        ObjectSet("WTSR19", OBJPROP_YDISTANCE, 35);
        ObjectCreate("WTSR20", OBJ_LABEL, WindowFind("TSR_Weekly"), 0, 0);
        ObjectSetText("WTSR20",DoubleToStr(StopLoss_Short,0),14, "Arial Bold", Red);
        ObjectSet("WTSR20", OBJPROP_CORNER, 0);
        ObjectSet("WTSR20", OBJPROP_XDISTANCE, 600);
        ObjectSet("WTSR20", OBJPROP_YDISTANCE, 32);
        
        ObjectCreate("WTSR21", OBJ_LABEL, WindowFind("TSR_Weekly"), 0, 0);
        ObjectSetText("WTSR21",DoubleToStr(SL_Long,nDigits),12, "Arial Bold", LimeGreen);
        ObjectSet("WTSR21", OBJPROP_CORNER, 0);
        ObjectSet("WTSR21", OBJPROP_XDISTANCE, 690);
        ObjectSet("WTSR21", OBJPROP_YDISTANCE, 20);
        ObjectCreate("WTSR22", OBJ_LABEL, WindowFind("TSR_Weekly"), 0, 0);
        ObjectSetText("WTSR22",DoubleToStr(SL_Short,nDigits),12, "Arial Bold",Red);
        ObjectSet("WTSR22", OBJPROP_CORNER, 0);
        ObjectSet("WTSR22", OBJPROP_XDISTANCE, 690);
        ObjectSet("WTSR22", OBJPROP_YDISTANCE, 35);
        
        ObjectCreate("WTSR23", OBJ_LABEL, WindowFind("TSR_Weekly"), 0, 0);
        ObjectSetText("WTSR23","Risk to Reward Ratio:", 10, "Arial Bold", CadetBlue);
        ObjectSet("WTSR23", OBJPROP_CORNER, 0);
        ObjectSet("WTSR23", OBJPROP_XDISTANCE, 350);
        ObjectSet("WTSR23", OBJPROP_YDISTANCE, 2);
        ObjectCreate("WTSR24", OBJ_LABEL, WindowFind("TSR_Weekly"), 0, 0);
        ObjectSetText("WTSR24",DoubleToStr( Risk_to_Reward_ratio ,0),12, "Arial Bold", Orange);
        ObjectSet("WTSR24", OBJPROP_CORNER, 0);
        ObjectSet("WTSR24", OBJPROP_XDISTANCE, 500);
        ObjectSet("WTSR24", OBJPROP_YDISTANCE, 2);
        
     
        double HIWeekly = iMA(Symbol(),PERIOD_W1,1,0,MODE_HIGH,PRICE_HIGH,0);
        double LOWWeekly = iMA(Symbol(),PERIOD_W1,1,0,MODE_LOW,PRICE_LOW,0); 
        //double YEST_HIDaily = iMA(Symbol(),PERIOD_D1,1,0,MODE_HIGH,PRICE_HIGH,1);
        //double YEST_LOWDaily = iMA(Symbol(),PERIOD_D1,1,0,MODE_LOW,PRICE_LOW,1); 
   
        ObjectCreate("Whigh", OBJ_LABEL, WindowFind("TSR_Weekly"), 0, 0);
        ObjectSetText("Whigh",DoubleToStr(HIWeekly,Digits), 12, "Arial Bold", Orange);
        ObjectSet("Whigh", OBJPROP_CORNER, 0);
        ObjectSet("Whigh", OBJPROP_XDISTANCE, 890);
        ObjectSet("Whigh", OBJPROP_YDISTANCE, 20);
        
        ObjectCreate("Whigh2", OBJ_LABEL, WindowFind("TSR_Weekly"), 0, 0);
        ObjectSetText("Whigh2","WeeklyHigh", 9, "Arial Bold", CadetBlue);
        ObjectSet("Whigh2", OBJPROP_CORNER, 0);
        ObjectSet("Whigh2", OBJPROP_XDISTANCE, 890);
        ObjectSet("Whigh2", OBJPROP_YDISTANCE, 2);
        
        ObjectCreate("Wlow", OBJ_LABEL, WindowFind("TSR_Weekly"), 0, 0);
        ObjectSetText("Wlow",DoubleToStr(LOWWeekly,Digits), 12, "Arial Bold", Orange);
        ObjectSet("Wlow", OBJPROP_CORNER, 0);
        ObjectSet("Wlow", OBJPROP_XDISTANCE, 830);
        ObjectSet("Wlow", OBJPROP_YDISTANCE, 20);
        
        ObjectCreate("Wlow2", OBJ_LABEL, WindowFind("TSR_Weekly"), 0, 0);
        ObjectSetText("Wlow2","WeeklyLow", 9, "Arial Bold", CadetBlue);
        ObjectSet("Wlow2", OBJPROP_CORNER, 0);
        ObjectSet("Wlow2", OBJPROP_XDISTANCE, 810);
        ObjectSet("Wlow2", OBJPROP_YDISTANCE, 2);
        
         double CURR = iMA(Symbol(),0,1,0,MODE_EMA,PRICE_CLOSE,0);
   
           
        ObjectCreate("Whigh3", OBJ_LABEL, WindowFind("TSR_Weekly"), 0, 0);
        ObjectSetText("Whigh3",DoubleToStr(CURR,Digits), 12, "Arial Bold", Coral);
        ObjectSet("Whigh3", OBJPROP_CORNER, 0);
        ObjectSet("Whigh3", OBJPROP_XDISTANCE, 890);
        ObjectSet("Whigh3", OBJPROP_YDISTANCE,35 );
            
        ObjectCreate("Whigh4", OBJ_LABEL, WindowFind("TSR_Weekly"), 0, 0);
        ObjectSetText("Whigh4",DoubleToStr(CURR,Digits), 12, "Arial Bold", Coral);
        ObjectSet("Whigh4", OBJPROP_CORNER, 0);
        ObjectSet("Whigh4", OBJPROP_XDISTANCE, 830);
        ObjectSet("Whigh4", OBJPROP_YDISTANCE,35 );
        
        ObjectCreate("Wlow4", OBJ_LABEL, WindowFind("TSR_Weekly"), 0, 0);
        ObjectSetText("Wlow4","Curr/Week", 9, "Arial ", LightSteelBlue);
        ObjectSet("Wlow4", OBJPROP_CORNER, 0);
        ObjectSet("Wlow4", OBJPROP_XDISTANCE, 760);
        ObjectSet("Wlow4", OBJPROP_YDISTANCE, 20);
        ObjectCreate("Wlow5", OBJ_LABEL, WindowFind("TSR_Weekly"), 0, 0);
        ObjectSetText("Wlow5","Price", 9, "Arial ", LightSteelBlue);
        ObjectSet("Wlow5", OBJPROP_CORNER, 0);
        ObjectSet("Wlow5", OBJPROP_XDISTANCE, 790);
        ObjectSet("Wlow5", OBJPROP_YDISTANCE, 37);
   
   


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