How to check if a Magic Number is already used?

 

In my EA, I use the following line of code to register its Magic Number:

m_trade.SetExpertMagicNumber(Magic);

The function has no return value, so is there a generic method to check whether a Magic Number is already in use? I'm looking to block an EA from loading if the Magic Number already exists.

Thanks

 
You need to somehow communicate with other EAs running. You may use global variables or data stored in files to communicate and avoid interference of magics.
 
ceejay1962:

In my EA, I use the following line of code to register its Magic Number:

The function has no return value, so is there a generic method to check whether a Magic Number is already in use? I'm looking to block an EA from loading if the Magic Number already exists.

Thanks

As @Yashar Seyyedin said, you'd have to somehow communicate with other EAs running, so you'd have to control the code for all the other EAs.

Alternatively, you could loop through the order history and check the magic numbers using HistoryOrderGetInteger(ticket, ORDER_MAGIC). If the EA has ever made a trade with that magic number, that would tell you. Wouldn't prevent it from using the same one as a running one that's never traded, but it's a lot better than nothing.

Also, you could randomly generate your magic number - the odds of duplicating any one number would be 1 in  2,147,483,647.

 
Scott Allen #:

As @Yashar Seyyedin said, you'd have to somehow communicate with other EAs running, so you'd have to control the code for all the other EAs.

Alternatively, you could loop through the order history and check the magic numbers using HistoryOrderGetInteger(ticket, ORDER_MAGIC). If the EA has ever made a trade with that magic number, that would tell you. Wouldn't prevent it from using the same one as a running one that's never traded, but it's a lot better than nothing.

Also, you could randomly generate your magic number - the odds of duplicating any one number would be 1 in  2,147,483,647.

Thanks for the info - I guess that checking history would only work if the EA with the existing magic number had made some trades. I'll take a look at using a random number. 

 
Yashar Seyyedin #:
You need to somehow communicate with other EAs running. You may use global variables or data stored in files to communicate and avoid interference of magics.

Thanks - it is a shame that the command doesn't warn on duplicate magic numbers. I'll check out globals.

 
ceejay1962 #:

Thanks - it is a shame that the command doesn't warn on duplicate magic numbers. I'll check out globals.

Magic numbers are 64 bit wide. Meaning it's not a chance of 1 in 2^32, but a chance of 1 in 2^64.

I suggest, for consistency, you calculate a CRC64 value from symbol and timeframe. This will create a 64 bit value that is always the same for a specific symbol and timeframe.

This will not disrupt your history data like a random number would. And you can use that to identify running instances, ensuring you don't double-launch your EA on the same TF and Symbol.

A CRC code can be found in code base.
 
Dominik Egert #:
Magic numbers are 64 bit wide. Meaning it's not a chance of 1 in 2^32, but a chance of 1 in 2^64.

I suggest, for consistency, you calculate a CRC64 value from symbol and timeframe. This will create a 64 bit value that is always the same for a specific symbol and timeframe.

This will not disrupt your history data like a random number would. And you can use that to identify running instances, ensuring you don't double-launch your EA on the same TF and Symbol.

A CRC code can be found in code base.

Which defeat the purpose of using a magic number. What if I want to use the EA with different parameters on the same symbol/timeframe ?

 
ceejay1962 #:

Thanks - it is a shame that the command doesn't warn on duplicate magic numbers. I'll check out globals.

"A shame" is really exaggerated.

It's a simple system which put the responsibility on the final user. If the coder want to have so control and add features on it, it can become tricky.

However it could be a lot better with an integrated management by the platform, but it's not the path the developers took. It's a choice, not a shame.

 
Alain Verleyen #:

"A shame" is really exaggerated.

It's a simple system which put the responsibility on the final user. If the coder want to have so control and add features on it, it can become tricky.

However it could be a lot better with an integrated management by the platform, but it's not the path the developers took. It's a choice, not a shame.

I think you've misinterpreted my use of the word "shame" - its not intended as a criticism - if I were to rephrase my comment, I'd say "it would be nice if......"

It is what it is, and I'll implement something to satisfy my requirement.

 
ceejay1962 #:

I think you've misinterpreted my use of the word "shame" - its not intended as a criticism - if I were to rephrase my comment, I'd say "it would be nice if......"

It is what it is, and I'll implement something to satisfy my requirement.

Thanks for the precision.
 
Dominik Egert #:
Magic numbers are 64 bit wide. Meaning it's not a chance of 1 in 2^32, but a chance of 1 in 2^64.

I suggest, for consistency, you calculate a CRC64 value from symbol and timeframe. This will create a 64 bit value that is always the same for a specific symbol and timeframe.

This will not disrupt your history data like a random number would. And you can use that to identify running instances, ensuring you don't double-launch your EA on the same TF and Symbol.

A CRC code can be found in code base.

Might want to add the EA name (either input or hard-coded variable) as input for that CRC as well, as you might have other EAs running on the same symbol and timeframe.