1111111
Download scriptThis custom technical indicator is designed to analyze price movements and provide a visual representation of market momentum or relative strength. It works by processing historical price data to calculate a smoothed trend value, which is then used to determine the balance between upward and downward pressure.
How the Logic Works
Data Smoothing and Weighting The tool begins by examining the closing prices of the asset over a specific number of bars (defined by the user). Rather than reacting impulsively to every tiny price flicker, it uses a "gamma" factor—a smoothing coefficient. This allows the script to blend current prices with previous trend data, creating a more fluid and stable baseline that filters out insignificant market noise.
Trend Calculation The logic creates four distinct layers of data (referred to as L0 through L3). Each layer builds upon the previous one using the smoothing factor. This mathematical layering helps the tool capture different "depths" of the price action, ensuring that the resulting movement reflects a broader trend rather than just immediate volatility.
Upward vs. Downward Momentum Once these smoothed layers are established, the script compares them against each other to measure the gap between consecutive values:
- It identifies instances where the price is moving higher and aggregates those differences into an "Upward" total.
- It identifies instances where the price is moving lower and aggregates those differences into a "Downward" total.
Final Output (Relative Strength) The final result displayed on your chart is a ratio of these two totals. By dividing the cumulative upward movement by the combined total of all movements (up and down), the tool produces a value that represents the relative strength of the current trend.
In practical terms, this means the indicator provides a normalized view of how much "weight" is behind the current price move, helping traders visualize whether the prevailing trend is gaining strength or losing steam.
//+------------------------------------------------------------------+
//| 1.mq4 |
//| Copyright © 2006, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property indicator_separate_window
#property indicator_color1 MediumBlue
//---- input parameters
extern double gamma=0.9;
extern int CountBars=100000;
double L0 = 0;
double L1 = 0;
double L2 = 0;
double L3 = 0;
double L0A = 0;
double L1A = 0;
double L2A = 0;
double L3A = 0;
double LRSI = 0;
double CU = 0;
double CD = 0;
double val1[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
//----
SetIndexBuffer(0,val1);
return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//---- TODO: add your code here
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
if (CountBars>Bars) CountBars=Bars;
SetIndexDrawBegin(0,Bars-CountBars);
int i;
int counted_bars=IndicatorCounted();
//if(CountBars<=Lookback) return(0);
//---- initial zero
//if(counted_bars<1)
//{
// for(i=1;i<=Lookback;i++) val1[CountBars-i]=0.0;
//}
i=CountBars-1;
while(i>=0)
{
L0A = L0;
L1A = L1;
L2A = L2;
L3A = L3;
L0 = (1 - gamma)*Close[i] + gamma*L0A;
L1 = - gamma *L0 + L0A + gamma *L1A;
L2 = - gamma *L1 + L1A + gamma *L2A;
L3 = - gamma *L2 + L2A + gamma *L3A;
CU = 0;
CD = 0;
if (L0 <= L1) CU = L0 - L1; else CD = L1 - L0;
if (L1 <= L2) CU = CU + L1 - L2; else CD = CD + L2 - L1;
if (L2 <= L3) CU = CU + L2 - L3; else CD = CD + L3 - L2;
if (CU + CD != 0) LRSI = CU / (CU + CD);
val1[i] = LRSI;
i--;
}
return(0);
}
//+------------------------------------------------------------------+
Comments