Using AFL formula with Exploration - What am I doing wrong?

Hi guys,

I am a new user of Amibroker. I am learning how it works and AFL programming with a pair of books of Howard Bandy. I am using an Excel Sheet to understand how AFL works and I have created a not real ticker with ten days of data (invented) to make probes with Exploration. It works fine for me, so it helps me to fully understand (or try it) what I am learning.

I didn't want to post here because I am aware there is a lot of level at the forum, and I am a newbie with Amibroker, but I am having a problem and I haven't found any solution searching in the historical posts.

I am using Amibroker Professional 6.20.1. I have made a little code to understand how IF-Else statement works in AFL. My problem appears when I replace "BarCount" for another number (just for experiencing). I am comparing results using BarCount in the IF-Else statement and using another number in two columns (so I am using two IF-Else statement to generate both kinds of results).

//If with For using BarCount. It works well.

for (i = 1; i < BarCount; i++)				
{
	if (volume[i] > volume[i-1])
	{
	 x[i] = volume [i];
	}
	else
	{
	x[i] = Low[i];
	}	
}

//Modifying Barcount. I am having a problem
//when i modify "i < number".

for (i = 1; i < 7; i++)				
{
	if (volume[i] > volume[i-1])
	{
	 y[i] = volume [i];
	}
	else
	{
	y[i] = Low[i];
	}	
}

Filter=1;

AddColumn(x,"i < BarCount",4.2);
AddColumn(y,"i  < less BarCount",4.2);

When I modify that number ("i < number"), sometimes it works and sometimes it seems as if Exploration don't use the new saved formula. I always save modifications, I have tried sending again my code to Analysis, manually reloading AFL formula, even restarting Amibroker, etc... but it seems like sometimes it works and sometimes it doesn't. I have made a lot of probes and, not understanding why it occurs, finally I have decided to make a video and post here to find out what I am doing wrong or what is happening.

Some help will be appreciated.

Thanks a lot.

First of all do not use loop where it is unnecessary.

Use array processing (below one doing the same thing as your loops):

end_idx = 7;

x = Nz(IIf(V > Ref(V,-1), V, L));

y = IIf(BarIndex() < end_idx, x, 0);

Filter=1;

AddColumn(x,"i < BarCount",4.2);
AddColumn(y,"i  < less BarCount",4.2);

Secondly on my end I can not reproduce your observation. Also right now it makes no sense to me why that should happen. Perhaps upload analysis project file ("File" > "Save as" from Analysis window).

Are you sure there is not a second, third, ... instance of AmiBroker opened (from your video it does not look like that is the case but still...)? Open Windows Task Manager and see whether there are more than one AB instances in there. If there are more than one to be seen there then you probably have opened editor of other AB instance. So in that case close all AB instances and re-do what you did.


Third if using loops (again do not use them as in 99% of cases it is unnecessary especially for beginners) please read this KB article on not making assumptions on number of bars.
So,

end_idx = 4;

//If with For using BarCount. It works well.
for (i = 1; i < BarCount; i++)				
{
	if (volume[i] > volume[i-1])
	{
	 x[i] = volume[i];
	}
	else
	{
	 x[i] = Low[i];
	}	
}

//Modifying Barcount. I am having a problem
//when i modify "i < number".
for (i = 1; i < Min(BarCount, end_idx); i++)				
{
	if (volume[i] > volume[i-1])
	{
	 y[i] = volume[i];
	}
	else
	{
	 y[i] = Low[i];
	}	
}

Filter=1;

AddColumn(x,"i < BarCount",4.2);
AddColumn(y,"i  < less BarCount",4.2);
2 Likes

@fxshrat Thanks a lot for responding and thank you very much for the advice.

I use IIF in my codes. I did another code using IF-Else statement and IIF (easier and I guess faster) with the same result. But I want to understand deeply different functions and statements in AFL. That is the reason why I am making probes with Exploration and Excel.

Regrettably, I have just checked my Windows Task Manager and I have found only one instance of Amibroker. I have saved my Analysis project, too, but it has not solved the problem. Sometimes Exploration uses recently saved changes and sometimes not, and I have not idea the reason.

I will do more tests trying to find out what is happening...

I meant that you should upload it to HERE but did not mean to just save it on your end. Project files save settings and AFL (so everything). Also upload your SYMBOL file from database sub-folder.

BTW, does my Iif() code from upper post do the same thing on your end as the loop one you have used (I mean not updating)? Again, on my end I can not reproduce your observation so....

2 Likes

@fxshrat Thanks for your help. Sorry, I have just uploaded both files. As none of the extensions is authorized you can download them with this link: https://we.tl/t-ZNfkF9zPLH

Using IIf(), Exploration seems to work (updating) perfectly. You can see used code to check it below:

y = 5;//I am modifying "y" value to check if Exploration load the last 
     //saved AFL formula.

UsingIIF = IIf ( Volume > Ref(Volume,-1), Volume, Low);

AddColumn(y,"y",4.2);
AddColumn(UsingIIF,"UsingIIF",4.2);

Filter = 1;

When I am using IF-Else statement, I have seen that my problem seems to be related to Analysis tabs. I mean, I open Amibroker, open an analysis tab and first modifications of my formula work well with Exploration. Then, it stops updating... but if I open another analysis tab to continue with my probes, again my first code modifications work until it stops working. And again and again...

I hope it helps to find out what is happening.

Thanks a lot,

I saw your whole video and probably the devs can look at it.

From what I feel, you should be consistent with how you use any tool or software.

I understand, you are doing "Send-to-Analysis" but can you try sticking to one thing.

