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

 
Artyom Trishkin:

How can I determine the time of opening the last position and the time of closing the last position?

In general, I would like to know the general principles and algorithms for obtaining information about open/closed positions in mql5.

What should be considered, what are the peculiarities at the moment?

The matter is that I am using MQL4-syntax, which is 1000 times easier and more comprehensible than MQL5-syntax for the trading part.

You know very well how easily and in three lines your task can be solved in MQL4. That's why I solve it that way.

For answers to all questions on MQL5 I look at the source code of the library. All known nuances have been considered there.

At the moment we can state that Hedge and Netting positions are very different. For example, in Netting you can always have only one position for months, while you are constantly trading. Therefore, the Netting-positions in the MT5 standard understanding is something ill-conceived and, accordingly, inconvenient. Therefore, for the same Netting it is possible to create the essence of your positions. In MT4 you can also have Netting positions, but with a much more clear interaction.

Open a position for 1 lot. Then close it at 0.99 and deposit the same amount. And so on several times.

Hedge is convenient not because you can have differently directed positions, but because you can have several positions for one symbol in ONE direction. That's exactly the way of working with positions in the Netting-systems. And not like in MT5.

Ideally, when you work with Netting accounts, you should always have a table that is fully consistent with the MT4-accounts. Because only such tables give you instant insight into what happened and how it happened in the trading account. And not the MT5-statistics.

As for the MT5-Hedge, everything is simple there. CloseBy, however, I didn't dig. Therefore I do not know.

 
fxsaber:
It is not always possible to know the SL/TP levels of a closed position.

From the quote it also follows that TP positions in MT5 are always a market order. Don't bookmark the limit counterpart - see above.

Now this is interesting... I need to know for sure that the last closed position was closed at take profit.

Is there any way to know it for sure?

 
Artyom Trishkin:

Now this is interesting... I need to know for sure that the last closed position was closed at take profit.

Is there any way to know this for sure?

I do as you advised:

//+------------------------------------------------------------------+
//| Возвращает причину закрытия позиции по Take Profit               |
//+------------------------------------------------------------------+
bool IsClosedByTP (const string symbol)
{
  //--- Получим комментарий последней сделки на указанном символе
  string last_comment = LastDealComment (symbol);
  //--- Если в комментарии есть строка "tp"
  if(StringFind (last_comment, "tp", 0) > -1)
    return (true);
  //--- Если нет строки "tp"
  return (false);
}
//+------------------------------------------------------------------+
//| Возвращает причину закрытия позиции по Stop Loss                 |
//+------------------------------------------------------------------+
bool IsClosedBySL (const string symbol)
{
  //--- Получим комментарий последней сделки на указанном символе
  string last_comment = LastDealComment (symbol);
  //--- Если в комментарии есть строка "sl"
  if(StringFind (last_comment, "sl", 0) > -1)
    return (true);
  //--- Если нет строки "sl"
  return (false);
}
//+------------------------------------------------------------------+
//| Возвращает комментарий последней сделки на указанном символе     |
//+------------------------------------------------------------------+
string LastDealComment (const string symbol)
{
  int    total_deals  = 0;  // Всего сделок в списке выбранной истории
  string deal_symbol  = ""; // Символ сделки
  string deal_comment = ""; // Комментарий сделки
                            //--- Если история сделок получена
  if(HistorySelect (0, TimeCurrent ()))
  {
    //--- Получим количество сделок в полученном списке
    total_deals = HistoryDealsTotal ();
    //--- Пройдемся по всем сделкам в полученном списке от последней сделки к первой
    for(int i = total_deals - 1; i >= 0; i--)
    {
      //--- Получим комментарий сделки
      deal_comment = HistoryDealGetString (HistoryDealGetTicket (i), DEAL_COMMENT);
      //--- Получим символ сделки
      deal_symbol = HistoryDealGetString (HistoryDealGetTicket (i), DEAL_SYMBOL);
      //--- Если символ сделки и текущий символ равны, остановим цикл
      if(deal_symbol == symbol)
        break;
    }
  }
  //---
  return (deal_comment);
}
 
Andrey Dik:

I do as you advised.

Well, how do I know that the last trade led to the closing or reversal of a position?

If the position is closed or reversed, then it makes sense to check its data, which will be necessary for the logic of the Expert Advisor: the volume, the fact of closing on take or stop, etc..

