Setting Magic number seems to not be working

To add comments, please log in or register
maxdraco
13
maxdraco  

Hi,

I recently ran into a issue with MT5 where my EA does not seem to be setting the magic number or know that it opened a position.


#include<Trade\Trade.mqh>
CTrade cTrade;

ulong magicNumber = 123456;

void OnInit()
  {
   cTrade.SetExpertMagicNumber(magicNumber);
   ulong tradeMagicNumber = cTrade.RequestMagic(); // returns 0
  }

bool canTrade()
  {
  
   int ticket = cTrade.RequestOrder(); // returns 0 even if trade is open
   if(cTrade.RequestMagic()==magicNumber && cTrade.RequestOrder()==0)
     {
      return true;
     }

   return false; // method always returns false as cTrade.RequestMagic returns 0
  }
Minions Labs
1184
Minions Labs  
Well, where is the initialization of the variable
magicNumber

 in your code??

;)

nicholi shen
1895
nicholi shen  
maxdraco:

Hi,

I recently ran into a issue with MT5 where my EA does not seem to be setting the magic number or know that it opened a position.


There are a few obvious issues with the way you're attempting to use the CTrade class. I'd suggest you use the debugger to dig into the code and diagnose why this isn't working for you. 

maxdraco
13
maxdraco  
Minions Labs:
Well, where is the initialization of the variable

 in your code??

;)

This was merely a code snippet
nicholi shen:

There are a few obvious issues with the way you're attempting to use the CTrade class. I'd suggest you use the debugger to dig into the code and diagnose why this isn't working for you. 

Could you please tell me what are the obvious issues?

nicholi shen
1895
nicholi shen  
maxdraco:
This was merely a code snippet

Could you please tell me what are the obvious issues?

cTrade.RequestMagic()

Returns the magic number assigned to a request. You haven't used CTrade to formulate a request and thus will not return a magic number. 

int ticket = cTrade.RequestOrder(); // returns 0 even if trade is open

Again, this is for a formulated request. You are looking for CPositionInfo not CTrade for this. 


So what you really need is:

bool can_trade()
{
   CPositionInfo pi;
   return !pi.SelectByMagic(_Symbol, magicNumber);
}
maxdraco
13
maxdraco  
nicholi shen:

Returns the magic number assigned to a request. You haven't used CTrade to formulate a request and thus will not return a magic number. 

Again, this is for a formulated request. You are looking for CPositionInfo not CTrade for this. 


So what you really need is:

Thank you so much for all your help. I corrected my mistakes and my EA is running smoothly. I still have a long way to go before I have MQL5 under the belt.
To add comments, please log in or register