_tp BrianIndex (SinglePair) V2.03

Download script
_tp BrianIndex (SinglePair) V2.03

This script is designed as a technical analysis tool for the MetaTrader 4 platform, specifically tailored to track price movements and volatility based on weekly historical data. It provides users with a visual representation of where the current price sits in relation to previous market behaviors.

Core Logic and Functionality

1. Historical Data Aggregation The script looks back at a specific number of past weeks (defined by the user) to calculate average price behavior. It identifies two key metrics from this history:

  • High-Point Deviations: The difference between the weekly highs and the previous week's closing prices.
  • Low-Point Deviations: The difference between the weekly lows and the previous week's closing prices.

By averaging these differences over many weeks, the script establishes a "baseline" for how much the price typically swings or stretches during a standard trading period.

2. Dynamic Level Calculation Using the historical averages mentioned above, the script generates four specific dynamic levels (HH1, HH2, LL1, and LL2). These levels are scaled using user-adjustable factors:

  • The High Levels (HH) represent projected upper boundaries based on historical volatility.
  • The Low Levels (LL) represent projected lower boundaries.

These levels help traders visualize potential "stretch" points where the price might face resistance or support based on past market patterns.

3. Current Position Tracking The script continuously calculates the current distance of the market price from the previous week's closing price. This allows the user to see, in real-time, how far the current price has moved (in pips/points) relative to where the market started at the beginning of the week.

4. Visual Interface and Real-Time Updates Rather than just plotting lines on a chart, the script provides a clean dashboard-style summary in a separate window:

  • Dynamic Labels: It creates on-screen text labels that display the current values for all four levels (HH1, HH2, LL1, LL2) and the current price displacement.
  • Automatic Updates: The tool is designed to update automatically as new data arrives, recalculating the averages whenever a new week begins to ensure the "baseline" remains relevant to recent market conditions.

Summary for the User

Essentially, this tool acts as a volatility gauge. It tells you: "Based on the last X weeks of history, how far is the price currently stretching compared to normal behavior?" It translates complex historical math into four easy-to-read levels that help identify whether the current market move is typical or extreme.

Price Data Components
Series array that contains close prices for each barSeries array that contains the highest prices of each barSeries array that contains the lowest prices of each barSeries array that contains close prices for each barSeries array that contains open time of each bar
Miscellaneous
Implements a curve of type %1
15 Views
0 Downloads
0 Favorites
_tp BrianIndex (SinglePair) V2.03
//+------------------------------------------------------------------+
//|                            _tp BrianIndex (SinglePair) V2.03.mq4 |
//|                                                                  |
//|  V2.00                                                           |
//|     1) Add 90% & 30% lines                                       |
//|                                                                  |
//|  V2.01                                                           |
//|     1) Show multiple history week value                          |
//|                                                                  |
//|  V2.02                                                           |
//|     1) Fix ObjectCreate error                                    |
//|                                                                  |
//|  V2.03                                                           |
//|     1) Fix the wrong calculation of Cur                          |
//+------------------------------------------------------------------+
#property indicator_separate_window
#property indicator_buffers      5
#property indicator_color1       Blue        
#property indicator_color2       Orange       
#property indicator_color3       Orange        
#property indicator_color4       Lime        
#property indicator_color5       Lime        

#property indicator_style1 STYLE_SOLID
#property indicator_style2 STYLE_DOT
#property indicator_style3 STYLE_DOT
#property indicator_style4 STYLE_DOT
#property indicator_style5 STYLE_DOT


#property indicator_level1       0
#property indicator_levelcolor   DarkGray

extern double Level1Factor = 2.3;
extern double Level2Factor = 0.85;
extern int HistoryWeek      = 104;
extern int FontSize         = 16;

string TimeFrame_value = "TimeFrame(0,M5,M15,M30,H1,D1)";
string TimeFrame = "0";
string MA_Type_value = "SMA, EMA, LWMA";
string MA_Type = "SMA";

int timeFrame = 0;
double pair1Close;
int totalWeek = 0;
int lastDay = 0;
int maType;

double BufBI[];
double BufBIHHLevel1[];
double BufBIHHLevel2[];
double BufBILLLevel2[];
double BufBILLLevel1[];

int MaxWeek = 0;
double WeekCloseArray[];
double WeekHHpipsArray[];
double WeekLLpipsArray[];
int History2, History3, History4, History5;

datetime LastWeekDateTime = 0;
int LastWeekBarShift;

string sShortName = "_tp BI (SP) V2.03";

int objectcnt=1;



int init() {

   IndicatorBuffers(5);

   IndicatorShortName(sShortName);


   SetIndexStyle(0,DRAW_LINE);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexStyle(2,DRAW_LINE);
   SetIndexStyle(3,DRAW_LINE);
   SetIndexStyle(4,DRAW_LINE);
   
   SetIndexLabel(0,"BI");
   SetIndexLabel(1,"HH1");
   SetIndexLabel(2,"HH22");
   SetIndexLabel(3,"LL2");
   SetIndexLabel(4,"LL1");

   SetIndexBuffer(0,BufBI);
   SetIndexBuffer(1,BufBIHHLevel1);
   SetIndexBuffer(2,BufBIHHLevel2);
   SetIndexBuffer(3,BufBILLLevel2);
   SetIndexBuffer(4,BufBILLLevel1);
   
   
   if (MA_Type == "SMA") {

      maType = MODE_SMA;

   } else if (MA_Type == "EMA") {

      maType = MODE_EMA;

   } else if (MA_Type == "LWMA") {

      maType = MODE_LWMA;

   } else {

      maType = MODE_SMA;

   }


   if (TimeFrame == "M5") {

      timeFrame = PERIOD_M5;

   } else if (TimeFrame == "M15") {

      timeFrame = PERIOD_M15;

   } else if (TimeFrame == "M30") {

      timeFrame = PERIOD_M30;

   } else if (TimeFrame == "H1") {

      timeFrame = PERIOD_H1;

   } else if (TimeFrame == "D1") {

      timeFrame = PERIOD_D1;

   } else {

      timeFrame = 0;

   }

//   HistoryWeek = MathMin(HistoryWeek+1, iBars(NULL,PERIOD_W1)) - 1;
   ArrayResize(WeekCloseArray, HistoryWeek);
   ArrayResize(WeekHHpipsArray, HistoryWeek);
   ArrayResize(WeekLLpipsArray, HistoryWeek);

   return(0);

}

int deinit() {

   ObjectsDeleteAll(0, OBJ_VLINE);

   return(0);
}

int start() {

   int i, j, counted_bars = IndicatorCounted();
   int AvailableWeek = 0;
   
   i = Bars - counted_bars - 1;
   
   
   double TotalHHtoLastFriClose = 0.0; 
   double TotalLLtoLastFriClose = 0.0;

   if (LastWeekDateTime != iTime(NULL,PERIOD_W1,1) )
   {
      for (j = HistoryWeek; j>0; j--)
      {
         if (iClose(NULL,PERIOD_W1,j+1) > 0)
         {
            TotalHHtoLastFriClose = TotalHHtoLastFriClose  + ( iHigh(NULL,PERIOD_W1,j) - iClose(NULL,PERIOD_W1,j+1) );
            TotalLLtoLastFriClose = TotalLLtoLastFriClose  + ( iLow(NULL,PERIOD_W1,j) - iClose(NULL,PERIOD_W1,j+1) );
            AvailableWeek++;
         }
      }
      TotalHHtoLastFriClose = TotalHHtoLastFriClose / AvailableWeek / MarketInfo(Symbol(),MODE_POINT);
      TotalLLtoLastFriClose = TotalLLtoLastFriClose / AvailableWeek / MarketInfo(Symbol(),MODE_POINT);

      LastWeekDateTime = iTime(NULL,PERIOD_D1,1);
    }

   while (i >= 0) 
   {

      LastWeekBarShift = iBarShift(NULL, PERIOD_W1, Time[i]) + 1;

      pair1Close = iClose(NULL, PERIOD_W1, LastWeekBarShift);  // closes of last week
 
      if (pair1Close > 0) 
      {
          
          if (lastDay != TimeDayOfWeek(iTime(NULL, timeFrame, i))) 
          {
            lastDay = TimeDayOfWeek(iTime(NULL, timeFrame, i));
            totalWeek = 0;

          }

          BufBI[i] = (iClose(Symbol(), timeFrame, i) - pair1Close) / MarketInfo(Symbol(),MODE_POINT);
          BufBIHHLevel1[i] = TotalHHtoLastFriClose * Level1Factor;
          BufBIHHLevel2[i] = TotalHHtoLastFriClose * Level2Factor;
          BufBILLLevel2[i] = TotalLLtoLastFriClose * Level2Factor;
          BufBILLLevel1[i] = TotalLLtoLastFriClose * Level1Factor;
      } 

      i--;

   }
   
   objectcnt=1;
   
   CreateLabelObject("HH1 = ",FontSize,3,Orange);
   CreateLabelObject(DoubleToStr(BufBIHHLevel1[0],0),FontSize,2,Orange);
   objectcnt++;
   CreateLabelObject("HH2 = ",FontSize,3,Orange);
   CreateLabelObject(DoubleToStr(BufBIHHLevel2[0],0),FontSize,2,Orange);
   objectcnt++;
   CreateLabelObject("Cur = ",FontSize,3,White);
   CreateLabelObject(DoubleToStr(BufBI[0],0),FontSize,2,White);
   objectcnt++;
   CreateLabelObject("LL2 = ",FontSize,3,Lime);
   CreateLabelObject(DoubleToStr(BufBILLLevel2[0],0),FontSize,2,Lime);
   CreateLabelObject(DoubleToStr(AvailableWeek,0),8,1,Gray);
   objectcnt++;
   CreateLabelObject("LL1 = ",FontSize,3,Lime);
   CreateLabelObject(DoubleToStr(BufBILLLevel1[0],0),FontSize,2,Lime);
   objectcnt++;


   return(0);
}

string formatInteger(int value) {

   if (value < 10) {
   
      return("0" + value);
   
   } else {
   
      return("" + value);
   
   }

}




void CreateLabelObject(string sString, int iFontSize, int iColumn, color cColor)
{
   string objectname = sShortName+objectcnt+"_"+iColumn;
   int xx;
   
   if (iColumn == 1)
      xx = 10;
   else if (iColumn == 2)
      xx = 10 + 20;
   else if (iColumn == 3)
      xx = 10 + 60;

   ObjectDelete(objectname);  
   ObjectCreate(objectname, OBJ_LABEL, WindowFind(sShortName), 0, 0);
   ObjectSet(objectname, OBJPROP_CORNER, 1);
   ObjectSet(objectname, OBJPROP_XDISTANCE, xx);
   ObjectSet(objectname, OBJPROP_YDISTANCE, 3+(objectcnt-1)*(iFontSize + 6));  
   ObjectSetText(objectname,sString, iFontSize,"Arial" ,cColor);

}

//+------------------------------------------------------------------+

Comments