Automatic Magic Function Required

 

I am looking for a simple function that will create an automatic Magic Number based on the symbol type.

Does anyone have something like this? I thought this would be easier than allocating a number to each symbol type.

I need this for an EA I am writing which will attach to almost all charts at one time. I need a different magic number for each symbol without having to manually change it on each chart.

Thanks.

Steve

 

The following code combines the symbol, expert name & time frame for a Magic Number:

int MagicNumberFromExpertName(string expert_name)

{

int magic_number = 0;

//We use symbol here as an expert has to be attached to a chart

for(int i=0; i < 5; i++)

{

magic_number = magic_number * 3 + StringGetChar(Symbol(), i);

}

for(i = 0; i < StringLen(expert_name); i++)

{

magic_number = magic_number * 3 + StringGetChar(expert_name, i);

}

magic_number = magic_number * 3 + Period();

return (magic_number);

}

 

Thanks

Thank you, Much appreciated.

 

here is another version

string s="GBPUSD", magicStr = "";

int len = StringLen(s), tmp;

for(int i=0; i<len; i++){

tmp = StringGetChar(s, i);

magicStr = magicStr+(tmp-64);

}

int magic = StrToInteger(magicStr);

64 was subtracted under assumption that symbol is ascii string

Without -64 the resulting # will not fit into int type resulting in negative #, i dont know if thats ok and if number is unique.

I dont know if subtracting 64 for other pair other than g/u gives small number.

Anyway this was intended for you to think and try writing the function yourself.

 

Auto MagicNumber

The following code combines the symbol, expert name & time frame for a Magic Number:

int MagicNumberFromExpertName(string expert_name)

{

int magic_number = 0;

//We use symbol here as an expert has to be attached to a chart

for(int i=0; i < 5; i++)

{

magic_number = magic_number * 3 + StringGetChar(Symbol(), i);

}

for(i = 0; i < StringLen(expert_name); i++)

{

magic_number = magic_number * 3 + StringGetChar(expert_name, i);

}

magic_number = magic_number * 3 + Period();

return (magic_number);

}

I get allot of errors when I add this to my code. Can you be more clear.

I added this;

extern string expert_name = "EA Name"

Moved this up to top with the other extern variables;

int magic_number = 0;

And deleted this;

int MagicNumberFromExpertName(string expert_name)

I'll try this

Thank you

 

Magic Number Base On Symbol And Time Frame

MAGIC NUMBER BASE ON SYMBOL AND TIME FRAME FUNCTION

int init()

{

MagicNumber = subGenerateMagicNumber(MagicNumber, Symbol(), Period());

}

-------

-------

int subGenerateMagicNumber(int MagicNumber, string symbol, int timeFrame)

{

int isymbol = 0;

if (symbol == "EURUSD") isymbol = 1;

else if (symbol == "GBPUSD") isymbol = 2;

else if (symbol == "USDJPY") isymbol = 3;

else if (symbol == "USDCHF") isymbol = 4;

else if (symbol == "AUDUSD") isymbol = 5;

else if (symbol == "USDCAD") isymbol = 6;

else if (symbol == "EURGBP") isymbol = 7;

else if (symbol == "EURJPY") isymbol = 8;

else if (symbol == "EURCHF") isymbol = 9;

else if (symbol == "EURAUD") isymbol = 10;

else if (symbol == "EURCAD") isymbol = 11;

else if (symbol == "GBPUSD") isymbol = 12;

else if (symbol == "GBPJPY") isymbol = 13;

else if (symbol == "GBPCHF") isymbol = 14;

else if (symbol == "GBPAUD") isymbol = 15;

else if (symbol == "GBPCAD") isymbol = 16;

else isymbol = 17;

if(isymbol<10) MagicNumber = MagicNumber * 10;

return (StrToInteger(StringConcatenate(MagicNumber, isymbol, timeFrame)));

}

 

Or this initialization function:

int MagicNumber;

//+------------------------------------------------------------------+

//| expert initialization function |

//+------------------------------------------------------------------+

int init()

{

if (Symbol() == "AUDCADm" || Symbol() == "AUDCAD") { MagicNumber = 427101; }

if (Symbol() == "AUDJPYm" || Symbol() == "AUDJPY") { MagicNumber = 427102; }

if (Symbol() == "AUDNZDm" || Symbol() == "AUDNZD") { MagicNumber = 427103; }

if (Symbol() == "AUDUSDm" || Symbol() == "AUDUSD") { MagicNumber = 427104; }

if (Symbol() == "CHFJPYm" || Symbol() == "CHFJPY") { MagicNumber = 427105; }

if (Symbol() == "EURAUDm" || Symbol() == "EURAUD") { MagicNumber = 427106; }

if (Symbol() == "EURCADm" || Symbol() == "EURCAD") { MagicNumber = 427107; }

if (Symbol() == "EURCHFm" || Symbol() == "EURCHF") { MagicNumber = 427108; }

if (Symbol() == "EURGBPm" || Symbol() == "EURGBP") { MagicNumber = 427109; }

if (Symbol() == "EURJPYm" || Symbol() == "EURJPY") { MagicNumber = 427110; }

if (Symbol() == "EURUSDm" || Symbol() == "EURUSD") { MagicNumber = 427111; }

if (Symbol() == "GBPCHFm" || Symbol() == "GBPCHF") { MagicNumber = 427112; }

if (Symbol() == "GBPJPYm" || Symbol() == "GBPJPY") { MagicNumber = 427113; }

if (Symbol() == "GBPUSDm" || Symbol() == "GBPUSD") { MagicNumber = 427114; }

if (Symbol() == "NZDJPYm" || Symbol() == "NZDJPY") { MagicNumber = 427115; }

if (Symbol() == "NZDUSDm" || Symbol() == "NZDUSD") { MagicNumber = 427116; }

if (Symbol() == "USDCHFm" || Symbol() == "USDCHF") { MagicNumber = 427117; }

if (Symbol() == "USDJPYm" || Symbol() == "USDJPY") { MagicNumber = 427118; }

if (Symbol() == "USDCADm" || Symbol() == "USDCAD") { MagicNumber = 427119; }

if (MagicNumber == 0) { MagicNumber = 427199; }

pair = Symbol();

return(0);

}

 

Thank you for the info

 

Not sure what this is doing - pair = Symbol(); wouldn't compile so I removed it.

 
matrixebiz:
Not sure what this is doing - pair = Symbol(); wouldn't compile so I removed it.

Yes you can remove it. This line is for other stuff in the rest of the code where the example was taken.

This MagicNumber copy and past is a part of a coding I modify long time ago here in this forum. (The number tell me the exact EA where it come from !!!)

FerruFx

 

Great, thanks