Multiple Stock alerts from AFL code

Hi Guys,

I want to set up multiple stock alerts. I have done single and copied and pasted to make the multiple alert. But that is cumbersome. What I want to do was storing the value in the multidiemntional array and loop the array and function to check to generate. I already looked at Martrix(). Not sure it is the one. Array needs to be dynamic because length of array is not known beforehand. for example, all user needs to do is just fill in value such as StockName, AlertPrice and Message. i.e...


Alerts = { 
               StockName1, AlertPrice1,Message1
               StockName2, AlertPrice2,Message2 and so on
             }



//Below code is what I use currently, I have to recreate the code for every stock i want to have alert. //Function is used to check the price is near alert price by whatever percent set eg. distance between //alert and price is 1%. 


function _NearMyAlert(_AlertPrice,_CurrentPrice)
{
	_alert = False;
	_percent = 1; //percent near the alert price
	_CurrentPrice = _CurrentPrice[BarCount - 1];
	_diff = _CurrentPrice - _AlertPrice; //check the different between price
	_diff = abs(_diff); //convert to absolut value
	_per = (_diff/_AlertPrice) * 100; 
	if( _per <= _percent ) 
		_alert = True;
	else	
		_alert = False;
		
	return _alert;
}
/*

*/

_code1 = "PRU";  //Enter Stock Code
_alertPrice1 = 1.30;  //Enter Alert Price
_Alert1 = Name() == _code1 AND 
		  _NearMyAlert(_alertPrice1,C);
AlertIf(_Alert1, "", _code1 +" Alert @ "+ _alertPrice1);

_code2 = "ABP";  //Enter Stock Code
_alertPrice2 = 2.72;  //Enter Alert Price
_Alert2 = Name() == _code2 AND 
		  _NearMyAlert(_alertPrice2,C);
AlertIf(_Alert2, "", _code2 +" Alert @ "+ _alertPrice2);

_code3 = "MGR";  //Enter Stock Code
_alertPrice3 = 2.40;  //Enter Alert Price
_Alert3 = Name() == _code3 AND 
		  _NearMyAlert(_alertPrice3,C);
AlertIf(_Alert3, "", _code3 +" Alert @ "+ _alertPrice3);

You still have copy/pasted code. Instead of doing this, use STRING LIST.

strlist = "PRU,1.30,message1\n"+
          "ABP,2.72,message2\n"+
          "MGR,2.40,message3";

for( i = 0; ( item = StrExtract( strlist, i, '\n' ) ) != ""; i++ )
{
	ticker = StrExtract( item, 0 );
	pricelevel = StrToNum( StrExtract( item, 1 ) );
	msg = StrExtract( item, 2 );

	printf("Ticker: %s, PriceLevel %g, Message: %s\n", ticker, pricelevel, msg	); 
}
3 Likes

thanks Tomasz. that will do what I wanted.