Do your signals to close the trades occur ? Do seelTrade and/or buyTrade ever = true ? why don't you print these variables so you can see what is happening ?
A few other comments:
if you are using MarketInfo instead of PredefinedVariables you don't need to use RefreshRates, RefreshRates refreshes the PredefinedVariables.
You don't need the NormalizeDouble, Ask is already Normalized.
Does your OrderSelect work ? check it's return value . . .
Why aren't you testing the return value from the OrderSend ? if it fails don't you want to know ?
You set sellTrade = false and buyTrade = false even if you don't close any trades . . . you don't check if there are any trades left open.
for (int i = OrdersTotal() - 1; i >= 0; i--) { OrderSelect(i, SELECT_BY_TICKET); if ( OrderType() == OP_BUY ) { int OldTicket=OrderTicket(); double lots=OrderLots();} RefreshRates(); //OrderSelect() needed ? double symAsk = NormalizeDouble( MarketInfo( Sym, MODE_ASK ), SymDigits ); bool closeSellOrder = OrderClose( OldTicket, lots, symAsk, 0, CLR_NONE );
- if the type is a buy you set the ticket/size to pass to orderClose. What are you passing if it's NOT a buy to close? What if there are multiple open orders?
- You open a buy at the Ask, and close at the Bid. Why are you trying to close at the Ask?
- RefreshRates is unnecessary unless you use Bid/Ask and have multiple server calls.
- Why are you not checking the OrderClose, return code and printing a message on failure so you find out WHY.
- If the OrderSelect fails so does everything there after. Always test return codes
- Why are you selecting by ticket when you're passing a position index.
- Not filtering by magic number means the EA in incompatible with every other, including itself on other charts.
- it is never necessary to use normalizeDouble, it's a kludge, don't use it, ever. ref1 ref2
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. && OrderType() == OP_BUY ){ bool closeSellOrder = OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(), 0, CLR_NONE ); if ( !closeSellOrder) Alert("OrderClose failed: ", GetLastError());
Thanks you guys, I will look into every one of your comments/suggestions. Looks like I got some work to do!
BTW, I actually have written an entire function solely dedicated for opening/closing errors, I just haven't incorporated it into my EA yet.
For the most part, don't errors mainly occur in live trading and not so much in backtesting?
I figured I really don't need the error function now since I am only using the EA for backtesting at this point.
For the most part, don't errors mainly occur in live trading and not so much in backtesting?
I figured I really don't need the error function now since I am only using the EA for backtesting at this point.
Ok, I got working after hours and hours of racking my brain on it! LOL.
Now i just need to get the signal timing improved and filter out unwanted trades.
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- 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 am working on an EA, but no matter what I do, I can't make it close trades. It opens them just fine, but when I want them closed, the EA will not close them.
I'll post part of my EA code so maybe one of you experienced coders can figure out why I'm having this problem. The vars 'buyTrade' and 'sellTrade' are global to whole EA.
NOTE: I only want ONE trade open at any given time, either long or short. The trades are "always in" so when a long signal comes, the existing short should be closed and vice versa.
It is NOT doing this. What it is doing is opening trades and then opens more thrades ... and opens more ... and keeps them open until they all eventually hit their stops.
Thanks in advance.