For creating stops use ApplyStop function but not ValueWhen.
/// Do not remove links from code you have not written
/// It's not been your time spent
/// @link https://www.amibroker.com/guide/afl/applystop.html
/// @link http://www.amibroker.com/kb/index.php?s=applystop
/// Code source:
/// @link https://forum.amibroker.com/t/finding-way-to-store-the-entry-price-for-calculating-exit-level/19045/2
SetOption("ActivateStopsImmediately", 1);
SetOption("ReverseSignalForcesExit", 0);
SetOption("MaxOpenPositions", 2);
SetPositionSize(1, spsPercentOfEquity);
/// In case of wanting to have Buy and Short
/// opened at same time per symbol:
//SetBacktestMode(backtestRegularRawMulti);
BuyPrice = Open;
ShortPrice = Open;
myma = MA(Close, 15);
Buy = Cross(Close, myma);
Short = Cross(myma, Close);
Buy = Ref(Buy, -1);
Short = Ref(Short, -1);
Sell = Cover = 0;
SellPrice = CoverPrice = Close;
ApplyStop(stopTypeLoss, stopModePoint, loss_amount = 60, exitatstop = 0);
ApplyStop(stopTypeProfit, stopModePoint, tgt_amount = 100, exitatstop);
// For chart pane display
if ( Status("action") == actionIndicator ) {
eq = Equity(1, 0);
intrade_long = Flip(Buy, Sell);
intrade_short = Flip(Short, Cover);
stoplevel_long = ValueWhen(Buy, BuyPrice-loss_amount);
tgt_level_long = ValueWhen(Buy, BuyPrice+tgt_amount);
stoplevel_short = ValueWhen(Short, ShortPrice+loss_amount);
tgt_level_short = ValueWhen(Short, ShortPrice-tgt_amount);
Plot( C, "Price", colorDefault, styleBar );
PlotShapes(Buy*shapesmallUpTriangle,colorGreen,0, L);
PlotShapes((Sell>0)*shapeDownArrow,colorRed,0,H);
PlotShapes(Short*shapesmallDownTriangle,colorRed,0,H);
PlotShapes((Cover>0)*shapeUpArrow,colorAqua,0, L);
Plot(IIf(intrade_long, tgt_level_long, Null), "tgt_level_long", colorGreen );
Plot(IIf(intrade_long, stoplevel_long, Null), "stoplevel_long", colorRed );
Plot(IIf(intrade_short, tgt_level_short, Null), "tgt_level_short", colorPaleGreen );
Plot(IIf(intrade_short, stoplevel_short, Null), "stoplevel_short", colorOrange );
}
