Is there a way to check when an order closes whether it hit it's stop loss or take profit?
How do youm know when it has closed to trigger some code?
You should get what you want from a couple of code snippets I wrote for another user in this thread.
'Need help with coding using history values'
CB
You should get what you want from a couple of code snippets I wrote for another user in this thread.
'Need help with coding using history values'
CB
Thanks.
Can I get the function to return sResult and then check that?
is it just:
string Result = fnHistoryCheck();
Do I need to put return(sResult); in the code?
int fnHistoryCheck() { string sResult = "EMPTY"; int iOrders = OrdersHistoryTotal()-1; for (int iO = iOrders; iO>=0; iO--) { OrderSelect(iO,SELECT_BY_POS,MODE_HISTORY); if (OrderSymbol() == Symbol()) { if ((TimeDayOfYear(OrderOpenTime()) == DayOfYear()) && (TimeYear(OrderOpenTime()) == Year())) { if (OrderProfit() >= 0) sResult = "PROFIT"; else sResult = "LOSS"; } } } return(0); }
Yep, you'd need to alter the code from return(0) to return(sResult) if you declare it to return a string.
Note also that the code above won't give you exactly what you need. ie. It just lets you know the following:
- that an order that was made in the current server day has been closed
- whether that order yielded a profit or loss
The order, of course, could have been closed by your EA, rather than by a broker invoked SL or TP.
I've just pointed you towards this as a framework in which to do your own final tweaking.
CB

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Is there a way to check when an order closes whether it hit it's stop loss or take profit?
How do youm know when it has closed to trigger some code?