"expressions are not allowed on a global scope"

 
Does it mean I should write the code under the event handling function? I'm doing it here, but still error.
void OnTick()
{
 double takeProfit = NormalizeDouble(Point*TakeProfitS, Digits);
 double stopLoss = NormalizeDouble(Point*StopLossS, Digits);
 double shortStopLoss = Bid + stopLoss;
 double shortTakeProfit = Bid - takeProfit;
 double buyStopLoss = Ask + stopLoss;
 double buyTakeProfit = Ask - takeProfit;

   
 
   double FastMovingAverage = iMA(NULL, PERIOD_M5, 50, 0, MA_METHOD, PRICE_METHOD, 0);
   double SlowMovingAverage = iMA(NULL, PERIOD_M5, 100, 0, MA_METHOD, PRICE_METHOD, 0);
 // Open Order
   if (SlowMovingAverage < FastMovingAverage)
   { 
   
    OrderOpenID = OrderSend(NULL, OP_SELL, LotSize, Bid, 0, shortStopLoss, shortTakeProfit, NULL, MagicNumber, 0, clrRed);
   if (OrderOpenID < 0) Alert("Error. Sell order was rejected." + GetLastError());
     
   }
   
   if (SlowMovingAverage > FastMovingAverage)
   {
     OrderOpenID = OrderSend(NULL, OP_BUY, LotSize, Ask, 0, buyStopLoss, buyTakeProfit, NULL, MagicNumber, 0, clrBlue);
     if (OrderOpenID < 0) Alert("Error. Buy order was rejected." + GetLastError());
   
   }
   
 // Close Order
  if (SlowMovingAverage > FastMovingAverage)
  
    OrderClose(OrderOpenID,  LotSize, Ask,0,clrRed);
    
  }
  
  if (SlowMovingAverage < FastMovingAverage)
  {  
   OrderClose(int OrderOpenID, LotSize, Bid, 0, clrBlue);
     
  }

 bool CheckIfOpenOrdersByMagicNB(int MagicNB)
 { 
  int openOrders = OrdersTotal();
  
  for (int i = 0; i<OrdersTotal(); i++)
  if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)==True)
  { 
   if (OrderMagicNumber() == MagicNumber)
   {
    return true;
   }
    
  }
 }
  return false;
} 

 



   
 
// Close Order
  if (SlowMovingAverage > FastMovingAverage)
  
    OrderClose(OrderOpenID,  LotSize, Ask,0,clrRed);
  } // This bracket terminates OnTick. Lines below are outside of any function.
  
  if (SlowMovingAverage < FastMovingAverage)
  {  
   OrderClose(int OrderOpenID, LotSize, Bid, 0, clrBlue);
  }
Like wise
 bool CheckIfOpenOrdersByMagicNB(int MagicNB)
 { 
  int openOrders = OrdersTotal();
  
  for (int i = 0; i<OrdersTotal(); i++)
  if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)==True)
  { 
   if (OrderMagicNumber() == MagicNumber)
   {
    return true;
   }
    
  }
 } // This braket terminates CheckIfOpenOrdersByMagicNB. Everything below is outside of any function.
  return false;
} 
 
I already told you, you should do your loop reverse.

  for (int i = OrdersTotal()-1; i>=0 && !_StopFlag; i--)
 
Dominik Christian Egert #: I already told you, you should do your loop reverse.
  1. Op's problem is a compilation problem.
  2. #2 is your first post in this thread. You didn't tell him anything before.
  3. The function returns true if an order exists with the magic number. Counting down or counting up makes no difference.
 
William Roeder #:
  1. Op's problem is a compilation problem.
  2. #2 is your first post in this thread. You didn't tell him anything before.
  3. The function returns true if an order exists with the magic number. Counting down or counting up makes no difference.
I did in another thread. I've been giving him some advice here and there, specifically to this user.

As far as I am aware, he is learning, and therefore my effort was to show him a reliable way to program loops.

If any of the orders disappears while looping through, you get to skip one, right?

So I pointed out, it is more reliable to go backwards in such cases, as you cannot skip an order, in case the number of orders changes while looping through.




 

Dominik Christian Egert #:

If any of the orders disappears while looping through, you get to skip one, right?

So I pointed out, it is more reliable to go backwards in such cases, as you cannot skip an order, in case the number of orders changes while looping through.
  1. Orders do not "disappear" unless you delay the loop with server calls/sleep.

  2. In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading), while you are waiting for the current operation (closing, deleting, modifying) to complete, any number of other operations on other orders could have concurrently happened and changed the position indexing and order count:

    1. For non-FIFO (non-US brokers), (or the EA only opens one order per symbol), you can simply count down, in an index loop, and you won't miss orders. Get in the habit of always counting down.
                Loops and Closing or Deleting Orders - MQL4 programming forum

    2. For In First Out (FIFO rules — US brokers), and you (potentially) process multiple orders per symbol, you must find the earliest order (count up), close it, and on a successful operation, reprocess all positions (from zero).
                CloseOrders by FIFO Rules - Strategy Tester - MQL4 programming forum - Page 2 #16
                MetaTrader 5 platform beta build 2155: MQL5 scope, global Strategy Tester and built-in Virtual Hosting updates - Best Expert Advisors - General - MQL5 programming forum #1.11

    3. and check OrderSelect in case later positions were deleted.
                What are Function return values ? How do I use them ? - MQL4 programming forum
                Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles

    4. and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use, on the next order / server call, the Predefined Variables (Bid/Ask.) Or instead, be direction independent and just use OrderClosePrice().

 
William Roeder #:
  1. Orders do not "disappear" unless you delay the loop with server calls/sleep.

  2. In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading), while you are waiting for the current operation (closing, deleting, modifying) to complete, any number of other operations on other orders could have concurrently happened and changed the position indexing and order count:

    1. For non-FIFO (non-US brokers), (or the EA only opens one order per symbol), you can simply count down, in an index loop, and you won't miss orders. Get in the habit of always counting down.
                Loops and Closing or Deleting Orders - MQL4 programming forum

    2. For In First Out (FIFO rules — US brokers), and you (potentially) process multiple orders per symbol, you must find the earliest order (count up), close it, and on a successful operation, reprocess all positions (from zero).
                CloseOrders by FIFO Rules - Strategy Tester - MQL4 programming forum - Page 2 #16
                MetaTrader 5 platform beta build 2155: MQL5 scope, global Strategy Tester and built-in Virtual Hosting updates - Best Expert Advisors - General - MQL5 programming forum #1.11

    3. and check OrderSelect in case later positions were deleted.
                What are Function return values ? How do I use them ? - MQL4 programming forum
                Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles

    4. and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use, on the next order / server call, the Predefined Variables (Bid/Ask.) Or instead, be direction independent and just use OrderClosePrice().

Yes, that's the complete explanation.


Reason: