Strategy tester opening & closing sell trades immediately yet buy trades are okay. What could be the problem?

 
The logic is that all trades should be closed on opposite signal but only buy trades respect this rule. Sell trades are closed immediately.  Buy and sell trades are executed properly when there is a buy or a sell signal, however calling these functions, CloseSellPositions() on Buy signal and CloseBuyPositions() on Sell signal, sell trades are closed immediately they're opened but buy trades are only closed on sell signal. I'm using the CPositionInfo class with post as the position object in the Trade.mqh library. Help.
void CloseSellPositions(){

 for(int i= PositionsTotal()-1; i>=0; i--){
    post.SelectByIndex(i);
    ulong ticket = post.Ticket();
        if(post.Symbol()==_Symbol && post.Magic()==inpMagic && post.Type()==POSITION_TYPE_SELL){
           trade.PositionClose(ticket);
        }
   }
}

void CloseBuyPositions(){

 for(int i= PositionsTotal()-1; i>=0; i--){
    post.SelectByIndex(i);
    ulong ticket = post.Ticket();
        if(post.Symbol()==_Symbol && post.Magic()==inpMagic && post.Type()==POSITION_TYPE_BUY){
           trade.PositionClose(ticket);
        }
   }
}
 
Douglas Mwaura Ndungu:
The logic is that all trades should be closed on opposite signal but only buy trades respect this rule. Sell trades are closed immediately.  Buy and sell trades are executed properly when there is a buy or a sell signal, however calling these functions, CloseSellPositions() on Buy signal and CloseBuyPositions() on Sell signal, sell trades are closed immediately they're opened but buy trades are only closed on sell signal. I'm using the CPositionInfo class with post as the position object in the Trade.mqh library. Help.

The full logic is clearly not in the above, so how do you expect anybody to find your error?

and i recommend that you search this site for how to use SelectByIndex