Calling FILTER within a Function body

I want to create price alert using AFL.
In the main part of the body, I will call a function with Scrip name, crossing UP or DOWN and the price.

The function will compare the level with current price and should give the results as well as an Alert.
The code compiles successfully without any error but generates no result.

When I take out the FILTER from function body and put it in the main program, the results are generated. But that makes program less convenient as I have put many conditions in FILTER program (one condition for one price alert).
I want to simply use one line in the mail program to call the Price alert function.
Here is the code:

function PA(scrip,dir,price) {
      if(dir=="U") cond=Name()==scrip AND Cross(C,price);
      if(dir=="D") cond=Name()==scrip AND Cross(price,c);
      Filter=cond;
	  AlertIf(cond, "SOUND C:\\Windows\\Media\\Alarm9.wav", "Audio alert"+Name(), 1);
	  SetSortColumns(-2);
}
PA("TITAN","U",1037.5);
PA("INFY","D",45.65);
PA("LUPIN","U",725.35);

If you refer to global variables from inside function you have to declare the variable as global, as identifiers inside functions are local by default, so

function YourFunction()
{
  global Filter; // we want to refer to GLOBAL variable

  Filter = 1;
}

Recommended reading:
http://www.amibroker.com/guide/keyword/global.html
http://www.amibroker.com/guide/AFL.html

1 Like

Hi @Tomasz,

Thanks for your input.
I did try with this approach, still I am not getting any output. Here is the code I modified as suggested.

function PA(script,dir,price) {
      global Filter;
      if(dir=="U") cond=Name()==script AND Cross(C,price);
      if(dir=="C") cond=Name()==script AND Cross(price,c);
      Filter=cond;
	  AlertIf(cond, "SOUND C:\\Windows\\Media\\Alarm9.wav", "Audio alert"+Name(), 1);
	  SetSortColumns(-2);
}
PA("TITAN","U",1037.5);
PA("INFY","D",45.65);
PA("LUPIN","U",725.35);

But I have another set which is working, but I don't prefer it as one has put OR operator multiple times. You can have a look at this code, so you understand what I am trying to achieve.

function PA(script,dir,price) {
      if(dir=="U") cond=Name()==script AND Cross(C,price);
      if(dir=="C") cond=Name()==script AND Cross(price,c);
      return(cond);
     }

cond1=PA("TITAN","U",1037.5);
cond2=PA("INFY","D",45.65);
cond3=PA("LUPIN","U",725.35);
condd=cond1 OR cond2 OR cond3; // want to avoid putting OR repeatedly..
Filter=condd; //want to move inside PA function including calling alertif
AlertIf(condd, "SOUND C:\\Windows\\Media\\Alarm9.wav", "Audio alert"+Name(), 1);
SetSortColumns(-2);```

It works, but you are ASSIGNING a NEW value to Filter each time you call PA function. Any assignment overwrites anything what existed before. Fix your code. If you want to "OR" conditions you have to actually WRITE "OR" operator in the function as well.

Filter = 0;

function PA(...)
{
  global Filter;
  // ....
  Filter = Filter OR cond; // you need to call OR if you want to combine values from many calls
}
1 Like

Thanks for your input. It works as suggested.