Need Help! - How to make trailing stop not take losses when negative profit?

 
I am back testing using MB trading demo. Th standard trailing stop code will not take into consideration of the commission and order swap.
Anyone had tried successful to have the trailing stop not take losses with negative profit?

typical trailing stop code:

void ShortTrailingStop()
{
int scnt;
int stotal = OrdersTotal();

for(scnt=stotal-1; scnt >= 0; scnt--)
{
OrderSelect(scnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())
{
if(OrderType()==OP_SELL) // short position is opened
{
if(TrailingStop>0)
{
if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
{
if((OrderStopLoss()>(Ask+(Point*Tra ilingStop))) || (OrderStopLoss()==0))
{
OrderModify(OrderTicket(),OrderOpen Price(),Ask+(Point*TrailingStop),Or derTakeProfit(),0,Red);
return(0);
}
}
}
}
}
}
}
 

Just first check if the order is profitable:

Profit = OrderProfit() + OrderSwap() + OrderCommission();//Sum of the order's profit and expences
if(Profit > Limit)
{
//CODE
}
 

Just replace

if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
to

//if((OrderOpenPrice()-Ask)>(Point*TrailingStop))

Reason: