Breakeven and partial

 
void OnTick(){

//+------------------------------------------------------------------+
//|               Lógica para realização parcial e breakeven compra 1|
//+------------------------------------------------------------------+

         if(PositionsTotal() == 1 && PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY && !fezParcialCompra1){
            fezParcialCompra1 = realizarParcialCompra1(rates2[0].close);
         }
      
         if(fezParcialCompra1 && PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY && !fezBreakevenCompra1){
            fezBreakevenCompra1 = breakevenCompra1();
         }
         
//+------------------------------------------------------------------+
//|               Lógica para realização parcial e breakeven compra 2|
//+------------------------------------------------------------------+

         if(fezBreakevenCompra1 && PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY && !fezParcialCompra2){
            fezParcialCompra2 = realizarParcialCompra2(rates2[0].close);
         }
      
         if(fezParcialCompra2 && PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY && !fezBreakevenCompra2){
            fezBreakevenCompra2 = breakevenCompra2();
         }

//+------------------------------------------------------------------+
//|                Lógica para realização parcial e breakeven venda 1|
//+------------------------------------------------------------------+

            if(PositionsTotal() == 1 && PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL && !fezParcialVenda1){
               fezParcialVenda1 = realizarParcialVenda1(rates2[0].close);
            }
      
            if(fezParcialVenda1 && PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL && !fezBreakevenVenda1){
               fezBreakevenVenda1 = breakevenVenda1();
            }
            
//+------------------------------------------------------------------+
//|                Lógica para realização parcial e breakeven venda 2|
//+------------------------------------------------------------------+

            if(fezBreakevenVenda1 && PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL && !fezParcialVenda2){
               fezParcialVenda2 = realizarParcialVenda2(rates2[0].close);
            }
      
            if(fezParcialVenda2 && PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL && !fezBreakevenVenda2){
               fezBreakevenVenda2 = breakevenVenda2();
            }

}


//+------------------------------------------------------------------+
//|                             Lógica da realização parcial compra 1|
//+------------------------------------------------------------------+

bool realizarParcialCompra1(double preco){

      for(int k = PositionsTotal() - 1; k >= 0; k--){
         string symbol = PositionGetSymbol(k);
         ulong type = PositionGetInteger(POSITION_TYPE);

            if(symbol == Symbol() && type == POSITION_TYPE_BUY){
               ulong ticket = PositionGetTicket(k);

               double precoEntrada = PositionGetDouble(POSITION_PRICE_OPEN);
               double precoParcial = precoEntrada + 300.00 * _Point;

             if(preco >= precoParcial){
               return trade.PositionClosePartial(ticket, 3, 0);
             }
             }
      }
      return false; 
}

//+------------------------------------------------------------------+
//|                                      Lógica do breakeven compra 1|
//+------------------------------------------------------------------+

bool breakevenCompra1(){

     for(int i = PositionsTotal() - 1; i >= 0; i--){
         string symbol = PositionGetSymbol(i);
         ulong type = PositionGetInteger(POSITION_TYPE);

            if(symbol == Symbol() && type == POSITION_TYPE_BUY){
               ulong ticket = PositionGetTicket(i);
               
               return trade.PositionModify(ticket, 0, PositionGetDouble(POSITION_TP));
            }
     }
   return false; 
}

//+------------------------------------------------------------------+
//|                              Lógica da realização parcial venda 1|
//+------------------------------------------------------------------+

bool realizarParcialVenda1(double preco){

      for(int i = PositionsTotal() - 1; i >= 0; i--){
         string symbol = PositionGetSymbol(i);

            if(symbol == Symbol()){
               ulong ticket = PositionGetTicket(i);

               double precoEntrada = PositionGetDouble(POSITION_PRICE_OPEN);
               double precoParcial = precoEntrada - 300 * _Point;

             if(preco <= precoParcial){
             
               return trade.PositionClosePartial(ticket, 3.0, 0);
               
             }
             }
      }
      return false; 
}

//+------------------------------------------------------------------+
//|                                       Lógica do breakeven venda 1|
//+------------------------------------------------------------------+

bool breakevenVenda1(){

     for(int i = PositionsTotal() - 1; i >= 0; i--){
         string symbol = PositionGetSymbol(i);
         ulong type = PositionGetInteger(POSITION_TYPE);

            if(symbol == Symbol()){
               ulong ticket = PositionGetTicket(i);
               
               return trade.PositionModify(ticket, 0, PositionGetDouble(POSITION_TP));
            }
     }
   return false; 
}

//+------------------------------------------------------------------+
//|                             Lógica da realização parcial compra 2|
//+------------------------------------------------------------------+

bool realizarParcialCompra2(double preco){

      for(int i = PositionsTotal() - 1; i >= 0; i--){
         string symbol = PositionGetSymbol(i);
         ulong type = PositionGetInteger(POSITION_TYPE);

            if(symbol == Symbol() && type == POSITION_TYPE_BUY){
               ulong ticket = PositionGetTicket(i);

               double precoEntrada = PositionGetDouble(POSITION_PRICE_OPEN) * _Point;
               double precoParcial = precoEntrada + 500.00 * _Point;

             if(preco >= precoParcial){
               return trade.PositionClosePartial(ticket, 2, 0);
             }
             }
      }
      return false; 
}

//+------------------------------------------------------------------+
//|                                      Lógica do breakeven compra 2|
//+------------------------------------------------------------------+

bool breakevenCompra2(){

     for(int i = PositionsTotal() - 1; i >= 0; i--){
         string symbol = PositionGetSymbol(i);
         ulong type = PositionGetInteger(POSITION_TYPE);

            if(symbol == Symbol() && type == POSITION_TYPE_BUY){
               ulong ticket = PositionGetTicket(i);
               double precoEntrada = PositionGetDouble(POSITION_PRICE_OPEN) * _Point;
               double precoBreakeven = precoEntrada + 300.00 * _Point;
               
               return trade.PositionModify(ticket, precoBreakeven, PositionGetDouble(POSITION_TP));
            }
     }
   return false; 
}

//+------------------------------------------------------------------+
//|                              Lógica da realização parcial venda 2|
//+------------------------------------------------------------------+

bool realizarParcialVenda2(double preco){

      for(int i = PositionsTotal() - 1; i >= 0; i--){
         string symbol = PositionGetSymbol(i);
         ulong type = PositionGetInteger(POSITION_TYPE);

            if(symbol == Symbol() && type == POSITION_TYPE_SELL){
               ulong ticket = PositionGetTicket(i);

               double precoEntrada = PositionGetDouble(POSITION_PRICE_OPEN) * _Point;
               double precoParcial = precoEntrada - 500.00 * _Point;

             if(preco <= precoParcial){
               return trade.PositionClosePartial(ticket, 2, 0);
             }
             }
      }
      return false; 
}

//+------------------------------------------------------------------+
//|                                       Lógica do breakeven venda 2|
//+------------------------------------------------------------------+

bool breakevenVenda2(){

     for(int i = PositionsTotal() - 1; i >= 0; i--){
         string symbol = PositionGetSymbol(i);
         ulong type = PositionGetInteger(POSITION_TYPE);

            if(symbol == Symbol() && type == POSITION_TYPE_SELL){
               ulong ticket = PositionGetTicket(i);
               double precoEntrada = PositionGetDouble(POSITION_PRICE_OPEN) * _Point;
               double precoBreakeven = precoEntrada - 300.00 * _Point;
               
               return trade.PositionModify(ticket, precoBreakeven, PositionGetDouble(POSITION_TP));
            }
     }
   return false; 
}

Hey guys, I'm having issues about getting partials and realizing breakevens. The code I've used is this. Can anyone see what's wrong about that?

 
angelohomen:

Hey guys, I'm having issues about getting partials and realizing breakevens. The code I've used is this. Can anyone see what's wrong about that?

Try coding this way:

         for(int i=0;i<PositionsTotal();i++){
            ulong iTicket=PositionGetTicket(i);
            if(PositionGetString(POSITION_SYMBOL)==_Symbol){
               if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY){
                  double PClose=PositionGetDouble(POSITION_PRICE_OPEN)+Distance*_Point; // PartialClose target
                  if(PositionGetDouble(POSITION_PRICE_CURRENT)>PClose&&
                  (PositionGetDouble(POSITION_SL)<PositionGetDouble(POSITION_PRICE_OPEN))){
                     if(!trade.PositionClosePartial(iTicket,NormalizeDouble(PositionGetDouble(POSITION_VOLUME)*Percent,2),ULONG_MAX)){ //PartialClose volume
                        Print("PositionClosePartial error ",trade.ResultRetcode());
                        }
                     else if(!trade.PositionModify(iTicket,PositionGetDouble(POSITION_PRICE_OPEN),PositionGetDouble(POSITION_TP)){ // Breakeven
                        Print("PositionModify error ",trade.ResultRetcode());
                        }
 
David Diez:

Try coding this way:

It worked! Thank you so much!
Reason: