New to mql4, advise is appreciated.

 

Hi fellow EA experts,

I have recently got involved in forex and mql4, and is trying to learn programming as well. I am creating an EA as follow just to understand how OrderSend, OrderClose work, and loop it indefinitely.
The comment about "i" is to help me understand if the loop is working as the value of i will increase by 1 in each loop. Sleep to slow down the disappearance of comment for visual purpose.

I am anticipating it to create a buy order when OrdersTotal is 0, when the order is executed (OrdersTotal becomes 1), it goes to next "if" condition where it compares the opened order's profit and close it.
From the logic it should be constantly opening 1 order at Ask price and close it instantly at Bid price but it is behaving weirdly, example:

Constantly opening EURUSD trade at exactly 1.11737 and close at 1.11712, continue to open and close when the price hit the same value only.
The price varies everytime time i apply the EA, repeating the trades at following scenarios.

Open 1.11719, Close 1.11698
Open 1.11706, Close 1.11683
Open 1.11677, Close 1.11656
Open 1.11941, Close 1.12045

I have not specified the limit Ask price or limit Bid price but it seems to be restricting to a hidden price. Can anyone help me to understand what is wrong? Thanks and appreciate your help. 

int OnInit()
{
int i=0;

do
{        
    Comment("What is i = ", +i);
    Sleep(1000);  
    if (OrdersTotal()==0)
    int ticket=OrderSend(Symbol(),OP_BUY,0.01,Ask,3,NULL,NULL,"My order",1234,0,clrGreen);

    else if(OrdersTotal()==1)  
    {
     if(OrderSelect(0,SELECT_BY_POS)==true)
     Sleep(1000);
     Comment("Profit for the order = ",OrderProfit(),
                   "\nTotal orders = ",OrdersTotal());
      Sleep(1000);
      if(OrderProfit() > 0)  
      bool res = OrderClose(OrderTicket(),0.01,Bid,3,Red);  
      else if(OrderProfit() < 0)  
      bool res = OrderClose(OrderTicket(),0.01,Bid,3,Red);}

      if(i==9)
      {    
       i=0;
       Comment("i = ", +i);
       Sleep(1000);
       }

   i++;
}

while(i<10);
return(INIT_SUCCEEDED);

 
OnInit() only executes once when the EA is launched and has a time constraint. Use OnTick() or OnTimer().
 
Ernst Van Der Merwe:
OnInit() only executes once when the EA is launched and has a time constraint. Use OnTick() or OnTimer().
Thanks for helping. I moved it to OnTick() and is still behaving the same. Could it be that the Ask/Bid value is being stored somewhere during first EA execution and the subsequent loops are constantly referring to the stored value to open/close trades?
Can you advise me how to clear the Ask/Bid value after first execution so it will pick up whatever the market Ask/Bid is? Thanks.
 
MoneyMonster:
Thanks for helping. I moved it to OnTick() and is still behaving the same. Could it be that the Ask/Bid value is being stored somewhere during first EA execution and the subsequent loops are constantly referring to the stored value to open/close trades?
Can you advise me how to clear the Ask/Bid value after first execution so it will pick up whatever the market Ask/Bid is? Thanks.

Maybe add a time or price check.

      if(OrdersTotal()==0 && Time[0]<=TimeCurrent())
      if(OrdersTotal()==0 && NormalizeDouble(Close[0]-Bid,_Digits-1)==0.0)