trying to make a function but failing. prolly something small.

To add comments, please log in or register
Dmitri Mikhalev
445
Dmitri Mikhalev  

Hi all,

so I am new to MQL4 and not really great at programming overall. Anyway, I am trying to learn from a tutorial (this one: https://www.cashbackforex.com/school/mql4-programming/basic-expert-advisor-example-moving-average-cross/ ) and there seems to be some stuff there that doesn't work. I haven't completed all of it yet, as I am trying to figure stuff out bit by bit.

So what happens here, is that I am trying to make the function below work, but I keep getting this error: "'close' - function declarations are allowed on global, namespace or class scope only"

I have tried to remove "void" before "close" but then I get other errors like "type - unidentified identifier". Why is there a void there, I guess there should actually be "void" there, since the author put it there, but then, I don't know what is wrong...

Please help!

And thank you in advance!


void close(int type)
{
   if(OrdersTotal() > 0)
   {
      for(Counter = OrdersTotal() - 1; Counter >= 0; Counter--)
      {
         OrderSelect(Counter, SELECT_BY_POS, MODE_TRADES); 

         if(type==OP_BUY && OrderType() == OP_BUY)
         {
            if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
            {
               RefreshRates();
               OrderClose(OrderTicket(), OrderLots(), NormalizeDouble(Bid, Digits), vSlippage);
            }
         }      

         if(type==OP_SELL && OrderType() == OP_SELL)
         {
            if(OrderSymbol()==Symbol() && OrderMagicNumber() == MagicNumber)
            {
               RefreshRates();
               OrderClose(OrderTicket(), OrderLots(), NormalizeDouble(Ask, Digits), vSlippage);
            }
         }
      }
   }
}

Premier Global Holdings
529
Matthew Renner  
Dmitri Mikhalev:

Hi all,

so I am new to MQL4 and not really great at programming overall. Anyway, I am trying to learn from a tutorial (this one: https://www.cashbackforex.com/school/mql4-programming/basic-expert-advisor-example-moving-average-cross/ ) and there seems to be some stuff there that doesn't work. I haven't completed all of it yet, as I am trying to figure stuff out bit by bit.

So what happens here, is that I am trying to make the function below work, but I keep getting this error: "'close' - function declarations are allowed on global, namespace or class scope only"

I have tried to remove "void" before "close" but then I get other errors like "type - unidentified identifier". Why is there a void there, I guess there should actually be "void" there, since the author put it there, but then, I don't know what is wrong...

Please help!

And thank you in advance!


void close(int type)
{
   if(OrdersTotal() > 0)
   {
      for(Counter = OrdersTotal() - 1; Counter >= 0; Counter--)
      {
         OrderSelect(Counter, SELECT_BY_POS, MODE_TRADES); 

         if(type==OP_BUY && OrderType() == OP_BUY)
         {
            if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
            {
               RefreshRates();
               OrderClose(OrderTicket(), OrderLots(), NormalizeDouble(Bid, Digits), vSlippage);
            }
         }      

         if(type==OP_SELL && OrderType() == OP_SELL)
         {
            if(OrderSymbol()==Symbol() && OrderMagicNumber() == MagicNumber)
            {
               RefreshRates();
               OrderClose(OrderTicket(), OrderLots(), NormalizeDouble(Ask, Digits), vSlippage);
            }
         }
      }
   }
}

Try renaming the function to something other than "close", instead something like "closeOrders".  Modern MetaQuotes uses an array named "close" and might be confusing your function of "close" with that.

Also, make sure the function is entirely outside OnInit, OnDeinit, OnTick, and/or OnTimer functions.

Dmitri Mikhalev
445
Dmitri Mikhalev  
Matthew Renner:

Try renaming the function to something other than "close", instead something like "closeOrders".  Modern MetaQuotes uses an array named "close" and might be confusing your function of "close" with that.

Also, make sure the function is entirely outside OnInit, OnDeinit, OnTick, and/or OnTimer functions.

Thank you!


The function wasn't actually outside OnInit, so I moved it outside and the errors are gone! :)

To add comments, please log in or register