UHLMA afl code

Dear gentlemen, I am trying to make an AFL code of the Uhlma index.

For the time being I have two problems in which I ask for your help.
(1) The CTS line is not accurately reflected.
(2) The CMA line does not appear in the Amibroker's chart at all and writes to me that it is Empty.

I quote the code I have written,
thanking in advance for every possible help.

_SECTION_BEGIN( "UHLMA Crossover System" );

SetBarsRequired(100, 0);
Plot( C, "Price", colorDefault, styleCandle );

Length = Param("Length", 100, 10, 500, 10);
Mult = Param("Multiplier", 1.0, 0.1, 5.0, 0.1);
Src = ParamField("Source", 3);

Mean = MA(Src, Length);
Var = Sum((Src - Mean)^2, Length) / Length * Mult;

SMAVal = MA(Src, Length);
CMA = SMAVal;
CTS = Src;

for (i = 1; i < BarCount; i++)
{
    secma = (SMAVal[i] - CMA[i-1])^2;
    sects = (Src[i] - CTS[i-1])^2;
    
    ka = IIf(Var[i] < secma, 1 - Var[i] / (secma+1e-9), 0);
    kb = IIf(Var[i] < sects, 1 - Var[i] / (sects+1e-9), 0);
    
    CMA[i] = ka * SMAVal[i] + (1 - ka) * CMA[i-1];
    CTS[i] = kb * Src[i] + (1 - kb) * CTS[i-1];
}

Plot(CTS, "CTS", colorBlue, styleLine|styleThick);
Plot(CMA, "CMA", colorRed, styleLine|styleThick);
ColorFill = IIf(CTS > CMA, colorBlue, colorRed);
PlotOHLC(CTS, CTS, CMA, CMA, "", ColorFill, styleCloud);

_SECTION_END();

@chrismet , try this:

Length = Param( "Length", 100, 10, 500, 10 );
Mult = Param( "Multiplier", 1.0, 0.1, 5.0, 0.1 );
Src = ParamField( "Source", 3 );

Var = ( StDev( Src, Length ) ^ 2 ) * Mult;
sma = MA( Src, Length );
CMA = sma;
CTS = Src;

for( i = 1; i < BarCount; i++ )
{
    secma = nz( sma[i] - CMA[i - 1] ) ^ 2;
    sects = nz( Src[i] - CTS[i - 1] ) ^ 2;

    ka = IIf( Var[i] < secma, 1 - SafeDivide( Var[i], secma ), 0 );
    kb = IIf( Var[i] < sects, 1 - SafeDivide( Var[i], sects ), 0 );

    CMA[i] = ka * sma[i] + ( 1 - ka ) * nz( CMA[i - 1], src[i] );
    CTS[i] = kb * Src[i] + ( 1 - kb ) * nz( CTS[i - 1], src[i] );
}

Plot( C, "Price", colorDefault, styleCandle );
Plot( CTS, "CTS", colorBlue, styleLine | styleThick );
Plot( CMA, "CMA", colorRed, styleLine | styleThick );
ColorFill = IIf( CTS > CMA, colorBlue, colorRed );
PlotOHLC( CTS, CTS, CMA, CMA, "", ColorFill, styleCloud );

I did not compare the plot to other tools, so maybe it is not correct (I only examined a sample source code from another platform to apply some edits to your code).

5 Likes

Mr @Beppe your help was valuable.
It's exactly what it should be.
Thank you very much.

1 Like