Questions from Beginners MQL5 MT5 MetaTrader 5 - page 854

 
I'm fiddling with CTrade::PositionClose(const ulong ticket,const ulong deviation=ULONG_MAX) and realize that it's not exactly organized inside. It's all about 2 return codes - bool and ResultRetcode(). RetCode is not always zeroed internally before returning false, so if PositionClose gives false, RetCode may well return TRADE_RETCODE_DONE. It would be nice for the developers to synchronize these 2 return values.
 
. ... Rick D. ... .:
I'm fiddling with CTrade::PositionClose(const ulong ticket,const ulong deviation=ULONG_MAX) and realize that it's not exactly organized inside. It's all about 2 return codes - bool and ResultRetcode(). RetCode is not always zeroed internally before returning false, so if PositionClose gives false, RetCode may well return TRADE_RETCODE_DONE. It would be nice for the developers to synchronize these 2 return values.

Code + example + logs.

 
I found one strange bug. Maybe it is only for me.

COrderInfo::OrderType() always returns 0.

To check it, you can manually place any pending order (BuyStop, SellStop, ...),
then enter it into the script.
#include <Trade\Trade.mqh>

COrderInfo m_order;

void OnStart()
{
  ulong ticket = 250262937; //здесь ваш тикет
  
  if (!m_order.Select(ticket))
  {
    PrintFormat("COrderInfo::Select(#%I64u) failed", ticket);
    return;
  }
  
  PrintFormat("OrderType= %d", m_order.Type());
}

Maybe something with the virtual function table? In debugging, I only get to virtual int CObject::Type()

PS. I've already figured it out myself. COrderInfo has 2 similar methods: Type() and OrderType().

 
Vladimir Karputov:

Code + example + logs.

See what situation I mean.

#include <Trade\Trade.mqh>

CTrade m_trade;

void OnStart()
{ 
  int LErr;
  bool bRC;
  double price;

  ResetLastError();
  
  price = 0.0;
  bRC = m_trade.Buy(0.1, "EURUSD", price, 0.0, 0.0);

  LErr = GetLastError();

  PrintFormat("! Buy:  (bRC= %s)(RetCode= %u: %s)(LErr= %d)",
    ticket,
    (string)bRC, 
    m_trade.ResultRetcode(), 
    m_trade.ResultComment(), 
    LErr
  );
  
  //В m_trade.ResultRetcode() теперь TRADE_RETCODE_DONE
  
  //---

  ulong ticket = 1; //Позиции с таким тикетом нет
  
  ResetLastError();

  bRC = m_trade.PositionClose(ticket);  

  LErr = GetLastError();
  
  PrintFormat("! Close #%I64u:  (bRC= %s)(RetCode= %u: %s)(LErr= %d)",
    ticket,
    (string)bRC, 
    m_trade.ResultRetcode(), 
    m_trade.ResultComment(), 
    LErr
  );
  
  //В m_trade.ResultRetcode() все еще TRADE_RETCODE_DONE
}

I don't see any SetResultRetcode and SetResultComment methods in CTrade to reset them before each new operation.

I have to inherit my class, where I add one single method


class CTradeEx : public CTrade
{
public:
  void SetResult(MqlTradeResult& result)
  {
    m_result = result;
  }  
};
 
. ... Rick D. ... .:

See what situation I mean.

I don't see any SetResultRetcode and SetResultComment methods in CTrade to reset them before each new operation.

What for? Completely unnecessary functions.

First: you shoot in the air - you send a trade order without first checking - without checking what the operation returned. Look at the reference:PositionClose is of the bool type.

Second, let's look at the position close chain: when the position is not found (it cannot be selected by the specified ticket), "false" is returned.

//+------------------------------------------------------------------+
//| Close specified opened position                                  |
//+------------------------------------------------------------------+
bool CTrade::PositionClose(const ulong ticket,const ulong deviation)
  {
//--- check stopped
   if(IsStopped(__FUNCTION__))
      return(false);
//--- check position existence
   if(!PositionSelectByTicket(ticket))
      return(false);
   string symbol=PositionGetString(POSITION_SYMBOL);
//--- clean

At that, the structures are not cleared anywhere - because it makes no sense - after all, the position could not be selected before, hence there is no information in the structures.

 
Vladimir Karputov:

What's the point? Completely unnecessary functions.

First: you shoot in the air - you send a trade order without first checking - without checking what the operation returned. Let's look at the reference:PositionClose is of the bool type.

Second, let's look at the position close chain: when the position is not found (it cannot be selected by the specified ticket), "false" is returned.

At the same time, the structures are not cleared anywhere, since it makes no sense - after all, the position could not be selected before, hence there is no information in the structures.

1) We may as well say that ResetLastError() is an absolutely unnecessary function.

2) There is a check for the returned result. Imagine, by analogy, you call some WinAPI function, it returns an error, and GetLastError() (analogue of ResultRetcode) returns any last code.

If I add some check before calling PositionClose (which I will), it doesn't override any other checks before any other calls, when the RetCode won't change.

My Expert Advisor writes information in the log. All I need is to write in log sane ResultRetcode and ResultComment if some call returned false.

 
. ... Rick D. ... .:

1) We might as well say that ResetLastError() is completely unnecessary.

2) There is a check for return result. Imagine, by analogy, you call some WinAPI function, it returns an error, and GetLastError() (analogue of ResultRetcode) returns any last code.

If I add some check before calling PositionClose (which I will), it doesn't override any other checks before any other calls when the RetCode doesn't change.

My Expert Advisor writes information in the log. All I need to do is write sane ResultRetcode and ResultComment in the log if some call returned false.

Read in CTrade class help when you need tocheck result of trade request execution (trade server return code) by callingResultRetcode()method.

 
Vladimir Karputov:

Read in CTrade class help when tocheck the result of trade request execution (trade server return code) by callingResultRetcode()method.

You can assert that.

1) If PositionClose returns false, then the Retcode is never filled?

2) If PositionClose returns true, then the Retcode is always populated?

 
Vladimir Karputov:

Order -> trade -> position. Take the opening price from the position.

Can you show a picture of how this Order -> trade -> position, it is below where the trades are written so it does not allow copying there


 
Seric29:

Can you show a picture of how this Order -> trade -> position, it's at the bottom where trades are written so it doesn't let you copy.


Please either rewrite your question or put punctuation marks, or better yet add on a picture what, where and who. Otherwise it turns out that I see familiar letters, but can not understand the meaning and your point.

Reason: