Author: Forte928
geL2WMA[1]

This script is designed to provide a specialized technical analysis tool for the MetaTrader 4 platform. It functions by calculating and displaying a specific type of moving average on your trading chart, which helps traders smooth out price fluctuations to identify underlying trends more clearly.

Core Logic and Behavior

The primary purpose of this script is to generate a Linear Weighted Moving Average (LWMA) variant. Unlike standard moving averages that treat all past price points with equal importance, or simple weighted averages, this specific logic applies a "layered" weighting system.

Here is how the tool processes your data:

  • Multi-Layered Weighting: The script looks back over a specific number of historical candles (defined by the user as the "Period"). Instead of applying one single weight to these candles, it performs a nested calculation. It essentially calculates weighted sums for smaller windows of time and then combines those results into a final, overarching weighted average. This creates a more sophisticated smoothing effect compared to standard indicators.
  • Dynamic Calculation Range: To ensure the platform remains fast and responsive, the script includes an intelligent "Counter" system. Rather than trying to calculate data for every single candle in the history of your chart (which could slow down your computer), it automatically determines the optimal number of recent candles to process based on your settings and the available data.
  • Visual Output: The result of these complex calculations is projected onto your chart as a solid line. This line represents the "smoothed" price action, allowing you to see the momentum of the market while filtering out minor "noise" or temporary price spikes.

User-Adjustable Parameters

The script provides three main settings that allow you to customize how the data is processed:

  1. L2WMAPrd (Period): This determines the look-back window. A higher number will make the resulting line smoother and slower to react, while a lower number will make it more sensitive to immediate price changes.
  2. Shift: This allows you to move the resulting line forward or backward on the chart relative to the current price.
  3. Counter: This sets the limit for how many historical data points the script should analyze at one time, helping to balance calculation depth with platform performance.

In summary, this tool is built for traders who need a more nuanced moving average that balances historical weight and price sensitivity through a layered mathematical approach.

Miscellaneous
It issuies visual alerts to the screenImplements a curve of type %1
15 Views
0 Downloads
0 Favorites
geL2WMA[1]
//+------------------------------------------------------------------+
//|																geL2WMA.mq4		|
//|                      Copyright © 2007, ver 1.0							|
//|                      Forte928           									|
//+------------------------------------------------------------------+
#property copyright "Forte928"
#property link      ""
#define		IndicatorName "geL2WMA"

#property indicator_chart_window
//#property indicator_separate_window

#property indicator_buffers 3
#property indicator_color1 Tomato
#property indicator_color2 Yellow
#property indicator_color3 Wheat

extern int		L2WMAPrd	=8;
extern int		Shift		=0;
extern int	 	Counter	=2000;

//double  FreqTOL  =0.0001;//Tolerance of frequency calculation for Method 1

