Dynamic Magic Number: Unique & Referenced even if the EA is compiled over the same pair, timeframe & period

 

I have been searching the MT4 forums & database to find a way to automatically generate magic numbers but to be able to do it easily and still be able to track orders after MT4 closes or your EA is compiled. This should work no matter what if for an example there two orders of the same: currency pair - time-frame - period.

Theres only one "random" piece of code that I found that will randomize an integer:

https://docs.mql4.com/math/MathSrand and https://docs.mql4.com/math/MathRand

What I would want with a Dynamic Magic Number is:

1. When a Buy or Sell order is going to open; the magic number must not be in use.

2. When EA is reinitialized; Magic number is referenced with order comment so open orders will be assigned the original magic number.

The main focus is to: Generate unique magic numbers to open orders and use the order comment to track Timeframe, TP & SL, and parse order comment to get magic number.

Therefore; it was my thought that order comment would assist the best dynamically reference magic number no matter multiple orders of the same currency pair, same time-frame and same period. Does anybody use this method or something similar?

 
Turn some Integers into String and Merge them. Example OrderTicket()+Sys_Magic+Period()+StopLoss+TakeProfit+Etc. Put this within an Array and Save to file. OrderComment() can only be use once and not updated, also brokers may over-write the OrderComment.
 
Subgenius:

I have been searching the MT4 forums & database to find a way to automatically generate magic numbers but to be able to do it easily and still be able to track orders after MT4 closes or your EA is compiled.

1. When a Buy or Sell order is going to open; the magic number must not be in use.

2. When EA is reinitialized; Magic number is referenced with order comment so open orders will be assigned the original magic number.

The main focus is to: Generate unique magic numbers to open orders and use the order comment to track Timeframe, TP & SL, and parse order comment to get magic number.

  1. If you want to generate magic numbers, then by definition they will not be constant over compiles and reattachment. The only way it to try and read a file and generate one if the file doesn't exist. You can easily generate one using int mn = WindowHandle(Symbol(), Period()); in init. Why bother? Just use a extern
  2. Magic number is only so an EA can identify its orders. I just use extern MN=20120901; the date I started coding it.
  3. Order Comments are unreliable, don't use them. https://www.mql5.com/en/forum/134204
  4. Just use a range of numbers if you need to identify the TF. https://www.mql5.com/en/forum/134204


 
calculate a hash over symbol + timeframe + fixed EA-name. There is a function makeMagicNumber() in common_functions.mqh that will calculate an 31 bit hash for that purpose.
 
/**
* create a positive integer for the use as a magic number.
*
* The function takes a string as argument and calculates
* an 31 bit hash value from it. The hash does certainly not
* have the strength of a real cryptographic hash function
* but it should be more than sufficient for generating a
* unique ID from a string and collissions should not occur.
*
* use it in your init() function like this:
*    magic = makeMagicNumber(WindowExpertName() + Symbol() + Period());
*
* where name would be the name of your EA. Your EA will then
* get a unique magic number for each instrument and timeframe
* and this number will always be the same, whenever you put
* the same EA onto the same chart.
*
* Numbers generated during testing mode will differ from those
* numbers generated on a live chart.
*/
int makeMagicNumber(string key) {
   int i, k;
   int h = 0;

   if (IsTesting()) {
      key = "_" + key;
   }

   for (i = 0; i < StringLen(key); i++) {
      k = StringGetChar(key, i);
      h = h + k;
      h = bitRotate(h, 5); // rotate 5 bits
   }

   for (i = 0; i < StringLen(key); i++) {
      k = StringGetChar(key, i);
      h = h + k;
      // rotate depending on character value
      h = bitRotate(h, k & 0x0000000F);
   }

   // now we go backwards in our string
   for (i = StringLen(key); i > 0; i--) {
      k = StringGetChar(key, i - 1);
      h = h + k;
      // rotate depending on the last 4 bits of h
      h = bitRotate(h, h & 0x0000000F);
   }

   return(h & 0x7fffffff);
}

/**
* Rotate a 32 bit integer value bit-wise
* the specified number of bits to the right.
* This function is needed for calculations
* in the hash function makeMacicNumber()
*/
int bitRotate(int value, int count) {
   int i, tmp, mask;
   mask = (0x00000001 << count) - 1;
   tmp = value & mask;
   value = value >> count;
   value = value | (tmp << (32 - count));
   return(value);
}
Files:
Reason: