Download MetaTrader 5

Trail stop coding and Invalid request error

To add comments, please log in or register
Share links on the Forum. Now they have Rich Content!
Young Ho Seo
15886
Young Ho Seo 2014.01.24 01:18 

I am getting "invalid request" error for my trail stop function.

 

I could not figure out why I am getting error message like this:

 

 

2014.01.24 00:11:31.014 2013.08.12 11:00:20   Invalid request


2014.01.24 00:11:31.014 2013.08.12 11:00:20   failed modify sell 0.10 GBPUSD sl: 1.55366, tp: 0.00000 -> sl: 1.55052, tp: 0.00000 [Invalid request]

 

 

I am little desperate to get this bit of code correctly. Any help will be really appreciated. 

 

 

 


void TrailStop()
{

   string symbolToTrade = SymbolArray[symbolCode];

   MqlTradeRequest request;
   MqlTradeResult result;

   double lastPrice=SymbolInfoDouble(symbolToTrade,SYMBOL_LAST);
   double mAsk    = SymbolInfoDouble(symbolToTrade, SYMBOL_ASK);
   double mBid    = SymbolInfoDouble(symbolToTrade, SYMBOL_BID);
   double mSpread = int(SymbolInfoInteger(symbolToTrade,SYMBOL_SPREAD));

   double mPoint  = SymbolInfoDouble(symbolToTrade, SYMBOL_POINT);
   int    mDigits = (int) SymbolInfoInteger(symbolToTrade, SYMBOL_DIGITS);
   ulong  stopLevel = SymbolInfoInteger(symbolToTrade, SYMBOL_TRADE_STOPS_LEVEL);

   double trailPrice = 0.0;
   double newSL = 0.0;
  
  
   //--- trailing position  
   for(int i=0;i<PositionsTotal();i++)
   {
      if(symbolToTrade==PositionGetSymbol(i))
      {
         if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
         {
           
            if(PositionGetDouble(POSITION_SL) > 0.0)
            {
               newSL = PositionGetDouble(POSITION_SL) + mSpread * mPoint + trailStop * mPoint;
            }
            else
            {
               newSL = PositionGetDouble(POSITION_PRICE_OPEN) + mSpread * mPoint;
            }
           
           

            trailPrice = newSL + trailStop * mPoint + stopLevel;
          
            if(mAsk > trailPrice && mAsk > newSL)
            {
           
               double diff = (mAsk - newSL)/mPoint;
              
               printf("Position Type= Buy, trailPrice = %f, Ask price = %f, newStopLoss = %f, openPrice=%f, diff=%f, stopLevel = %d\n", trailPrice, mAsk, newSL, PositionGetDouble(POSITION_PRICE_OPEN), diff, stopLevel);
           
               request.action = TRADE_ACTION_SLTP;
               request.symbol = symbolToTrade;
               request.sl = NormalizeDouble(newSL,mDigits);
               request.tp = PositionGetDouble(POSITION_TP);

              
               OrderSend(request,result);
               if(result.retcode==10009 || result.retcode==10008) // request executed
                  Print("Moving Stop Loss of Buy position #",request.order);
               else
                 {
                  Print(ResultRetcodeDescription(result.retcode));
                  return;
                 }
               return;
              }
          }


         if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
         {
        
            if(PositionGetDouble(POSITION_SL) > 0.0)
            {
                newSL = PositionGetDouble(POSITION_SL) - mSpread * mPoint - trailStop * mPoint;
            }
            else
            {
                newSL = PositionGetDouble(POSITION_PRICE_OPEN) - mSpread * mPoint;
            }
           
           
            //sl=PositionGetDouble(POSITION_PRICE_OPEN)- Spread * Point;
            trailPrice = newSL - trailStop * mPoint - stopLevel;
           

            if(mBid < trailPrice && mBid < newSL)
            {
           
               double diff = (newSL-mBid)/mPoint;
              
               printf("Position Type= Sell, trailPrice = %f, bid =%f, newStopLoss = %f, openPrice=%f, diff=%f, stopLevel=%d\n", trailPrice, mBid, newSL, PositionGetDouble(POSITION_PRICE_OPEN), diff, stopLevel);
           
               request.action = TRADE_ACTION_SLTP;
               request.symbol = symbolToTrade;
               request.sl = NormalizeDouble(newSL, mDigits);
               request.tp = PositionGetDouble(POSITION_TP);
               OrderSend(request,result);
             
              
               if(result.retcode==10009 || result.retcode==10008) // request executed
                  Print("Moving Stop Loss of Sell position #",request.order);
               else
                 {
                  Print(ResultRetcodeDescription(result.retcode));
                  return;
                 }
               return;
            }
         }
      }
   }
    
}

 

 
 

//+------------------------------------------------------------------+
//| ResultRetcodeDescription                                         |
//+------------------------------------------------------------------+
string ResultRetcodeDescription(int retcode)
  {
   string str;

   switch(retcode)
     {
      case TRADE_RETCODE_REQUOTE:
         str="Requote";
         break;
      case TRADE_RETCODE_REJECT:
         str="Request rejected";
         break;
      case TRADE_RETCODE_CANCEL:
         str="Request canceled by trader";
         break;
      case TRADE_RETCODE_PLACED:
         str="Order placed";
         break;
      case TRADE_RETCODE_DONE:
         str="Request executed";
         break;
      case TRADE_RETCODE_DONE_PARTIAL:
         str="Request partially executed";
         break;
      case TRADE_RETCODE_ERROR:
         str="Request processing error";
         break;
      case TRADE_RETCODE_TIMEOUT:
         str="Request canceled because of time out";
         break;
      case TRADE_RETCODE_INVALID:
         str="Invalid request";
         break;
      case TRADE_RETCODE_INVALID_VOLUME:
         str="Invalid request volume";
         break;
      case TRADE_RETCODE_INVALID_PRICE:
         str="Invalid request price";
         break;
      case TRADE_RETCODE_INVALID_STOPS:
         str="Invalid request stops";
         break;
      case TRADE_RETCODE_TRADE_DISABLED:
         str="Trade disabled";
         break;
      case TRADE_RETCODE_MARKET_CLOSED:
         str="Market is closed";
         break;
      case TRADE_RETCODE_NO_MONEY:
         str="Insufficient funds for request execution";
         break;
      case TRADE_RETCODE_PRICE_CHANGED:
         str="Prices changed";
         break;
      case TRADE_RETCODE_PRICE_OFF:
         str="No quotes for request processing";
         break;
      case TRADE_RETCODE_INVALID_EXPIRATION:
         str="Invalid order expiration date in request";
         break;
      case TRADE_RETCODE_ORDER_CHANGED:
         str="Order state changed";
         break;
      case TRADE_RETCODE_TOO_MANY_REQUESTS:
         str="Too many requests";
         break;
      case TRADE_RETCODE_NO_CHANGES:
         str="No changes in request";
         break;
      case TRADE_RETCODE_SERVER_DISABLES_AT:
         str="Autotrading disabled by server";
         break;
      case TRADE_RETCODE_CLIENT_DISABLES_AT:
         str="Autotrading disabled by client terminal";
         break;
      case TRADE_RETCODE_LOCKED:
         str="Request blocked for processing";
         break;
      case TRADE_RETCODE_FROZEN:
         str="Order or position frozen";
         break;
      case TRADE_RETCODE_INVALID_FILL:
         str="Unsupported type of order execution for balance is specified";
         break;
      case TRADE_RETCODE_CONNECTION:
         str="No connection to the trading server";
         break;
      case TRADE_RETCODE_ONLY_REAL:
         str="Operation is allowed only for real accounts";
         break;
      case TRADE_RETCODE_LIMIT_ORDERS:
         str="Number of pending orders reached the limit";
         break;
      case TRADE_RETCODE_LIMIT_VOLUME:
         str="Volume of orders and positions for this symbol reached the limit";
         break;

      default:
         str="Unknown result";
     }

   return(str);
  }
//+------------------------------------------------------------------+ 


 

Alain Verleyen
Moderator
30752
Alain Verleyen 2014.01.24 01:37  

Try this :

 MqlTradeRequest request={0};
Young Ho Seo
15886
Young Ho Seo 2014.01.24 01:51  
angevoyageur:

Try this :

It is working beautifully. Thanks so much. Can you also explain why it works for this poor coder as well as for other beginners? Kind regards,
Alain Verleyen
Moderator
30752
Alain Verleyen 2014.01.24 15:17  
FinanceEngineer:
It is working beautifully. Thanks so much. Can you also explain why it works for this poor coder as well as for other beginners? Kind regards,

It's simple. In mql5 variable aren't initialized implicitly, so your request variable may contain data that aren't compatible with your actual request.

It's a good practice to always initialize a variable before using it.

Young Ho Seo
15886
Young Ho Seo 2014.01.24 15:45  
angevoyageur:

It's simple. In mql5 variable aren't initialized implicitly, so your request variable may contain data that aren't compatible with your actual request.

It's a good practice to always initialize a variable before using it.

Thanks for the clarification and really helpful.
To add comments, please log in or register