POSITION_MAGIC - Returning zero (always)

 

Hi folks, 


My EA was working fine since last week, but now some functions it's not working anymore.
Then I figure out that all my positions magic number are returning "0".


As I'm using CTrade class, I inserted the following lines:


ulong EA_Magic=1234;

int OnInit()
   {
    Trade.SetExpertMagicNumber(EA_Magic);


    return(INIT_SUCCEEDED);

   }


void OnTick()
   {
    OpenOrder();

    for(int z=PositionsTotal()-1; z>=0; z--)
     {
      if(PositionGetInteger(POSITION_MAGIC)==EA_Magic)
         {
          ulong sel=PositionGetTicket(z);
          if(PositionSelectByTicket(sel)) 
              if(PositionGetString(POSITION_SYMBOL)==_Symbol)
                {
                 total++;
                 Print(PositionGetInteger(POSITION_MAGIC));
                 break;
                }
         }
     }
   }



void OpenOrder()
   {
    double last=SymbolInfoDouble(Symbol(),SYMBOL_LAST);
    if(iClose(Symbol(),PERIOD_M15,1)>iOpen(Symbol(),PERIOD_M15,1))
        Trade.SellStop(1,last,Symbol(),0,0);
   }


Since I defined :

Trade.SetExpertMagicNumber(EA_Magic);


Does all my positions opened by my EA shouldn't return my "magic number" when I call the function:

Print(PositionGetInteger(POSITION_MAGIC));
 
Guilherme Mendonca:

Hi folks, 


My EA was working fine since last week, but now some functions it's not working anymore.
Then I figure out that all my positions magic number are returning "0".


As I'm using CTrade class, I inserted the following lines:



Since I defined :


Does all my positions opened by my EA shouldn't return my "magic number" when I call the function:

Your code is incorrect. You need to select the position (in the loop) BEFORE using any PositionGetXXX function.
 
Alain Verleyen #:
Your code is incorrect. You need to select the position (in the loop) BEFORE using any PositionGetXXX function.

Thanks Alain!

Reason: