Download MetaTrader 5

Own CTrade class shows huge deviation and won't open Positions

To add comments, please log in or register
They are already making profit using the Market. You can join them!
Harald Duftschmied
66
Harald Duftschmied 2013.07.03 23:35 
class CTrade
  {
private:

   
   MqlTradeRequest   request;
   MqlTradeResult    result;
   // ADD SL TP Point Conversion
   
public:
   bool openPosition (ENUM_ORDER_TYPE pType, double pVolume, string pSymbol, double pStopLoss = 0, double pTakeProfit = 0, string pComment = NULL, ENUM_TRADE_REQUEST_ACTIONS pAction = TRADE_ACTION_DEAL);

  };
  
   bool CTrade::openPosition(ENUM_ORDER_TYPE pType,double pVolume,string pSymbol,double pStopLoss=0,double pTakeProfit=0, string pComment = NULL, ENUM_TRADE_REQUEST_ACTIONS pAction=1)
   {
      request.action = pAction;
      request.symbol = pSymbol;
      request.type   = pType;
      request.sl     = pStopLoss;
      request.tp     = pTakeProfit;
      request.comment= pComment;
      request.volume = calculateLotsize(pType, pVolume, pSymbol);
      request.price  = calculatePrice  (pType,pSymbol);
      
      OrderSend(request,result);
      return true;
   }
   
   double calculateLotsize(ENUM_ORDER_TYPE pType, double pVolume, string pSymbol)
   {
      double   positionLots = 0;
      long     positionType = WRONG_VALUE;
      
      if (PositionSelect(pSymbol)==true)
      {
         positionLots = PositionGetDouble(POSITION_VOLUME);
         positionType = PositionGetInteger(POSITION_TYPE);
      }
      
      if ((pType == ORDER_TYPE_BUY && positionType == POSITION_TYPE_SELL) || (pType == ORDER_TYPE_SELL && positionType == POSITION_TYPE_BUY))
      {
         return (pVolume + positionLots);
      }   
      else return (pVolume);
   }
   
   double calculatePrice(ENUM_ORDER_TYPE pType, string pSymbol)
   {
      if       (pType == ORDER_TYPE_BUY ) return SymbolInfoDouble(pSymbol,SYMBOL_ASK);
      else if  (pType == ORDER_TYPE_SELL) return SymbolInfoDouble(pSymbol,SYMBOL_BID);
      else                                return WRONG_VALUE;
   }

Hey there.

Im about to create my own trading class but now i got stucked. It can be only a small error but im not able to find it.

when i call "openPosition" like that:

   CTrade trader;
   trader.openPosition(ORDER_TYPE_SELL,0.1,_Symbol);
  

This error appears:

2013.07.03 23:28:40    Trades    '1485928': failed instant sell 1.40 AUDCAD at 0.95428 (deviation: 8992421) [Invalid request]


What am i doing wrong ?

Thanks in advance,


Greetings,

Distinctive

Documentation on MQL5: Standard Constants, Enumerations and Structures / Trade Constants / Order Properties
Documentation on MQL5: Standard Constants, Enumerations and Structures / Trade Constants / Order Properties
  • www.mql5.com
Standard Constants, Enumerations and Structures / Trade Constants / Order Properties - Documentation on MQL5
Alain Verleyen
Moderator
30739
Alain Verleyen 2013.07.04 00:16  
Distinctive:
...

Hello. Try to add this line in the beginning of your method openPosition() :

    ZeroMemory(request);
Harald Duftschmied
66
Harald Duftschmied 2013.07.04 08:09  
angevoyageur:

Hello. Try to add this line in the beginning of your method openPosition() :

Yes, it worked.

Thank you very much !

To add comments, please log in or register