Help coders, my EA has no trouble opening trades but doesn't close any. I suspect the trouble is in my OrderSelect loop but i can't figure out what's wrong. HELP!!!!
I suspect your suspicion is wrong.
Add some Print statements to help you see what is going on . . . we can't figure this out for you as you don't give enough info, for example: what are these ? CloseBuy1_1 < CloseBuy1_2 && CloseBuy2_1 >= CloseBuy2_2 && CloseBuy3_1 > CloseBuy3_2 and these (EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount . . . they are all involved in the decision taking involved in getting to your OrderClose . . .
By the way . . Bars is unreliable, you shouldn't use it.
- Always test return codes
if (!OrderClose(...)) Alert("OrderClose failed: ", GetLastError());
If you don't get the message, then you're not even reaching the close. for (int x = OrdersTotal() - 1; x >= 0; x--) if (OrderSelect(x, SELECT_BY_POS, MODE_TRADES)) { if (OrderType() == OP_BUY && OrderMagicNumber() == MagicNumber && OrderSymbol() == Symbol()) bool CloseBuyOrder = true; }
After the first iteration, what will be the value of CloseBuyOrder when it encounters other orders (non-buy, non-MN, or non-pair?) Answer, what ever the first value was!for(int iPos = OrdersTotal()-1; iPos >= 0 ; iPos--) if ( OrderSelect(iPos, SELECT_BY_POS) // Only my orders w/ && OrderType() == OP_BUY && OrderMagicNumber() == Magic.Number // my magic number && OrderSymbol() == chart.symbol // and my pair. ){ // no CloseBuyOrder needed. Just close right here.
- Always test return codes
If you don't get the message, then you're not even reaching the close.
-
After the first iteration, what will be the value of CloseBuyOrder when it encounters other orders (non-buy, non-MN, or non-pair?) Answer, what ever the first value was!
Thanks for the reply WHRoeder, with respect to point 2 in your reply, where is do I place the closing criterion (CloseBuy1_1.......)????
where is do I place the closing criterion (CloseBuy1_1.......)????
bool closeBuys = ..., closeSells= ...; if (closeBuys) CloseAllOrders(OP_BUY); if (closeSells) CloseAllOrders(OP_SELL); //or if (closeBuys || closeSells) CloseAllOrders(); : void CloseAllOrders(int op = -1){ for(int iPos = OrdersTotal()-1; iPos >= 0 ; iPos--) if ( OrderSelect(iPos, SELECT_BY_POS) // Only my orders w/ && OrderMagicNumber() == Magic.Number // my magic number && OrderSymbol() == chart.symbol // and my pair, && (op < 0 || op == OrderType()) // and wanted type. ){ // Don't combine with &&'s Compiler bug. CloseOrder(); } } void CloseOrder(){ if (!OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), Slippage.Pips*pips2points, op.color) ) Alert("OrderClose failed: ", GetLastError()); }

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Help coders, my EA has no trouble opening trades but doesn't close any. I suspect the trouble is in my OrderSelect loop but i can't figure out what's wrong. HELP!!!!