Generating the MagicNumber

 

I have written an EA that I run on many different currency pairs. At the moment I must change the MagicNumber for every different instrument so that it will open new trades when there are already open trades on other pairs.

I have started to write a bit of code to do this but it does not work, because it does not print the result.

Can somebody help me to get this to work please?

Here is the code:

      string CurrPair = Symbol();
      string FirstCurr = StringSubstr(CurrPair,0,3);
      string SecondCurr = StringSubstr(CurrPair,3,3);

      if (FirstCurr == "USD")r = 0;
      if (FirstCurr == "EUR")r = 1;
      if (FirstCurr == "GBP")r = 2;
      if (FirstCurr == "CHF")r = 3;
      if (FirstCurr == "JPY")r = 4;
      if (FirstCurr == "AUD")r = 5;
      if (FirstCurr == "CAD")r = 6;
      if (FirstCurr == "NZD")r = 7;
   
      if (SecondCurr == "USD")s = 0;
      if (SecondCurr == "EUR")s = 1;
      if (SecondCurr == "GBP")s = 2;
      if (SecondCurr == "JPY")s = 4;
      if (SecondCurr == "AUD")s = 5;
      if (SecondCurr == "CAD")s = 6;
      if (SecondCurr == "NZD")s = 7;
      
    //  MagicNumber = StringConcatenate("r" + "s");
      
      string Number = StringConcatenate("r" + "s");
      
      Print ("MagicNumber is ", Number);
 
string Number = StringConcatenate((string)r,(string)s);
 
gooly:
Thanks a lot Gooly ! Much appreciated!
 

But still does not work. Does not even execute the "Print" statement which I cannot understand at all.

Tried the following code also without success>

      string CurrPair = Symbol();
      string FirstCurr = StringSubstr(CurrPair,0,3);
      string SecondCurr = StringSubstr(CurrPair,3,3);

      if (FirstCurr == "USD")r = 0;
      if (FirstCurr == "EUR")r = 1;
      if (FirstCurr == "GBP")r = 2;
      if (FirstCurr == "CHF")r = 3;
      if (FirstCurr == "JPY")r = 4;
      if (FirstCurr == "AUD")r = 5;
      if (FirstCurr == "CAD")r = 6;
      if (FirstCurr == "NZD")r = 7;
   
      if (SecondCurr == "USD")s = 0;
      if (SecondCurr == "EUR")s = 1;
      if (SecondCurr == "GBP")s = 2;
      if (SecondCurr == "JPY")s = 4;
      if (SecondCurr == "AUD")s = 5;
      if (SecondCurr == "CAD")s = 6;
      if (SecondCurr == "NZD")s = 7;
      
    //  MagicNumber = StringConcatenate("r" + "s");
      
      string Number = StringConcatenate(r,s);
      
      Print("MagicNumber is ", Number);
 
ernest02:

But still does not work. Does not even execute the "Print" statement which I cannot understand at all.

Tried the following code also without success>

Why do you want to concatenate numbers ? A magic number is an int.

      string CurrPair = Symbol();
      string FirstCurr = StringSubstr(CurrPair,0,3);
      string SecondCurr = StringSubstr(CurrPair,3,3);

      if (FirstCurr == "USD")r = 0;
      if (FirstCurr == "EUR")r = 1;
      if (FirstCurr == "GBP")r = 2;
      if (FirstCurr == "CHF")r = 3;
      if (FirstCurr == "JPY")r = 4;
      if (FirstCurr == "AUD")r = 5;
      if (FirstCurr == "CAD")r = 6;
      if (FirstCurr == "NZD")r = 7;
   
      if (SecondCurr == "USD")s = 0;
      if (SecondCurr == "EUR")s = 1;
      if (SecondCurr == "GBP")s = 2;
      if (SecondCurr == "JPY")s = 4;
      if (SecondCurr == "AUD")s = 5;
      if (SecondCurr == "CAD")s = 6;
      if (SecondCurr == "NZD")s = 7;
      
      int MagicNumber = r*10+s;
            
      Print("MagicNumber is ", MagicNumber);
 
angevoyageur:

Why do you want to concatenate numbers ? A magic number is an int.

I need to generate unique double numbers representing the different currency pairs.

These numbers to be used as unique MagicNumbers.


 

To create a unique number from a string you can use a hash-function.

There is a hash.mqh but I haven't used it, so I can't tell you anything.

 
ernest02:

I have written an EA that I run on many different currency pairs. At the moment I must change the MagicNumber for every different instrument so that it will open new trades when there are already open trades on other pairs.

I have started to write a bit of code to do this but it does not work, because it does not print the result.

Can somebody help me to get this to work please?

Here is the code:

Leaving aside the problems with the code for a moment.

It is not clear from your post whether your EA is

1. intended to work with multiple pairs apart from the chart symbol that it is attached to

or

2. it is intended to work only with the chart symbol and will be attached to more than 1 chart

 

In either case, why is there a need to generate unique magic numbers? 

if 1. -  Check the magic number and ignore any trade with a different magic number

if 2. -  Check the magic number and the OrderSymbol and ignore any trade that is not the chart symbol

 
Last time I tried to do an auto magic number generator and it was horrible experience. Then I just use a magic number for specific chart and take note of it. Done deal. Simple.
 

Thanks for all the help guys.

I have decided to abandon the generation of unique MagicNumbers and rather adjust my code according the recommendation given by GumRai, which is:

"f 2. -  Check the magic number and the OrderSymbol and ignore any trade that is not the chart symbol"

I really appreciate all your feedback and advice. Thank you!

 
ernest02: I have written an EA that I run on many different currency pairs. At the moment I must change the MagicNumber for every different instrument
No you don't. One MN identifies the EA's trades. Filter them by symbol. Done. Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 forum


Reason: