Best way to execute afl code when certain criteria are met

Hi Everybody,

I'm a relatively new to Amibroker, but I'm trying to write a script to send a slack message to myself when specific criteria are met. I have a variable

matchingShortInProgress

which can be 0 (no criterial are met) or 1 (all criteria are met) as an array. Whats the best way to execuate a block of afl code if

matchingShortInProgress

is equal to one? I know this is wrong becuase its comparing an array to a scalar

if ( matchingShortInProgress == 1) 
{
///code to send slack message
}
if ( LastValue( matchingShortInProgress ) == 1) 
{
///code to send slack message
}

And this gives all zeros for LastValue( matchingShortInProgress), as I've seen in an exploration.

Can anyone give me some guidance here? Very much apprechiate any help!

Best,
Finith

Please read Understanding how AFL works
It is necessary because you lack fundamentals.

The variable can be either ARRAY (then it has multiple values, separate value for each bar) or SCALAR (then it has ONE value).

If you run exploration using ARRAY, it will display separate value for every bar.
If you run exploration using SCALAR, it will display SAME single value for every bar.

LastValue as it name says gets LAST VALUE of array, i.e. array[ BarCount - 1 ].

The program flow instruction like if( ... ) { } must decide which way to go. Therefore it must have ONE value to decide this or the other way. For this reason you have to use SCALAR value for flow control because program must know which way to go. If you pass the array, you would also need to pass the information WHICH BAR you want to use, like

if( array[ BarCount - 1 ] )
{
   // this will execute when array element at last bar (BarCount-1) index is true
}

Use debugger to single step your code to understand what is happening in your code
https://www.amibroker.com/guide/h_debugger.html

General answer to your question is:

// conditional execution based on scalar value
if( scalar_value )
{

   ...
}

// conditional execution based on ONE ELEMENT of array value
index = ... whatever bar index you are interested in...
if( array_value[ index ] )
{

   ....
}

The type of variable depends on value you have assigned to it.

VariableA = 1; // it will be scalar
VariableB = True; // this is scalar too
VariableC = LastValue( Close > Open ); // that is scalar too
VariableD = Open > Close; // this is array
VariableE = VariableD[ 0 ]; // this is scalar

To get better understanding of what is happening in your code and how functions work, use advice given here: How do I debug my formula?

1 Like

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.