Better understanding on how AFL code executes in Amibroker

Hi

I am newcomer to Amibroker (AB), however I do have some former background in programming.
I am working on a simple strategy, the outcome being a plotted ribbon (with 2 colors) is displayed below the bars indicating in (buy) or out ( sell) of the market.
Issue is that the ribbon keeps on changing states every time I click on the chart. This led me down the path of isolating and debugging a section of the code using the trace command, hence this posting.
I would like understand more on how/when AB executes AFL code. I have read some of the online document which makes sense, however the following issue ( for me) does not make sense.
Referring to the following extract of code, this basically checks that there are sufficient history bars for the strategy and we want to start the indicator/calculations from startbar ( which should be 110 bars from the last bar) . StartBar will be 0 of there are insufficient bars are available.

 //Set up Debug Control and function to trace

DBVOn = ParamToggle("DebugView","OFF|ON", 1);   // default on

procedure TRACE( DBVString )
{
	global DBVOn;
	if( DBVOn ) _TRACE( "## "+ DBVString );  // Based on my understand this the filtering of messages starting with ## in debugView
}

// ****    DEFINE STATIC PARAMETERS    **** 
 
hist_bars =110; 
max_bar_num = BarCount; 
history_bars = IIF( max_bar_num > hist_bars, hist_bars, 0);   // Set history_bars to  max bars if insufficient hist_bars
TRACE(" max_bar_num is: " + max_bar_num +  "  ,history_bars is: " + history_bars);
startbar = IIf(history_bars==0, 0, max_bar_num - history_bars); //Set up where ?? to start from  
earlybar = iif(BarIndex() < 65 OR BarIndex() < startbar OR startbar==0,True,False  );
TRACE("Startbar is: " + startbar + "  ,earlybar is: " + earlybar+ "\n");
lastbar = BarIndex() == LastValue(BarIndex());

TRACE("Last Value " + LastValue( BarIndex())+ "\n");
TRACE("\n");

Issue I am experiencing as follows is with the trace whereby the values retrieved from the trace ( debugView) does not make sense.
When I apply the above code to an current Symbol on weekly timeframe (which has sufficient data) and filter set between dates of approx. 2 years I get the following results below from trace (DebugView).

Based on the results below, I have the following questions:

  • Firstly when I click on “AFL” icon to check the syntax , DebugView displays Log 1, why is BarCount only 200 ?

  • Then when I apply this code to current Symbol I get Log2 output .
    Can some explain why the values are different in the latter case and not consistent for a single “apply”

Thanks

######
Log 1
*00000642 1.04401255 [20964] ## BarCount is: 200 ,history_bars is: 110 *
*00000643 1.04448318 [20964] ## Startbar is: 90 ,earlybar is: 0 *
*00000644 1.04455888 [20964] ## Last Value 199 *
*00000645 1.04465175 [20964] ## *

######
Log2
*00000010 0.81142211 [20964] ## BarCount is: 200 ,history_bars is: 110 *
*00000011 0.81178093 [20964] ## Startbar is: 90 ,earlybar is: 0 *
*00000012 0.81188440 [20964] ## Last Value 199 *
*00000013 0.81191587 [20964] ## *
*00000017 0.97247851 [20964] ## max_bar_num is: 851 ,history_bars is: 110 *
*00000018 0.97274739 [20964] ## Startbar is: 741 ,earlybar is: 0 *
*00000020 0.97683871 [20964] ## BarCount is: 1442 ,history_bars is: 110 *
*00000021 0.97706932 [20964] ## Startbar is: 1332 ,earlybar is: 0 *
*00000022 0.97709721 [20964] ## Last Value 1441 *
*00000023 0.97720832 [20964] ## *
*00000025 0.98246992 [20964] ## BarCount is: 10 ,history_bars is: 0 *
*00000026 0.98261660 [20964] ## Startbar is: 0 ,earlybar is: 1 *
*00000027 0.98272449 [20964] ## Last Value 9 *
*00000028 0.98282647 [20964] ## *
*00000030 1.05050600 [20964] ## BarCount is: 158 ,history_bars is: 110 *
*00000031 1.05079567 [20964] ## Startbar is: 48 ,earlybar is: 0 *
*00000032 1.05090785 [20964] ## Last Value 157 *
*00000033 1.05098104 [20964] ## *
###

A quick guide is for you to start from here
https://www.amibroker.com/kb/2008/07/03/quickafl/

In short, unlike conventional methods, a bar is not bound to a specific Bar index and therefore even BarCount under conditions like QuickAFL can keep changing.
The only thing certain about a bar is its DateTime, and if you want to keep something constant, then this is the right approach.

Do read it, i'm sure it will clear a lot of doubts.

2 Likes

Thanks Travick . the link provided some clarification.

See also other Knowledge Base articles:

http://www.amibroker.com/kb/2015/02/03/when-and-how-often-afl-code-is-executed/
and
http://www.amibroker.com/kb/2014/09/22/do-not-make-assumptions-on-number-of-bars/

The Knowledge Base is a goldmine waiting for you to discover. Literally every topic is covered in either Users Guide or Knowledge Base.

4 Likes