Checking if last closed orders were profitable

 
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.
 
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.
 
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
 
Hi,

I see your account deleted.
I have one for checking last 3 order profitable or not, but without buy or sell separation.

I hope this give some idea.

// Indicator is profitable by history
bool isProfitable_1 = NULL;
bool isProfitable_2 = NULL;
bool isProfitable_3 = NULL;


bool IsLastProfitable(){
   bool isProfitable = true;
   int no = 0;
   for(int i=OrdersHistoryTotal(); i>=1; i--)
    {
      no++;
      if(OrderSelect(i-1, SELECT_BY_POS,MODE_HISTORY)==true){
         if(OrderSymbol()==Symbol())
          {
            bool isProfit = OrderProfit() > 0 ? true : false;
            
            if(no == 1)
               isProfitable_1 = isProfit;
            else if(no == 2)
               isProfitable_2 = isProfit;
            else if(no == 3)
               isProfitable_3 = isProfit;
            else
               return isProfitable_1;
          }
       }
    }
    return isProfitable_1;
}

void OnTick()
  {
    IsLastProfitable();
  }
 
  1. Your code
    bool isProfit = OrderProfit() > 0 ? true : false;
    
    Simplified
    bool isProfit = OrderProfit() > 0;
    
              Increase Order after stoploss - MQL4 programming forum #1.3 (2017)

  2.    for(int i=OrdersHistoryTotal(); i>=1; i--)
        {
          no++;
          if(OrderSelect(i-1, SELECT_BY_POS,MODE_HISTORY)==true){
             if(OrderSymbol()==Symbol())

    You are incrementing no even if the order is not valid.

    1. Do not assume history has only closed orders.
                OrderType() == 6, 7 in the history pool? - MQL4 programming forum (2017)

    2. Do not assume history is ordered by date, it's not.
                Could EA Really Live By Order_History Alone? (ubzen) - MQL4 programming forum (2012)
                Taking the last profit and storing it in a variable | MQL4 - MQL4 programming forum #3 (2020)