Download MetaTrader 5

I get a error from this Traillingstop Function

To add comments, please log in or register
Sell your CPU capacity and earn money!
Trading
13
Trading 2013.11.28 11:55 

Hello,

i have here a good looking trailingstop function, but when i use it in my EA i get a error message during backtest, the error message is: "Incorrect Request/Result Structure when Selling."

an somebody help me to get this function running without error? 

void DoTrailing()
{
   if (PositionsTotal() > 0)
   {
      if (PositionSelect(_Symbol))
      {
         if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
         {
            if (SymbolInfoDouble(Symbol(), SYMBOL_BID) - PositionGetDouble(POSITION_PRICE_OPEN) >= TrailingStop * Poin)
            {
             if (PositionGetDouble(POSITION_SL) < (SymbolInfoDouble(Symbol(), SYMBOL_BID) - TrailingStop * Poin))
             {
               MqlTradeRequest Request;
                Request.action = TRADE_ACTION_SLTP;
                Request.symbol = _Symbol;
                Request.sl = NormalizeDouble(SymbolInfoDouble(Symbol(), SYMBOL_BID) - TrailingStop * Poin, _Digits);
                Request.tp = PositionGetDouble(POSITION_TP);
               Request.deviation = Deviation;
               
               MqlTradeResult Result;
               
                bool result = OrderSend(Request, Result); 
                if (result == false)    Print("Incorrect Request/Result Structure when Selling.");
                else Print("Result Code: ", Result.retcode, ", ", "Deal: ", Result.deal, ", ", "Volume: ", Result.volume, ", ", "Price: ", Result.price, ", ", "Requote Bid: ", Result.bid, ", ", "Requote Ask: ", Result.ask);
             }
            }
         }
         else
         {
            if (PositionGetDouble(POSITION_PRICE_OPEN) - SymbolInfoDouble(Symbol(), SYMBOL_ASK) >= TrailingStop * Poin)
            {
             if (PositionGetDouble(POSITION_SL) > (SymbolInfoDouble(Symbol(), SYMBOL_ASK) + TrailingStop * Poin))
             {
               MqlTradeRequest Request;
                Request.action = TRADE_ACTION_SLTP;
                Request.symbol = _Symbol;
                Request.sl = NormalizeDouble(SymbolInfoDouble(Symbol(), SYMBOL_ASK) + TrailingStop * Poin, _Digits);
                Request.tp = PositionGetDouble(POSITION_TP);
               Request.deviation = Deviation;
               
               MqlTradeResult Result;
               
                bool result = OrderSend(Request, Result); 
                if (result == false)    Print("Incorrect Request/Result Structure when Selling.");
                else Print("Result Code: ", Result.retcode, ", ", "Deal: ", Result.deal, ", ", "Volume: ", Result.volume, ", ", "Price: ", Result.price, ", ", "Requote Bid: ", Result.bid, ", ", "Requote Ask: ", Result.ask);
             }
            }
         
         }
      }
   }
}
Alain Verleyen
Moderator
30742
Alain Verleyen 2013.11.28 13:54  

You have to initialize your MqlTradeRequest structure :

 MqlTradeRequest Request={0};

Also if OrderSend returns false, that doesn't means you have an "Incorrect Request/Result Structure when Selling." Especially as it's not a request to sell but to modify SL/TP.

To add comments, please log in or register