Hi I have trying to close my EA for all open positions as soons as profit reaches to lets say $30.
I created input variable and function but it actually not working. Can someone guide what's missing there.
Hi I have trying to close my EA for all open positions as soons as profit reaches to lets say $30.
I created input variable and function but it actually not working. Can someone guide what's missing there.
input double StopProfit = 30.0; void OnTick() { double profit = AccountInfoDouble(ACCOUNT_EQUITY)-AccountInfoDouble(ACCOUNT_BALANCE); if (profit >= StopProfit) ExpertRemove(); }
I created input variable and function but it actually not working.
-
Do you really want to close the EA? The trade will still be active and the EA not. Perhaps you mean you want to close the trade.
- Do you really expect an answer? There are no mind readers here and our crystal balls are cracked.
“Doesn't work” is meaningless — just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires — meaningless.
How To Ask Questions The Smart Way. (2004)
When asking about code
Be precise and informative about your problem
Nope, I also treid it. Not working.
I also written another code. Compiling fine. But again it's also not working. Not sure what's the challenge
Hi I have trying to close my EA for all open positions as soons as profit reaches to lets say $30.
I created input variable and function but it actually not working. Can someone guide what's missing there.
// Variables double starting_balance; double current_profit; // Initialization function int OnInit() { starting_balance = AccountInfoDouble(ACCOUNT_BALANCE); return(INIT_SUCCEEDED); } // Start function void OnTick() { current_profit = AccountInfoDouble(ACCOUNT_PROFIT); // Close EA if profit reaches the target if(current_profit - starting_balance >= profit_target) { ExpertRemove(); } }Help needed:
-
Do you really want to close the EA? The trade will still be active and the EA not. Perhaps you mean you want to close the trade.
- Do you really expect an answer? There are no mind readers here and our crystal balls are cracked.
“Doesn't work” is meaningless — just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires — meaningless.
How To Ask Questions The Smart Way. (2004)
When asking about code
Be precise and informative about your problem
No, I would like to close my EA and all open trade as soon desired profit booked.
The ExpertRemove(); built-in function should only be called when a for loop functions checks there are no orders running after the target profit is reached.
Another good pratice to do is setting a "offset profit" just in order to "protect" the balance from slippages or even the delay the Terminal takes to close each order individually during the "close_all_orders" process.
//---- The piece of code below:
// Variables double starting_balance; double profit_target; double offset_profit; // Initialization function int OnInit() { starting_balance = AccountInfoDouble(ACCOUNT_BALANCE); return(INIT_SUCCEEDED); } // Start function void OnTick() { check_profit(); } //--- Function triggered when the profit target is reached void check_profit() { double current_equity=AccountEquity(); if(current_equity>=(starting_balance+profit_target+offset_profit))check_close(); } //--- Function to manage the order closing process void check_close() { close_all_orders(); if(check_open_orders()==false) // only if there are no orders open the ExpertRemove() function will be called { Print("Profit target reached. The EA will be removed for safety."); ExpertRemove(); } } } //--- Function to close all orders void close_all_orders() { for(int i=OrdersTotal()-1; i >= 0; i--) { bool res=OrderSelect(i,SELECT_BY_POS,MODE_TRADES); if (OrderType()<=1 && OrderMagicNumber()==MAGICMA) { if(!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,NULL))Print("Order n° "+OrderTicket()+" failed to be closed."); } } } //--- Function to check if there are orders open bool check_open_orders() { for( int i = 0 ; i < OrdersTotal() ; i++ ) { bool res = OrderSelect( i, SELECT_BY_POS, MODE_TRADES ); if (OrderSymbol() == Symbol() && OrderMagicNumber()==MAGICMA) return(true); } return(false); }
{ double equity = AccountInfoDouble(ACCOUNT_EQUITY); double balance = AccountInfoDouble(ACCOUNT_BALANCE); double profit = equity - balance; if (!SoftStopLossReached && profit >= (StopProfit + SoftStopLossBuffer)) { SoftStopLossReached = true; } if (SoftStopLossReached || profit >= HardStopLossLevel) { ExpertRemove(); } }
The ExpertRemove(); built-in function should only be called when a for loop functions checks there are no orders running after the target profit is reached.
Another good pratice to do is setting a "offset profit" just in order to "protect" the balance from slippages or even the delay the Terminal takes to close each order individually during the "close_all_orders" process.
//---- The piece of code below:
Not working
![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
Hi I have trying to close my EA for all open positions as soons as profit reaches to lets say $30.
I created input variable and function but it actually not working. Can someone guide what's missing there.