Is it OK to generate Magic Number automatically?

 
//|-----------Global Variable
int Magic;

int init()
{
string chartTF=Period();
switch (chartTF){
                case PERIOD_M1:         Magic=8; break;
                case PERIOD_M5:         Magic=9; break;
                case PERIOD_M15:        Magic=1; break;
                case PERIOD_M30:        Magic=2; break;
                case PERIOD_H1:         Magic=3; break;
                case PERIOD_H4:         Magic=4; break;
                case PERIOD_D1:         Magic=5; break;
                case PERIOD_W1:         Magic=6; break;
                case PERIOD_MN1:        Magic=7;
        }

}


thanks in advance

 

You do NOT want to "generate Magic Number automatically," i.e. generate using hash/windowHandle etc. because, then you must use permanent storage to find your orders in case of restart.

Yours is just using a range of numbers. I do it slightly differently

extern int      Magic.Number.Base               = 20110202;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int     magic.number;                           // Export to ModifyStops
string  TF.text,                                // Export to Decide
:
int     init(){
    /*++++ Adjust for the current chart timeframe */{
    int     TFperiod[]  = { PERIOD_M1,  PERIOD_M5,  PERIOD_M15, PERIOD_M30,
                            PERIOD_H1,  PERIOD_H4,  PERIOD_D1,  PERIOD_W1,
                            PERIOD_MN1  };
    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;
    /*---- Adjust for the current chart timeframe */}
:
}
OrderSelect loops filter on magic.number and pair.