[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [amibroker] Re: How to code a Adaptive StDev()



PureBytes Links

Trading Reference Links

Title: Re: [amibroker] Re: How to code a Adaptive StDev()


Thanks everyone for your kind help! 


The Adaptive BB is getting and better! Unless someone comes forward with a DLL (?) I don't think we can improve on BB5() (visible bars only) and next BB2() (All bars) below. This goes to show that if you put some heads together you can get surprising results! I copy my full test code below. You can check the plots yourself they are all within 5 digits accuracy. 


Funny thing is that there appears to be no difference if I turn QuickAFL On or Off (going by the execution time show on the chart). Not sure how to compare execution on All-Bars vs QuickAFL.


herman.



Periods         = Param("Periods", 15, 4, 50, 1 );

Width         = Param("Width", 2, 0, 10, 0.05 );


function StDev1( priceArray, Periods) // Periods < 25

{

   global BBTop, BBBot;

   Periods = Min( 25, periods );

   Periods = Max( 2, Periods);

   Deviation =

   IIf( Periods == 2, StDev( PriceArray, 2 ),

   IIf( Periods == 3, StDev( PriceArray, 3 ),

   IIf( Periods == 4, StDev( PriceArray, 4 ),

   IIf( Periods == 5, StDev( PriceArray, 5 ),

   IIf( Periods == 6, StDev( PriceArray, 6 ),

   IIf( Periods == 7, StDev( PriceArray, 7 ),

   IIf( Periods == 8, StDev( PriceArray, 8 ),

   IIf( Periods == 9, StDev( PriceArray, 9 ),


   IIf( Periods == 10, StDev( PriceArray, 10 ),

   IIf( Periods == 11, StDev( PriceArray, 11 ),

   IIf( Periods == 12, StDev( PriceArray, 12 ),


   IIf( Periods == 13, StDev( PriceArray, 13 ),

   IIf( Periods == 14, StDev( PriceArray, 14 ),

   IIf( Periods == 15, StDev( PriceArray, 15 ),


   IIf( Periods == 16, StDev( PriceArray, 16 ),

   IIf( Periods == 17, StDev( PriceArray, 17 ),

   IIf( Periods == 18, StDev( PriceArray, 18 ),


   IIf( Periods == 19, StDev( PriceArray, 19 ),

   IIf( Periods == 20, StDev( PriceArray, 20 ),

   IIf( Periods == 21, StDev( PriceArray, 21 ),


   IIf( Periods == 22, StDev( PriceArray, 22 ),

   IIf( Periods == 23, StDev( PriceArray, 23 ),

   IIf( Periods == 24, StDev( PriceArray, 24 ),

   StDev( PriceArray, 25 ))))))))))))))))))))))));

   return Deviation;

}


function xStDev( priceArray, periods )

{

    local deviation;

    local firstBar;

    local lastBar;

    deviation = 0;

    firstBar = 0;                                                         //Max( 0, Status( "firstvisiblebar" ) );

    lastBar  = LastValue(BarIndex());         //Min( BarCount - 1, Status( "lastvisiblebar" ) );

    periods = Max( 4, periods );

    for ( i = firstBar; i <= lastBar; i++ )

    {

                 if ( periods[i] <= ( i + 1 ) )

        {

            sumX = 0;

            sumXSq = 0;

            for ( j = periods[i]; j > 0; j-- )

            {

                price = priceArray[( i + 1 ) - j];

                sumX += price;

                sumXSq += price ^ 2;

            }

            deviation[i] = sqrt( ( sumXSq - ( sumX * sumX / periods[i] ) ) / periods[i] );

        }

        else

        {

            deviation[i] = 0;

        }

    }

    return deviation;

}


function xStDev2( priceArray, periods )

{

    local deviation;

    local firstBar;

    local lastBar;

    deviation = 0;

    firstBar = Max( 0, Status( "firstvisiblebar" ) );

    lastBar  = Min( BarCount - 1, Status( "lastvisiblebar" ) );

    periods = Max( 4, periods );

    for ( i = firstBar; i <= lastBar; i++ )

    {

                 if ( periods[i] <= ( i + 1 ) )

        {

            sumX = 0;

            sumXSq = 0;

            for ( j = periods[i]; j > 0; j-- )

            {

                price = priceArray[( i + 1 ) - j];

                sumX += price;

                sumXSq += price ^ 2;

            }

            deviation[i] = sqrt( ( sumXSq - ( sumX * sumX / periods[i] ) ) / periods[i] );

        }

        else

        {

            deviation[i] = 0;

        }

    }

    return deviation;

}




function BB()

{

    BBTop = BBandTop( C, Periods, Width );

    BBBot = BBandBot( C, Periods, Width );

    Plot( BBTop, "", 4, 1 );

    Plot( BBBot, "", 4, 1 );

}


function BB1()

{

    global BSN;

    SMAP = MA( C, Periods );

    Dev = Width * StDev1( C, Periods );

    BBTop = SMAP + Dev;

    BBBot = SMAP - Dev;

    Plot( BBTop, "", 3, 1 );

    Plot( BBBot, "", 3, 1 );

}


function BB2()

{

    global BSN;

    MAC = MA( C, periods );

    Dev = Width * sqrt( MA( C*C, periods ) - MAC*MAC );

    BBTop = MAC + Dev;

    BBBot = MAC - Dev;

    Plot( BBTop, "", 5, 1 );

    Plot( BBBot, "", 5, 1 );

}


function BB3()

{

    global BSN;

    SMAP = MA( C, Periods );

    Dev = Width * xStDev( C, Periods );

    BBTop = SMAP + Dev;

    BBBot = SMAP - Dev;

    Plot( BBTop, "", 6, 1 );

    Plot( BBBot, "", 6, 1 );


}


function BB4()

{

    global BSN;

    SMAP = MA( C, Periods );

    Dev = Width * sqrt( MA( C^ 2, periods ) - MA( C, periods ) ^ 2 );

    BBTop = SMAP + Dev;

    BBBot = SMAP - Dev;

    Plot( BBTop, "", 7, 1 );

    Plot( BBBot, "", 7, 1 );

}


function BB5()

{

    global BSN;

    SMAP = MA( C, Periods );

    Dev = Width * xStDev2( C, Periods );

    BBTop = SMAP + Dev;

    BBBot = SMAP - Dev;

    Plot( BBTop, "", 9, 1 );

    Plot( BBBot, "", 9, 1 );


}



//SetBarsRequired(sbrAll,sbrAll);

Plot(C,"",1,128);

Plot( MA( C, Periods ),"",2,1);


bb();

BB1();

BB2();

BB3();

BB4();

BB5();






__._,_.___


**** IMPORTANT PLEASE READ ****
This group is for the discussion between users only.
This is *NOT* technical support channel.

TO GET TECHNICAL SUPPORT send an e-mail directly to
SUPPORT {at} amibroker.com

TO SUBMIT SUGGESTIONS please use FEEDBACK CENTER at
http://www.amibroker.com/feedback/
(submissions sent via other channels won't be considered)

For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/





Your email settings: Individual Email|Traditional
Change settings via the Web (Yahoo! ID required)
Change settings via email: Switch delivery to Daily Digest | Switch to Fully Featured
Visit Your Group | Yahoo! Groups Terms of Use | Unsubscribe

__,_._,___