Skip to main content

Gauss Smoothing 4th, 8th, 12th Order. Include smooth 1st & 2nd Derivatives for Amibroker (AFL)

anotatta 10 months ago Amibroker (AFL)

  • Rating:
    0 / 5 (Votes 0)
  • Tags:
    amibroker, indicator, oscillator

Gauss Smoothing
22025-08-17 By anotatta
Approximate 4th, 8th, 12th, Order Gauss Kernel smoothing, 1st & 2nd derivative, indicator only
Price is Price Field, P is Period, deriv is derivative desired (0=smoothing, 1 = slope, …)
If plotted along with the price chart & derivative is > 0, then use styleLeftAxis or styleOwnscale

Indicator / Formula

Copy & Paste Friendly

Appy individually or to price chart

//***************************************************************** 
//Gauss Smoothing
//22025-08-17 By anotatta
//Approximate 4th, 8th, 12th, Order Gauss Kernel smoothing, 1st & 2nd derivative, indicator only
//Price is Price Field, P is Period, deriv is derivative desired (0=smoothing, 1 = slope, ...)
//If plotted along with the price chart & derivative is > 0, then use styleLeftAxis or styleOwnscale
function Gauss4th(Price, P, deriv)
{
    // ===== 1. Calculate coefficients =====
    beta1 = (1 - cos(2 * 3.141592653 / P)) / (2 ^ (1 / 4) - 1);
    alpha = -beta1 + sqrt(beta1 ^ 2 + 2 * beta1);
    alpha4 = alpha * alpha * alpha * alpha; // alpha^4

    t1 = 1 - alpha;
    t2 = t1 * t1; t3 = t2 * t1; t4 = t3 * t1;
    P2 = P * P; 

    // ===== 2. Apply IIR (4th-order max) =====
    ysmooth = IIR(Price, alpha4, 4 * t1, 0, -6 * t2, 0, 4 * t3, 0, -t4, 0);

    if (deriv == 1)            // 1st derivative
        ysmooth = IIR(ysmooth, alpha4 / P, 4 * t1, -alpha4 / P, -6 * t2, 0, 4 * t3, 0, -t4, 0);
    else if (deriv == 2)       // 2nd derivative
        ysmooth = IIR(ysmooth, 0, 4 * t1, 0, -6 * t2, alpha4 / P2, 4 * t3, -2 * alpha4 / P2, -t4, alpha4 / P2);

    return ysmooth;
}
//***************************************************************** 
// ===== Main =====
pField = ParamField("Price field", -1);
Period = Param("Period", 21, 3, 200, 1);
deriv = Param("Derivative (smooth, 1st, 2nd)", 0, 0, 2, 1);
order = Param("Order (1=4th, 2=8th, 3=12th)", 1, 1, 3, 1);
findiff = ParamToggle("Finite Difference","No|Yes",0);	//finite difference

// Calculate precise scaling factors
scale = IIf( order == 2, sqrt( 2 ), IIf( order == 3, 3 ^ ( 1 / 3 ), 1 ) );

// Apply smoothing
smoothed = pField;
for( i = 1; i <= order; i++ )
{
    smoothed = Gauss4th( smoothed, Period * scale, IIf( i < order OR findiff, 0, deriv ) );
}

// Finite difference alternative
if( findiff AND deriv > 0 )
{
    if( deriv == 1 )
    {
        smoothed = ( Ref( smoothed, 1 ) - Ref( smoothed, -1 ) ) / 2;
    }
    else
        if( deriv == 2 )
        {
            smoothed = Ref( smoothed, 1 ) - 2 * smoothed + Ref( smoothed, -1 );
        }
}

//Plot Indicator
PlotGrid( 0, colorLightGrey , 10, 1, False );
Plot( smoothed, _DEFAULT_NAME(), thecolor = ParamColor( "Gauss4th Color", colordefault ), ParamStyle( "Gauss4th style", styleDots , maskall ) );

if( typeof( Title ) == "undefined" ) _N( Title =  EncodeColor( thecolor ) + _DEFAULT_NAME() + " - " ) ;
else _N( Title +=  "\n" + EncodeColor( thecolor ) + _DEFAULT_NAME() + " - " ) ;

1 comments

10 months ago

My first contribution. Gauss smoothing with smooth derivatives. Check it out. And Good Trading.

Leave Comment

Please login here to leave a comment.