We are our own best indicator.
Dynamic TakeProfit & Dynamic StopLoss EA/AI/Algorithm 0 replies
Need Script/EA can only generate Magic number 5 replies
GBPUSD Magic Number 45 replies
can we manually assign a magic number 4 replies
Ever seen this magic number bug? 4 replies
DislikedWould you be so kind to either post the code or explain how it works?
Thanks,
--RRIgnored
/** * 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(IndicatorShortName() + 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); }
//+------------------------------------------------------------------+ int hash_string(string s) //+------------------------------------------------------------------+ { // this is the djb2 string hash algo int h = 5381, l = StringLen(s), i; for (i=0; i<l; i++) h = h * 33 + StringGetChar(s,i); return (h); }
DislikedI use a simple DJB2 hash algorithm to build 32-bits hash values out of strings.
It is used by Java compilers to build their symbol tables (so they'd better have a good one!).Ignored
#include <[url="http://7bit.99k.org/common__functions_8mqh.html"]common_functions.mqh[/url]> int magic; int init() { magic = [url="http://7bit.99k.org/common__functions_8mqh.html#a4005e449c291909f9930354ed447d09d"]makeMagicNumber[/url](WindowExpertName() + Symbol() + Period()); } int start() { }
DislikedMaybe you could tell the EA to generate a random number within a certain range, say, between 1 and 500, and tell it to look for magic numbers within that range? And, if you want to assign magic numbers to other EA's, just don't assign them within that range.
Hmm ... I think I might try that myself.Ignored
DislikedReally? I don't see it requiring more than one or two lines of code in order to generate the random number. The rest is pretty straight forward ... i.e., if(Magic>=1 && Magic<=500){ proceed ... }Ignored
DislikedAnd how will you make sure you get the same number again after a crash/restart/recompile so that the EA can continue where it left?Ignored
DislikedIt won't matter, so long as that number is in the specified range, as this is what the EA is set to look for.
As far as that's concerned, I don't see why you couldn't specify a different magic number for each trade (if you wanted to), again, so long as that number falls within the specified range.Ignored
DislikedHi,
maybe I miss something.
I'm assigning the result of GetTickCount() fct as magicnumber.
It should per definitionem be unique ...Ignored
DislikedHi,
maybe I miss something.
I'm assigning the result of GetTickCount() fct as magicnumber.
It should per definitionem be unique ...
Have not had any problems so far.
But I'm not sure if there any hidden shortcomings of this approach.
CU,
MaMoodIgnored
DislikedIf you want to get the magic number back after a re-boot of compile of code that won't work. That's the point.Ignored
DislikedThen you have to specify a range instead of a number and that range must be unique for each EA and for each instance ...Ignored
QuoteDisliked... and you are back at square one.
QuoteDislikedIts even more complicated since you now have two numbers for every chart where you run an EA instead of only one.
QuoteDislikedGenerating it with a hash makes all this go away and you don't even have to think about it anymore.
DislikedMakes what go away? Oh, can this hash thingy generate a random number specific to each order, and yes, recall it later?Ignored
DislikedEach instance of what? Are you referring to each individual trade or, the EA itself? Again, so long as you specify a specific range, as I am suggesting, you can vary the random number by each individual trade, and yes, recall them later.
Now, can this hash reference you are talking about do the same thing?Ignored
int mn_base = makeMagicNumber(...) & 0xfffffffc; // 2 lowest bits zero int mn_type0 = mn_base; int mn_type1 = mn_base + 1; int mn_type2 = mn_base + 2; int mn_type3 = mn_base + 3;