Ulitmate Bands S&C May2024

hello

in the May issue of the magazine Stocks and Commodieties there is an article about new envelope curves. Unfortunately, the AFL code for the indicators and trading systems discussed there can no longer be found. Has anyone already put these Ulitmate Channels and Ulitmate Bands into an AFL code?
Thanks for your feedback. Martin

I used a loop. It is self referencing so not sure how to solve it using arrays. I guess you need to use either
https://www.amibroker.com/guide/afl/ama2.html
or
https://www.amibroker.com/guide/afl/iir.html

So maybe someone else can show the array version. Here is the loop version

// https://www.tradingview.com/script/4Plc7Zjk-TASC-2024-05-Ultimate-Channels-and-Ultimate-Bands/

PI = 3.14159265358979;
length = Param( "Length", 20, 1, 50, 1 );
lengthSTR = Param( "STR Length", 20, 1, 50, 1 );
multiplier = Param( "Width Multiplier", 1, 1, 50, 1 );
plist = ParamList( "Select", "Bands|Channel", 1 );

// global variables
mid = str = sd = Null;

function UltimateSmoother( src, period )
{
    a1 = exp( ( -sqrt( 2 ) * PI ) / period );
    c2 = 2 * a1 * cos( ( sqrt( 2 ) * PI ) / period );
    c3 = -a1 * a1;
    c1 = ( 1 + c2 - c3 ) / 4;
    us = src;

    for( i = 4; i < BarCount; i++ )
    {
        us[i] = ( 1 - c1 ) * src[i]
                + ( 2 * c1 - c2 ) * src[i - 1]
                - ( c1 + c3 ) * src[i - 2]
                + c2 * us[i - 1]
                + c3 * us[i - 2];
    }

    return us;
}

function UltimateChannel( length, lengthSTR, mult )
{
    mid = UltimateSmoother( Close, length );
    str = UltimateSmoother( ATR( 1 ), lengthSTR ) * mult;
}

function UltimateBands( src, length, mult )
{
    mid = UltimateSmoother( src, length );
    sd  = StDev( src - mid, length ) * mult;
}

SetChartBkColor( ColorRGB( 0, 0, 0 ) );
SetChartOptions( 0, chartShowArrows | chartShowDates );
Plot( C, "C", colorWhite, styleCandle, Null, Null, 0, 0, 0 );

if( plist == "Channel" )
{
    UltimateChannel( length, lengthSTR, multiplier );
    Plot( mid + str, "channel top", colorAqua, styleLine | styleNoRescale, Null, Null, 0, 0, 1 );
    Plot( mid - str, "channel bot", colorAqua, styleLine | styleNoRescale, Null, Null, 0, 0, 1 );
}

if( plist == "Bands" )
{
    UltimateBands( close, length, multiplier );
    Plot( mid + sd, "bands top", colorAqua, styleLine | styleNoRescale, Null, Null, 0, 0, 1 );
    Plot( mid - sd, "bands bot", colorAqua, styleLine | styleNoRescale, Null, Null, 0, 0, 1 );
}
4 Likes

You can use IIR function for that
https://www.amibroker.com/f?iir
and you have ready-to-use code right before your eyes.

The "Super Smoother" example given in AmIBroker Users' Guide is the same as "UltimateSmoother" from TASC

2 Likes

thanks. There seems to be a difference between "Super Smoother" and "Ultimate Smoother". I took the coefficients from a couple of contributors, see second link (top of code). So I added the IIR version to the code

// https://www.tradingview.com/script/4Plc7Zjk-TASC-2024-05-Ultimate-Channels-and-Ultimate-Bands/
// https://www.traders.com/Documentation/FEEDbk_docs/2024/05/TradersTips.html

PI = 3.14159265358979;
length = Param( "Length", 20, 1, 50, 1 );
lengthSTR = Param( "STR Length", 20, 1, 50, 1 );
multiplier = Param( "Width Multiplier", 1, 0.1, 50, 0.1 );
plist = ParamList( "Select", "Bands|Channel", 1 );
meth = ParamList( "Method", "fLoop|fIIR", 1 );

// global variables
mid = str = sd = bup = bdn = Null;

function UltimateSmoother_loop( src, period )
{
    a1 = exp( ( -sqrt( 2 ) * PI ) / period );
    c2 = 2 * a1 * cos( ( sqrt( 2 ) * PI ) / period );
    c3 = -a1 * a1;
    c1 = ( 1 + c2 - c3 ) / 4;
    us = src;

    for( i = 4; i < BarCount; i++ )
    {
        us[i] = ( 1 - c1 ) * src[i]
                + ( 2 * c1 - c2 ) * src[i - 1]
                - ( c1 + c3 ) * src[i - 2]
                + c2 * us[i - 1]
                + c3 * us[i - 2];
    }

    return us;
}

