Trailing Stop - Invalid Stops

 

Hi,

Can someone tell me whats wrongs with my trailing stop code? I keep getting invalid stops, I run the function during every tick. Any help would be greatly appreciated.

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CheckForProfit()
  {
   double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
//SET THE BID PRICE TO A VARIABLE
   double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
   double CurrentTP= PositionGetDouble(POSITION_TP);
   double CurrentSL = PositionGetDouble(POSITION_SL);
   for(int i=PositionsTotal()-1; i>=0; i--) // returns the number of current position
      if(m_position.SelectByIndex(i))     // selects the position by index for further access to its properties
         if(m_position.Symbol()==Symbol())
           {
            if(m_position.Profit() >   1)
              {
               if(m_position.Type() == POSITION_TYPE_BUY)
                 {
                  trade.PositionModify(m_position.Ticket(),CurrentSL + (10 * _Point), CurrentTP);// close a position by the specified symbol
                 }
               if(m_position.Type() == POSITION_TYPE_SELL)
                 {
                  trade.PositionModify(m_position.Ticket(),CurrentSL - (10 * _Point), CurrentTP);// close a position by the specified symbol
                 }
              }
            if(m_position.Profit() >   2)
              {
               if(m_position.Type() == POSITION_TYPE_BUY)
                 {
                  trade.PositionModify(m_position.Ticket(),CurrentSL + (10 * _Point), CurrentTP);// close a position by the specified symbol
                 }
               if(m_position.Type() == POSITION_TYPE_SELL)
                 {
                  trade.PositionModify(m_position.Ticket(),CurrentSL - (10 * _Point), CurrentTP);// close a position by the specified symbol
                 }
              }
            if(m_position.Profit() >   3)
              {
               if(m_position.Type() == POSITION_TYPE_BUY)
                 {
                  trade.PositionModify(m_position.Ticket(),CurrentSL + (10 * _Point), CurrentTP);// close a position by the specified symbol
                 }
               if(m_position.Type() == POSITION_TYPE_SELL)
                 {
                  trade.PositionModify(m_position.Ticket(),CurrentSL - (10 * _Point), CurrentTP);// close a position by the specified symbol
                 }
              }
            if(m_position.Profit() >   4)
              {
               trade.PositionClose(m_position.Ticket());
              }
           }
  }
The Fundamentals of Testing in MetaTrader 5
The Fundamentals of Testing in MetaTrader 5
  • www.mql5.com
The idea of ​​automated trading is appealing by the fact that the trading robot can work non-stop for 24 hours a day, seven days a week. The robot does not get tired, doubtful or scared, it's is totally free from any psychological problems. It is sufficient enough to clearly formalize the trading rules and implement them in the algorithms, and...
 

These are the lines:

   double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
//SET THE BID PRICE TO A VARIABLE
   double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
   double CurrentTP= PositionGetDouble(POSITION_TP);
   double CurrentSL = PositionGetDouble(POSITION_SL);

you need to hide it inside the loop.

   for(int i=PositionsTotal()-1; i>=0; i--) // returns the number of current position
      if(m_position.SelectByIndex(i))     // selects the position by index for further access to its properties
         if(m_position.Symbol()==Symbol())
           {
            double Ask=SymbolInfoDouble(m_position.Symbol(),SYMBOL_ASK);
            double Bid=SymbolInfoDouble(m_position.Symbol(),SYMBOL_BID);
            double CurrentTP=PositionGetDouble(POSITION_TP);
            double CurrentSL=PositionGetDouble(POSITION_SL);


I didn't look further. You need to fix this error first ...

 
jmpmilli2019:

Hi,

Can someone tell me whats wrongs with my trailing stop code? I keep getting invalid stops, I run the function during every tick. Any help would be greatly appreciated.

You have to select orders by ticket:

         for(int i=0;i<PositionsTotal();i++){
            ulong iTicket=PositionGetTicket(i);
            if(PositionSelectByTicket(iTicket)&&
            PositionGetString(POSITION_SYMBOL)==_Symbol){
               if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY){
                  double TStop=(YOUR TRAILING STOP);
                  if(TStop>PositionGetDouble(POSITION_PRICE_OPEN)){
                     if(TStop>PositionGetDouble(POSITION_SL)){
                        if(!trade.PositionModify(iTicket,TStop,PositionGetDouble(POSITION_TP))){
                           Print("PositionModify error ",trade.ResultRetcode());
                           return;
                           }
                        }
                     }
                  }
 
David Diez :

You have to select orders by ticket:

He does it anyway :)

 //+------------------------------------------------------------------+ 
 //| Select a position on the index                                   | 
 //+------------------------------------------------------------------+ 
 bool CPositionInfo:: SelectByIndex ( const int index)
  {
    ulong ticket= PositionGetTicket (index); 
   return (ticket> 0 );
  }
 
Vladimir Karputov:

These are the lines:

you need to hide it inside the loop.


I didn't look further. You need to fix this error first ...

Thanks that worked. I appreciate it. I thought it was already available before the loop.