LSMA_NRP
Download scriptThis script is a technical analysis tool designed for the MetaTrader 4 platform to help traders visualize price momentum and trend direction. It functions by calculating a specialized moving average and translating its movements into distinct visual signals on your trading chart.
Core Logic and Functionality
The indicator operates based on three primary layers of logic:
1. The Base Calculation (LSMA) At its foundation, the script calculates a customized "Linear Weighted Moving Average" (LSMA). Instead of using a standard average, it combines two different types of moving averages—the Linear Weighted and the Simple Moving Average. By weighting these two methods against each other, the tool creates a smoothed line that is more responsive to recent price action than a standard average, yet filtered enough to ignore minor market "noise."
2. Trend Detection The script constantly monitors the direction of this calculated line.
- If the line moves upward compared to its previous position, the script identifies an upward trend.
- If the line moves downward, it identifies a downward trend.
3. Dynamic Visual Mapping (The "Plotting" Logic) The most unique feature of this script is how it handles these trends visually. Rather than simply drawing one continuous line, it uses a logic gate to separate "Up" movements from "Down" movements into different visual sets:
- Trend Segmentation: When the trend switches direction, the script identifies the transition point. It then separates the data into two distinct paths. This allows the chart to highlight bullish movements in one color/style and bearish movements in another.
- Continuous Connection: The logic includes a "cleaning" and "plotting" mechanism. This ensures that when the price is moving consistently in one direction, the lines remain connected and fluid. When a reversal occurs, the script manages the data points to ensure the transition is clean, effectively "breaking" or "restarting" the visual path to reflect the new market sentiment.
Summary for the User
In practical terms, this tool helps you see the "path of least resistance." By visualizing the smoothed momentum of the market and separating upward trends from downward trends into distinct visual paths, it provides a clearer view of whether the current market cycle is gaining strength or losing steam, helping you identify trend changes more effectively than watching raw price action alone.
//+------------------------------------------------------------------+
//| LSMA nrp.mq4 |
//| mladen |
//+------------------------------------------------------------------+
#property copyright "copyleft mladen"
#property link "mladenfx@gmail.com"
#property indicator_chart_window
#property indicator_buffers 5
#property indicator_color1 Yellow
#property indicator_color2 Green
#property indicator_color3 Green
#property indicator_color4 Orange
#property indicator_color5 Orange
#property indicator_width1 2
#property indicator_width2 2
#property indicator_width3 2
#property indicator_width4 2
#property indicator_width5 2
//
//
//
//
//
extern int LSMAPeriod = 14;
extern int LSMAPrice = 0;
//
//
//
//
//
double lsma[];
double lsmaua[];
double lsmaub[];
double lsmada[];
double lsmadb[];
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
//
//
//
//
//
int init()
{
SetIndexBuffer(0,lsma);
SetIndexBuffer(1,lsmaua);
SetIndexBuffer(2,lsmaub);
SetIndexBuffer(3,lsmada);
SetIndexBuffer(4,lsmadb);
return(0);
}
//
//
//
//
//
int start()
{
int counted_bars=IndicatorCounted();
int limit,i;
if(counted_bars < 0) return(-1);
if(counted_bars>0) counted_bars--;
limit = MathMin(Bars-counted_bars,Bars-1);
//
//
//
//
//
if (lsma[limit] > lsma[limit+1]) CleanPoint(limit,lsmaua,lsmaub);
if (lsma[limit] < lsma[limit+1]) CleanPoint(limit,lsmada,lsmadb);
for(i = limit; i >= 0; i--)
{
lsma[i] = 3.0*iMA(NULL,0,LSMAPeriod,0,MODE_LWMA,LSMAPrice,i)-2.0*iMA(NULL,0,LSMAPeriod,0,MODE_SMA,LSMAPrice,i);
lsmaua[i] = EMPTY_VALUE;
lsmaub[i] = EMPTY_VALUE;
lsmada[i] = EMPTY_VALUE;
lsmadb[i] = EMPTY_VALUE;
if (lsma[i] > lsma[i+1]) PlotPoint(i,lsmaua,lsmaub,lsma);
if (lsma[i] < lsma[i+1]) PlotPoint(i,lsmada,lsmadb,lsma);
}
return(0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
//
//
//
//
//
void CleanPoint(int i,double& first[],double& second[])
{
if ((second[i] != EMPTY_VALUE) && (second[i+1] != EMPTY_VALUE))
second[i+1] = EMPTY_VALUE;
else
if ((first[i] != EMPTY_VALUE) && (first[i+1] != EMPTY_VALUE) && (first[i+2] == EMPTY_VALUE))
first[i+1] = EMPTY_VALUE;
}
//
//
//
//
//
void PlotPoint(int i,double& first[],double& second[],double& from[])
{
if (first[i+1] == EMPTY_VALUE)
{
if (first[i+2] == EMPTY_VALUE) {
first[i] = from[i];
first[i+1] = from[i+1];
second[i] = EMPTY_VALUE;
}
else {
second[i] = from[i];
second[i+1] = from[i+1];
first[i] = EMPTY_VALUE;
}
}
else
{
first[i] = from[i];
second[i] = EMPTY_VALUE;
}
}
Comments