function UltimateSmoother_IIR( src, period )
{
    a1 = exp( ( -sqrt( 2 ) * PI ) / period );
    c2 = 2 * a1 * cos( ( sqrt( 2 ) * PI ) / period );
    c3 = -a1 * a1;
    c1 = ( 1 + c2 - c3 ) / 4;

    // input for IIR
    b0 = ( 1 - c1 );
    b1 = ( 2 * c1 - c2 );
    b2 = -( c1 + c3 );
    a1 = c2;
    a2 = c3;

    us = IIR( src, b0, a1, b1, a2, b2 );

    return us;
}

function UltimateChannel( length, lengthSTR, mult )
{
    if( meth == "fLoop" )
    {
        mid = UltimateSmoother_loop( Close, length );
        str = UltimateSmoother_loop( ATR( 1 ), lengthSTR ) * mult;
    }

    if( meth == "fIIR" )
    {
        mid = UltimateSmoother_IIR( Close, length );
        str = UltimateSmoother_IIR( ATR( 1 ), lengthSTR ) * mult;
    }
}

function UltimateBands( src, length, mult )
{
    if( meth == "fLoop" )
    {
        mid = UltimateSmoother_loop( src, length );
    }

    if( meth == "fIIR" )
    {
        mid = UltimateSmoother_IIR( src, length );
    }

    sd  = StDev( src - mid, length ) * mult;
}

SetChartBkColor( ColorRGB( 0, 0, 0 ) );
SetChartOptions( 0, chartShowArrows | chartShowDates );
Plot( C, "C", colorWhite, styleCandle, Null, Null, 0, 0, 0 );

if( plist == "Channel" )
{
    UltimateChannel( length, lengthSTR, multiplier );
    Plot( bup = mid + str, "channel top", colorAqua, styleLine | styleNoRescale, Null, Null, 0, 0, 1 );
    Plot( bdn = mid - str, "channel bot", colorAqua, styleLine | styleNoRescale, Null, Null, 0, 0, 1 );
}

if( plist == "Bands" )
{
    UltimateBands( close, length, multiplier );
    Plot( bup = mid + sd, "bands top", colorAqua, styleLine | styleNoRescale, Null, Null, 0, 0, 1 );
    Plot( bdn = mid - sd, "bands bot", colorAqua, styleLine | styleNoRescale, Null, Null, 0, 0, 1 );
}
2 Likes

I don't see any differences.

1 Like

i did not analyze what the differences exactly are I just get 2 different curves. Apparently the supersmoother is from 2014, and the ultimatesmoother from 2024 see code below. I do not have the articles themselves.

The guy who asked the question doesn't seem interested anyways. But I learned some new things

// https://www.tradingview.com/script/4Plc7Zjk-TASC-2024-05-Ultimate-Channels-and-Ultimate-Bands/
// https://www.traders.com/Documentation/FEEDbk_docs/2024/05/TradersTips.html
// https://traders.com/documentation/feedbk_docs/2014/01/traderstips.html

PI = 3.14159265358979;
length = Param( "Length", 20, 1, 50, 1 );

function UltimateSmoother( src, period )
{
	// using: // https://www.traders.com/Documentation/FEEDbk_docs/2024/05/TradersTips.html
    a1 = exp( ( -sqrt( 2 ) * PI ) / period );
    c2 = 2 * a1 * cos( ( sqrt( 2 ) * PI ) / period );
    c3 = -a1 * a1;
    c1 = ( 1 + c2 - c3 ) / 4;

    // input for IIR
    b0 = ( 1 - c1 );
    b1 = ( 2 * c1 - c2 );
    b2 = -( c1 + c3 );
    a1 = c2;
    a2 = c3;

    us = IIR( src, b0, a1, b1, a2, b2 );

    return us;
}

function SuperSmoother( src, period )
{
	// using: https://www.amibroker.com/guide/afl/iir.html
	// https://traders.com/documentation/feedbk_docs/2014/01/traderstips.html
    c1 = 1.41421 * 3.14159 / period;
    c2 = 2.71828 ^ -c1;
    a1 = 2 * c2 * cos( c1 );
    a2 = -c2 ^ 2;
    b0 = ( 1 - a1 - a2 ) / 2;
    b1 = b0;

    us = IIR( src, b0, a1, b1, a2 );

    return us;
}

s_smth = UltimateSmoother( Close, length );
u_smth = SuperSmoother( Close, length );
        
SetChartBkColor( ColorRGB( 0, 0, 0 ) );
SetChartOptions( 0, chartShowArrows | chartShowDates );
Plot( C, "C", colorWhite, styleCandle, Null, Null, 0, 0, 0 );
Plot( s_smth, "Super Smoother", colorAqua, styleLine | styleNoRescale, Null, Null, 0, 0, 1 );
Plot( u_smth, "Ultimate Smoother", colorGold, styleLine | styleNoRescale, Null, Null, 0, 0, 1 );

2 Likes