After you restart AB,

  1. open Analysis Window.
  2. Click Folder icon > select an AFL and Click "Open" "Mouse Hover Text or Tool Tip says: Pick a file"
  3. Click explore button

Editing AFL part.
4. Click the Edit icon, next to the "Folder icon" in same AA window "Mouse Hover Text or Tool Tip says: Edit"

  1. Edit formula and save it. (Use Ctrl+s in Formula Editor)
  2. Click explore and check new results.

Can you cycle through these steps atleast and see if you're results are fine each time ?

2 Likes

@travick Thanks a lot. If I follow those steps Exploration uses the last updated AFL formula. It works.

Opening the AFL code, following step two is necessary. I mean, if I follow step one at first, but not step two every time I modify AFL code, it doesn't work.

Anyway, I have seen another problem with results doing this exercise, so I have decided to reinstall Amibroker. I hope it will help.

Thanks,

Bernie, I confirm your observation in AB 6.20.1 64-bit as well as in AB 6.29 64-bit using your symbol and your project file.
But it only (seems to be) related to your symbol data. I used other analysis settings (my ones for example) and issue is still there. So it does not seem to be some analysis setting causing it.

Also in my tests applied on other non synthetic symbols such as the ones coming with default AB database of AB setup file that issue is not present on my end (64-bit and 32-bit AB). So send your Symbol file and APX file to AB support at support@amibroker.com Also send video. But I am quite certain developer will read here also.

What I also did use is AmiBroker's default database that comes with installation initially.
So I think AB should be able to reproduce it. Running Windows 7 64-bit here.

Using 32-bit AB version 6.20/6.29 the issue does not seem to be there even if using your symbol.
So it seems to be 64-bit issue with your sample data.

Moderator comment: your findings are INCORRECT (see my answers below). It is not about data or AmiBroker version. The formula is simply wrong (uninitialized x[ 0 ] and y[ 0 ])

3 Likes

@fxshrat Thank you very much for your help. I really appreciate it.

I'll send the email tonight.

There is no error. You are simply saving your changes to (different) "AMIFORUM" file (without AFL extension) - see the caption bar of AFL formula editor, while Analysis window is using different file (previously saved), called "AMIFORUM.afl". So your results are coming from old version of the file.

image

You should NOT be sending the very same formula over and over to very same Analysis window multiple times.

Instead correct operation is MUCH simpler:
once Analysis is attached to your formula, you should make changes in the editor and without doing anything just click "Explore"
AmIBroker will know that it needs to save the formula when you press "Explore". It will automatically save any changes. See the video below, I am changing the formula and NOT clicking "Save" or "Send to analysis". I am just clicking "Explore" and modified formula is saved and applied automatically:

test

1 Like

I am quoting myself again

Especially watch second 12 onwards in below video (seconds are visible in bottom right corner of GIF animation).
Again, we found out that apparently his uploaded symbol "PRUEBAS" was causing it (and apparently only in AB 6.20+ 64-bit). No such issues if using "normal" stock data such as the ones of default DB of AB setup.
Also as you can see it is same formula file both with AFL extension.
Another observation... first row does return very large numbers.
Used the code of first post in this video.

test

This is the data of test symbol

602

Ticker	Date/Time	Open	High	Low	Close	Vol	
PRUEBAS	2018-10-01	1.00	3.00	1.00	1.00	1000
PRUEBAS	2018-10-02	2.00	3.00	1.00	2.00	2000
PRUEBAS	2018-10-03	3.00	5.00	2.00	3.00	3000
PRUEBAS	2018-10-04	4.00	6.00	3.00	4.00	4000
PRUEBAS	2018-10-05	5.00	7.00	4.00	5.00	5000
PRUEBAS	2018-10-08	6.00	8.00	5.00	6.00	6000
PRUEBAS	2018-10-09	7.00	9.00	6.00	7.00	7000
PRUEBAS	2018-10-10	8.00	10.00	7.00	8.00	8000
PRUEBAS	2018-10-11	9.00	11.00	8.00	9.00	9000
PRUEBAS	2018-10-12	10.00	12.00	9.00	10.00	10000


On the other hand by adding initial x and y then there is no issue of non changing values in result list.

//If with For using BarCount. It works well.
x = 0;
for (i = 1; i < BarCount; i++)				
{
	if (volume[i] > volume[i-1])
	{
	 x[i] = volume [i];
	}
	else
	{
	x[i] = Low[i];
	}	
}

y = 0;
for (i = 1; i < 3; i++)				
{
	if (volume[i] > volume[i-1])
	{
	 y[i] = volume [i];
	}
	else
	{
	y[i] = Low[i];
	}	
}

Filter=1;

AddColumn(x,"i < BarCount",4.2);
AddColumn(y,"i  < less BarCount",4.2);
1 Like

AmiBroker is fine and data are fine too. Data have nothing to do with that (i.e. the fact that you see it sometimes is just pure chance).

Your code is incorrect. Your x[0] and y[0] are UNINITALIZED (because your for loop starts from 1 instead of 0) ! Hence you are getting random value for first element. Uninitialized array elements get RANDOM value (actually the value that was in RAM in that particular cell before - since it is likely to be used for something else completely unrelated - you are getting weird values).

for (i = 1; i < 3; i++)	// array indices start from ZERO, but you are starting from 1
{
  x[ i ] = ..; // x[ 0 ] will never get assigned !

You only assign x[1], x[2], and so on.. You forgot to assign value to x[0]. You can do this either by doing

x[ 0 ] = 0; // anywhere

or by doing

x = 0; // before the loop

The second thing works because of type coercion When scalar becomes an array, aka. type coercion in AFL so scalar 0 becomes array filled with zeros, once you start treating it as array (using indices).

7 Likes