MQL4 - automated forex trading   /  

Forum

First EA, help please.

Back to topics list To post a new topic, please log in or register

avatar
1
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);
  }
//+------------------------------------------------------------------+

article

Reporting the 11th Week of the Championship (10-16 December)

Eleven weeks are already behind us. Here is the final straight.


avatar
2462
phy 2007.08.07 07:52 
void MathSrand( int seed)
The MathSrand() function sets the starting point for generating a series of pseudorandom integers. To reinitialize the generator, use 1 as the seed argument. Any other value for seed sets the generator to a random starting point. MathRand retrieves the pseudorandom numbers that are generated. Calling MathRand before any call to MathSrand generates the same sequence as calling MathSrand with seed passed as 1.

avatar
5
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
Back to topics list  

To add comments, please log in or register