Otherwise, if the fact of closing on take was detected in the last trade, but the position remained, then there is no sense to check the data on the last closed position (it still exists).

In general, of course, it is somewhat difficult to quickly switch to the logic of the MT5 order system after the four - so many questions appear when thinking about the logic of advisor's behavior, which is tied with the data on the last closed position - in the four everything is transparent - the position has its own type, and it is the last - it fits - then we take the data and use it. But here ... hedge, netting ... and we need the same result... ...and then we're out of our minds...

 
Artyom Trishkin:

Well, how do you know if the last trade led to a closing or reversal of a position?

1. If the position is closed or reversed, then it makes sense to check its data, which will be necessary for logic of the Expert Advisor: volume, the fact of closing on take or stop, etc...

Otherwise, if the fact of closing on take was detected in the last trade, but the position remained, then there is no sense to check the data on the last closed position (it still exists).

In general, of course, it is somewhat difficult to quickly switch to the logic of the MT5 order system after the four - so many questions appear when thinking about the logic of advisor's behavior, which is tied to the data on the last closed position - in the four it is all transparent - the position has its own type, and it is the last - it is suitable - take the data and use it. But here ... hedge, netting ... and we need the same result... ...and let's start thinking all over the place...

1. Check the deal type, IN, OUT or IN/OUT.

2. If a position is closed by SL or TP, it is already closed; there is no need to check the deal type.

 
Andrey Dik:

1. Check the deal type, IN, OUT or IN/OUT

2. If the position is closed by SL or TP, it is no longer there, it is closed, there is no need to check the type of trade.

Is it valid for netting? Or is it true for hedges as well? Can´t there be several positions in one direction on a hedge account? Or is it just one position, and ....? What's the "and?" ... Shit... I don't know what "and" is... There are a lot of questions, and the answers are still hard to come by.
 
Artyom Trishkin:
Is it true for netting? Or is it true for hedging as well? In a hedge account, may there not be several positions in one direction? Or is it just one position, and ....? What's the "and?" ... Shit... I don't know what "and" is... There are a lot of questions, the answers are still not in my head.

For the hedge too.

Over several years, my brain has been sharpened for netting. Order->Transaction->Position. There may be many deals in a position (in case of a hedge, there is only one deal, correct me if I'm wrong), and the type of deal allows you to determine what happened to the position.

In a hedge, of course, it is easier to understand, but more difficult to follow. Of course, this is my personal opinion.

 
Andrey Dik:

For a hedge too.

Over several years, my brain has been sharpened for netting. Order->Transaction->Position. There may be many deals in a position (in case of a hedge, there is only one deal, correct me if I'm wrong), and the type of deal allows you to determine what happened to the position.

In a hedge, of course, it is easier to understand, but more difficult to follow. IMHO of course.

Okay, thank you. I will figure it out.
 
Artyom Trishkin:

Now this is interesting... I need to know for sure that the last closed position was closed at take profit.

Is there any way to know this for sure?

When you find the last deal DEAL_ENTRY_OUT and select the entire history of the position HistorySelectByPosition in this list, there are 2 deals and 2 orders, opening a position and closing it. The order on the basis of which the closing occurred contains ORDER_PRICE_OPEN. Then you check the type of the position and the type of the order which was used to close it. If the order has an opposite type, it means that it was closing. Accordingly, the order price is the price you are looking for, with a little modification: If the order profit is > 0, then this is the price of TP, if it is < 0, then the position was closed by SL.

If it is less than 0, then the position has closed with SL. If it is less than 0, then the position has closed with SL. I wouldn't rely on the comment to the order.

 
Alexey Viktorov:

When you find the last trade DEAL_ENTRY_OUT and select the entire history of the position HistorySelectByPosition in this list there are, for hadge accounts 2 trades and 2 orders, opening a position and closing it. The order on the basis of which the closing occurred contains ORDER_PRICE_OPEN. Then you check the type of the position and the type of the order which was used to close it. If the order has an opposite type, it means that it was closing. Accordingly, the order price is the price you are looking for, with a little modification: If the order profit is > 0, then this is the price of TP, if it is < 0, then the position was closed by SL.

If it is less than 0 then the position has closed with SL. If we rely on the comment to the order I would not do that.

What about the SL trawl? How will you determine if the SL trawl closed in profit?
Reason: