_(T_S_R)-Weekly_Range_Calculator

Download script
Author: Copyright � 2006, Ogeima
_(T_S_R)-Weekly_Range_Calculator

This script serves as a professional analytical tool designed to calculate and display weekly price range statistics. It is primarily used by traders to visualize historical volatility and determine potential risk management parameters—specifically regarding "Stop Loss" levels—based on average market movements.

Core Functionality

1. Historical Range Analysis The logic automatically scans past weekly data to determine the difference between the highest and lowest prices reached during specific timeframes. It calculates these ranges for:

  • The previous single week.
  • An average of the last 5 weeks.
  • An average of the last 10 weeks.
  • An average of the last 20 weeks.

2. Dynamic Average Calculation The tool synthesizes these various historical ranges into a single "Average Weekly Range." This provides the user with a standardized metric of how much the currency pair typically fluctuates within a week, smoothing out extreme one-time events to provide a more reliable baseline for expectations.

3. "Room" and Volatility Assessment The script calculates two specific metrics, referred to as "Room Up" and "Room Down." These values measure the distance between the current market price and the high/low of the current week relative to the calculated average range. This helps a trader understand how much "space" is left for the price to move before it hits typical volatility boundaries.

4. Automated Risk Management (Stop Loss Calculation) One of the most practical features of this logic is its ability to suggest Stop Loss positions. By using a user-defined Risk-to-Reward Ratio, the script performs the following:

  • It calculates a potential stop loss in "Pips" based on the available room in the market.
  • It converts those pips into actual price coordinates (e.g., "Price at 1.2345").
  • It provides separate calculations for both Long (buying) and Short (selling) positions, ensuring that the risk taken is mathematically aligned with the expected reward.

Visual Presentation

Rather than just providing raw numbers in a list, the script generates a comprehensive dashboard directly on the trading chart. This visual interface displays:

  • The current symbol and timeframe.
  • A breakdown of the 1, 5, 10, and 20-week ranges.
  • The calculated Average Weekly Range.
  • The "Room" values for both directions.
  • Specific price levels for Long and Short Stop Losses.
  • Current market prices compared against the previous week's high and low.

Summary for the User

In essence, this tool takes complex historical data (looking back at 20 weeks of price action) and simplifies it into an actionable dashboard. It tells you how much a pair normally moves, where the current price sits in relation to that movement, and exactly where you should place your safety net (Stop Loss) to maintain a specific risk-to-reward profile.

Price Data Components
Series array that contains the highest prices of each barSeries array that contains the lowest prices of each bar
Indicators Used
Moving average indicator
15 Views
0 Downloads
0 Favorites
_(T_S_R)-Weekly_Range_Calculator
//+------------------------------------------------------------------+
//|                             #(T_S_R)-Weekly Range Calculator.mq4 |
//|                      Copyright © 2006, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"
//+------------------------------------------------------------------+
//|                             #(T_S_R)-Daily Range Calculator .mq4 |
//|                      Copyright © 2006, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"
#property link      "Data window 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;
int nDigits;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
IndicatorShortName("TSR_Weekly");
   if(Symbol()=="GBPJPY" || Symbol()=="EURJPY" || Symbol()=="USDJPY" || Symbol()=="GOLD" || Symbol()=="USDMXN") nDigits = 2;
   if(Symbol()=="GBPUSD" || Symbol()=="EURUSD" || Symbol()=="NZDUSD" || Symbol()=="USDCHF"  ||
   Symbol()=="USDCAD" || Symbol()=="AUDUSD" || Symbol()=="EURUSD" || Symbol()=="EURCHF"  || Symbol()=="EURGBP"
   || Symbol()=="EURCAD" || Symbol()=="EURAUD" )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<=5;i++)
      R5    =    R5  +  (iHigh(NULL,PERIOD_W1,i)-iLow(NULL,PERIOD_W1,i))/Point;
   for(i=1;i<=10;i++)
      R10   =    R10 +  (iHigh(NULL,PERIOD_W1,i)-iLow(NULL,PERIOD_W1,i))/Point;
   for(i=1;i<=20;i++)
      R20   =    R20 +  (iHigh(NULL,PERIOD_W1,i)-iLow(NULL,PERIOD_W1,i))/Point;

   R5 = R5/5;
   R10 = R10/10;
   R20 = R20/20;
   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 01 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, 165);
        ObjectSet("WTSR5", OBJPROP_YDISTANCE, 20);
        
        ObjectCreate("WTSR6", OBJ_LABEL, WindowFind("TSR_Weekly"), 0, 0);
        ObjectSetText("WTSR6","Prev 05 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, 165);
        ObjectSet("WTSR7", OBJPROP_YDISTANCE, 35);
        
        ObjectCreate("WTSR8", OBJ_LABEL, WindowFind("TSR_Weekly"), 0, 0);
        ObjectSetText("WTSR8","Prev 10 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, 360);
        ObjectSet("WTSR9", OBJPROP_YDISTANCE, 20);
        
        ObjectCreate("WTSR10", OBJ_LABEL, WindowFind("TSR_Weekly"), 0, 0);
        ObjectSetText("WTSR10","Prev 20 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, 360);
        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);
  }
//+------------------------------------------------------------------+

Comments