Build/Create Unique Magic Number

 

Does anyone see a problem using the following code to build a unique magic number ??

[I cannot use symbol or timeframe in the magic number]

int magic = TimeCurrent();

 
Yes, it's possible, but how can you use it later?
 
michaelB:

Does anyone see a problem using the following code to build a unique magic number ??

[I cannot use symbol or timeframe in the magic number]

int magic = TimeCurrent();

  1. Each time you refresh the charts the EA goes through a deInit/init cycle and you'll forget the previous magic number and loose all trades.
  2. Why not. Just use mn = base+tf. Always filter by symbol.
    extern int      Magic.Number.Base               = 20110202;
    int     magic.number;                           // Export to ModifyStops
    string  TF.text;                                // Export to Decide
    int     init(){
       /*++++ Adjust for the current chart timeframe */{               static
        int     TFperiod[]  = { PERIOD_M1,  PERIOD_M5,  PERIOD_M15, PERIOD_M30,
                                PERIOD_H1,  PERIOD_H4,  PERIOD_D1,  PERIOD_W1,
                                PERIOD_MN1  };                      static
        string  TFtext[]    = { "M1",       "M5",       "M15",      "M30",
                                "H1",       "H4",       "D1",       "W1",
                                "MN1"       };
        for(int iTF=0; TFperiod[iTF] < Period(); iTF++){}
        TF.text         = TFtext[iTF];
        magic.number    = Magic.Number.Base + iTF;
    
    }
    :
        for(pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
            OrderSelect(pos, SELECT_BY_POS)                 // Only my orders w/
        &&  OrderMagicNumber()  == magic.number             // my magic number
        &&  OrderSymbol()       == Symbol()                 // and my pair.
        ){
    

 
Roger:
Yes, it's possible, but how can you use it later?


Actually the statement should look like this-

static int magic = TimeCurrent();

Once set, the number should be available [and constant] as long as the EA is running.

 
michaelB:


Actually the statement should look like this-

static int magic = TimeCurrent();

Once set, the number should be available [and constant] as long as the EA is running.

That will not compile. You can only initialize a static with a constant. You could do
static int magic = 0;
if (magic == 0) magic = TimeCurrent();

But on any restart (chart closed/reopened) terminal restart, OS reboot etc. All currently opened trades will be abandoned.

[I cannot use symbol or timeframe in the magic number]

Why NOT?
 
WHRoeder:
That will not compile. You can only initialize a static with a constant.
I think that is the least of his issues . . .
Reason: