OrdersTotal() not working

 

I have this code in a loop:


if (OrdersTotal() > 0)   {
   monitorTrade();
}
if (OrdersTotal() == 0)  {
   orderOpen("buy");
   return;
}


I expected the EA to never open more than one single order at a time, but it sends many until I disable it.


I put the output of OrdersTotal() in a label and saw that the number of orders were indicated as 0 although several had been opened.

I placed a single order out of the loop and the label still indicated 0, not 1.


That is not even what I wanted in the first place. I'm used to doing it like this:

// at the end of an orderOpen function:

if (result.retcode == TRADE_RETCODE_PLACED)     {
   isTradeRunning = true;
   if (action == "buy")    {orderRunning = "buy";}
   if (action == "sell")   {orderRunning = "sell";}
} else {Print("Last error on " + _Symbol + ": " + IntegerToString(GetLastError()));}


And the actual loop is:

if (isOrderRunning == true)   {
   monitorTrade();
}
if (isOrderRunning == false)  {
   orderOpen("buy");
   return;
}


But that fails too. I mean, the EA will keep opening more than one order if I don't disable it.


I flip the boolean value from inside the orderOpen function after confirmation that the order has been sent successfully (which I still don't know how to do), but for the sake of testing I changed the loop to this:

if (isOrderRunning == true)   {
   monitorTrade();
}
if (isOrderRunning == false)  {
   orderOpen("buy");
   isTradeRunning = true;
   return;
}


Even with the boolean value flip inserted right in the loop, the EA still posts multiple orders.

So I have two problems:

1) Why is OrdersTotal() not working as expected?

2) Why is my global variable isOrderRunning not being acknowledged?

Documentation on MQL5: Trade Functions / OrdersTotal
Documentation on MQL5: Trade Functions / OrdersTotal
  • www.mql5.com
OrdersTotal - Trade Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
whoowl:

I have this code in a loop:

if (OrdersTotal() > 0)   {
   monitorTrade();
}
if (OrdersTotal() == 0)  {
   orderOpen("buy");
   return;
}

I expected the EA to never open more than one single order at a time, but it sends many until I disable it.
I put the output of OrdersTotal() in a label and saw that the number of orders were indicated as 0 although several had been opened.
I placed a single order out of the loop and the label still indicated 0, not 1. 
Even with the boolean value flip inserted right in the loop, the EA still posts multiple orders.

So I have two problems:
1) Why is OrdersTotal() not working as expected?
2) Why is my global variable isOrderRunning not being acknowledged?


Hello,

Use PositionsTotal() to see how many trades are currently running.

OrdersTotal() is used to see how many pending orders have been placed.

Kind regards.

Documentation on MQL5: Trade Functions / PositionsTotal
Documentation on MQL5: Trade Functions / PositionsTotal
  • www.mql5.com
PositionsTotal - Trade Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
whoowl:

I have this code in a loop:



I expected the EA to never open more than one single order at a time, but it sends many until I disable it.


I put the output of OrdersTotal() in a label and saw that the number of orders were indicated as 0 although several had been opened.

I placed a single order out of the loop and the label still indicated 0, not 1.


That is not even what I wanted in the first place. I'm used to doing it like this:


And the actual loop is:


But that fails too. I mean, the EA will keep opening more than one order if I don't disable it.


I flip the boolean value from inside the orderOpen function after confirmation that the order has been sent successfully (which I still don't know how to do), but for the sake of testing I changed the loop to this:


Even with the boolean value flip inserted right in the loop, the EA still posts multiple orders.

So I have two problems:

1) Why is OrdersTotal() not working as expected?

2) Why is my global variable isOrderRunning not being acknowledged?

For your 2nd question, because you are using two different variables. Change "isTradeRunning" to "isOrderRunning".
Reason: