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....
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.
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.
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.
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;
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:
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?
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.
Thank you Keith, that's what i have hoped for, since i can't found a better workaround.
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.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.