Avoiding multiple buy/sell signal on single bar

Hello guys,
I am trying to avoid multiple buy/sell signals triggered on a single bar. I tried logic, but it is overwritten with initialized value again and again.

       onBuy = onShort = false;
	if(LastValue(Buy,true) == True AND NOT onBuy){
		_TRACE("Placing order in long position");
		onBuy = True;
	}else if(LastValue(Short,true) == True AND NOT onShort){
		_TRACE("Placing order in short position");		
		onShort = True;
	}

What happens is, the onBuy or onShort variable remains false throughout the execution. And I am unable to execute that particular snippet only once.

See this KB article

https://www.amibroker.com/kb/2015/11/29/how-to-execute-part-of-the-formula-only-when-new-bar-is-added/

Although it is more centered around running a piece of code, once every bar, you can use the same logic.
You can store the state using a static variable, because otherwise each time the formula runs, it will evaluate the formula variables afresh.
Then update the state only when your desired event occurs.