Re entry after stop loss hit if buy condition is still valid and close above the initial buying price

I am a newbie when it comes to writing afl, I have this basic moving average crossover formula that I am using for back testing.

I need help writing a afl which generates a buy if the price closes above the initial buying price after the stop loss is triggered and the original buy condition is still valid. chart_01

I am attaching a screenshot of the chart with a brief description so its easier to understand.

The white arrow marks the buy point based on moving average crossover. Few candles later the price closed below the stop loss level (represented by yellow line) exiting the trade. And then a couple of candles later it closes above the initial buying price represented by dotted white line. while this happens the buy condition is still valid "FastMA(green line) > SlowMA (red line). Orange arrow marks the candle at the end of which the buy should be generated.

Below is the afl I am using to do backtesting

SetBarsRequired(10000,10000);
SetFormulaName("Sample System");
SetTradeDelays( 1, 1, 1, 1 );
SetOption( "initialequity", 100 );
PositionSize = -100;
SetOption( "MaxOpenPositions", 2 );


FastMA = 24;
SlowMA = 46;

buy = cross( ma( close, FastMA ), ma( close, SlowMA ) ); 
sell = cross( ma( close, SlowMA ), ma( close, FastMA ) );

Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );
Short = Sell;
Cover = Buy;

ApplyStop ( 0, 1, 3, 2, False, 1, 0, -1 );

Instead of "Cross" try using Greater than / Less than condition :-
Dont allow buy trade when stoploss is hit (stop_flag = true) and do not allow buy if max position is reached so that you will not by repeatedly. These conditions prevent repeated buy-sell on stop loss candle and prevent repeated buy trigger subsequebtly.

buy = ma( close, FastMA ) >  ma( close, SlowMA )  AND present_positions < MaxPositions AND !stop_flag ; 
sell = ma( close, SlowMA ) < ma( close, FastMA )  and present_positions > 0;