Features of the mql5 language, subtleties and tricks - page 173

 
fxsaber:

Updated above.

Forum on trading, automated trading systems and trading strategies testing

Peculiarities of mql5, tips and tricks

fxsaber, 2020.04.09 13:13

It looks like you have not encountered it at all. Have a look at the Documentation. There are two volumes per order.

These are live limiters. The first number is the original volume, the second is the poured volume. As soon as they become equal or are deleted, it goes into history.


Isn't that one order executed in two parts? It seems strange that the unexecuted volumes are the same.

 
Alexey Viktorov:

Isn't that one order executed in two parts? It seems strange that the unexecuted volumes are the same.

Coincidence. They even have different mags.

 
fxsaber:

There can be no order in order mode when it is alive. When dead - there will be a time of first execution, as originally said.


Found in history.

 
fxsaber:

When you collapse via CloseBy, it seems to lose the majicies. Need to check it out.

This seems to be the reason why the first code below will not work.

Forum on trading, automated trading systems and testing trading strategies

History Profit in MQL5 ?

fxsaber, 2017.08.26 19:16

  1. MQL5
    double Profit( void )
    {
     double Res = 0;
    
     if (HistorySelect(0, INT_MAX))
       for (int i = HistoryDealsTotal() - 1; i >= 0; i--)
       {
         const ulong Ticket = HistoryDealGetTicket(i);
         
         if((HistoryDealGetInteger(Ticket, DEAL_MAGIC) == MagicNumber) && (HistoryDealGetString(Ticket, DEAL_SYMBOL) == Symbol()))
           Res += HistoryDealGetDouble(Ticket, DEAL_PROFIT);
       }
         
      return(Res);
    }


  2. MQL5 + MQL4
    #include <MT4Orders.mqh> // https://www.mql5.com/ru/code/16006
    
    double Profit( void )
    {
     double Res = 0;
    
     for (int i = OrdersHistoryTotal() - 1; i >= 0; i--)
       if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY) && (OrderMagicNumber() == MagicNumber) && (OrderSymbol() == Symbol()))
         Res += OrderProfit();
         
      return(Res);
    }

Counting the profit in magic seems to be a problem.

 
Order prices are normalised, trades are not. The script finds such trades.
void OnStart()
{
  if (HistorySelect(0, INT_MAX))
    for (int i = HistoryDealsTotal() - 1; i >= 0; i--) // Перебираем все сделки
    {
      const ulong Ticket = HistoryDealGetTicket(i); // Тикет сделки
      const double Price = HistoryDealGetDouble(Ticket, DEAL_PRICE); // Цена сделки
      
      if (NormalizeDouble(Price, 5) != Price) // Если цена сделки не нормализована,      
        Print(Ticket);                        // печатаем тикет сделки.
    }
}
 

The arrows to open/close positions, which MT5 automatically places in real time, are based on TradeTransaction-events.


Just now I saw that these events (opening and closing about a dozen positions) didn't reach the terminal because of a momentary connection failure - it was so coincidental, I was sitting at my PC and looked at them by eye. As a result there are no corresponding arrows.


And, as has sometimes been said here, you can't rely on OnTradeTransaction in combat EAs. Too bad there is no reliable public mechanism for dealing with OrderSendAsync.

 
fxsaber:

Too bad there is no reliable public mechanism for dealing with OrderSendAsync.

can the services access the trades? - if so, then it is possible to monitor every 10 ms and send the event to the chart

i looked at the help, i thought maybe i got the services added, but i think it was like this last year:

-It is a programme which, in contrast to indicators, Expert Advisors and scripts, does not require binding to a chart. Similar to scripts, services do not handle any events except for the launching event. In order to start a service, its code must contain the OnStart handler function. Services do not accept any other events except Start, but they can send custom events to charts themselves using EventChartCustom

UPD: I wrote up a service script.

struct SOrderInfo
{
   int positionstotal;
   int orderstotal;
   int historyorderstotal;
   bool operator==(const SOrderInfo &v){return(positionstotal==v.positionstotal && orderstotal==v.orderstotal && historyorderstotal==v.historyorderstotal);}
};
//+------------------------------------------------------------------+
int OnStart()
{
   SOrderInfo CurrState = UpdateOrdersInfo();
   SOrderInfo LastState = CurrState;
   while(!IsStopped())
   {
      Sleep(10);
      CurrState = UpdateOrdersInfo();
      if(CurrState == LastState) continue;
      LastState = CurrState;
      Print("Orders changed!");
   }
   return(0);
}
//+------------------------------------------------------------------+
SOrderInfo UpdateOrdersInfo()
{
   SOrderInfo result;
   result.positionstotal     = PositionsTotal();
   result.orderstotal        = OrdersTotal();
   result.historyorderstotal = HistoryOrdersTotal();
   return(result);
}
//+------------------------------------------------------------------+

I opened several orders and closed them manually.

 
Igor Makanu:

can the services access the trades? - if so, it is possible to monitor every 10ms and send an event to the chart

This is no different than the same overrun in sovtenik. Only it's not enough to compare counts, the internals may change.

 
Andrey Khatimlianskii:

It's no different from the same overkill in Owletnik. Only it's not enough to compare the quantity, the internals can change.

Services like Alerters are very good. You don't need charts, they are automatically started with Terminal. You just have to work out the situations of switching between accounts.

The service can also be a combat advisor. Without the GUI it's just not very convenient.
 
Andrey Khatimlianskii:

It's no different from the same overkill in Owletnik. Only it's not enough to compare the quantity, the internals can change.

is different.

there are so-called control and management tasks

EA - management, service - control

control must not be superfluous - you will take all resources of the system and get an unstable system instead of control

you have to define the criticality of the events, imho, closing and opening of orders - these are critical events that need control, while changing TP and SL could be done as before (a few failed attempts - let it go, on the next tick we will try again)

and the way you suggest - controlling everything is possible - you can repeat the state of orders in SQLite, then there will be a synchronization problem of the database with the service and EA.... all this is unnecessary


I think the scheme should be simpler - it should look as follows: get theOnTradeTransaction event from the terminal in EA and generate the OnChartEvent event from the service for additional control in EA

Reason: