Hi Cougar
I got near to the same result as the inbuilt code. Sorry for stealing your snippet
Maybe you have another idea because I also wonder how to rebuild it for better understanding.
function fCustomWMA( Price, Period ) {
//Based on https://www.investopedia.com/terms/l/linearlyweightedmovingaverage.asp
Numerator = 0;
Denominator = 0;
for( i = 0; i < Period; i++ ) {
Numerator = Numerator + ( Ref( Price, -i ) * ( Period - i ) );
Denominator = Denominator + ( Period - i );
}
return Numerator / Denominator;
}
function fCustomWMA2( Price, Period ) {
Numerator = 0;
Denominator = 0;
Period_final = 0;
if (BarCount < Period) {
Period_final = BarCount;
}
else {
Period_final = Period;
}
for( i = 0; i < Period_final; i++ ) {
Numerator = Numerator + ( Ref( Price, -i ) * ( Period_final - i ) );
Denominator = Denominator + ( Period_final - i );
}
return Numerator / Denominator;
}
_SECTION_BEGIN( "WMA" );
arr = ParamField( "Select Array", 3 );
perWMA = Param( "WMA Period", 20, 1, 1440, 1 );
InBuiltWMA = WMA( arr, perWMA );
CustomWMA = fCustomWMA( arr, perWMA );
CustomWMA2 = fCustomWMA2( arr, perWMA );
_SECTION_END();
_SECTION_BEGIN( "Plot or Explore" );
if( Status( "Action" ) == actionIndicator ) {
Plot( C, "Price", colorDefault, styleBar | styleThick );
Plot( InBuiltWMA, "InBuiltWMA", colorLightOrange );
Plot( CustomWMA, "CustomWMA", ColorRGB( 70, 160, 255 ) );
}
if( Status( "Action" ) == actionExplore ) {
Filter = 1;
AddColumn( InBuiltWMA, "InBuiltWMA", 1.2 );
AddColumn( CustomWMA, "CustomWMA", 1.2 );
AddColumn( CustomWMA2, "CustomWMA 2", 1.2 );
CheckCond = CustomWMA != InBuiltWMA;
TextSelector = IIf( CheckCond, 0, 1 );
TextList = "Not-Matching\n ";
fgcolor = IIf( CheckCond, colorWhite, colorDefault );
bkcolor = IIf( CheckCond, colorRed, colorDefault );
AddMultiTextColumn( TextSelector, TextList, "Check", 1.2, fgcolor, bkcolor, 88 );
CheckCond2 = CustomWMA2 != InBuiltWMA;
TextSelector2 = IIf( CheckCond, 0, 1 );
fgcolor = IIf( CheckCond2, colorWhite, colorDefault );
bkcolor = IIf( CheckCond2, colorRed, colorDefault );
AddMultiTextColumn( TextSelector2, TextList, "Check 2", 1.2, fgcolor, bkcolor, 88 );
AddColumn( abs( CustomWMA - InBuiltWMA ), "Marginal Difference", 1.8 );
AddColumn( abs( CustomWMA2 - InBuiltWMA ), "Marginal Difference 2", 1.8 );
}
_SECTION_END();