How can I search for my last position, and how can I check if it was a profitable or lost position?
Any code suggestion?
You need to loop on all orders, select each one in turn (using OrderSelect()) and filter them according to your criteria to find the 'last' one (which is in 'scope' of your EA). To find out if it is profitable, you need to select it (unless it has already been selected) and check the return of OrderProfit(). You can find an explanation of the concept of trade/history pools and order selection here -> https://www.mql5.com/en/forum/126165.
Edit: if swap or commission is relevant, then they should be added to the order's profit; they can be retrieved using OrderSwap() and OrderCommission() (after order has been selected).
Snap, I've just finish writing the code. Hate to follow Gordon's post. If you want example here it is. I recommend you follow the links and learn what it all means tho. So that you don't have to keep asking.
int start(){ //+------------------------------------------------++------------------------------------------------+ //+------------------------------------------------++------------------------------------------------+ int Ticket; for(int i=OrdersHistoryTotal()-1; i>=0; i--){ if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==true){ if(OrderProfit() - (OrderCommission()+OrderSwap()) < 0) {Ticket=OrderTicket(); break;}}} //+------------------------------------------------++------------------------------------------------+ //+------------------------------------------------++------------------------------------------------+ return(Ticket);}
Snap, I've just finish writing the code. Hate to follow Gordon's post. If you want example here it is. I recommend you follow the links and learn what it all means tho. So that you don't have to keep asking.
int start(){ //+------------------------------------------------++------------------------------------------------+ //+------------------------------------------------++------------------------------------------------+ int Ticket; for(int i=OrdersTotal()-1; i>=0; i--){ if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==true){ if(OrderProfit() - (OrderCommission()+OrderSwap()) < 0) {Ticket=OrderTicket(); break;}}} //+------------------------------------------------++------------------------------------------------+ //+------------------------------------------------++------------------------------------------------+ return(Ticket);}
your code is not correct. as written in documentation OrdersTotal() returns the amount of Market and Pending orders. even if the loop works, you will always iterate only through a little part (depending on current open orders) of your history. you have to use OrdersHistoryTotal() instead.
also a check if the order was actually triggerd and closed should be added.
Thank you all of you for your help.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi everyone,
How can I search for my last position, and how can I check if it was a profitable or lost position?
Any code suggestion?
Regards
Francis