Questions from Beginners MQL5 MT5 MetaTrader 5 - page 1266

 

Good time to you all!

I have a question about ResultDeal() function in CTrade class.

The thing is, in the Strategy Tester this function returns the trade ticket without any problem, but when you throw the EA on the Chart it always returns "zero", in spite of the fact that the ResultRetcode() function always returns "TRADE_RETCODE_DONE".

Example code:

/////////////////////////////////////////////////////////////////////
#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>
/////////////////////////////////////////////////////////////////////
CTrade Trade;
CSymbolInfo SymbolInfo;

uint Retcode;
ulong TicketDeal;
bool first_calc;
bool second_calc;
bool third_calc;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
//---
   TicketDeal = 9999999;
   
   first_calc = true;
   second_calc = false;
   third_calc = false;   
//---
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
//---
   if(first_calc)
   {
      first_calc = false;
      second_calc = true;
      
      SymbolInfo.Name(Symbol());
      SymbolInfo.RefreshRates();
      Trade.Buy(1.0, Symbol(), SymbolInfo.Ask(), 0.0, 0.0, "BUY");
//---
      Retcode = Trade.ResultRetcode();
      if(Retcode == TRADE_RETCODE_DONE) TicketDeal = Trade.ResultDeal();
      
      Print("first_calc ////////////////////////////////////");
      Print("PositionsTotal(): ", PositionsTotal());
      Print("Retcode: ", Retcode, "; TicketDeal: ", TicketDeal);
   }
   else
   {
      if(second_calc)
      {
         second_calc = false;
         third_calc = true;
//---
         Retcode = Trade.ResultRetcode();
         if(Retcode == TRADE_RETCODE_DONE) TicketDeal = Trade.ResultDeal();
         
         Print("second_calc ////////////////////////////////////");
         Print("PositionsTotal(): ", PositionsTotal());
         Print("Retcode: ", Retcode, "; TicketDeal: ", TicketDeal);
      }
      else
      {
         if(third_calc)
         {
            third_calc = false;
//---
            Retcode = Trade.ResultRetcode();
            if(Retcode == TRADE_RETCODE_DONE) TicketDeal = Trade.ResultDeal();
            
            Print("third_calc ////////////////////////////////////");
            Print("PositionsTotal(): ", PositionsTotal());
            Print("Retcode: ", Retcode, "; TicketDeal: ", TicketDeal);
         }
         else
         {
            //Print("TicketDeal: ", Trade.ResultDeal());
         }
      }
   }   
}
//+------------------------------------------------------------------+

Execution log in the Tester:

2020.11.25 18:08:54.225 Core 1  2020.11.23 00:05:00   first_calc ////////////////////////////////////
2020.11.25 18:08:54.225 Core 1  2020.11.23 00:05:00   PositionsTotal(): 1
2020.11.25 18:08:54.225 Core 1  2020.11.23 00:05:00   Retcode: 10009; TicketDeal: 2
2020.11.25 18:08:54.225 Core 1  2020.11.23 00:05:02   second_calc ////////////////////////////////////
2020.11.25 18:08:54.225 Core 1  2020.11.23 00:05:02   PositionsTotal(): 1
2020.11.25 18:08:54.225 Core 1  2020.11.23 00:05:02   Retcode: 10009; TicketDeal: 2
2020.11.25 18:08:54.225 Core 1  2020.11.23 00:05:05   third_calc ////////////////////////////////////
2020.11.25 18:08:54.225 Core 1  2020.11.23 00:05:05   PositionsTotal(): 1
2020.11.25 18:08:54.225 Core 1  2020.11.23 00:05:05   Retcode: 10009; TicketDeal: 2

Execution log on the Chart:

2020.11.25 18:08:13.991 _TestSelect (EURUSD,M1) first_calc ////////////////////////////////////
2020.11.25 18:08:13.991 _TestSelect (EURUSD,M1) PositionsTotal(): 0
2020.11.25 18:08:13.991 _TestSelect (EURUSD,M1) Retcode: 10009; TicketDeal: 0
2020.11.25 18:08:14.026 _TestSelect (EURUSD,M1) second_calc ////////////////////////////////////
2020.11.25 18:08:14.027 _TestSelect (EURUSD,M1) PositionsTotal(): 1
2020.11.25 18:08:14.027 _TestSelect (EURUSD,M1) Retcode: 10009; TicketDeal: 0
2020.11.25 18:08:14.260 _TestSelect (EURUSD,M1) third_calc ////////////////////////////////////
2020.11.25 18:08:14.260 _TestSelect (EURUSD,M1) PositionsTotal(): 1
2020.11.25 18:08:14.260 _TestSelect (EURUSD,M1) Retcode: 10009; TicketDeal: 0


What am I doing wrong? How do I get a trade ticket in real time?

And the second question (of less importance), why immediately after a trade execution the PositionsTotal() function returns "zero"? And it shows reliable data with the next tick. Is it possible to obtain this information at once?


Files:
 
ZAKST:

Good time to you all!

I have a question about ResultDeal() function in CTrade class.

