Download MetaTrader 5
To add comments, please log in or register
Start making profit using the Market. Publish your product!
Carim Mishtal
20
Carim Mishtal 2014.09.06 01:25 
Hello,
I would like to check if my last three closed sell or buy trades were profitable, and change a string value depending on it.

I have this code (MT4):


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

for(int j=(OrdersHistoryTotal()-1);j>=0;j--);
 {
   OrderSelect(j, SELECT_BY_POS,MODE_HISTORY);
   if(OrderSymbol()==Symbol() && OrderMagicNumber()==SELL)
    {
       //for sell order
       if(OrderType()==OP_SELL && OrderProfit()>0) last2=1;
       if(OrderType()==OP_SELL && OrderProfit()<0) last2=0;
    }
 }

But it doesn't work for me.
How I should modify it?

Thanks for help.
Keith Watford
Moderator
7982
Keith Watford 2014.09.06 02:53  
if(OrderSymbol()==Symbol() && OrderMagicNumber()==BUY)                        ??????????

if(OrderSymbol()==Symbol() && OrderMagicNumber()==SELL)                       ??????????
Apart from that, you are not checking for the most recent and not checking for the last 3. Apparently, it is best not to assume that history is in any set order.
whroeder1
13641
whroeder1 2014.09.06 15:56  
for(int i=(OrdersHistoryTotal()-1);i>=0;i--);
 {
   OrderSelect(i, SELECT_BY_POS,MODE_HISTORY);
   if(OrderSymbol()==Symbol() && OrderMagicNumber()==BUY)
    {
       //for buy order
       if(OrderType()==OP_BUY && OrderProfit()>0) last=1;
       if(OrderType()==OP_BUY && OrderProfit()<0) last=0;
    }
 }
  1. You don't need separate magic numbers for buy and sell. See also Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 forum
  2. This code sets last=1 when the earliest buy order was profitable.
  3. Don't use 0 and 1 when you mean boolean.
  4. Check your return codes (OrderSelect) What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
/
To add comments, please log in or register