Unable to use Counted Function with OnTick

 

Hi

I am in the process of learning MQL4 and would like to seek advice on how and where to define the functions properly in my EA as I am struggling to get it to work.
Below is the general sequence of events I am trying to code.

1. On every tick, count the number of open buy/sell orders

2. If the number of buy or sell orders has decreased from when counted on the previous tick, open a new order (if there is no decreased, then no action). 

3. Once done, define the open buy and sell order count as LastBuyCount and LastSellCount to be used when the loop is run again on the next tick.

The issue I am facing is I want the Count loop to run on every tick but MQL4 does not allow me to declare the function for my loop in "void OnTick()" so I am also unable use that function for my following IF statement.

Thank you

input double DefaultLot=0.01;
input int TakeProfit=100;
input int MagicNumber=08092020;



void OnTick()
  {
  
   int CheckOrderCount()
   {
      int NewBuyCount=0;
      for (int buycount=OrdersTotal()-1;buycount>=0;buycount--) 
      {
      OrderSelect(buycount, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber )
      if (OrderType() == OP_BUY)NewBuyCount++;
      }
      return NewBuyCount;
            
      int NewSellCount=0;
      for (int sellcount=OrdersTotal()-1;sellcount>=0;sellcount--) 
      {
      OrderSelect(sellcount, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
      if (OrderType() == OP_SELL) NewSellCount++;
      }
      return NewSellCount;
   }
                         
     if(NewBuyCount()<LastBuyCount())
                OrderSend(_Symbol,OP_BUY,DefaultLot,Ask,3,0,Ask+TakeProfit*_Point,NULL,MagicNumber,0,Green);
     if(NewSellCount()<LastSellCount())
                OrderSend(_Symbol,OP_SELL,DefaultLot,Bid,3,0,Bid+TakeProfit*_Point,NULL,MagicNumber,0,Green);

int LastBuyCount=NewBuyCount;
int LastSellCount=NewSellCount;  

  }
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Trade Orders in DOM
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Trade Orders in DOM
  • www.mql5.com
For equity securities, the Depth of Market window is available, where you can see the current Buy and Sell orders. Desired direction of a trade operation, required amount and requested price are specified for each order. To obtain information...
 
densukeningyou1:

Hi

I am in the process of learning MQL4 and would like to seek advice on how and where to define the functions properly in my EA as I am struggling to get it to work.
Below is the general sequence of events I am trying to code.

1. On every tick, count the number of open buy/sell orders

2. If the number of buy or sell orders has decreased from when counted on the previous tick, open a new order (if there is no decreased, then no action). 

3. Once done, define the open buy and sell order count as LastBuyCount and LastSellCount to be used when the loop is run again on the next tick.

The issue I am facing is I want the Count loop to run on every tick but MQL4 does not allow me to declare the function for my loop in "void OnTick()" so I am also unable use that function for my following IF statement.

Thank you

Please post code that compiles.

You cannot put a function in OnTick(), a function must be outside of any other function.

   int CheckOrderCount()
   {
   }

Always use property strict.

 
Move your function outside and then call it.
 
Hi Keith/William

Thank you for getting back, really appreciate the help.
Apologies I know I should be posting codes that compile but I was unable to get it to the point to compile successfully hence I posted it as is. 
I moved the "int CheckBuyCount()", "int NewBuyCount" and "int NewSellCount" outside as advised and now this part compiles fine.
However now the "return" in my for loop does not seem to be working as it in the "void" function.
The obvious fix seems like moving the for loop out of void OnTick but then I am thinking it will not count the orders on every tick which is what I want it to do.
Would you be able to advise what would be the right way to code this?

Thank you again
Densuke

 

Always use

#property strict

in your code.

I'm not sure exactly what your logical reasons for counting the trades are but this just concerns your function to count the trades

//+------------------------------------------------------------------+
void OnTick()
{
   int new_buy_count=0;
   int new_sell_count=0;   //Pass these 2 variables to the function by reference that way the function can calculate the value of multiple variables

   CheckOrderCount(new_buy_count,new_sell_count); //Now the 2 variables will be updated
}
//+------------------------------------------------------------------+
void CheckOrderCount(int &new_buy_count,int &new_sell_count)  //The & means that the variables are passed by reference. 
                                                              //They don't have to have the same names as in the main code
{
   for (int i=OrdersTotal()-1; i>=0; i--)
     {
      if(OrderSelect(i, SELECT_BY_POS) && OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
        {
         if (OrderType() == OP_BUY)
            new_buy_count++;
         else if (OrderType() == OP_SELL)
            new_sell_count++;
        }
     }
}
//+------------------------------------------------------------------+
 
Thank you Keith was able to sort with your help.