Automatic Magic Number

 

Hello everybody, this is my first post in this forum.


I would like to ask what do you think about this code to make an unique Magic Number for each graph atached EA. Seems to be ok?

Thanks.


int MagicNumber;

int init()
{
   MagicNumber = WindowHandle(Symbol(),0);
   Comment(MagicNumber);
}


 
NablaQuant:

I would like to ask what do you think about this code to make an unique Magic Number for each graph atached EA. Seems to be ok?

That's quite cunning - provided that you don't need the magic number to stay the same for each EA across restarts of MT4. (There's also a very small chance that, across restarts, an EA can get given a window handle previously used by another EA.)

 

And the integer size could be greater than MQL4 integer

And IIRC hWnd can be a negative value? Not good for a magic number!

-BB-

 
BarrowBoy:

And the integer size could be greater than MQL4 integer

And IIRC hWnd can be a negative value? Not good for a magic number!

-BB-

I hadn't thought of that, but MT4 does allow negative magic numbers - at least on the broker I regularly use for testing. (I've done this before by accident.) Therefore, there shouldn't be a problem from that point of view: the 32 bits of the hWnd simply get treated as a signed 32-bit integer.

 
Use hash function
 
antslag:
Use hash function

Well, yes, but a hash of what ?

 
jjc:

Well, yes, but a hash of what ?

I did this a while ago and it worked great. I assigned a 2-digit number to each pair, 1 digit to short and long (some of my EAs traded only one way), and a 2-digit number for TFs.

Then simply append the numbers and you have the MagicNumber.

 
DayTrader:

I did this a while ago and it worked great. I assigned a 2-digit number to each pair, 1 digit to short and long (some of my EAs traded only one way), and a 2-digit number for TFs.

That's great... but the clever thing about NablaQuant's method is that it generates unique magic numbers even if there are multiple EAs running on charts for the same symbol and timeframe. The ideal is something which does that and persists across restarts of MT4, but I can't see a way of achieving this using any information within MT4 (or a hash of that information).

 
jjc:

That's great... but the clever thing about NablaQuant's method is that it generates unique magic numbers even if there are multiple EAs running on charts for the same symbol and timeframe. The ideal is something which does that and persists across restarts of MT4, but I can't see a way of achieving this using any information within MT4 (or a hash of that information).

It can be done but it has to be handled carefully...

Attached is an EA I made a while ago ( lotsa work, not very profitable)... The AutoMagic is in there.

Files:
 
DayTrader:

It can be done but it has to be handled carefully...

Attached is an EA I made a while ago ( lotsa work, not very profitable)... The AutoMagic is in there.

I'm missing something here. If I open two charts for e.g. USDJPY H1, and add an instance of the EA to each of them, then both use the the magic number 9999033.

 
jjc wrote >>

I'm missing something here. If I open two charts for e.g. USDJPY H1, and add an instance of the EA to each of them, then both use the the magic number 9999033.

So is the MT terminal app a 'container' - with a single hWnd for <the whole thing>?

Reason: