Take Profit Goes 0.00 When Modifying Trailing Stop Loss

 

Hello All,

I am very much new to MQL5 programming language and since couple of weeks I have been working on an EA that enters into buy or sell positions based upon a set conditions and rules. Additionally, it also take trailing stops into consideration if there is a movement in the right direction.

However, when it takes that decision and modifies the position, my TP (Take Profit) target becomes "0.00" and eventually the position remains open until:


1. I manually close it.

2. Trailing SL (Stop Loss) gets triggered.


This way I am not able to leverage the optimum movement of a currency pair. Here is the piece of code that is responsible for position modification. I want trailing SL but TP should not be changing. In this case the TP becomes 0.00 when the position is modified.

This is the function that gets called from the OnTick() function to modify the position. 

//---Create The Function To Adjust The Trailing Stop Loss (Sell Position)
void CheckTrailingStopSell(double Bid)
 {

//---Set The Stop Loss to 40 Points
   double SLS = NormalizeDouble(Bid+100*_Point,_Digits);
      
//---Go Through All Open Positions
   for(int i=PositionsTotal()-1; i>=0; i--)
      {
         string symbol = PositionGetSymbol(i); //Get The Symbol Of The Position
         if (_Symbol==symbol) //Check If Pair Is A Match
         if (PositionGetInteger(POSITION_TYPE)==ORDER_TYPE_SELL) //Check If Position Is Sell
            {
               ulong PositionTicket = PositionGetInteger(POSITION_TICKET); //Get Position Ticket Number
               double CurrentStopLossSell = PositionGetDouble(POSITION_SL); //Get Position Stop Loss
               double CurrentTakeProfitSell = PositionGetDouble(POSITION_TP); //Get Position Take Profit
               if (CurrentStopLossSell>SLS)
                  {
                     trade.PositionModify(PositionTicket,(CurrentStopLossSell-50*_Point),0);                 
                  }
            }
      }   
 }


All the help is highly appreciated.


Thanks.. Mohammad Imran Shamsi

 

Type of POSITION_TYPE

ENUM_POSITION_TYPE

Identifier

Description

POSITION_TYPE_BUY

Buy

POSITION_TYPE_SELL

Sell


NOT ORDER_TYPE_SELL !!!


//+------------------------------------------------------------------+
//| Check Trailing Stop Sell Position                                |
//+------------------------------------------------------------------+
void CheckTrailingStopSell(double Bid)
  {
//--- set The Stop Loss to 40 Points
   double SLS=NormalizeDouble(Bid+100.0*Point(),Digits());
//--- go Through All Open Positions
   for(int i=PositionsTotal()-1; i>=0; i--)
     {
      string symbol = PositionGetSymbol(i); // get The Symbol Of The Position
      if(symbol=Symbol())  // check If Pair Is A Match
         if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)  // check If Position Is Sell
           {
            ulong PositionTicket=PositionGetInteger(POSITION_TICKET); // get Position Ticket Number
            double CurrentStopLossSell=PositionGetDouble(POSITION_SL); // get Position Stop Loss
            double CurrentTakeProfitSell=PositionGetDouble(POSITION_TP); // get Position Take Profit
            if(CurrentStopLossSell>SLS)
              {
               //--- modify only Stop Loss, Take profit do not touch
               trade.PositionModify(PositionTicket,(CurrentStopLossSell-50.0*Point()),CurrentTakeProfitSell);
              }
           }
     }
  }
 
//---Create The Function To Adjust The Trailing Stop Loss (Sell Position)
void CheckTrailingStopSell(double Bid)
 {

//---Set The Stop Loss to 40 Points
   double SLS = NormalizeDouble(Bid+100*_Point,_Digits);
      
//---Go Through All Open Positions
   for(int i=PositionsTotal()-1; i>=0; i--)
      {
         string symbol = PositionGetSymbol(i); //Get The Symbol Of The Position
         if (_Symbol==symbol) //Check If Pair Is A Match
         if (PositionGetInteger(POSITION_TYPE)==ORDER_TYPE_SELL) //Check If Position Is Sell
            {
               ulong PositionTicket = PositionGetInteger(POSITION_TICKET); //Get Position Ticket Number
               double CurrentStopLossSell = PositionGetDouble(POSITION_SL); //Get Position Stop Loss
               double CurrentTakeProfitSell = PositionGetDouble(POSITION_TP); //Get Position Take Profit
               if (CurrentStopLossSell>SLS)
                  {
                     trade.PositionModify(PositionTicket,(CurrentStopLossSell-50*_Point),0);                 
                  }
            }
      }   
 }

If you set it to 0, it will be changed to 0, it's a quiet obvious!

Replace 0 with CurrentTakeProfitSell 

 

Hello Fabio and Vladimir,

Thank you so much both of you for your valuable inputs. I was able to fix the issue using the solution provided.

Now it looks an obviously silly error.

Regards,

Mohammad Imran Shamsi