Two formulas in one AFL conditionally

Is it possible to load two external AFLs based on a condition? I tried the following, but it produces loads of errors:

if(InWatchListName("Watchlist1"))
{
#include "C:\Program Files\AmiBroker\Formulas\Custom\Formula1.afl"
}
else
{
#include "C:\Program Files\AmiBroker\Formulas\Custom\Formula2.afl"
}

No, that is wrong.

#include is pre-processor directive and is processed prior to your actual code execution.
https://en.wikibooks.org/wiki/C_Programming/Preprocessor_directives_and_macros

Besides the actual main purpose of #include files is to store library of functions to be included to your formulas. So mainly it should contain functions used by many formulas (functions being thoroughly tested before and being repeatedly used).


As for your WL dependent execution...

E.g., if your two formulas contain analysis code then instead just create batch processing routine to run APX file 1 containing Formula 1 and with saved Analysis Filter set to include watchlist X and run APX file 2 containing Formula 2 and with saved Analysis Filter set to include watchlist Y.

As reminder: APX files are Analysis Project files. They save all set Analysis settings (analysis tool bar settings and analysis preferences) and Formula file.

Also you could do like so (i.e. in Chart pane)
(and with Analysis project file's "Apply to" setting saved to "Current")

If( /*some trigger*/ ParamTrigger( "Run Batch 1", "CLICK HERE" )  ) {
    if( InWatchListName("WatchList1") )
        ShellExecute("runbatch", "path_to_batch_file.abb", "" );
    else Say( "Chart Symbol is not member of Watchlist 1" );
}

Or just do so in one single file

if(InWatchListName("Watchlist1"))
{
   // your actual code for WL 1 here (but without #includes)
} 
else 
{
  // your actual code for WL 2 here (but without #includes)
}

(And please learn to use code tags for forum posts even if it is short nonsense code.)

3 Likes

For what it is worth, technically #include can be placed (almost) anywhere in the code, including (no pun intended :slight_smile: ) inside { } blocks. It is true that #include is processed by pre-processor (before execution), so the result would be equivalent to just typing (or pasting) entire code in that particular place.

3 Likes

@Tomasz, you right that if #include file (let's call it TestInclude.afl) contains just something like below test variable x but not any AFL functions:

x = 1;

And in the main AFL it is used like this

if( True )
{
	#include <TestInclude.afl>
	printf( "x=%g", x );	
} 

Then x is returned without error.


Problem is only if Include file contains some AFL functions.
(Let's use include file name TestInclude.afl again.)
Now if you save a function in there e.g.

function TestFunc() 
{
	return 1;
}

And in main AFL it is used like this

if( True )
{
	#include <TestInclude.afl>
	x = TestFunc();
	printf( "x=%g", x );	
} 

Then you will receive error message because the following one (without any include file) is not allowed too.

if( True )
{
	function TestFunc() 
	{
		return 1;
	}
	
	x = TestFunc();

	printf( "x=%g", x );	
} 

Instead defined function has to be placed outside if statement

function TestFunc() 
{
	return 1;
}

if( True )
{	
	x = TestFunc();
	printf( "x=%g", x );	
} 

So in same way in such cases where #include file contains AFL functions you have to place #include directive outside of if statements.

#include <TestInclude.afl>

if( True )
{	
	x = TestFunc();
	printf( "x=%g", x );	
} 

So it depends on what is the content of your include file.
(I only use Include files for storing functions)

5 Likes

I don't know why do you feel desire to explain the obvious. And to whom your post is addressed. As I wrote include is equivalent to typing the code in-place. The code needs to be syntactically correct as a whole. If it isn't (as in your cases), then it is incorrect. It is like elaborating that the sun gives light, water is wet and 2+2=4.

You make assumptions about OP code and you make claims that he is doing wrong. I just pointed out that it does NOT need to be the case. Technically he could use #include the way he did (of course assuming that entire code is syntactically correct, and that along other things means no func declarations inside conditional blocks - but that is NOT the point I was making). There are many ways to skin a cat. It may be ugly and you may not like it, still technical possibilities exist. That was the point.

1 Like

The first part of first sentence of my previous post was addressed to you acknowledging that you were right of course. All the rest of it was mainly addressed to OP just in case he still did not know where error came from.

Because it might not have been obvious to others yet when this or that happens (and I am quite certain most people use include files to store functions. But just a guess).