LSMA_AppliedPrice1_v1

Download script
LSMA_AppliedPrice1_v1

This script is designed to act as a custom technical analysis tool for trading platforms, specifically providing a smoothed moving average line on your price chart. Unlike standard moving averages that might treat all historical data with equal importance, this tool uses a "Linear Weighted" approach, which gives more significance to recent price action while still maintaining a smooth visual flow.

Core Functionality

The primary purpose of this script is to calculate and display a Least Squares Moving Average (LSMA). In plain terms, it helps traders identify the underlying trend of an asset by filtering out minor market "noise." Because it uses a linear weighting system, it reacts more dynamically to current price movements than a simple moving average, while remaining stable enough to provide a clear directional guide.

How the Calculation Works

The logic follows a three-step process to determine the line's position at any given moment:

  1. Price Selection: The user can choose exactly which type of price the indicator should "watch." You aren't limited to just the closing price; the script allows you to calculate the average based on several different metrics, including:

    • Standard prices (Open, High, Low, or Close)
    • The Median (the midpoint between the high and low)
    • The Typical Price (an average of the high, low, and close)
    • The Weighted Price (a specific calculation that leans more heavily toward the closing price)
  2. Weighting Logic: Instead of simply adding up prices over a set period (like 14 days), the script applies a mathematical weight to each point in that period. It calculates a cumulative sum where the weights adjust based on the position of the data point relative to the current time, ensuring the most recent price data has a stronger influence on the resulting line.

  3. Smoothing and Display: Finally, the script performs a normalization calculation to convert those weighted sums into a single, smooth value. This value is then plotted as a solid yellow line directly onto your trading chart.

Key Features

  • Customizable Sensitivity: Through adjustable settings, you can define the look-back period (how much historical data to consider) and which specific price type the indicator should prioritize.
  • Error Prevention: The script includes a built-in safety check. If a user tries to input an unsupported price type (anything outside of the 0–6 range), the tool will trigger an alert to ensure the calculation remains accurate.
  • Visual Clarity: By rendering as a solid line on the chart window, it provides an immediate visual cue for trend identification, making it easier for traders to see whether the market is currently trending upward, downward, or moving sideways.
Miscellaneous
Implements a curve of type %1It issuies visual alerts to the screen
9 Views
0 Downloads
0 Favorites
LSMA_AppliedPrice1_v1
/*-----------------------------+
|			       |
| Shared by www.Aptrafx.com    |
|			       |
+------------------------------*/

//+------------------------------------------------------------------+
//|                                            LSMA_AppliedPrice.mq4 |
//|                      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_buffers 1
#property indicator_color1 Yellow      

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

extern int Rperiod = 14;
extern int Draw4HowLongg = 1500;
extern int LSMA_AppliedPrice = 0;
// Applied Price enumerations
// 0=close, 1=open, 2=high, 3=low, 4=median((h+l/2)), 5=typical((h+l+c)/3), 6=weighted((h+l+c+c)/4)

int Draw4HowLong;
int shift;
int i;
int loopbegin;
double sum1;
int length;
double lengthvar;
double tmp ;
double wt;
int c;
double price;

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

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

int start()

  {
   if (CheckValidUserInputs()) return(0);   
  
     Draw4HowLong = Bars-Rperiod - 5;
      length = Rperiod;
      loopbegin = Draw4HowLong - length - 1;
 
      for(shift = loopbegin; shift >= 0; shift--)
      { 
         sum1 = 0;
         for(i = length; i >= 1  ; i--)
         {
         lengthvar = length + 1;
         lengthvar /= 3;
         tmp = 0;
// Applied Price enumerations
// 0=close, 1=open, 2=high, 3=low, 4=median((h+l/2)), 5=typical((h+l+c)/3), 6=weighted((h+l+c+c)/4)
         switch(LSMA_AppliedPrice)
         {
         case 0: price = Close[length-i+shift];
                 break;
         case 1: price = Open[length-i+shift];
                 break;
         case 2: price = High[length-i+shift];
                 break;
         case 3: price = Low[length-i+shift];
                 break;
         case 4: price = (High[length-i+shift] + Low[length-i+shift])/2.0;
                 break;
         case 5: price = (High[length-i+shift] + Low[length-i+shift] + Close[length-i+shift])/3.0;
                 break;
         case 6: price = (High[length-i+shift] + Low[length-i+shift] + 2.0*Close[length-i+shift])/4;
                 break;
         }
                 
         tmp = ( i - lengthvar)*price;
         sum1+=tmp;
         }
         wt = sum1*6/(length*(length+1));
         
//========== COLOR CODING ===========================================               
        
       ExtMapBuffer1[shift] = wt;
       
      }
    
      return(0);
  }

//+------------------------------------------------------------------+
//| CheckValidUserInputs                                             |
//| Check if User Inputs are valid for ranges allowed                |
//| return true if invalid input, false otherwise                    |
//| Also display an alert for invalid input                          |
//+------------------------------------------------------------------+
bool CheckValidUserInputs()
{
  
   if (CheckAppliedPrice(LSMA_AppliedPrice))
   {
     Alert("LSMA_AppliedPrice requires a value from 0 to 6",'\n','\r',"You entered ",LSMA_AppliedPrice);
     return(true);
   }
   return(false);
}

//+-----------------------------------------------------+
//| Check for valid Applied Price enumerations          |
//|   0=close, 1=open, 2=high, 3=low, 4=median((h+l/2)) |
//|   5=typical((h+l+c)/3), 6=weighted((h+l+c+c)/4)     |
//|  return true if invalid, false if OK                |
//+-----------------------------------------------------+
bool CheckAppliedPrice(int applied_price)
{
   if (applied_price < 0) return (true);
   if (applied_price > 6) return (true);
   return(false);
}


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



Comments