Herman -
The discontinuities are straightforward to fix. They are caused by very
small numbers in the "dev" result. Simply apply Nz() to it. I've left
the debug approach in the code below.
Now, I'm just trying to help, but I don't know if you are going to see
the effect that I'm referring to until you place the following statement
at the END OF THE CODE -
SetBarsRequired( 500, 0 );
I'm taking some liberties with Tomasz's way of explaining this, but
another way to think of this is that it is limiting ALL internal
calculations in the AFL to 500 bars before the first visible bar PLUS
just visible bars. The reason that this is so important is that it
streamlines all calculations by limiting their bar scope.
I've left it in the code below and suggest that you run the following
experiment -
1. Bring the code below up in an editor.
2. Insert it into a chart and note the timings.
3. NOTICE THAT THE BARCOUNT ON THE SECOND LINE IS ALL BARS. This
isbecause the first execution of an AFL after an edit or insert
mustassume all bars for QuickAFL to calculate its requirements.
4. NOTE THE LAST LINE OF THE CODE - SetBarsRequired(500, 0).
Whatthat is doing is to override AFL and tell it to process only 500
barsbefore the first visible bar and no future bars.
5. Now, click anywhere on the chart and note how the barcount on
thesecond title line changes. QuickAFL is now processing a subset
ofbars. It should be bars visible + 500.
6. MOST IMPORTANTLY, look at the timings in the first line and how
they change.
In probably 99 out of 100 cases, an all array implementation
withQuickAFL utilized will outperform a looping implementation, usually
byfar.
-- BruceR
Periods = Param("Periods", 15, 4, 50, 1 );
Width = Param("Width", 2, 0, 10, 0.05 );
function BB2()
{
global periods, width, dev;
global temp;
MAC = MA( C, periods );
Dev = Width * sqrt( MA( C*C, periods ) - MAC*MAC );
temp = dev;
dev = Nz(dev);
BBTop = MAC + Dev;
BBBot = MAC - Dev;
Plot(BBTop, "", colorRed);
Plot(BBBot, "", colorRed);
}
LOOP = 10;
GetPerformanceCounter( True );
for ( i = 0; i < LOOP; i++ )
bb2dev = BB2();
BB2avg = GetPerformanceCounter( ) / LOOP;
Plot( C, "", colorDefault );
Plot( MA( C, periods ), "", colorGreen );
Title = StrFormat( "BB2 avg elapsed = %6.3f", bb2avg );
barsvis = LastValue( Cum( Status( "barvisible" ) ) );
Title = Title + StrFormat( "\nBarcount = %5.0f, bars visible
= %5.0f", BarCount, barsvis );
Title = Title + "\nIsNull(temp) = " + IsNull(temp);
SetBarsRequired( 500, 0 );