Modify Position doesn't work

 

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;
      }
   }   
 

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

 
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

 

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);
...


 
Vladimir Karputov #:

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

Your code use trailing stop point only. Before send command PositionModify you must compare trailing stop point with stop level

like this

 int stop_level                = (int)SymbolInfoInteger(eaposition.Symbol(), SYMBOL_TRADE_STOPS_LEVEL);
 
Mr Anucha Maneeyotin # :

Your code use trailing stop point only. Before send command PositionModify you must compare trailing stop point with stop level

like this

What code? I have a lot of codes: for beginners, for those who already know. Are you sure you understand what you are talking about? Have you read the article THE CHECKS A TRADING ROBOT MUST PASS BEFORE PUBLICATION IN THE MARKET ? Have you read the code of 'Trading engine 4.mq5'?

The checks a trading robot must pass before publication in the Market
The checks a trading robot must pass before publication in the Market
  • www.mql5.com
Before any product is published in the Market, it must undergo compulsory preliminary checks in order to ensure a uniform quality standard. This article considers the most frequent errors made by developers in their technical indicators and trading robots. An also shows how to self-test a product before sending it to the Market.
Reason: