Last trade profit/loss?

 
Hello,

Is there any other way to see whether our last EA trade is profit or loss other than :
string last;
for(int i=OrdersHistoryTotal()-1,i>=0,i--)
 {
   OrderSelect(OrdersHistoryTotal()-1, Select_by_pos,MODE_HISTORY);
   if(OrderSymbol()==Symbol() && OrderMagicNumber()==magic)
    {
       //for buy order
       if(OrderType()==OP_BUY && OrderClosePrice()>=OrderTakeProfit()) last="profit";
       if(OrderType()==OP_BUY && OrderClosePrice()<=OrderStopLoss()) last="loss";
    }
 }
The problem is, I want to use it on other pairs too, so we can't use "OrdersHistoryTotal()-1" to select the trade on a specific pair. For example I want to know the last closed trade on GBPUSD was in profit or loss but the last closed trade are on EURUSD. Any clue?

Thank you
 

D

> we can't use "OrdersHistoryTotal()-1" to select the trade on a specific pair

Well you can - because as written, the code is only looking at the current chart pair, presumably EURUSD, via Symbol().

To get last GBPUSD trade, you must loop through the OrderHistory in the same way as above but use


OrderSymbol () ==" GBPUSD "


Is that what you needed?


Good Luck


-BB-


 
Hi BB,

Thanks for your reply.
But does the symbol selecting is under orderselect function? It's before we try to filter the selected trade using OrderSymbol() and OrderMagicNumber().
So when we use this code :
OrderSelect(OrdersHistoryTotal()-1, Select_by_pos,MODE_HISTORY);
So"OrdersHistoryTotal()-1" is the last closed trade regardless it's pairs and it's magic number. Or can we add another OrderSelect() function inside the filtering code?

CMIIW please,
Thanks
 

D

Nice try - but no cigar this time :)

From https://docs.mql4.com/trading/OrderSelect

You only get two 'pools', i.e. collections with OrderSelect - all of the non-closed or all the closed items.

MODE_TRADES (default) - order selected from trading pool (opened and pending orders),
MODE_HISTORY - order selected from history pool (closed and canceled order).


So - the collections do not 'know' the Symbol you have to root through them all & interrogate each item...

This could be a Suggestion for MetaQuotes to give us an enhancement to the order collections object!



-BB-

 
Thanks BB..

However, if there's somebody has another idea, it's very appreciated.

Thank you
 
devilian1899:
Hello,
I want to use it on other pairs too, so ... Any clue?
Thank you
You need to add lines and remove:
if ( OrderSelect( OrdersHistoryTotal()-1, SELECT_BY_POS, MODE_HISTORY) ) /* if something selected */
 {
   if ( OrderMagicNumber() == magic ) { /* if used MagicNumber ... logics */ }
 }
 
Hello, thanks for all responses,
I think I manage to do it, "break" operator can help.
 

Devillian,


I think you just need to replace OrderHistoryTotal()-1 with "i" in your code. Your original code sets up a loop over all the orders indexed by "i" but you never use the "i". I also changed the profit conditions to make use of the OrderProfit() function. I believe this code should work.


string last;
for(int i=OrdersHistoryTotal()-1,i>=0,i--)
 {
   OrderSelect(i, SELECT_BY_POS,MODE_HISTORY);
   if(OrderSymbol()==Symbol() && OrderMagicNumber()==magic)
    {
       //for buy order
       if(OrderType()==OP_BUY && OrderProfit()>0) last="profit";
       if(OrderType()==OP_BUY && OrderProfit()<0) last="loss";
       break; 
    }
 }

-bencook

 
Why not log the last trade result in a variable then look for it later. May be faster :)
 

Hi,

U need fix an error in Line 2 with

;

Update

for(int i=OrdersHistoryTotal()-1,i>=0,i--)

to

for(int i=(OrdersHistoryTotal()-1);i>=0;i--)

regards

 
[Deleted]:

Devillian,


I think you just need to replace OrderHistoryTotal()-1 with "i" in your code. Your original code sets up a loop over all the orders indexed by "i" but you never use the "i". I also changed the profit conditions to make use of the OrderProfit() function. I believe this code should work.


-bencook

What about the sell orders? I think the code shows only the buy orders P/L?

Reason: