How to retrieve a specified closed order using HistoryTotal() ?

 
Hi All ;)
is there a way using HistoryTotal function to be able to find a particular order (buy or sell) closed ?
I tried with this code but it seems not working :

   int hstTotal=HistoryTotal();
   for(i=0;i<hstTotal;i++)
   {
     //---- check selection result
     if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false)
       {
        if (Debug==true) Print("Access to history failed with error (",GetLastError(),")");
        break;
       }
      if (OrderType()==OP_BUY && OrderComment()="LONG") 
      {
       ..take some action...
      }
      if (OrderType()==OP_SELL && OrderComment()="SHORT") 
      {
       ..take some action...
      }
   }



And when a buy order was in place using OrderSelect I identified this using "LONG" as comment (and "SHORT" if a sell order) :

 Sell:
  ..
  ..
  OrderSend(Symbol(),OP_SELL,Lots,Entry,Slippage,StopLoss,TakeProfit,"SHORT",0,0);
  ..
  ..

Buy:
 ..
 ..
  OrderSend(Symbol(),OP_BUY,Lots,Entry,Slippage,StopLoss,TakeProfit,"LONG",0,0);
 ..
 ..



Maybe is not supported OrderComment using MODE_HISTORY mode? And if so how can I solve this issue?

thx a lot ;)
Skyline



 
First of all, the compiler should've warned you about using single '=' in the conditional statement:
if (OrderType()==OP_BUY && OrderComment()[b]=[/b]"LONG")



That is one of reasons at least.

The second is that the comment is not guaranteed to be preserved over the orders life (and death). The server has a right to change or amend it completely.

Just don't rely on the comment and leave the first condition only:

if (OrderType()==OP_BUY)


Nobody can change the type of market order.

 
Curiosly enough you stated OP_SELL and "SHORT" for buy label in you post...
 
First of all, the compiler should've warn you about using single '=' in the conditional statement:
if (OrderType()==OP_BUY && OrderComment()[b]=[b]"LONG")



That is one of reasons at least.

The second is that the comment is not guaranteed to be preserved over the orders life (and death). The server has a right to change or amend it completely.

Just don't rely on the comment and leave the first condition only:

if (OrderType()==OP_BUY)


Nobody can change the type of market order.



Thx for quick reply Irtron :)
Yes i'm sorry i wrote wrong code correct one in my EA is with == , so is not a syntax problem. But as you said is a comment problem since as far I understand there is no way to retrieve it in history when an order is closed.
Is there any other way to retrieve a particular order already closed in history ?
 
Curiosly enough you stated OP_SELL and "SHORT" for buy label in you post...


Ops I was wrong , I changed my first post ;)
 
But as you said is a comment problem since as far I understand there is no way to retrieve it in history when an order is closed.

Why not? The original comment might be still there. Although it's likely to be changed by the server, e.g. if the position is closed by the server when stoploss or takeprofit triggers.
 
You might've used MagicNumber functionality to specify a particular property as well.
This is much more reliable way than the comment parsing.
 
You might've used MagicNumber functionality to specify a particular property as well.
This is much more reliable way than the comment parsing.


Mmm magicnumber is unique within the same EA and my problem is that I have to indentify two different kind of buy/sell , called BUY1,SELL1,BUY2,SELL2 because i have to handle them in different way, this is why i tried to find some unique information in history and magicnumber I think isn't usefull for this purpouse :(
 
You can use MN as a bitfield variable. Or part of it. Check out https://docs.mql4.com/basis/operations/bit.

#define BUY1FLAG   0x00010000
#define BUY2FLAG   0x00020000
#define SELL1FLAG  0x00040000
#define SELL2FLAG  0x00080000
#define EA_ID_MASK 0x0000ffff

int EaId = 1234; // < 0x0000ffff (= 65535)

int start()
{
    
    int mn = 0;
    
    switch (op)
    {
        case buy1:
            mn = BUY1FLAG;
            break;

        case buy2:
            mn = BUY2FLAG;
            break;

        case sell1:
            mn = SELL1FLAG;
            break;

        case sell2:
            mn = SELL2FLAG;
            break;
    }
    
    mn += EaId;
    
    OrderSend(..., mn, ...);
    // ...
    
    
    mn = OrderMagicNumber();
    
    if ((mn & EA_ID_MASK) == EaId)
    {
        if ((mn & BUY1FLAG) != 0)
            Print("buy1");
        else if ((mn & BUY2FLAG) != 0)
            Print("buy2");
        else if ((mn & SELL1FLAG) != 0)
            Print("sell1");
        else if ((mn & SELL2FLAG) != 0)
            Print("sell2");
        else
            Print("You\'re not my type");
    }
    else
        Print("You\'re not mine whatsoever");
        
    return (0);
}



Things like this are better discussed on programmer's forum https://www.mql5.com/forum/en

 
You can use MN as a bitfield variable....


Thanks a lot Irtron this code will be really usefull for my EA ;)
 
You can use MN as a bitfield variable....
Thanks a lot Irtron this code will be really usefull for my EA ;)

You're welcome.

mqlexpert [at] gmail [dot] com