# 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);

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

FirstEntry = CloseAboveLongTermMA AND Ultimate(2,4,6) < 40;
InFirstPos = Flip(FirstEntry, Sell);
FirstTrigger = ExRem(InFirstPos, Sell);

SecondEntry = CloseAboveLongTermMA AND C < FirstTriggerPrice AND InFirstPos AND Ref(InFirstPos,-1);
InSecondPos = Flip(SecondEntry, Sell);
SecondTrigger = ExRem(InSecondPos, Sell);

IIf( SecondTrigger,sigScaleIn, 0  ) );

//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;
``````