I do not know why I cannot OrdersClose()
I understand the parameters which seem clearly defined in the documentation
I am not getting any errors and believe the condition to close the orders are coded correctly
The issue is your for loop . . . study the { } braces . . .
This is what your for loop does . . .
for(int i = OrdersTotal()-1; i >= 0 ; i--) { bool result; }
This is what you wanted it to do . . .
bool result; for(int i = OrdersTotal()-1; i >= 0 ; i--) { if(OrderSelect(i, SELECT_BY_POS)) if(OrderMagicNumber() == MagicNumber) if(OrderSymbol()== Symbol()) // <----- note this . . . { if(OrderType() == OP_BUY) if(prevfaster > prevslower && faster < slower) //if direction goes down on a buy order then close the order { result = OrderClose(OrderTicket(), OrderLots(), Bid, 3*pips2points, Blue); if(result == false) Print("Order",OrderTicket(),"failed to close Error",GetLastError()); Print(" MACD DOWN CLOSE"); } if(OrderType() == OP_SELL) if(prevfaster < prevslower && faster > slower) //if direction goes up on a sell order then close the order { result = OrderClose(OrderTicket(), OrderLots(), Bid, 3*pips2points, Blue); if(result == false) Print("Order",OrderTicket(),"failed to close Error",GetLastError()); Print(" MACD UP CLOSE"); } } }
Also I added an additional set of braces . . . what you had will only work for OP_BUY . . . study and understand.
So no other code before the first code block brace ?
The bool result before the (for loop) brackets seemed to be the biggest problem
I am a little confused why the if(OrderType()==OP_BUY)== false would not then read if(OrderType() ==OP_SELL) and why the extra brackets are required ?
If here is some do's and don't on if() statement flow I will be sure to read it.
I'll keep searching around for something on that so I can understand better, thanks again
In order not to get mixed up by many ifs in a loop I put at the beginning of the loop everything I don't want:
for(int i = OrdersTotal()-1; i >= 0 ; i--) { if( !OrderSelect(i, SELECT_BY_POS)) continue; if( OrderMagicNumber() != MagicNumber) continue; if( OrderSymbol() != Symbol()) continue; bool result;// has been on a wrong place!! if(OrderType() == OP_BUY) { if(prevfaster > prevslower && faster < slower) //if direction goes down on a buy order then close the order { result = OrderClose(OrderTicket(), OrderLots(), Bid, 3*pips2points, Blue); if(result == false) Print("Order",OrderTicket(),"failed to close Error",GetLastError()); Print(" MACD DOWN CLOSE"); } } // else ? if(OrderType() == OP_SELL) { if(prevfaster < prevslower && faster > slower) //if direction goes up on a sell order then close the order { result = OrderClose(OrderTicket(), OrderLots(), Bid, 3*pips2points, Blue); if(result == false) Print("Order",OrderTicket(),"failed to close Error",GetLastError()); Print(" MACD UP CLOSE"); } } }
Thanks, I think I understand
So no other code before the first code block brace ?
The bool result before the (for loop) brackets seemed to be the biggest problem
I am a little confused why the if(OrderType()==OP_BUY)== false would not then read if(OrderType() ==OP_SELL) and why the extra brackets are required ?
If you don't use { } braces only the line following the if is executed due to the if . . . so in your code . . .
if(OrderSymbol()== Symbol())
if this is true this line is executed . . . . .
if(OrderType() == OP_BUY)
if it's true or false this line is also executed . . .
if(OrderType() == OP_SELL)
which is probably not what you wanted . . .
If you don't use { } braces only the line following the if is executed due to the if . . . so in your code . . .
if this is true this line is executed . . . . .
if it's true or false this line is also executed . . .
which is probably not what you wanted . . .
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I understand the parameters which seem clearly defined in the documentation
I am not getting any errors and believe the condition to close the orders are coded correctly.
I have moved the function call around, and used many print statment to attempt to diagnose the problem
I can print OrdersTotal() inside the function and shows 1 when an order is placed, but never closes it.
Please advise
Thanks