Modify Position doesn't work

To add comments, please log in or register
Cholischnufer
77
Cholischnufer  

Hey guys

I'm new to the mql5 programming and I want to modify my open positions. But if i run the EA in a Backtest, I always get the "Error: Invalid Stops". Can anyone help me with this?

//+------------------------------------------------------------------+
//| Modify an open Position                                          |
//+------------------------------------------------------------------+
   void Modify(string ptype,double stpl,double tkpf)
      {
      //--- New Stop Loss, New Take Profit, bid, ask
      double ntp,nsl,pbid,pask;
      long tsp=Trail_point;
      
      //--- adjust for 5 & 3 digit prices
      if(_Digits==5 || _Digits==3) tsp=tsp*10;
      
      //--- Check the minimal possible adjustment of the stop level
      long stplevel=mysymbol.StopsLevel();
      if(tsp<stplevel) tsp=stplevel;
      if(ptype=="BUY")
      {
         pbid=mysymbol.Bid();
         if(tkpf-pbid<=stplevel*_Point)
            {
             ntp=pbid+tsp*_Point;
             nsl=pbid-tsp*_Point;
            }
         else
            {
             ntp=tkpf;
             nsl=pbid-tsp*_Point;
            }
      }
      else//---Sell
      {
       pask=mysymbol.Ask();
       if(pask-tkpf<=stplevel*_Point)
         {
          ntp=pask-tsp*_Point;
          nsl=pask+tsp*_Point;
         }
      else
         {
          ntp=tkpf;
          nsl=pask+tsp*_Point;
         }
      }
    if(mytrade.PositionModify(_Symbol,nsl,ntp))
      {
       Alert("An open position position has been modified successfully!");
       return;
      }
    else
      {
       Alert("The position modify request could not be completed. Error: ",mytrade.ResultRetcodeDescription());
       return;
      }
   }   
Vladimir Karputov
Moderator
189284
Vladimir Karputov  

To set the StopLoss and TakeProfit levels, use the CSymbolInfo trade class and the CSymbolInfo::NormalizePrice

Cholischnufer
77
Cholischnufer  
Vladimir Karputov:

To set the StopLoss and TakeProfit levels, use the CSymbolInfo trade class and the CSymbolInfo::NormalizePrice


Thank you for your answer. You mean somehow like that?


void Modify(string ptype,double stpl,double tkpf)
      {
      //--- New Stop Loss, New Take Profit, bid, ask
      double ntp,nsl,pbid,pask;
      long tsp=Trail_point;
      
      //--- adjust for 5 & 3 digit prices
      if(_Digits==5 || _Digits==3) tsp=tsp*10;
      
      //--- Check the minimal possible adjustment of the stop level
      long stplevel=mysymbol.StopsLevel();
      if(tsp<stplevel) tsp=stplevel;
      if(ptype=="BUY")
      {
         pbid=mysymbol.Bid();
         if(tkpf-pbid<=stplevel*_Point)
            {
             ntp=NormalizeDouble(pbid+tsp*_Point,_Digits);
             nsl=NormalizeDouble(pbid-tsp*_Point,_Digits);
            }
         else
            {
             ntp=NormalizeDouble(tkpf,_Digits);
             nsl=NormalizeDouble(pbid-tsp*_Point,_Digits);
            }
      }
      else//---Sell
      {
       pask=mysymbol.Ask();
       if(pask-tkpf<=stplevel*_Point)
         {
          ntp=NormalizeDouble(pask-tsp*_Point,_Digits);
          nsl=NormalizeDouble(pask+tsp*_Point,_Digits);
         }
      else
         {
          ntp=NormalizeDouble(tkpf,_Digits);
          nsl=NormalizeDouble(pask+tsp*_Point,_Digits);
         }
      }
    if(mytrade.PositionModify(_Symbol,nsl,ntp))
      {
       Alert("An open position position has been modified successfully!");
       return;
      }
    else
      {
       Alert("The position modify request could not be completed. Error: ",mytrade.ResultRetcodeDescription());
       return;
      }
   }   


However it still doesn't work. I get the exact same result after testing it. I'm not quite sure if it's really this part of the code that doesn't work properly... The Backtesting graph looks pretty awkward. The EA does not only ignore the modify functio but also does not take the TakeProfits or closes the position in any way...

Backtesting

Vladimir Karputov
Moderator
189284
Vladimir Karputov  

No, it's wrong. You do not take into account the price quotation. It is necessary so:

Forum on trading, automated trading systems and testing trading strategies

Modify Position doesn't work

Vladimir Karputov, 2017.06.26 17:48

To set the StopLoss and TakeProfit levels, use the CSymbolInfo trade class and the CSymbolInfo::NormalizePrice

Example: N-_Candles_v4:

//+------------------------------------------------------------------+
//| Open Buy position                                                |
//+------------------------------------------------------------------+
void OpenBuy(double sl,double tp)
  {
   sl=m_symbol.NormalizePrice(sl);
   tp=m_symbol.NormalizePrice(tp);
...
//+------------------------------------------------------------------+
//| Open Sell position                                               |
//+------------------------------------------------------------------+
void OpenSell(double sl,double tp)
  {
   sl=m_symbol.NormalizePrice(sl);
   tp=m_symbol.NormalizePrice(tp);
...


To add comments, please log in or register