double	FxView1[];
double	FxView2[];
double	FxView3[];
double	FxView4[];
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~V~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Work Variables ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
double	TimeBuf1[];
double	TimeBuf2[];
double	TimeBuf3[];
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~V~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Initialization program Buffers ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void	InitBufferam(int iBufferSize)
{
	ArrayResize(TimeBuf1,iBufferSize);
	ArrayResize(TimeBuf2,iBufferSize);
	ArrayResize(TimeBuf3,iBufferSize);
	return;
}
void	EmptyBufferam()
{
	ArrayInitialize(TimeBuf1,EMPTY_VALUE);
	ArrayInitialize(TimeBuf2,EMPTY_VALUE);
	ArrayInitialize(TimeBuf3,EMPTY_VALUE);
// -->
	return;
}
void	DoneBufferam()
{
	ArrayResize(TimeBuf1,0);
	ArrayResize(TimeBuf2,0);
	ArrayResize(TimeBuf3,0);
	return;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~V~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Program Constants ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int		MaxPeriod=0;
int		CalcCount=0;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Custom indicator initialization function ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int init()
  {
//---- initialization program values  --------------------------------
	MaxPeriod=MathMax(L2WMAPrd,L2WMAPrd);
	InitBufferam(MaxCounter(Counter,MaxPeriod));
	CalcCount=InitCounter(Counter,MaxPeriod);
//---- initialization indicators -------------------------------------
	SetupChartLine(0,FxView1,0,1,"L2WMA"+L2WMAPrd);// SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,1); SetIndexBuffer(0,ValueBuf0);
	SetupChartLine(1,FxView2,0,1,"Line2");// SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,1); SetIndexBuffer(0,ValueBuf0);
	SetupChartLine(2,FxView3,0,1,"Line3");// SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,1); SetIndexBuffer(0,ValueBuf0);
	SetupChartLine(3,FxView4,0,1,"Line4");// SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,1); SetIndexBuffer(0,ValueBuf0);
   IndicatorShortName(IndicatorName+"("+L2WMAPrd+")");
  	IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
	
//----
   return(0);
  }
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~V~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Custom indicator deinitialization function ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int deinit()
  {
//----
	DoneBufferam();	
//----
   return(0);
  }
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~V~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Start defination ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int start()
{
	if (!StartCounter(IndicatorName,Counter,CalcCount,MaxPeriod)) return(-1);
//------------------------------------------------------------------------------------
	EmptyBufferam();
//+-------------------------- Begin Cycle ---------------------------------------------------+\\
	for (int Rx=CalcCount-1;Rx>=0;Rx--){
		double SumRx=0;
		double WeightRx=0;
		double SumLx=0;
		
		for (int Px=L2WMAPrd-1;Px>=0;Px--) {
			double SumPx=0;
			double WeightPx=0;
			for (int Sx=Px;Sx>=0;Sx--) {
				SumPx=SumPx+Close[Rx+Sx]*(Px-Sx+1);			// ôîðìèðîâàíèå ñóììû äëÿ L2WMA âíóòðåíèé öèêë
				WeightPx=WeightPx+(Sx+1);
			}
			SumRx=SumRx+SumPx/WeightPx*(L2WMAPrd-Px);		// ôîðìèðîâàíèå ñóììû äëÿ L2WMA âíåøíèé öèêë
//			SumLx=SumLx+Close[Rx+Px]*(L2WMAPrd-Px);		// ôîðìèðîâàíèå ñóììû äëÿ LWMA
			WeightRx=WeightRx+(Px+1);
		}
		TimeBuf1[Rx]=SumRx/WeightRx;
//		TimeBuf2[Rx]=SumLx/WeightRx;
	}


	ArrayCopy(FxView1,TimeBuf1,Shift,0,CalcCount);
//	ArrayCopy(FxView2,TimeBuf2,Shift,0,CalcCount);
	
//----
   return(0);
  }
//-----------------------------------------------------------------------------------------------
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~														 InitCounter																		~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 int InitCounter(int iPreCount,int iMaxPeriod)
{
	// Rule 1
	//if ((Result==0)||(Result>Bars)) Result=Bars;
	int Result=MaxCounter(iPreCount,iMaxPeriod);
	// Rule 2
	if ((Result+iMaxPeriod)>Bars) Result=Bars-iMaxPeriod;
	// Rule 3
	if (Result<iMaxPeriod) Result=0;
	else Result=Result+iMaxPeriod;
	return(Result);
}
//------------------------------------------ StartCounter --------------------------
bool StartCounter(string WindowName,int iPreCount,int& iCalcCount,int iMaxPeriod)
{
	if ((iPreCount>iCalcCount)||(iPreCount==0)) iCalcCount=InitCounter(iPreCount,iMaxPeriod);
	if (iCalcCount==0) 
	{
		Alert(WindowName+" "+Symbol()+"("+Period()+") - Limited Calculate Period");
		return(false);
	}
	return(true);
	//int iBarCount=IndicatorCounted();
	//if (iBarCount>0) return(true);
	//Print("StartCounter");
	return(false);
}
//------------------------------------------ StartCounter --------------------------
int MaxCounter(int iPreCount,int iMaxPeriod)
{
	int Result=iPreCount;
	// Rule 1
	//if ((Result==0)||(Result>Bars)) Result=Bars;
	if (Result==0) Result=Bars-1;
	if (Result<iMaxPeriod) Result=iMaxPeriod;
	return(Result+iMaxPeriod);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~														 Style active Line																~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void SetupChartLine(int Index,double& ViewAy[],int Style,int Width,string Label)
{
	switch(Style)
	{
		case 0 : SetIndexStyle(Index,DRAW_LINE,STYLE_SOLID,Width);break;
		case 1 : SetIndexStyle(Index,DRAW_HISTOGRAM,STYLE_SOLID,Width);break;
		case 2 : SetIndexStyle(Index,DRAW_ARROW,STYLE_SOLID,Width);break;
		default : SetIndexStyle(Index,DRAW_LINE,STYLE_SOLID,Width);break;
	}
	if (Label!="") SetIndexLabel(Index,Label);
	SetIndexBuffer(Index,ViewAy);
	return(0);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Comments