Signal on Fractals

 

Hi all, I'm trying to code a sample advisor based on Fractals but it's currently opening a single position, thus not closing (it should by opposite). I've coded this way:

      if(Spread<=MaxSpread){
         if(FRACTALS[1]<iLow(SName,WTimeframe,1)&&BuyOrders<1){
            if(!trade.PositionOpen(SName,ORDER_TYPE_BUY,LotSize,Ask,SL,TP,CustomComment)){
               Print("PositionOpen error ",trade.ResultRetcode());
               return;
               }
            }
         else if(FRACTALS[1]>iHigh(SName,WTimeframe,1)&&SellOrders<1){
            if(!trade.PositionOpen(SName,ORDER_TYPE_SELL,LotSize,Bid,SL,TP,CustomComment)){
               Print("PositionOpen error ",trade.ResultRetcode());
               return;
               }
            }
         }

Any idea? Thank you in advance.