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

 
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, thereis 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 opinion.

One as long as there is a position in the market. But when it is closed, there is a second order and a second trade.

ps; I guess I'm wrong. See ps in the post below.

 
Artyom Trishkin:
And the SL trawl? How will you determine what closed on the trawl SL in the plus?

Ok, I didn't take that into account. Then let's check the order price of the position opened and the order price of the position closed.

Although we can probably leave orders alone and operate only with deals.

ps; No... Something is wrong here. Every change of a Take or Stop price after the position has been opened is a new order and a new deal. Apparently, we should work with them.

I cannot check, I have no positions with stops. Only some with takes.

 
Alexey Viktorov:

When you find the last deal DEAL_ENTRY_OUT and select the whole history of the position HistorySelectByPosition in this list there are, for hadge accounts 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.

I wouldn't rely on the comment to the order.

It is not that simple. The last order may be the one that closed the position or the one that reversed it.

As for the SL and TP, the comment is the easiest and most reliable way to determine this. To compare prices for SL and TP will not always work correctly because SL and TP can be both in the profit and loss zone. This was already quite heatedly discussed on the forum and Mr. Sergeev gave the appropriate solution to the comment I cited.

 
Andrey Dik:

It's not that simple. The last order in the position can be either closing the trade, or reversing it.

And about SL and TP - the most simple and reliable way to determine from the comment. Comparing the prices of SL and TP will not always work correctly, because both SL and TP can be in the profit and loss zone.

I didn't say it was easy. The order which turned the position cannot be a Take or Stop. And only in netting accounts. In hedging accounts, to reverse a position, it must be closed by the opposite one with a larger volume. Isn't it so?

This topic is interesting, but I have no possibility to check it for the reason already mentioned. I do not have positions with stops and takeaways, especially trailing ones.

 
Alexey Viktorov:

An order that reversed a position cannot be a take or a stop.

TP and SL are not orders in the MT accounting system, they are position properties. That's why you can't work with SL/TP on orders/trades (even the wise site engine confirms it with a link).

Alexey Viktorov:

In hedging accounts, to reverse a position, it must be closed by the opposite order of a larger volume. Isn't it so?

No, it's not. In hedge accounts a position cannot be reversed, it can only be closed.

Документация по MQL5: Стандартные константы, перечисления и структуры / Торговые константы / Свойства позиций
Документация по MQL5: Стандартные константы, перечисления и структуры / Торговые константы / Свойства позиций
  • www.mql5.com
Стандартные константы, перечисления и структуры / Торговые константы / Свойства позиций - справочник по языку алгоритмического/автоматического трейдинга для MetaTrader 5
 
Andrey Dik:

TP and SL are not orders in the MT accounting system, they are position properties. That's why you can't work SL/TP on orders/trades (even the site's wise engine confirms with a link).

No, it's not. In hedge accounts a position cannot be reversed, it can only be closed.

But once a position is closed, its properties are no longer available. It only remains to search for the necessary information on orders and trades.
 

Forum on trading, automated trading systems and strategy tester

MQL5 How do I know if a trade closed on a stop loss or not?

fxsaber, 2017.02.14 19:46

Ticket order, which was the reason for closing the position, shove it here
#define ORDER_TYPE_TAKEPROFIT (-1)
#define ORDER_TYPE_STOPLOSS   (-2)

int GetOrderType(const ulong OrderTicket)
{
  int OrderType=(int)::HistoryOrderGetInteger(OrderTicket,ORDER_TYPE);

  if((OrderType==ORDER_TYPE_BUY) || (OrderType==ORDER_TYPE_SELL))
    {
     const string OrderComment=HistoryOrderGetString(OrderTicket,ORDER_COMMENT);
     const string OrderPrice=::DoubleToString(::HistoryOrderGetDouble(OrderTicket,ORDER_PRICE_OPEN),
                                              (int)::SymbolInfoInteger(HistoryOrderGetString(OrderTicket,ORDER_SYMBOL),SYMBOL_DIGITS));

     if(OrderComment=="tp "+OrderPrice)
        OrderType=ORDER_TYPE_TAKEPROFIT;
     else if(OrderComment=="sl "+OrderPrice)
        OrderType=ORDER_TYPE_STOPLOSS;
    }

  return(OrderType);
}
 
Andrey Dik:
Give me an example, please.
An example of what? That the properties of the position are not available after closing it?
 
Alexey Viktorov:
An example of what? That the properties of a position are not available after it is closed?
An example of how you determine what closed the position SL or TP on orders and trades. I gave an example of how you determine this from your comment.
 
Andrey Dik:
Example of how you determine what closed the position SL or TP on orders and trades. I gave an example of how you determine this from your comment.

I'm not doing anything. I found it useless to look through the order history even in mql4. I am still figuring out in what direction I may start digging when necessary. A little earlier, I said that I could not check it for the simple reason: I have no trailed positions in any terminal and do not want to do it specially.

Accordingly, after closing the position, there are only 2 orders and 2 deals referring to the position on my accounts. Using them, we may get at least some information about the position. For example, at what price the request was opened and at what exactly it was opened. Accordingly, it is easily determined by the slippage at which the position was opened.

I treat comments with caution since MT4 because I met that brokerage companies wipe all comments for their own reasons.

Reason: