Don't count up++ when closing orders. Countdown--. https://www.mql5.com/en/forum/139654.
for(int i=OrdersTotal()-1; i>=0; i--)Always put OrderSelect within an if(){statement}.
We write Ordersend( ), return the ticket number, e.g.
- Use OrderSelect loop in case of restart.
- Loops and Closing or Deleting Orders - MQL4 forum
- What are Function return values ? How do I use them ? - MQL4 forum
- Adjust for 4/5 digit brokers
We write Ordersend( ), return the ticket number, e.g.
and close with above code, which part of the code that call out the ticket number and close correctly?
as we know
search all the opened trade, it correctly search the ticket we want?
Use OrderMagicNumber() in your code.
extern int Magic_Number=1234; ... for (int pos = OrdersTotal() - 1; pos >= 0; pos --) { if (OrderSelect(SELECT_BY_POS, MODETRADES) && OrderSymbol() == Symbol && OrderMagicNumber() == Magic_Number) { switch (OrderType()) { case OP_BUY : ... case OP_SELL : ... ... } } }
I try to code as MACD < 0 then sell follow by OrderClose( ) to close the trade.
double MACD = iMACD(Symbol(),0,12,26,9,0,0,0); if(total < 1) //if no trade open { if (MACD < 0 ) OrderSend ( Sell ); for(cnt=OrdersTotal()-1;cnt>=0;cnt--) { OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES); //if( ! OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES) ) continue; if( OrderSymbol() == Symbol() && ( OrderType() == OP_BUY || OrderType() == OP_SELL ) ) Alert("Job Ticket:" ,OrderTicket(), " found"); OrderClose(OrderTicket(),OrderLots(),Bid,3,Blue); // close position return(0); // exit } //---- return(0); }
At OrderClose ( ) I still not give any command e.g " MACD > 0" then close, but the OrderClose ( ) close the trade when MACD > 0.
How this happen?
I try to code as MACD < 0 then sell follow by OrderClose( ) to close the trade.
At OrderClose ( ) I still not give any command e.g " MACD > 0" then close, but the OrderClose ( ) close the trade when MACD > 0.
How this happen?
1. Your Order Select code is wrong - which obviously you do not read and understand my code
for(cnt=OrdersTotal()-1;cnt>=0;cnt--) { OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES); //<<--- wrong //if( ! OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES) ) continue; //<<--- correct but why you don't use this
2. That because you using bar 0 for MACD

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
We write Ordersend( ), return the ticket number, e.g.
and close with above code, which part of the code that call out the ticket number and close correctly?
as we know
search all the opened trade, it correctly search the ticket we want?