Typical mistakes and how to deal with them when dealing with the trading environment - page 9

 
Algotrader18:

Good afternoon!

I have a similar question for everyone and especially for fxsaber.
It seems to be a primitive question, but I can't find the answer to it.
Here https://www.mql5.com/ru/forum/6343/page939 I was told "Try to search for it. fxsaber has been working on this problem for a while now. What worked out, I don't know, I'm not interested." So I decided to ask you.

The question is this. I opened position and want to print the content of MqlTradeResult:

retcode, order and volume are printed, but deal and price are always zeros.
Please, advise me what I'm doing wrong and how to get access (at least, print it first) to the content of result structure?

How about this?

//+------------------------------------------------------------------+
   MqlTradeRequest request={0};
   MqlTradeResult  result={0};
//---
   request.action   = TRADE_ACTION_DEAL;    // type of trade operation
   request.symbol   = Pair1;                // symbol
   request.volume   = lots;                 // volume of lot
   request.type     = oper;                 // order type
   request.price    = priceOpen;            // price for opening
   request.deviation= Slippage;             // allowed deviation from the price
   request.magic    = magic;   
//---
   if(OrderSend(request,result))
     {
      Print ("retcode = " +IntegerToString (result.retcode) + "; ");        
      Print ("deal = " +IntegerToString (result.deal) + "; ");
      Print ("order = " +IntegerToString (result.order) + "; "); 
      Print ("volume = " +DoubleToString (result.volume) + "; ");  
      Print ("price = " +DoubleToString (result.price) + "; ");
     }
   else
     {
      // Здесь действия при ошибке открытия
     }
//+------------------------------------------------------------------+
 
Algotrader18:

I open a position and want to print the content of MqlTradeResult like this:

retcode, order and volume are printed but deal and price are always zeros.
Please advise what I'm doing wrong and how to get access (at least print it first) to the contents of result structure?

Zeros are written in result.

 
Artyom Trishkin:

How about this?

Thanks, but the result is the same...
 
fxsaber:

Zeros and are prescribed in the result.

Well then it's not clear at all - I thought I was doing something wrong, and if there are zeros there, what's the point of result?
I see an open order, I get result.retcode 10009 and result.price = 0 ?
Can you please explain the meaning of result if it doesn't contain a result ?
 
Algotrader18:
Please explain what is the point of the result if it does not contain the result?

This is a feature of MT5 where the result is only an order (ticket and no price) but not a trade. You have several options

  1. Figure out on your own how things work.
  2. Use already prepared higher level (lower level is hidden) developments where almost all pitfalls are bypassed.
 
fxsaber:

This is a feature of MT5 where the result is only an order (ticket and no price) but not a trade. You have several options

  1. Figure out on your own how things work.
  2. Use already prepared higher level (lower level is hidden) developments where almost all pitfalls are bypassed.
Thank you.

In MQL4 there was no result structure and we received a ticket and selected the appropriate order and requested the necessary data. MQL5 has the result structure that contains many necessary data, and it would be illogical not to use it.

1. Before asking a question on the forum, I tried to "Figure out for myself how everything works" and what I see - in the result structure:

struct MqlTradeResult
  {
   uint     retcode;          // Operation return code
   ulong    deal;             // Deal ticket, if it is performed
   ulong    order;            // Order ticket, if it is placed
   double   volume;           // Deal volume, confirmed by broker
   double   price;            // Deal price, confirmed by broker
   double   bid;              // Current Bid price
   double   ask;              // Current Ask price
   string   comment;          // Broker comment to operation (by default it is filled by description of trade server return code)
   uint     request_id;       // Request ID set by the terminal during the dispatch
   uint     retcode_external; // Return code of an external trading system
  };


present double price; // Deal price, confirmed by broker.

In the sample code from the developer

//--- send the request
   if(!OrderSend(request,result))
      PrintFormat("OrderSend error %d",GetLastError());     // if unable to send the request, output the error code
//--- information about the operation
   PrintFormat("retcode=%u  deal=%I64u  order=%I64u",result.retcode,result.deal,result.order);
outputs result.deal, i.e. it is assumed to make sense and it is not always represented by zero in the result structure. Other examples could be given that indicate that result.price as well as result.deal can be obtained from MqlTradeResul, which is what I tried to do.


Nowhere have I seen information that "This is a feature of MT5, when the result is only an order (ticket and no price), but not a deal". Would you please tell me based on what information you came to this conclusion.

2. If you know of any "already ready higher level (low level is hidden) developments," which allow you to obtain the parameters of a position immediately after its opening (code 10009 appears), without prior selection by ticket, please give me an example or give me a link to it.

 
Algotrader18:
Thank you.

In MQL4 there was no result structure and we received a ticket and selected the appropriate order and requested the necessary data. MQL5 has the result structure with many necessary data and it would be illogical not to use it.

1. Before asking a question in the forum, I tried to "figure out for myself how everything works" and what I've seen is that the structure result:


There is a double price; // Deal price, confirmed by broker.

In the sample code from the developer

outputs result.deal, i.e. it is assumed to make sense and it is not always represented by zero in the result structure. There are other examples that show that result.price as well as result.deal can be obtained from MqlTradeResul, which is what I tried to do.


Nowhere have I seen information that "this is a feature of MT5 where the result is only an order (ticket and no price) but not a deal". Would you please tell me based on what information you came to this conclusion.

2. If you know of any "already prepared higher level (low level is hidden) developments," which allow you to get the parameters of a position immediately after its opening (code 10009 appears), without prior selection by ticket, please give me an example or give me a link to it.

A trade request results in either a rejection or a market order. This is not a trade, nor is it a position generated by a trade.

  1. A trade order to open a position is sent to the server (order = order)
  2. If the order is accepted, the result of accepting the order and placing it in the queue is returned.
  3. If the order has been executed, the trade - the result of order execution - appears.
  4. The result of the trade will be either a new position, or increasing/decreasing the size of the existing position, or closing the position, or a rollover. Or a new position (in a hedging account).
 
Artyom Trishkin:

A trade request results in either a rejection or a market order. This is not a trade, nor is it a position generated by a trade.

  1. A trade order to open a position is sent to the server (order = order)
  2. If the order is accepted, the result of accepting the order and placing it in the queue is returned.
  3. If the order has been executed, a trade will appear as the result of the order execution.
  4. The result of a trade will be either a new position, or an increase/decrease in the volume of an existing position, or the closing of a position, or a rollover. Or a new position (in case of a hedging account)
Thank you - please tell me:
- after which step, of those you listed, do you think the Trade Request Result Structure (MqlTradeResult) appears?

- According to MQL5 description:
"Structure of trade request result (MqlTradeResult)
As a response to a trade request, the trade server returns data containing the trade request processing result as a special predefined structure MqlTradeResult". and "10009 TRADE_RETCODE_DONE Order executed".

I.e. the trade server return code 10009 means that OrderSend request was successfully executed i.e. if I request data from MqlTradeResult, after receiving code 10009, how can I explain that result.price = 0 ?
 
Algotrader18:
Thank you:
- after which step, of those listed by you, does the structure of trade request result (MqlTradeResult) appear?

- According to MQL5 description:
"Structure of trade request result (MqlTradeResult)
As a response to a trade request, the trade server returns data containing the trade request processing result as a special predefined structure MqlTradeResult". and "10009 TRADE_RETCODE_DONE Order executed".

Hence, the return code of trade server 10009 means that OrderSend request was successfully executed i.e. if I request data from MqlTradeResult, after receiving code 10009, how can I explain that result.price = 0 ?

10009 TRADE_RETCODE_DONE - you have described it yourself - the order is executed. This is simply the fact that the order has been successfully queued to the server for execution.

Further, if this order is executed (after all, it can be rejected by the server), then there is a trade - this is the result of the trade with the opening price.

Read about

OnTradeTransaction

It is called in Expert Advisors when a TradeTransaction event occurs. The function is intended for processing the results of trade request execution.

voidOnTradeTransaction( )
const MqlTradeTransaction&trans,// structure of a trade transaction
const MqlTradeRequest&request,// request structure
const MqlTradeResult& result// structure of response
);

Документация по MQL5: Обработка событий / OnTradeTransaction
Документация по MQL5: Обработка событий / OnTradeTransaction
  • www.mql5.com
При обрабокте транзакций типа TRADE_TRANSACTION_REQUEST для получения дополнительной информации необходимо анализировать второй и третий параметры функции OnTradeTransaction() – Отправка торгового запроса на покупку приводит к цепи торговых транзакций, которые совершаются на торговом счете: 1) запрос  принимается на обработку, 2) далее для...
 
Algotrader18:

Nowhere have I seen the information that "This is a feature of MT5 where the result is only an order (ticket and no price) but not a trade". Kindly tell me on the basis of what information you have come to this conclusion.

Own experience.

2. If you know of any "already ready higher level (low level is hidden) developments," which allow you to get the parameters of a position immediately after its opening (code 10009 appears), without prior selection by ticket, please give an example or give a link to it.

#include <MT4Orders.mqh> // https://www.mql5.com/ru/code/16006

#define Bid SymbolInfoDouble(_Symbol, SYMBOL_BID)
#define Ask SymbolInfoDouble(_Symbol, SYMBOL_ASK)

void Variant1()
{
  if (OrderSelect(OrderSend(_Symbol, OP_BUY, 0.1, Ask, 100, 0, 0), SELECT_BY_TICKET))
    OrderPrint();
}

void Variant2()
{
  OrderSend(_Symbol, OP_SELL, 0.1, Bid, 100, 0, 0);
  MqlTradeResult result = MT4ORDERS::LastTradeResult;
  
  Print ("retcode = " +IntegerToString (result.retcode) + "; ");        
  Print ("deal = " +IntegerToString (result.deal) + "; ");
  Print ("order = " +IntegerToString (result.order) + "; "); 
  Print ("volume = " +DoubleToString (result.volume) + "; ");  
  Print ("price = " +DoubleToString (result.price) + "; ");
}

void OnStart()
{
  Variant1();
  Variant2();
}
#2212868739 2018.11.02 21:09:33 buy 0.10 EURUSD 1.13895 0.00000 0.00000 1.13892 -0.18 0.00 -0.26 0
retcode = 10009; 
deal = 2210317244; 
order = 2212868740; 
volume = 0.10000000; 
price = 1.13892000; 


There is another option, but it is "advanced".

Reason: