magic behind magic numbers

 
I need a quick tutorial on magic numbers and their relevance. Here are some questions.

1. Are magic numbers needed in order to maintain separate "state" or "namespace" for each instance of an EA applied within the same terminal application?

2. I assume the answer to number 1 is "yes", but, then I'm confused as to why magic numbers are often user-defined. Is there some scenario whereby one may want to have two different instances of the same EA in the same client with the same magic number? Wouldn't these lead to confusion at runtime? If there is no scenario where this makes sense (e.g. to have the same magic number for the same EA in multiple instances), then, why make it an option to have it that way? Seems to me this just easily leads to potential problems which must be coded around.

3. If the assumptions above are correct, what are best practices for automatically generating unique magic numbers (e.g. unique number for each instance regardless of which chart it is applied to)? I've contemplated using the TimeOnDropped function, but, as I understand this only works if the user actually drops the EA on the chart via the mouse and doesn't work if it is attached via the keyboard. Therefore, I'm not readily thinking of a way to ensure the magic number is unique wich each instance. I guess I could use a random number generation routine...

4. Lastly, why is it called "magic"? What is the significance behind the name? What is the magic in magic? :)

If there's any other pointers on how to effectively use magic numbers, I appreciate the insight.

Thanks

Bill
 
magic number gives you a possibility to group trades. Look at closing side. If you have different experts running in different windows with a different strategy then how you close all USDJPY open positions what opened expert "x" in a window USDJPY 15-min but none of opened trades made by another expert in USDJPY 1 hour window.

If you use protective positions or hedge then you have to define which one of the total list is the right one. Easiest way is to use magic number.

About question 3 -- maybe is the good idea to use opened time [iTime(NULL,0,0)] so every trade has his own number (this function gives you current bar opening time in unix format)

Magic - if you have a list of 100 opened trades and you have a way to find excatly that trade what you want -- isn't that like magic ;)
 
zolero wrote:
magic number gives you a possibility to group trades. Look at closing side. If you have different experts running in different windows with a different strategy then how you close all USDJPY open positions what opened expert "x" in a window USDJPY 15-min but none of opened trades made by another expert in USDJPY 1 hour window.

If you use protective positions or hedge then you have to define which one of the total list is the right one. Easiest way is to use magic number.

About question 3 -- maybe is the good idea to use opened time [iTime(NULL,0,0)] so every trade has his own number (this function gives you current bar opening time in unix format)

Magic - if you have a list of 100 opened trades and you have a way to find excatly that trade what you want -- isn't that like magic ;)
Thanks zolero. I get the point, however, couldn't one specify the EA in one's routine for cross-EA routines? I understand how grouping under like magic numbers can help in the scenario outlined above, but, isn't that scenario more the exception than the norm? It seems to be that for normal use, the coder needs to work harder to build a routine for ensuring abstraction between multiple like EA instances.

And, believe me, it is VERY easy for even experienced users to accidentially apply the same magic number when setting up charts which can yield undesirable results. Just trying to best understand how to protect against this.

I'll toy around with iTime. Thanks.

Bill
 
billworld:

And, believe me, it is VERY easy for even experienced users to accidentially apply the same magic number when setting up charts which can yield undesirable results. Just trying to best understand how to protect against this.

magic number helps you only if you have multiple trades with the same symbol and you need to separate them from each other somehow. otherwise it doesn't give you nothing. If you use multiple symbols you have no need to touch it.

For example you want to limit opened trades number for one timeframe. if you have for a symbol only one timeframe it is not a problem, just check if(OrderSymbol()==Symbol()) and that's it, but if you have more timeframes then this control is not enough.

Maybe you need it if you buy more than one symbol from one window and different codes can buy the same symbol at the same time. Otherwise it doesn't give you a thing. It is something what you need not so often but without it some possibilities wouldn't be there.

To avoid errors like many codes with a same magic number you should write a calculation of code inside of your expert(s) - some kind of algoritm.
 
 
zolero wrote:
billworld wrote:

And, believe me, it is VERY easy for even experienced users to accidentially apply the same magic number when setting up charts which can yield undesirable results. Just trying to best understand how to protect against this.

magic number helps you only if you have multiple trades with the same symbol and you need to separate them from each other somehow. otherwise it doesn't give you nothing. If you use multiple symbols you have no need to touch it.

For example you want to limit opened trades number for one timeframe. if you have for a symbol only one timeframe it is not a problem, just check if(OrderSymbol()==Symbol()) and that's it, but if you have more timeframes then this control is not enough.

Maybe you need it if you buy more than one symbol from one window and different codes can buy the same symbol at the same time. Otherwise it doesn't give you a thing. It is something what you need not so often but without it some possibilities wouldn't be there.

To avoid errors like many codes with a same magic number you should write a calculation of code inside of your expert(s) - some kind of algoritm.
Thanks zolero for the feedback and stringo for the article link. I'll research/learn some more. Bill
 

Have been looking around this site and .ru site without any absolute foolproof resolution concerning order identity.

billworld is [I believe] wanting *unique* automatic EA generated magic number. Let me say the magic number base value to which can be added in datums like bar type or symbol type.

For example, is it not possible that an EA can be run on two charts which have same periodicity and indeed if previously attached to these two charts and then Terminal is started up - will not these two EA's start at same time?

Or, can time be an answer to achieve *unique* magic number?

For example: value = MathSrand(GetTickCount());

Above would be unique but only if 1 or more milliseconds elapse between each EA executing above code, yes?

Ok, for sure it seems pointless to have one or more of: same EA on same symbol on same period,... but things can happen and mistakes be made, yes?

IF trading live and this did happen would not one's deposit be at risk? ))

Perhaps someone has [simple] code to use in making *unique* magic number?

Reason: