LastValue should not be used in backtest, was: Simplest code doesn't work

Hi !
I'm trying to adjust buyprice so stoporder to initiate long could be executed at more correct price if we have gap . It is as simple as possible but still doesn't work, price is not adjusted. Could someone help ?

entrylongprice=Ref( HHV(High,100) ,-1);
Buy=(High>entrylongprice);
if (  (LastValue(O)>LastValue(entrylongprice)) ) {entrylongprice= O;}
BuyPrice=entrylongprice;

Thanks a lot in advance..

Your code is simply incorrect. You are using LastValue which is big mistake. This function should never be used in backtest. For correct coding see http://www.amibroker.com/kb/2014/11/26/handling-limit-orders-in-the-backtester/
See also http://www.amibroker.com/f?lastvalue and http://www.amibroker.com/guide/h_understandafl.html