Exit logic coding help

I have put together a mean reversion system. Simple entry logic, First entry is when ultimate oscillator is oversold, second entry is when price drops below entry price and final exit is when oscillator is overbought. Money management is built in. Now i want to change the exit to close trade when price closes above the first entry price. I have tried several attempts but could not figure it out. Code is below. Thanks

SetOption( "InitialEquity", 100000 );
MaxPos = 5;
SetOption( "MaxOpenPositions", MaxPos );
PositionScore = 100-Ultimate(2,4,6);


MALongPeriod = 200;

// Long logic

CloseAboveLongTermMA = C > EMA(C, MALongPeriod);

SetTradeDelays(0, 0, 0, 0);

Sell = Ultimate (2,4,6) > 80;


BarsSinceSell = BarsSince(Sell);

FirstEntry = CloseAboveLongTermMA AND Ultimate(2,4,6) < 40;
InFirstPos = Flip(FirstEntry, Sell);
FirstTrigger = ExRem(InFirstPos, Sell);
BarsSinceFirstTrigger = BarsSince(FirstTrigger);
FirstTriggerPrice = IIf(BarsSinceFirstTrigger < BarsSinceSell, Ref(C,-BarsSinceFirstTrigger), 0 );

SecondEntry = CloseAboveLongTermMA AND C < FirstTriggerPrice AND InFirstPos AND Ref(InFirstPos,-1);
InSecondPos = Flip(SecondEntry, Sell);
SecondTrigger = ExRem(InSecondPos, Sell);
BarsSinceSecondTrigger = BarsSince(SecondTrigger);
SecondTriggerPrice = IIf(BarsSinceSecondTrigger < BarsSinceSell, Ref(C,-BarsSinceSecondTrigger), 0);

Buy = IIf( FirstTrigger, 1, 
             IIf( SecondTrigger,sigScaleIn, 0  ) );


Sell = ExRem(Sell,Buy);

//Position sizing

EntryPosSize1 = 10; // Size for first entry
EntryPosSize2 = 10; // Size for second entry
PosSize = (IsTrue(FirstTrigger) * EntryPosSize1) + (IsTrue(SecondTrigger) * EntryPosSize2);
             
SetPositionSize(PosSize, spsPercentOfEquity);


PlotShapes(IIf(FirstTrigger, shapeUpTriangle, shapeNone),colorGreen, 0, L);
PlotShapes(IIf(Secondtrigger, shapeupTriangle, shapeNone),colorBlue, 0, L);
PlotShapes(IIf(Sell,shapeDownTriangle, shapeNone),colorRed, 0, H);

Filter = Firstentry;