Setting Magic number seems to not be working

 

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
  }
 
Well, where is the initialization of the variable
magicNumber

 in your code??

;)

 
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. 

 
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?

 
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);
}
 
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.
 

Interesting Topic, sorry to drag this up.

Clarification needed: Is this (code below) , a wrong way to initialize an EAs I.D ? I do know we must also set magic numbers, just before we submit a trade.

Is it okay to declare the Expert Magic Number OnInit(), I see no examples in the MQL5 documentation.

I have several instances (7) of the same name EA , on different chart Symbols and TimeFrames, all on 1 MT5 Terminal.

How does Each EA, know it is different to another, if we only set the Expert Magic Number a millisecond before we send the Buy Market order for example.

Should we (EA coders) declare the Experts I.D OnInit(), well before a trade has been submitted?

Or does MT5 know which EA instructions belong to which chart Symbol, TimeFrame from the same name EA? I hope I have made sense.

#include<Trade\Trade.mqh>

CTrade cTrade;

ulong magicNumber = 123456;


void OnInit()
  {
   cTrade.SetExpertMagicNumber(magicNumber);
   
  }