The thing is, in the Strategy Tester this function returns the trade ticket without any problem, but when you throw the EA on the Chart it always returns "zero", in spite of the fact that the ResultRetcode() function always returns "TRADE_RETCODE_DONE".

Example code:

Execution log in the Tester:

Execution log on the Chart:


What am I doing wrong? How do I get a trade ticket in real time?

And the second question (of less importance), why immediately after a trade execution the PositionsTotal() function returns "zero"? And it shows reliable data with the next tick. Is it possible to obtain this information at once?


Hmm... Suddenly the data I need in real time is returned by ResultOrder() function of the sameCTrade class, though I didn't use pending orders...

The question is the same: What am I doing wrong?

 

There is a macro like this.

#define  Warning if(ComentWarnig)LogWarnig.Write(__FILE__":"+IntegerToString(__LINE__,4,'0')+" ",

I use it like this.

Warning"Warnig  сработал. ");

Question: What needs to be corrected in the macro definition to be able to use it like this ?

Warning("Warnig  сработал. ");

i.e. with a parenthesis between Warning and "Warnig worked. "Tried to use a parenthesis in the macro definition

#define  Warning( if(ComentWarnig)LogWarnig.Write(__FILE__":"+IntegerToString(__LINE__,4,'0')+" ",

but the compiler won't let me use a parenthesis.

 
ZAKST:

Hmm... Suddenly, the ResultOrder() function of the sameCTrade class returns the data I need in real time, although I didn't use pending orders...

The question is the same: What am I doing wrong?

Just understand the order system of MT5, order - trade - position.

ResultOrder is the opening order. The position will be with POSITION_IDENTIFIER equal to the ticket of this order.

All of the deals can be found in the history by the position identifier. Do not confuse with the ticket of the position, it can change, and the identifier is constant.

s.s. The tester is different in that there is no delay in executing the order and the deal is created immediately. In real life, time passes and at the time of successful creation of an order there is no deal yet, it has to be caught in the OnTrade events
Документация по MQL5: Константы, перечисления и структуры / Торговые константы / Свойства позиций
Документация по MQL5: Константы, перечисления и структуры / Торговые константы / Свойства позиций
  • www.mql5.com
Тикет позиции. Уникальное число, которое присваивается каждой вновь открытой позиции. Как правило, соответствует тикету ордера, в результате которого она была открыта, за исключением случаев изменения тикета в результате служебных операций на сервере. Например, начисления свопов переоткрытием позиции. Для нахождения ордера, которым была открыта...
 
pivomoe:

There is a macro like this.

I use it like this.

Question: What needs to be corrected in the macro definition to be able to use it like this ?

i.e. with a parenthesis between Warning and "Warnig worked. "Tried to use a parenthesis in the macro definition

but the compiler won't let me use a parenthesis.

you want to use a parametric form, but you've specified a non-parametric form. it has to be like this

#define  Warning(CUSTOM) (if(ComentWarnig)LogWarnig.Write(__FILE__":"+IntegerToString(__LINE__,4,'0')+"",string(CUSTOM)))


Warning("Warnig  сработал. ");
 

I didn't explain it well. Let's start from the beginning. Now I'm using the macro like this:

Warning,"Warnig  сработал. i=",i);

Here's the macro itself:

#define  Warning if(LogWarnig)MoiLoggerWarnig.Write(__FILE__":"+IntegerToString(__LINE__,4,'0')

In the macro call between "Warning," and ");" An arbitrary number of parameters of arbitrary types. This is all achieved by repeatedly reloading theWrite function.

Question. What should I correct in the macro to be able to call it like this:

Warning("Warnig  сработал. i=",i);
 
pivomoe:

I didn't explain it well. Let's start from the beginning. Now I'm using the macro like this:

Here's the macro itself:

In the macro call between "Warning," and ");" An arbitrary number of parameters of arbitrary types. This is all achieved by repeatedly reloading theWrite function.

Question. What should be corrected in the macro to enable its call like this:

Answer: also reload the macro multiple times)

But it's better to use operator + for strings and convert everything to strings, then you don't need a version of function (and macro) with lots of parameters

 
Aleksey Mavrin:

Just understand the order system of MT5, order - trade - position.

ResultOrder is the opening order. The position will be with POSITION_IDENTIFIER equal to the ticket of this order.

All of the deals can be found in the history by the position identifier. Do not confuse with the ticket of the position, it can change, and the identifier is constant.

s.w. The tester is different in that there is no delay in executing the order and immediately a deal is born. In real trade the time passes and at the time of successful execution there is no deal yet, it has to be caught in the OnTrade events.
Thank you for your reply. I will deal with it))
 
Hello! How do I access the properties of an indicator from an EA? For example, I want to change the colour of a line from an EA knowing the indicator handle. I searched through the help, I couldn't find anything, maybe I'm not looking hard enough?
 
Tango_X:
Hello! How do I access indicator properties from an EA? For example I want to change the line colour from an EA if I know the indicator handle. I searched for it, i couldn't find it, maybe i am looking too hard?

An indicator for this must have an input colour parameter. That is, it must be a custom indicator.

Reason: