Can any amibroker expert give popup window alert for below code?

i am having following relevant popup code but not giving any alert, but when it is called in FOR loop working but generating both Buy and Sell signals same time with multiple windows and unable to close also.

1 ) where to all this function exactly in code, so that only the recent signal BUY or SELL popup generated where window will be closed manually or with time interval

  1. please provide modified or relevant code if any

function PopupWindowEx( popupID, bodytext, captiontext, timeout, left, top )
{
displayText = bodytext + captiontext;
if ( ( StaticVarGetText( “prevPopup” + popupID ) != displayText) OR ( StaticVarGet( “prevPopupTime” + popupID ) < GetSecondNum() ) )
{
StaticVarSetText( “prevPopup” + popupID, displayText);
StaticVarSet( “prevPopupTime” + popupID, GetSecondNum() + timeout );
PopupWindow( bodytext, Captiontext + popupID, timeout, Left, top );

}

}

Buy = Buy condition

Sell = Sell condition

for( i = 0; i < BarCount; i++ )
{
if( Buy[i] )
{
PlotText( “\nBuy:” + L[ i ] + "\nT= " + (L[i]*1.005) + "\nSL= " + (L[i]*0.9975), i, L[ i ]-dist[i], colorGreen, colorWhite );
}
if( Sell[i] )
{
PlotText( “Sell:” + H[ i ] + "\nT= " + (H[i]*0.995) + "\nSL= " + (H[i]*1.0025), i, H[ i ]+dist1[i], colorRed, colorWhite );
}
}

if (Buy)
{
PopupWindowEx( ) ;
}
if (Sell)
{
PopupWindowEx( ) ;
}

Check the below example function as reference for popup window with alert for reference

	if (trend<0) 
		{ 
			DownTrendLine[i]=bsmax[i];
			if (SoundON==True && !TurnedDown && i==BarCount-1 && IsEmpty(DownTrendLine[i-1])) 
			{
				Say("Attention please, Market Going, DOWN");
				PopupWindow( Date()+ "\n  > " + Name() + "\n  > Sell : " + H[i] + "\n  > Target : " + (H[i]*0.995)+"\n  > Stop Loss : " +(H[i]*1.0025),"Alert", 100, 640*mtRandom(), 480*mtRandom());
				TurnedUp=StaticVarSet("TurnedUp",0);
				TurnedDown=StaticVarSet("TurnedDown",1);
			} 
		} 
	} 

thanks for sharing,
i am using amibroker 5.6 ,will if (trend) condition work with out [ ] square brackets in your case , i am using Buy and sell conditions to remove excess signal, shall i replace this if statement like this for my case ,
if (i==BarCount-1])