Hello all,
I want to build an EA for this purpose, "when account equity reach to defined value by user, close all positions of all symbols",
I write below code, but it doesn't work.
Please help me for modify it.
Thanks.
Try this.
#property strict #include <stdlib.mqh> input double TargetEquity = 5000.00; input int Attempts = 10; void OnTick() { if(AccountEquity() >= TargetEquity) for(int i=0; i<Attempts; i++) if(close_all()) goal_achieved(); } bool close_all() { for(int i=OrdersTotal()-1; i>=0; i--) { if(OrderSelect(i,SELECT_BY_POS) && OrderSymbol() == Symbol() && OrderType() < 2 //is a live trade, i.e. Buy or Sell ){ RefreshRates(); int ticket = OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(), 5); if(ticket < 0) { Print("OrderCloseError: ", ErrorDescription(GetLastError())); return false; } } } return true; } void goal_achieved() { Alert("Equity reached and all positions closed."); ExpertRemove(); }
Try this.
Thanks for your reply, But in my MT4 terminal this code doesn't work.
Did you test it?
maybe my mt4 has problem.
Thanks for your reply, But in my MT4 terminal this code doesn't work.
Did you test it?
maybe my mt4 has problem.
What that means "doesn't work" ?
- ronwhitman.com
What that means "doesn't work" ?
I compile it but in live chart can't close "open positions".
I compile it but in live chart can't close "open positions".
You repeat exactly the same with other words.
Give more information ! Not words.
For example the logs (Experts tab) or eventually screenshots.
- www.chiark.greenend.org.uk
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello all,
I want to build an EA for this purpose, "when account equity reach to defined value by user, close all positions of all symbols",
I write below code, but it doesn't work.
Please help me for modify it.
Thanks.