MVV_LinearRegression

MVV_LinearRegression

This tool is designed to provide traders with a visual representation of market trends and price projections based on specific historical data points. By combining linear regression mathematics with moving average filtering, the script helps identify potential price targets and trend directions starting from a user-defined point in time.

Core Functionality

1. Historical Anchor Point The logic begins with a fixed date and time selected by the user. This acts as the "origin" of the analysis. The script looks back to this specific moment to establish the foundation for its calculations, ensuring that the trend analysis is rooted in a specific historical context rather than just the most recent price action.

2. Smart Trend Detection Before performing any complex math, the script evaluates the market's momentum at your chosen starting point using a 21-period Smoothing Moving Average (SMMA). It checks whether the closing price was above or below this average to determine the primary "sentiment" of the market:

  • If the price is trending upward relative to the average, it identifies a high point as a reference.
  • If the price is trending downward relative to the average, it identifies a low point as a reference.

3. Linear Regression Projection Once the starting point and sentiment are established, the script performs a linear regression calculation. In simple terms, it looks at the sequence of closing prices from your start date up until the current moment. It calculates a "line of best fit"—a straight path that most accurately represents the price movement over that period.

4. Visual Outputs The script translates these complex calculations into two clear visual elements on your chart:

  • The Trend Line: A solid line that connects the historical start point to the identified high or low point, showing you the trajectory of the market's movement.
  • The Price Target (Stop/Projection): The script calculates a specific price coordinate based on the regression formula and places a visual marker (an arrow) at that level. This represents where the current trend is mathematically projected to land based on its historical trajectory.

Summary for the Trader

Essentially, this tool answers the question: "Based on the price movement since [Date/Time], what is the mathematical path of the trend, and where is it heading?" It filters out minor market noise by using a moving average to set the direction and then uses regression math to project that direction forward onto your current chart.

Price Data Components
Series array that contains close prices for each bar
Indicators Used
Moving average indicator
9 Views
0 Downloads
0 Favorites
MVV_LinearRegression
//+------------------------------------------------------------------+
//|                                    MVV_LinearRegression+STOP.mq4 |
//+------------------------------------------------------------------+

#property indicator_chart_window

extern string _FixedDateTime = "2008.02.01 15:00";
extern color STOP_Color = Red;
extern color _TrendLineColor = Black;
extern int LR_WIDTH = 1;
int _N_Time;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init() {
//----
 IndicatorShortName("MVV_LinearRegression+STOP ("+_FixedDateTime+")");
  //----------------------LR------------------------------
  ObjectCreate("LR("+_FixedDateTime+")",4,0,Time[0],0,Time[0],0);
  ObjectSet("LR("+_FixedDateTime+")",OBJPROP_COLOR,_TrendLineColor); 
  ObjectSet("LR("+_FixedDateTime+")",OBJPROP_RAY,1);
  ObjectSet("LR("+_FixedDateTime+")",OBJPROP_STYLE,STYLE_SOLID);
  ObjectSet("LR("+_FixedDateTime+")",OBJPROP_WIDTH,LR_WIDTH);
  _N_Time = 0;
  ObjectCreate("STOP("+_FixedDateTime+")",OBJ_ARROW,0,iTime(NULL, 0,0),0,0,0,0,0);                     
  ObjectSet("STOP("+_FixedDateTime+")",OBJPROP_ARROWCODE,SYMBOL_RIGHTPRICE);
  ObjectSet("STOP("+_FixedDateTime+")",OBJPROP_COLOR,STOP_Color);
return(0);
            }
//+------------------------------------------------------------------+

int deinit() {
  ObjectDelete("LR("+_FixedDateTime+")");
  ObjectDelete("STOP("+_FixedDateTime+")");
return(0);
             }

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

int start() {if ( _N_Time == Time[0] ) return(0);

int start, stop, i_max, i_min;

datetime _time = StrToTime(_FixedDateTime);
start = iBarShift(NULL, Period(), _time, false);

 i_min= iLowest(NULL,0,MODE_LOW,start,1);
 i_max= iHighest(NULL,0,MODE_HIGH,start,1); 
 if(iClose(NULL,0,start) < iMA(NULL,0,21,0,MODE_SMMA,PRICE_CLOSE,start))  stop=i_max;
 if(iClose(NULL,0,start) > iMA(NULL,0,21,0,MODE_SMMA,PRICE_CLOSE,start))  stop=i_min;
 
 _N_Time = Time[0];

if(stop>5)
  {int n=start+1;
//---- calculate price values
   double value=iClose(Symbol(),0,0);
   double a,b,c;
   double sumy=value;
   double sumx=0.0;
   double sumxy=0.0;
   double sumx2=0.0;
   for(int i=1; i<n; i++)
     {
      value=iClose(Symbol(),0,i);
      sumy+=value;
      sumxy+=value*i;
      sumx+=i;
      sumx2+=i*i;
     }
   c=sumx2*n-sumx*sumx;
   if(c==0.0) return;
   b=(sumxy*n-sumx*sumy)/c;
   a=(sumy-sumx*b)/n;
  }
ObjectMove("LR("+_FixedDateTime+")", 0, Time[start],0);
ObjectMove("LR("+_FixedDateTime+")", 1, Time[stop],0);
ObjectMove("STOP("+_FixedDateTime+")",0, iTime(NULL, 0,0),a);
//Alert(LR.price.2);

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

Comments