Error of Money Management into my EA

 
Hello,

I have a problem about writing my Money Management.

This must work like this :

If I have a BUY signal, I open with 1 lot.
The first time I have a profit (variable Profit), I want modify the order with a StopLoss equal to open price, and put a new TakeProfit (current price + Decrement_TP) and close a little part (Decrement = 0.1 lot) of the trade.
After for each profit, I want to modify with a new TakeProfit level, but do not change the StopLoss which must equal to the initial open order price.
When I have only 0.1 lot into the trade, I'm waiting a SELL signal to close and open a new trade (SELL trade)
Decrement_TP = 5, Decrement = 0.1, initial lots at open trade >= 0.1 usualy 1 lot.

When price turn to below the StopLoss, the EA must close the entire trade and wait the next bar to check the signal.

With the code below, expert doesn't work like I want, sometimes it only close one part (for an initial 1 lot), sometimes it doesn't modify or even close trade.
Expert doesn't respect the next bar signal and somtimes it doesn't follow instruction about reverse signal...

Could you help me please ?

Thank you,

Have a nice day !



  bool   IsTrade  = False ;
  
  for ( int i = 0 ; i < Total ; i++ )
  {
    if ( !OrderSelect ( i , SELECT_BY_POS ) )
    {
      Print ( "Error OrderSelect (" , i , ") # " , GetLastError ( ) ) ;
      continue ;
    }
    
    if ( OrderSymbol ( ) != Symbol ( ) ) continue ;
    
    if ( OrderMagicNumber ( ) != MagicNumber ) continue;
    
    OrderSelect ( i , SELECT_BY_POS , MODE_TRADES ) ;
    if ( OrderType ( ) <= OP_SELL &&  OrderSymbol ( ) == Symbol ( ) )
    {
      IsTrade = True ;
      
      if ( OrderType ( ) == OP_BUY )
      {
        if ( UseDecrement )
        {
          if ( OrderLots() != Decrement )
          {
            if ( OrderTakeProfit ( ) != ''  && Bid == OrderTakeProfit ( ) )
            {
              OrderModify ( OrderTicket ( ) , OrderOpenPrice ( ) , OrderStopLoss ( ) , Bid + ( Decrement_TP * Point ) , 0 , Color_CloseBuy ) ;
              OrderClose ( OrderTicket ( ) , Decrement , Bid , Slippage , MediumSeaGreen ) ;
            }
            
            Profit = OrderOpenPrice ( ) + ( Decrement_TP * Point ) ;
            
            if ( OrderTakeProfit ( ) == ''  && Bid == Profit )
            {
              OrderModify ( OrderTicket ( ) , OrderOpenPrice ( ) , OrderOpenPrice ( ) , Bid + ( Decrement_TP * Point ) , 0 , Color_CloseBuy ) ;
              OrderClose ( OrderTicket ( ) , Decrement , Bid , Slippage , MediumSeaGreen ) ;
            }
          } else {
            if ( SignalCC == 0 && ( ( EachTickMode && !TickCheck ) || ( !EachTickMode && ( Bars != BarCount ) ) ) )
            {
              OrderClose ( OrderTicket ( ) , OrderLots ( ) , Bid , Slippage , MediumSeaGreen ) ;
              if ( !EachTickMode ) BarCount = Bars ;
              IsTrade = False ;
              continue ;
            }
          }
        } else {
          if ( SignalCC == 0 && ( ( EachTickMode && !TickCheck ) || ( !EachTickMode && ( Bars != BarCount ) ) ) )
          {
            OrderClose ( OrderTicket ( ) , OrderLots ( ) , Bid , Slippage , MediumSeaGreen ) ;
            if ( !EachTickMode ) BarCount = Bars ;
            IsTrade = False ;
            continue ;
          }
        }
        
        if ( UseTrailingStop && TrailingStop > 0 )
        {
          if ( Bid - OrderOpenPrice ( ) > Point * TrailingStop )
          {
            if ( OrderStopLoss ( ) < Bid - Point * TrailingStop )
            {
              OrderModify ( OrderTicket ( ) , OrderOpenPrice ( ) , Bid - Point * TrailingStop , OrderTakeProfit ( ) , 0 , Color_CloseBuy ) ;
              if ( !EachTickMode ) BarCount = Bars ;
              continue ;
            }
          }
        }
        
      } else {
        
        if ( SignalCC == 1 && ( ( EachTickMode && !TickCheck ) || ( !EachTickMode && ( Bars != BarCount ) ) ) )
        {
          OrderClose ( OrderTicket ( ) , OrderLots ( ) , Ask , Slippage , DarkOrange ) ;
          if ( !EachTickMode ) BarCount = Bars ;
          IsTrade = False ;
          continue ;
        }
        
        if ( UseTrailingStop && TrailingStop > 0 )
        {
          if ( ( OrderOpenPrice ( ) - Ask ) > ( Point * TrailingStop ) )
          {
            if ( ( OrderStopLoss ( ) > ( Ask + Point * TrailingStop ) ) || ( OrderStopLoss ( ) == 0 ) )
            {
              OrderModify ( OrderTicket ( ) , OrderOpenPrice ( ) , Ask + Point * TrailingStop , OrderTakeProfit ( ) , 0 , Color_CloseSell ) ;
              if ( !EachTickMode ) BarCount = Bars ;
              continue ;
            }
          }
        }
      }
    }
  }
Reason: