creating a magic number - page 4

 
7bit:

No, they can be anything you want. just like the comment. see them as some sort of a numerical comment. All manual trades opened with the normal MT4 user interface will have the magic number 0, so you can for example loop over all orders and close/delete all manual trades and orders while leaving all EA trades untouched.

For uniquely identifying only one certain order there is the ticket number.

thank you for clearing that up & sharing your views. Fwiw, I learned a lot..

 
7bit:
/**
* Rotate a 32 bit integer value bit-wise 
* the specified number of bits to the right.
* This function is needed for calculations
* in the hash function makeMacicNumber()
*/
int bitRotate(int value, int count){
   int i, tmp, mask;
   mask = MathPow(2, count) - 1;
   tmp = value & mask;
   value = value >> count;
   value = value | (tmp << (32 - count));
   return(value);
}

I don't need MathPow(), why didn't anybody see this?
/**
* Rotate a 32 bit integer value bit-wise 
* the specified number of bits to the right.
* This function is needed for calculations
* in the hash function makeMacicNumber()
*/
int bitRotate(int value, int count){
   int i, tmp, mask;
   mask = (0x00000001 << count) - 1;
   tmp = value & mask;
   value = value >> count;
   value = value | (tmp << (32 - count));
   return(value);
}
updating the first posting now.
 
7bit:
I don't need MathPow(), why didn't anybody see this?

Three main reasons spring to mind:

  • Anyone who's already using a hashing function is unlikely to replace it with your code, and therefore won't care about the detail of your code.
  • Anyone who's not currently using a hashing function, but who thinks it might be a good idea, will probably just take code such as yours on trust, and only care about whether or not it works, and not about how elegant or optimised it is.
  • Your replacement of MathPow() with a bitwise operation will save substantially less than a millisecond per call to makeMagicNumber(). You're suggesting that makeMagicNumber() is called once during an EA's lifetime, in init(). Therefore, your change is saving less than one millisecond per EA lifetime. It's an utterly trivial change (and arguably reduces the clarity of the code).
 
jjc: arguably reduces the clarity of the code

I wouldn't say it reduces the clarity. My main intention is clarity and elegance and accuracy, not performance.

I could also use power, multiplication, division (and modulo) to do the actual bit shifting without using any bitwise operators at all. But for the sake of elegance (and also clarity) i should do it either exclusively with one or the other, but not mixed. And since the meaning of the function is to shift bits around I would only obfuscate the code if i did bit shifts with multiplications.

Also while addition and multiplication may be done with integers the MathPow() function internally uses logarithms, smells like approximation and is incredibly complex compared to what i need it for, it uses and returns a double value and this has nothing to do in an integer function that should only shift n bits.

Maybe I should have asked the question why didn't I see this. Why did I use MathPow() in the first place? I don't know it. The last time I did some assembler was 15 years ago, today I'm used to think about problems in higher abstractions. Maybe this was the reason. Somewhere in my brain there is a strong connection with the nth bit being 2^n, I just wrote this down without thinking a second about it. An assembler programmer never would have done this. Shifting left is multiplying by 2, multiplying by 2 is shifting left.

 
Hey guys, just curious why you'd need more than one expert running the same tf, and symbol with same settings?

My solution for persistence is to use the 32 bits of the int to hold the tf's I use(10 of them), the expert version or trade method that opened the order(i have 4 of those). Still many bits to spare if i needed. I use the bitwise operations and some conversion functions to handle this. I know others do this or something similar, but since this doesn't seem to be enough for your situations, i'm still trying to wrap my head around the question I asked.
 
7bit:

How would in your example the Symbol() become part of the MN? You have an EA number and a Timeframe number, but what about the symbol?

It wouldn't, but the money management that I am using bases it on the MN. So in this case I need them all to be the same so that the MM is taking all of the open(ing) trades into account. I always put the EA including Version and the priority settings that I am using in the Trade Comment field so that I know which version and TimeFrame that the EA that I am developing is using and logging in the MQL4 Strategy Tester and can sort them with the trade#, currency pair or comment field. When I view the journal it tells me which currency the trade is for. When I transfer them to Excel for analysis I can sort them by whichever field I want.
I can see the advantages of your approach and may utilize it at some point in the future. But being a NuB I am utilizing MM from the code base in MQL4.Com. So at this point in my programing learning curve, MM is not my priority. Though if I can find a better method that I can splice in I will until such time that MM gets to the top of my priority list I'm not opposed to using it. Us NuBs need all of the help that we can get!
In regards to the name &/or version number, the MN only allows numbers and not letters.
I didn't go through your code but I'm assuming that your algorithm is converting the name to part of your MN in which case it would not immediately tell you of which currency pair you are addressing.
 
FourX:
your algorithm is converting the name to part of your MN in which case it would not immediately tell you of which currency pair you are addressing.

It will reliably convert any string into a unique integer number. This conversion is a one-way street, but I never ever need to convert it back.

All I need to know is that these trades with the same number belong together. It is just an ID number to easily find all the trades of this specific EA/symbol/TF combination with just one single if() in the loop. The only thing that has to be known about this number is that it is unique for each EA/symbol/timeframe combination and that it is deterministic and will not change if my PC decides to crash.

All trades logically belonging together will have the same MN. If i ever at some later time need to tell on which symbol they were made (for example to do some statistics at the end of the month) I can simply use OrderSymbol() on these orders.

 
circlesquares wrote >>
Hey guys, just curious why you'd need more than one expert running the same tf, and symbol with same settings?

My solution for persistence is to use the 32 bits of the int to hold the tf's I use(10 of them), the expert version or trade method that opened the order(i have 4 of those). Still many bits to spare if i needed. I use the bitwise operations and some conversion functions to handle this. I know others do this or something similar, but since this doesn't seem to be enough for your situations, i'm still trying to wrap my head around the question I asked.


I can't speak for anyone else, but I have an EA that can take awhile to close an order, and it misses new entry points because it's still not closed from the previous order. I would like for it to handle each entry independently and follow each trade according to the rules of each trade (moving sl, close half when tp first reached, etc).
 
joetrader:


I can't speak for anyone else, but I have an EA that can take awhile to close an order, and it misses new entry points because it's still not closed from the previous order. I would like for it to handle each entry independently and follow each trade according to the rules of each trade (moving sl, close half when tp first reached, etc).

I see. So it's tied up waiting on the server response, and meanwhile you could be opening other orders?

But, doesn't the trade context being busy stop any order related actions?

I guess as long as it works, that's all that really matters.

 

Not so much server response, but just having an order open. So the EA might get a buy signal and buy, and then stay open. A few hours later another buy signal comes, but since the already has an open position, it doesn't buy because it's waiting to close the first order. But there's no reason to assume the first buy signal will be more profitable than the second or the that there's anything to be gained by waiting for the first to close out before allowing the EA to open additional orders. But I'm still working on it, so for me it's still theory. :)

Reason: