Least%20Square%20MA

Download script
Least%20Square%20MA

This specialized technical tool is designed to help traders visualize price momentum and trends by calculating a weighted average of historical price data. Unlike standard indicators that treat all previous prices equally, this script applies a mathematical logic that prioritizes the significance of specific data points within a set timeframe.

Core Operational Logic

The primary function of this script is to analyze price movement over a defined look-back period (determined by the user). It performs several key steps to transform raw price data into a visual trend signal:

1. Weighted Calculation The tool examines the closing prices over a specific range. Instead of simply adding these prices together, it applies a weighting system where the "importance" of each price point is adjusted based on its position relative to the timeframe. This creates a more nuanced view of whether the current market direction has actual strength or if it is merely a temporary fluctuation.

2. Trend Smoothing By processing the weights through a mathematical formula, the script produces a smoothed value. This helps filter out "market noise"—the minor, erratic price jumps that can often trigger false signals. The result is a cleaner line that reflects the underlying trajectory of the market.

3. Dynamic Visual Mapping The tool uses three distinct color channels (Yellow, Green, and Red) to communicate trend changes to the user in real-time:

  • Directional Monitoring: The script constantly compares the current weighted value against the previous one.
  • Color Transitions: When the logic detects a specific shift in momentum—specifically whether the weighted value is increasing or decreasing compared to the preceding bar—it dynamically assigns colors to the visual output.
  • Visual Clarity: By utilizing "Empty Values" during specific conditions, the script ensures that the visual display only shows active signals. This prevents the chart from becoming cluttered with unnecessary data, allowing the trader to see exactly where a trend is gaining strength or losing steam.

Summary for the User

Essentially, this tool acts as a momentum filter. It looks back at recent history, calculates the "true" weight of those movements, and provides a color-coded visual guide. It helps you identify whether the market's current path is supported by consistent weighted momentum or if it is beginning to reverse, making it easier to spot potential shifts in trend direction at a glance.

Miscellaneous
Implements a curve of type %1
12 Views
0 Downloads
0 Favorites
Least%20Square%20MA
/*-----------------------------+
|			       |
| Shared by www.Aptrafx.com    |
|			       |
+------------------------------*/

//+------------------------------------------------------------------+
//|                                                        |
//|                      Copyright © 2004, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property  copyright "Copyright © 2005, FX Sniper "
#property  link      "http://www.metaquotes.net/"

//---- indicator settings
//#property  indicator_chart_window
#property indicator_separate_window
#property  indicator_buffers 3
#property indicator_color1 Yellow      
#property indicator_color2 Green
#property indicator_color3 Red

//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
int width;

extern int Rperiod = 34;
extern int Draw4HowLongg = 1500;
int Draw4HowLong;
int shift;
int i;
int loopbegin;
double sum[];
int length;
double lengthvar;
double tmp ;
double wt[];
int c;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- 2 additional buffers are used for counting.
   IndicatorBuffers(5);
   
//---- drawing settings
   SetIndexBuffer(2,ExtMapBuffer1);
   SetIndexBuffer(1,ExtMapBuffer2);
   SetIndexBuffer(0,ExtMapBuffer3);
   SetIndexBuffer(3,sum);
   SetIndexBuffer(4,wt);
   
   SetIndexStyle(2,DRAW_LINE,STYLE_SOLID,2);
   SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,2);
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2);

//---- initialization done
   return(0);
  }

int start()

  {   Draw4HowLong = Bars-Rperiod - 5;
      length = Rperiod;
      loopbegin = Draw4HowLong - length - 1;
 
      for(shift = loopbegin; shift >= 0; shift--)
      { 
         sum[1] = 0;
         for(i = length; i >= 1  ; i--)
         {
         lengthvar = length + 1;
         lengthvar /= 3;
         tmp = 0;
         tmp = ( i - lengthvar)*Close[length-i+shift];
         sum[1]+=tmp;
         }
         wt[shift] = sum[1]*6/(length*(length+1));
         
//========== COLOR CODING ===========================================               
        
       ExtMapBuffer3[shift] = wt[shift]; //red 
       ExtMapBuffer2[shift] = wt[shift]; //green
       ExtMapBuffer1[shift] = wt[shift]; //yellow
       
       //  for(c=loopbegin;c==shift;c++)
       // {
        if (wt[shift+1] > wt[shift])
        {
        ExtMapBuffer2[shift+1] = EMPTY_VALUE;
  // ObjectCreate("smiley_face", OBJ_ARROW, 0, Time[shift], Low[shift]-Point*20);
  // Print("time=  ",Time[shift]);
  // ObjectSet("smiley_face", OBJPROP_ARROWCODE, 242);
  // ObjectSet("smiley_face", OBJPROP_COLOR , Red);
  // ObjectSet("smiley_face", OBJPROP_WIDTH  , 1);
  // ObjectsRedraw();

        //ExtMapBuffer3[shift+1] = EMPTY_VALUE;
        //ExtMapBuffer3[shift+1] = EMPTY_VALUE;
        
        }
       else if (wt[shift+1] < wt[shift]) 
        {
        ExtMapBuffer1[shift+1] = EMPTY_VALUE; //-1 red/greem tight
       //ExtMapBuffer3[shift+1] = EMPTY_VALUE;
        
        }
         else 
         {
         
         ExtMapBuffer1[shift+1]=CLR_NONE;//EMPTY_VALUE;
         ExtMapBuffer2[shift+1]=CLR_NONE;//EMPTY_VALUE;
         }
        
      }
    
      return(0);
  }
//+------------------------------------------------------------------+

Comments