EMA EA problem at closing order
OrderClose(buy_ticket,lotSize, PRICE_CLOSE,slippage,clrNONE);
PRICE_CLOSE is the constant zero. Use a real price.
You would have known that had you Check your return codes, and report your errors (including market prices and your variables). Don't look at GLE/LE unless you have an error. Don't just silence the compiler (MT5 / MT4+strict), it is trying to help you.
What are Function return values ? How do I use them ? - MQL4 programming forum (2012)
Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles (2014)
PRICE_CLOSE is the constant zero. Use a real price.
You would have known that had you Check your return codes, and report your errors (including market prices and your variables). Don't look at GLE/LE unless you have an error. Don't just silence the compiler (MT5 / MT4+strict), it is trying to help you.
What are Function return values ? How do I use them ? - MQL4 programming forum (2012)
Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles (2014)
Hi,
Thanks for replying i solved the issue.
To close a buy order one must close it at bid price. To close a sell order one must close it at ask price. That means we rewrite the code : if(buying_order && currentFastEMA < currentSlowEMA) //closing buy order { OrderClose(buy_ticket,lotSize, **Bid**,slippage,clrNONE); buying_order=false; } if(selling_order && currentFastEMA > currentSlowEMA) //closing sell order { OrderClose(sell_ticket,lotSize,**Ask**,slippage,clrNONE); selling_order=false; }
if(buying_order && currentFastEMA < currentSlowEMA) //closing buy order { OrderClose(buy_ticket,lotSize, **Bid**,slippage,clrNONE); buying_order=false;
EAs must be coded to recover. If the power fails, OS crashes, terminal or chart is accidentally closed, on the next tick, any static / global ticket variables will have been lost. You will have an open order / position but don't know it, so the EA will never try to close it, trail SL, etc. How are you going to recover?
Use a OrderSelect / Position select loop on the first tick, or persistent storage (GV+flush or files) of ticket numbers required.
Hi
I agree with William Roeder above, instead of remembering the tickets, create a functions that will close only trades in chosen direction, so when you detect the signal for closing. Just call CloseOrders(OP_BUY) or CloseOrders(OP_SELL) and those would close all of your buy or sell trades opened by the EA (identified by magic number). For example:
void CloseOrders(int Type) { int i=OrdersTotal(); datetime l_datetime = TimeCurrent(); bool closed = false; while (i >=0) { if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { if( OrderMagicNumber()==magicNumber&& OrderType() == Type) { if (OrderSymbol() == Symbol()){ l_datetime = TimeCurrent(); closed = false; int cnt =0; int ticket = OrderTicket(); while(!closed && TimeCurrent() - l_datetime < 60 && cnt<20 && !IsStopped()) { closed = OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 100); err = GetLastError(); if (err == 150) return; Sleep(1000); while (IsTradeContextBusy() && !IsStopped()) Sleep(1000); RefreshRates(); cnt++; } } } } i--; } }
Have a nice weekend
Hello,
Thank you for your suggestions I noticed there was a problem this week with closing orders considering that I'm operating on 2 computers. I'm confused on how to use your closeorder function, i should implement it in a separate EA or my main EA ?
Hi
Use this in your ea. Copy the function in your code and in your main function instead of those “OrderClose” functions you have - you can call this one:
If(OrdersTotal()==0){ //code for entry } else{ //àclose trades if(currentFastEMA < currentSlowEMA) { CloseOrders(OP_BUY) } else{ CloseOrders(OP_SELL); } }
You don’t need those additional conditins then with buyng_order and sellng_order.
Best Regards
Hi,
I'm trying to build my own EA using EMA. When the program starts it will buy/sell according to the relative position of slow and fast EMA. Then the program should close the trade at the next EMA crossover. My first obstacle was to find a way to calculate EMA at the closing of candlestick, so I asked chatgpt to help me. After lots of attempts he brought me something using currenTime and prevTime values + the if condition. The calculations work even though i don't really understand why. I would be glad if someone can explain it to me. Now i'm struggling at closing my orders, when printing the EMA values I see the crossover but the program still won't close the trade. I'm sharing the code, i am backtesting it on GBP/JPY on M15 from 24 apr to 25 apr, there are 2 crossovers (picture), the program should run 3 trades on that day. Any help? here's my code below.