See this topic, probably a little confusing but you can find the idea.
The MT5 trading system is very different from MT4, so you have to change your thinking.
- www.mql5.com
I'm writing an EA, modifying my EA written for MT4. I open an order using OrderSend.
Simply if I need to understand (perhasps from History), if the order sent (by the way.... I have to check Order, Deals or Position?), has been closed because it has reached StopLoss.
I checked on forums, but I have not found what I need....
ORDER_PRICE_CURRENT | gives only the current price of the order symbol |
that cannot be compared to ORDER_SL. I suppose ORDER_PRICE_CURRENT is only the current price of symbol and not the Order close price .
Thanks to all for your support.
Is it this?
//-------------------------------- ES CIERRE POR SL ---------------------------------------------- bool esCierreSL(int ind, int modoInd= SELECT_BY_TICKET, int modoHist= MODE_TRADES) { bool resp= OrderSelect(ind, modoInd, modoHist)? (StringFind(OrderComment(), "[sl]", 0)>-1): false; return(resp); }
Is it this?
The question is about mql5, your code is mql4.
Order comment is not a reliable way to code.
The question is about mql5, your code is mql4.
Order comment is not a reliable way to code.
Sorry for necro, but this is quite interesting in my opinion.
I'm doing transition from mql4 to mql5, and i find the trading and history systems changes quite challenging.
Iin order to know if a trade on history book was closed for stop-loss, take-profit, or otherwise:
- in MT4 you could comparate OrderStopLoss(), OrderTakeProfit() and OrderClosePrice(), or simply check OrderComment().
- in MT5 the only possible way seems to me checking the comment of a deal: HistoryDealGetString(tk, DEAL_COMMENT)
It's interesting but if i understood it correctly, it's aimed on identifying trade changes based on event handling, on the fly.
So there must be a program listening (like an expert advisor running on a chart) or else nothing is detected.
In my opinion, trade close reason detection must be done on history trade book because it is (or should be) the safest way to know what happened, since it's writed somewhere and not catched from the air.
Is my analysis wrong? what do you think?
- www.mql5.com
Marco: Both MT4 and MT5 when an order closes for stop-loss or take-profit write [SL price] or [TP price] in the comment (in different ways.. but still). | No, they do not. That is your broker. Not a good idea, brokers can change comments, including complete replacement. |
No, they do not. That is your broker. |
Ok, both MT4 and MT5 brokers put that string on the comment.. at least the few i have tested.
And i agree, this is not a good idea, since the comment may be subject to change by broker's hand.
But i fail to see how this could be done differently, since i can't find other traces in trade history book.
Is there a better way to achieve this goal?
I contacted the service desk 2 years ago (regarding MT4).
and they replied
so it is done by the system, not the broker. Still not helpful if the broker can then change it though.
Mind you, I have not noticed any occasion when the broker has changed the order comment.
Usually, in my code, it is more important to know whether a trade closed with profit or loss. When using a trailing stop a trade can hit the SL, but still be in profit.
I contacted the service desk 2 years ago (regarding MT4).
and they replied
so it is done by the system, not the broker. Still not helpful if the broker can then change it though.
Mind you, I have not noticed any occasion when the broker has changed the order comment.
Usually, in my code, it is more important to know whether a trade closed with profit or loss. When using a trailing stop a trade can hit the SL, but still be in profit.
Thank you Keith, that's what i have hoped for, since i can't found a better workaround.
I believe both posters are converting MQL4 into MQL5
Anyway, just compare close price with stoploss price.
Note: In real (especially ECN) account, probably trigger price has a gap between pre-set stoploss price, so use "<=" or ">=".
Anyway, just compare close price with stoploss price.
Note: In real (especially ECN) account, probably trigger price has a gap between pre-set stoploss price, so use "<=" or ">=".
You cannot compare the close price to the stop loss price. Slippage means that it could close higher or lower than the stop loss.
So "<=" or ">=" is not useful.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I'm writing an EA, modifying my EA written for MT4. I open an order using OrderSend.
Simply if I need to understand (perhasps from History), if the order sent (by the way.... I have to check Order, Deals or Position?), has been closed because it has reached StopLoss.
I checked on forums, but I have not found what I need....
ORDER_PRICE_CURRENT
gives only the current price of the order symbol
that cannot be compared to ORDER_SL. I suppose ORDER_PRICE_CURRENT is only the current price of symbol and not the Order close price .
Thanks to all for your support.
EDIT :
This topic is now mostly obsolete due to new possibilities offered by MT5 since build 1640.
See the last posts of the topic for some examples.