Please Help, Try Modify just SL or TP of Position and failed

Gh Exe  

Hi, my code is:

how please modify tp of position and keep sl as it was

//+-------------------------------------------------------------------+
void ModifyTrades(){
   
   
  for(int i=PositionsTotal()-1; i>=0; i--){
    ulong ticket=PositionGetTicket(i);
    if(PositionSelectByTicket(ticket)){
      if(PositionGetString(POSITION_SYMBOL)==Symbol() && PositionGetInteger(POSITION_MAGIC)==MagicNumber && PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY){
      
        double SL        = PositionGetDouble(POSITION_SL);
        double TP       = PositionGetDouble(POSITION_TP);
        double Entry    = PositionGetDouble(POSITION_PRICE_OPEN);
        double Distance = Entry-SL;
        if(TP!=Entry+Distance*Reward_Ratio){
          if(OrderModify(ticket,PositionGetDouble(POSITION_SL),NormalizeDouble(Entry+Distance*Reward_Ratio, Digits())))printf("Done");
        }
      }
      if(PositionGetString(POSITION_SYMBOL)==Symbol() && PositionGetInteger(POSITION_MAGIC)==MagicNumber && PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL){
      
        double SL        = PositionGetDouble(POSITION_SL);
        double TP       = PositionGetDouble(POSITION_TP);
        double Entry    = PositionGetDouble(POSITION_PRICE_OPEN);
        double Distance = SL-Entry;
        if(TP!=Entry-Distance*Reward_Ratio){
          if(OrderModify(ticket, PositionGetDouble(POSITION_SL), NormalizeDouble(Entry-Distance*Reward_Ratio, Digits())))printf("Done");
        }
      }
   }
  }
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
bool OrderModify(ulong ticket, double stoploss, double takeprofit){

  if(trade.PositionModify(ticket, stoploss, takeprofit))
    return true;
  else return false;
}
//+------------------------------------------------------------------+
Ben  

Maybe u can try this. Mine is as below, if the price move 100 pips further and it will modify the SL.


But need to include  (#include <Trade/Trade.mqh>) at the beginning.


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

      ulong posTicket = PositionGetTicket(i);

      

      if(PositionSelectByTicket(posTicket)){

      

         if(PositionGetString(POSITION_SYMBOL) == _Symbol){

            double posSl = PositionGetDouble(POSITION_SL);

            double posTp = PositionGetDouble(POSITION_TP);

            double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);

            double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);

            double posOpenPrice = PositionGetDouble(POSITION_PRICE_OPEN);

            

            if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY){

            

               if( iclose1 > ( posOpenPrice + 1000 * SymbolInfoDouble(_Symbol,SYMBOL_POINT))){

                  double sl = posOpenPrice;

                  

                  if(sl > posSl){

                     trade.PositionModify(posTicket,sl,posTp);

                     Print("Over 100 pips, Breakeven!");

                     }

                  }

         

            }else if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL){

            

               if( iclose1 < ( posOpenPrice - 1000 * SymbolInfoDouble(_Symbol,SYMBOL_POINT))){

                  double sl = posOpenPrice;

                  

                  if(sl < posSl){

                     trade.PositionModify(posTicket,sl,posTp);

                     Print("Over 100 pips, Breakeven!");

                     }

                 }

            }

        }

      }

      }

Sugeng Lutfi Yatama  
void Singgle_TP() //================
 {  
 
 double NewTPB=0, NewTPS=0, NewSLB=0, NewSLS=0;
 //----------------------------------------  
    for(int i=PositionsTotal()-1; i>=0; i--)
       {
        if(m_position.SelectByIndex(i))
           {      
         if(New_TP != 0.0)
               {   
               NewTPB = NormalizeDouble(m_position.PriceOpen() + New_TP *PairPip, Spipsfactor) ;  
               NewTPS = NormalizeDouble(m_position.PriceOpen() - New_TP *PairPip, Spipsfactor ) ; 
               }else{
                     NewTPB = m_position.TakeProfit();
                     NewTPS = m_position.TakeProfit();
                    }    
       if(New_SL !=0.0 )
              {   
              NewSLB = NormalizeDouble(m_position.PriceOpen() - (New_SL*PairPip), Spipsfactor) ;  
              NewSLS = NormalizeDouble(m_position.PriceOpen() + (New_SL*PairPip), Spipsfactor ) ;  
              }else
                {
                NewSLB = m_position.StopLoss();
                NewSLS = m_position.StopLoss();
                }       
                      
       string order_commB =  " [Pair : "+ _Symbol+ " ]--[ Ask: "+ DoubleToString(m_symbol.Ask(),Spipsfactor)+ "]--[ Open Order: "+ DoubleToString(m_position.PriceOpen(),Spipsfactor) 
         + "]--[ SL: "+ DoubleToString(m_position.StopLoss(),Spipsfactor)+" => "+ DoubleToString(NewSLB,Spipsfactor) + "]--[ TP: "+ DoubleToString(m_position.TakeProfit(),Spipsfactor)+" => "+ DoubleToString(NewTPB,Spipsfactor)+" ]";
         
       string order_commS =  " [Pair : "+ _Symbol+ " ]--[ Bid: "+ DoubleToString(m_symbol.Bid(),Spipsfactor)+ "]--[ Open Order: "+ DoubleToString(m_position.PriceOpen(),Spipsfactor) 
         + "]--[ SL: "+ DoubleToString(m_position.StopLoss(),Spipsfactor)+" => "+ DoubleToString(NewSLS,Spipsfactor) + "]--[ TP: "+ DoubleToString(m_position.TakeProfit(),Spipsfactor)+" => "+ DoubleToString(NewTPS,Spipsfactor)+" ]";
         
                                     
       if(m_position.Symbol() == _Symbol && m_position.Magic() == Magic_Ea && m_position.PositionType() == POSITION_TYPE_BUY  )
              {
                if( ( NewTPB != 0.0 && NewTPB != m_position.TakeProfit() ) || ( NewSLB != 0.0  && NewSLB != m_position.StopLoss() ) )  
                if(m_trade.PositionModify(m_position.Ticket(), NewSLB, NewTPB)) 
                     Print("Modify TP/SL BUY : "+order_commB);  
                           else  
                              Print("Modify TP/SL BUY FAILED : "+order_commB + get_error_message(GetLastError()));  
                           Sleep(100);       
                 }
        if(m_position.Symbol() == _Symbol && m_position.Magic() == Magic_Ea && m_position.PositionType() == POSITION_TYPE_SELL  )
              {   
              if( ( NewTPS != 0.0 && NewTPS != m_position.TakeProfit() ) || ( NewSLS != 0.0  && NewSLS != m_position.StopLoss() ) )  
              if(m_trade.PositionModify(m_position.Ticket(), NewSLB, NewTPB)) 
                     
                     Print("Modify TP/SL SELL :"+order_commS); 
                              else  
                                 Print("Modify TP/SL SELL FAILED:"+ order_commS+ get_error_message(GetLastError()));  
                           Sleep(100);    
                    }     
                    //===================
              }    
        }
      //-------------------------------------------------------------------------------------------------------------    
   }
try this, friend..