Problem about store the Buy/Short Price

I want to write Limit 1 day 1 trade and store the Buy/Short price to variable when buy condition and sell at 50pts profit or 30pts cut loss
But as you see the pic, seems fail to store the buy/short price.
Thanks for help!

_SECTION_BEGIN("Real Time Trade");

newDay = Day() != Ref(Day(),-1);

time=TimeNum();

ma15 = ma(C,8);
Plot (ma15, "MA 15",colorlightorange, styleDots|styleThick,Null,Null,Null,11);

_Buy = Ref(C,-1) > Ref(ma15,-1) AND time >093500;
buypr=ValueWhen(_Buy,O,1);
_Sell = C > buypr + 50 OR C< buypr - 30 OR time >153500;
_Short = Ref(C,-1) < Ref(ma15,-1) AND time >0935000;
shortpr=ValueWhen(_Short,O,1);
_Cover = C < shortpr - 50 OR C > shortpr + 30 OR time>153000;

N = 1;
Buy = Sell = Short = Cover = Null;
LongFlag = ShortFlag = tradeCount = 0; 
buypr = Null;  // Initialize buypr variable to null

for (i = 0; i < BarCount; i++) {
    if (newDay[i]) tradeCount = 0;



    // Long Positions
    if (_Buy[i] AND tradeCount < N AND LongFlag == 0) {
        Buy[i] = 1;
        LongFlag = 1;
        buypr = Open[i];  // Store the executed price in buypr
        tradeCount++;  // To record that we are in Long position
    }
    if (_Sell[i] AND LongFlag == 1) {
        Sell[i] = 1;  // Selling-off the Long position
        buypr = Null;  // Clear the buypr variable
        LongFlag = 0;  // Resetting LongFlag back to False, to denote that we are no longer in "Long" position
    }
    
    // Short Positions
    if (_Short[i] AND tradeCount < N AND ShortFlag == 0) {
        Short[i] = 1;
        ShortFlag = 1;
        shortpr = Open[i];  // Store the executed price in shortpr
        tradeCount++;  // To record that we are in Short position
    }
    if (_Cover[i] AND ShortFlag == 1) {
        Cover[i] = 1;  // Covering the Short position
        shortpr = Null;  // Clear the shortpr variable
        ShortFlag = 0;  // Resetting ShortFlag back to False, to denote that we are no longer in "Short" position
    }
}

PlotShapes(IIf(Buy, shapeuptriangle, shapeNone),colorTurquoise, 0, L, Offset=-65);
PlotShapes(IIf(Cover, shapehollowuptriangle, shapeNone),colorTurquoise, 0, L, Offset=-80);

PlotShapes(IIf(Short, shapedowntriangle, shapeNone),colorCustom12, 0, H, Offset=-75);
PlotShapes(IIf(sell, shapehollowdowntriangle, shapeNone),colorCustom12, 0, H, Offset=-60);




_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);


Plot( C, "Close", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 


_SECTION_END();
 



To store price at the point of buy, you have to have signals in IMPULSE form.
Currently your code uses STATE form.
If you use STATE form, ValueWhen would return most recent occurence, not the first one. You can use ExRem to convert from State to Impulse form, however, it is all not needed at all.

Instead of trying implement stops yourself, you should use ApplyStop function instead.

ApplyStop Function not only for Backtest?
It can be use in Program Trade as well?

ApplyStop is instruction for backtest. If you need to evaluate ApplyStop in non-backtest code, you can use Equity(1) function that is backtester-in-a-box.

It means if for real time trade i set sell = applystop 50pts, then it can execute the order?