| / | Forum |
|
lolinternets
2007.08.07 05:54
So, I just started with MQL4 and am writing my first EA. It's an extremely simply
EA for USD/JPY. On initiliazation it's supposed to generate a random number, normalize
it so the number is either 1 or 0, and depending on the number it will place a
buy or sell order. Then on each tick it checks for an open order; if one is open
it does nothing, if no orders are open then it generates a random number, normalizes
it so the number is either 1 or 0, and depending on the number places a buy or
sell order. Now my problem is, the MathRand() function doesn't seem to be generating
random numbers, it keeps generating the same number so the EA keeps placing buy
orders. Below is the code and any help is appreciated. Thank you.
int ticket, err; double rand; //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() { MathSrand(TimeLocal()); rand = MathRand(); rand = rand/32767; rand = MathRound(rand); if ( rand == 0 ) { ticket = OrderSend( Symbol(), OP_BUY, 1, Ask, 3, Ask-0.10, Ask+0.10, "", 16384, 0, Green); err = GetLastError(); } else { ticket = OrderSend( Symbol(), OP_SELL, 1, Ask, 3, Ask+0.10, Ask-0.10, "", 16384, 0, Green); err = GetLastError(); } if (err > 0) { Print("error(",err,"): "); } return(0); } //+------------------------------------------------------------------+ //| expert deinitialization function | //+------------------------------------------------------------------+ int deinit() { OrderClose(ticket,1,Ask,3); return(0); } //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() { if ( OrderSelect(ticket, SELECT_BY_TICKET) == true ) { if (OrderCloseTime() > 0) { rand = MathRand(); rand = rand/32767; rand = MathRound(rand); if ( rand == 0 ) { ticket = OrderSend( Symbol(), OP_BUY, 1, Ask, 3, Ask-0.10, Ask+0.10, "", 16384, 0, Green); } else { ticket = OrderSend( Symbol(), OP_SELL, 1, Ask, 3, Ask+0.10, Ask-0.10, "", 16384, 0, Green); } } } return(0); } //+------------------------------------------------------------------+ |
|
Reporting the 11th Week of the Championship (10-16 December) Eleven weeks are already behind us. Here is the final straight. |
|
phy
2007.08.07 07:52
|
|
barton
2007.08.22 22:54
lolinternets -- I am also just writing my first EA and will be watching your thread
with interest. If you need a guina pig, please let me know. I just posted a thread
asking if anybody had any free EA that actually runs on demo ... I need to observe,
etc. But am not finding ones that actually run ... even the "sample"
on the platform. Am not getting much help from MT4 so far ... they have not answered
my last two emails So I hope you get it going ... let me know if i can be of any
value to you, or if you care to suggest anything for me. I will be reading the
MT4 info in this thread and in the F1 from the "Create an EA" on the
client terminal
|