/** * 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; }
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.
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?
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?
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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());
}
}
}
}
}
}