Issues with Random MQL4 EA

 

Hello

I just started MQL4 recently and being the complete newbie that I am, I would like to ask some help on the code below.

Basically I am trying to have my EA randomly choose buy/sell on every tick and enter an order until it reaches the max number of orders.

However after running the code on Strategy Tester and checking the journal, it seems to be always sending consecutive buys, then consecutive sells, then consecutive buys again and so on.

Can someone pls help check what needs to be changed to have the random generator to run for every order?

Thank you
John


double DefaultLot=0.01;
double MaxOrderCount=50;

void OnTick()
  {
   if(OrdersTotal()<MaxOrderCount)
        {
         string signal="";
   
         MathSrand(GetTickCount());
   
         double RandomNumber=MathRand()%2;
   
         if(RandomNumber==0)signal="buy";
   
         if(RandomNumber==1)signal="sell";
            
      if(signal=="buy")
      OrderSend(_Symbol,OP_BUY,DefaultLot,Ask,3,Ask-50*_Point,Ask+200*_Point,NULL,0,0,Blue);
      
      if(signal=="sell")
      OrderSend(_Symbol,OP_SELL,DefaultLot,Bid,3,Bid+50*_Point,Bid-200*_Point,NULL,0,0,Red);     
      

         }
  }
Documentation on MQL5: Trade Functions / OrdersTotal
Documentation on MQL5: Trade Functions / OrdersTotal
  • www.mql5.com
Do not confuse current pending orders with positions, which are also displayed on the "Trade" tab of the "Toolbox" of the client terminal. An order is a request to conduct a transaction, while a position is a result of one or more deals.
 
densukeningyou1:

Hello

I just started MQL4 recently and being the complete newbie that I am, I would like to ask some help on the code below.

Basically I am trying to have my EA randomly choose buy/sell on every tick and enter an order until it reaches the max number of orders.

However after running the code on Strategy Tester and checking the journal, it seems to be always sending consecutive buys, then consecutive sells, then consecutive buys again and so on.

Can someone pls help check what needs to be changed to have the random generator to run for every order?

Thank you
John


Remove the

MathSrand(GetTickCount());

from the function, and put it into the initialization part.

 
linux80s:

Remove the

from the function, and put it into the initialization part.

Hi linux80s



What you did worked!

Appreciate the help, thank you.


Best regards
Akira

Reason: