Incomplete Sell Signal in AFL code as my requirement , need to fix it

Hi everyone,
What is the way just to mark condition as a flag (not sell at once) in the first time occur but sell it if condition arises again in the second times?

{
    PosQty = 1;  //number of stock holding
    SetOption( "InitialEquity", 10000000 );
    SetOption( "MaxOpenPositions", PosQty );
    SetOption( "MinShares", 100 );
    RoundLotSize = 100;
    SetPositionSize( 100 / PosQty, spsPercentOfEquity );
    SetOption( "CommissionMode", 1 );
    SetOption( "CommissionAmount", 0.0 );
    SetTradeDelays( 0, 0, 0, 0 );
    BuyPrice = Close;
    SellPrice = Close;
}
{
buyInStrongUptrend=C>HHV(Ref(H,-1),50);
Buy=buyInStrongUptrend ;

raiseFallEvent=( C<MA(C,50) AND C<EMA(C,50) AND C< (HHV(Ref(H,-1),50)+LLV(Ref(L,-1),50))/2 )  AND  C>LLV(Ref(L,-1),50);

//checkSecondTime_RaiseFallEvent= {what is logic to Sell if raiseFallEvent== True in the second time}
//sellOnWeakness_SecondTime=(raiseFallEvent AND checkSecondTime_RaiseFallEvent ) or   C<LLV(Ref(L,-1),50);
//Sell=sellOnWeakness_SecondTime;

Sell=raiseFallEvent;

The above is a sample code but it still misses some additional logic as my requirement.
mega1

The figure above, The Sell signal occur when condtion is true , as a result the system do a buy and a sell twice.

raiseFallEvent=( C<MA(C,50) AND C<EMA(C,50) AND C< (HHV(Ref(H,-1),50)+LLV(Ref(L,-1),50))/2 )  AND  C>LLV(Ref(L,-1),50);  

//checkSecondTime_RaiseFallEvent= {what is logic to Sell if raiseFallEvent== True in the second time}
//sellOnWeakness_SecondTime=(raiseFallEvent AND checkSecondTime_RaiseFallEvent ) or   C<LLV(Ref(L,-1),50);


There are 2 comments as above that I don’t have any coding ideas for fixing it.

I don’t want to sell when this condition is true at once ON THE FIRST TIME. Actually I would like to just use this condition to mark an event condition as the flag (like True/False) in order to sell it when this condition arises again on THE SECOND TIME.

The below show sell signal as my requirement.

pm2

The signal will sell when the raiseFallEvent variable is true in the second time.
BUT , if raiseFallEvent fire event in the firstime along with C< C<LLV(Ref(L,-1),50) , the system can sell.

These below show a few charts that are the same pattern as I question.
mega

wha

Thank you very much for every help.
Pongthorn

You could just count sell conditions since buy:

Sell = SumSince( Buy, raiseFallEvent ) == 2; // Sell if raiseFallEvent occured twice since buy

SumSince requires AmiBroker 6.10 http://www.amibroker.com/f?sumsince

Hi

I revise as you help a bit, this way solve consecutive condtion count.
Sell = SumSince( Buy, Exrem(raiseFallEvent,!raiseFallEvent) ) == 2;
// Sell if raiseFallEvent occured twice since buy