LSMA_AppliedPrice1
Download scriptThis script is designed to provide traders with a specialized moving average on their charts, specifically a Linear Weighted Moving Average (LSMA). Unlike standard moving averages that treat all data points within a period equally, this tool applies a mathematical weight to the data, giving more significance to the most recent price action. This makes it a highly responsive tool for identifying current trends and potential momentum shifts.
Core Functionality
The logic of the script can be broken down into three main components:
1. Flexible Price Source Selection The script allows you to choose exactly which price data point should be used as the basis for the calculation. Instead of being locked into one metric, you can choose from several different perspectives of market activity:
- Standard metrics like Open, High, Low, or Close prices.
- Calculated averages such as the Median (the middle point between high and low), the Typical Price (an average of high, low, and close), or a Weighted Price (which emphasizes the closing price more heavily).
2. Weighted Calculation Logic The "Linear" part of this indicator refers to how it calculates the average. The script iterates through a user-defined period (the number of previous bars) and assigns a linear weight to each bar. Because it multiplies the price by its position in that sequence, the most recent price has a much higher impact on the final value than older prices. This helps smooth out "noise" while remaining sensitive to immediate market movements.
3. Automated Validation To ensure the tool functions correctly without errors, the script includes an integrated safety check. It automatically verifies that the settings provided by the user fall within the allowed parameters (specifically regarding the price source types). If an unsupported value is entered, the script will provide a clear alert to the user, ensuring that the technical analysis remains accurate and reliable.
Summary for the Trader
In simple terms, this script takes your chosen market price (e.g., the Close price), looks back over a specific number of candles, and creates a weighted trend line. Because it prioritizes recent data, it is intended to react faster to price changes than a traditional moving average, helping you stay aligned with the most current market flow.
//+------------------------------------------------------------------+
//| 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