Wont work in EA

 

This script works when you use it manually. It is suppose to close all orders that are open. In the Expert I am simply stating that if my profit is greater then X close all orders. X being any number.

Why when programmed into the Expert will it not work then?

Any ideas?

Here is the code.

--Script Code

/**
* Closes all currently open orders
*/
int start() {
bool result;
double price;
int cmd,error;

// Loop through all orders and close them
for(int i = (OrdersTotal() - 1); i >= 0; i--) {
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) {
// Get the order type
cmd = OrderType();

// Set the price based on order type
if(cmd == OP_BUY) {
price = Bid;
} else if (cmd == OP_SELL) {
price = Ask;
}

// If the order is pending close it first
if(cmd != OP_BUY && cmd != OP_SELL) {
// Delete pending orders
result = OrderDelete(OrderTicket());
if(result != TRUE) {
Print("LastError = ", GetLastError());
}
} else {

// Close the order
result = OrderClose(OrderTicket(), OrderLots(), price, 3,CLR_NONE);
if(result != TRUE) {
error = GetLastError();
Print("LastError = ", error);
if(error == 135) {
RefreshRates();
}
} else {
Print( "Error when order select ", GetLastError());
}
}
}
}
return(0);
}
//+------------------------------------------------------------------+

--Code in Expert

/**
* Close all open and pending orders
*/
void closeAllOrders() {
bool result;
double price;
int cmd,error;

while (OrdersTotal() > 0) {
// Loop through all orders and close them
for (int i = (OrdersTotal() - 1); i >= 0; i--) {
if (OrderSelect(i,SELECT_BY_POS, MODE_TRADES)) {
// Get the order type
cmd = OrderType();

// Set the price based on order type
if (cmd == OP_BUY) {
price = Bid;
} else if (cmd == OP_SELL) {
price = Ask;
}

// If the order is pending close it first
if (cmd != OP_BUY && cmd != OP_SELL) {
// Delete pending orders
result = OrderDelete(OrderTicket());
if(result != TRUE) {
Print("LastError = ", GetLastError());
}
} else {
// Close the order
result = OrderClose(OrderTicket(), OrderLots(), price, 3,CLR_NONE);
if(result != TRUE) {
error = GetLastError();
Print("LastError = ", error);
if (error == 135) {
RefreshRates();
}
} else {
Print( "Error when order select ", GetLastError());
}
}
}
}
}
}

 
Your expert's code not includes special function start() . Without this function expert dose not work. That's right attempt:

/**
 * Close all open and pending orders
 */
void closeAllOrders() {
   bool   result;
   double price;
   int    cmd,error;
  
   while (OrdersTotal() > 0) {
      // Loop through all orders and close them
      for (int i = (OrdersTotal() - 1); i >= 0; i--) {
         if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) {
            // Get the order type
            cmd = OrderType();
     
            // Set the price based on order type
            if (cmd == OP_BUY) {
               price = Bid;
            } else if (cmd == OP_SELL) {
               price = Ask;
            }
        
            // If the order is pending close it first
            if (cmd != OP_BUY && cmd != OP_SELL) {
               // Delete pending orders
               result = OrderDelete(OrderTicket());
               if(result != TRUE) {
                  Print("LastError = ", GetLastError());
               }
            } else {        
               // Close the order
               result = OrderClose(OrderTicket(),OrderLots(),price,3,CLR_NONE);
               if(result != TRUE) {
                  error = GetLastError();
                  Print("LastError = ",error);
                  if (error == 135) {
                     RefreshRates();
                  }
               } else {
                  Print( "Error when order select ", GetLastError());
               }
            }
         }
      }
   }
}

int start()
{
closeAllOrders();
return;
}
 

We have the int start function at the top of the code. This is just a snipit that is in there.

Does error 129 have anything to do with it?

 
See https://docs.mql4.com/trading/errors
I don't see the simplest check OrderSymbol()==Symbol(). May be EA tries to close any order on price form another Symbol. For example, it is ticket on USDCHF but EA performs on EURUSD chart, etc.
 

The code seems to work when the price is going up and it creates a positive then the orders are closed.

When the price is going down, it creates a positive and tries to close order but I receive the order error 129.

Any help?

 

If I switch the BID and ASK price around still error 129.

Does anyone have any ideas?

 

What price is it trying to close at, and what is the Bid/Ask of that pair at that time?

 

It just calculates the profit column. When it becomes positive say above 10 dollars it will close all my orders.

The Bid and Ask price are what ever the current price at the times are. Should I define price as current ask or current bid?

 

Put a Print statement in there and verify the prices are correct, if you have not.

Error says bad price. Is it?

 
phy:

Put a Print statement in there and verify the prices are correct, if you have not.

Error says bad price. Is it?


We have the print statment in, and a clear error or refresh rates if 135 should this be 129?

 

Is the price which you are sending with the order correct?

Reason: