I would like to know if there is a way to determine if my last trade was long or short and if it hit the stop loss. Advice appreciated.
![](https://resources.faireconomy.media/images/logos/logo-print-ff.png)
Stop Loss vs. Hidden Stop Loss 26 replies
my broker didn't execute buy stop, and stop loss hit 6 replies
EA to close trade when 'x' negative profit is hit (stop loss) 0 replies
Detecting definition of a 'pip' 1 reply
Detecting stopped order? 1 reply
[color=#000000][color=#ff8000]#define PLUS 1 #define MINUS 2 [/color][color=#0000bb]int TradeHistory[/color][color=#007700](){ [/color][color=#0000bb]int cnt[/color][color=#007700]=[/color][color=#0000bb]HistoryTotal[/color][color=#007700](),[/color][color=#0000bb]tkt[/color][color=#007700];[/color][color=#0000bb]datetime ord[/color][color=#007700]; for([/color][color=#0000bb]int i[/color][color=#007700]=[/color][color=#0000bb]0[/color][color=#007700];[/color][color=#0000bb]i[/color][color=#007700]<[/color][color=#0000bb]cnt[/color][color=#007700];[/color][color=#0000bb]i[/color][color=#007700]++){ if([/color][color=#0000bb]OrderSelect[/color][color=#007700]([/color][color=#0000bb]i[/color][color=#007700],[/color][color=#0000bb]SELECT_BY_POS[/color][color=#007700],[/color][color=#0000bb]MODE_HISTORY[/color][color=#007700])){ if([/color][color=#0000bb]OrderCloseTime[/color][color=#007700]()>[/color][color=#0000bb]ord[/color][color=#007700]){ [/color][color=#0000bb]tkt[/color][color=#007700]=[/color][color=#0000bb]OrderTicket[/color][color=#007700](); [/color][color=#0000bb]ord[/color][color=#007700]=[/color][color=#0000bb]OrderCloseTime[/color][color=#007700](); } } } if([/color][color=#0000bb]OrderSelect[/color][color=#007700]([/color][color=#0000bb]tkt[/color][color=#007700],[/color][color=#0000bb]SELECT_BY_TICKET[/color][color=#007700],[/color][color=#0000bb]MODE_HISTORY[/color][color=#007700])){ if([/color][color=#0000bb]OrderProfit[/color][color=#007700]()>=[/color][color=#0000bb]0[/color][color=#007700])return([/color][color=#0000bb]PLUS[/color][color=#007700]);else return([/color][color=#0000bb]MINUS[/color][color=#007700]); } return(-[/color][color=#0000bb]1[/color][color=#007700]); } [/color][/color]
DislikedAm I missing something here.....![]()
you don't know if your last trade was long or short and if it hit the SL......hmmmmmm
man I must be thick......Ignored
DislikedYes, I think Mr Doe was approaching the idea without knowing it is a trading robot we are talking about. Although, this thread is in the MetaTrader Forum
This is a nice and compact piece of code.. thanks for posting it. Coincidentally it is something I have been working on as well and can make use of it... Thanks
PS, where did you find it? I would like to look at more of this stuff.. thanks again.Ignored
DislikedJust a note:
That code snippet simply identifies whether a trade closed out at a profit or a loss, it doesn't identify if the loss was at the stoploss price. A technicality, but it might be important depending on what you're doing.
If you need to confirm it was the StopLoss price, you can compare OrderStopLoss() to OrderClosePrice().Ignored
bool IsLastTrade(string closedBy) { int totalOrders = HistoryTotal(); if(totalOrders>=1) { if(OrderSelect(totalOrders-1,SELECT_BY_POS,MODE_HISTORY)) { string orderComment = OrderComment() return (StringFind(orderComment,closedBy, 0)!=-1); } } return(0); }
Quoting original posterDislikedHi, mike. Try it. PS.: In "closedBy" parameter you put "[tp]" to know if last trade closed by Take Profit or put "[sl]" to know if closed by Stop Loss.
Bye,
Alessandro PaulinoIgnored