Since you didn't provide any code, I'll not aswell - if you want some code, show your attempt and we may guide you through the right path.
However, let's take a look at your problem. When you code something, try to always think algorithmically - that is, set a goal and divide the problem into parts to be solved in an easier way. Something like this:
Objective: 'pause' the EA for a certain amount of time when a certain profit/loss is reached.
Things to do:
- check periodically for the profit/loss;
- if a certain profit/loss was reached, do not send any orders. 'Pausing', as you referred, just corresponds to do nothing;
- fetch the last trade date and define how much time since the last trade must have been passed to start trading again;
- check periodically if the defined X time has passed since the last trade;
- go back to step 1.
Considering this, I would take a look at the following functions in the documentation:
- HistorySelect (to define the time range and retrieve the history deal info)
- HistoryDealGetTicket (to get the ticket to a deal)
- HistoryDealGetDouble (to get the profit)
- HistoryDealGetInteger (to get the last trade time)
Hello. Thank you for your response. i am sorry I did not provide the code earlier. Please see below
#include <Trade\Trade.mqh> #include <Trade\DealInfo.mqh> //------- CTrade trade; CDealInfo m_deal; //-------- bool m_need_close_all_buy = false; bool m_need_close_all_sell = false; bool m_need_delete_all_buy = false; bool m_need_delete_all_sell = false; //-------- input double FirstLot = 0.01; input int TakeProfitPoints = 20; input int GridLevelBuy = 3; input int GridLevelSell = 3; input int GridDistancePoints = 20; input int InpMagic = 123456; //------- void OnInit() { trade.SetExpertMagicNumber(InpMagic); } //------- void OnDeinit(const int reason) { } //------- void OnTick() { if(PositionsTotal() == 0 && OrdersTotal() == 0) { Sleep(2000); OpenNewTrades(); } if(PositionsTotal() >= 1 && OrdersTotal() == 0) { closeLastTrades(); } } //+------------------------------------------------------------------+ //| TradeTransaction function | //+------------------------------------------------------------------+ void OnTradeTransaction(const MqlTradeTransaction &trans, const MqlTradeRequest &request, const MqlTradeResult &result) { //--- get transaction type as enumeration value ENUM_TRADE_TRANSACTION_TYPE type=trans.type; //--- if transaction is result of addition of the transaction in history if(type==TRADE_TRANSACTION_DEAL_ADD) { if(HistoryDealSelect(trans.deal)) { m_deal.Ticket(trans.deal); if(m_deal.DealType() == DEAL_TYPE_SELL && (m_deal.Entry() == DEAL_ENTRY_OUT || m_deal.Entry() == DEAL_ENTRY_INOUT)) { long deal_reason = -1; if(m_deal.InfoInteger(DEAL_REASON, deal_reason)) { if((ENUM_DEAL_REASON)deal_reason == DEAL_REASON_TP) { DeleteAllPendingBuyOrders(); } } } if(m_deal.DealType() == DEAL_TYPE_BUY && (m_deal.Entry() == DEAL_ENTRY_OUT || m_deal.Entry() == DEAL_ENTRY_INOUT)) { long deal_reason = -1; if(m_deal.InfoInteger(DEAL_REASON, deal_reason)) { if((ENUM_DEAL_REASON)deal_reason == DEAL_REASON_TP) { DeleteAllPendingSellOrders(); } } } } } } //+------------------------------------------------------------------+ //| Close all positions | //+------------------------------------------------------------------+ void CloseAllBuyPositions(void) { int totalPositions = PositionsTotal(); for(int i = totalPositions - 1; i >= 0; i--) { ulong ticket = PositionGetTicket(i); string symbol = PositionGetString(POSITION_SYMBOL); if(PositionGetInteger(POSITION_MAGIC) == InpMagic && PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY && symbol == _Symbol) { trade.PositionClose(ticket); } } } void CloseAllSellPositions(void) { int totalPositions = PositionsTotal(); for(int i = totalPositions - 1; i >= 0; i--) { ulong ticket = PositionGetTicket(i); string symbol = PositionGetString(POSITION_SYMBOL); if(PositionGetInteger(POSITION_MAGIC) == InpMagic && PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL && symbol == _Symbol) { trade.PositionClose(ticket); } } } //+------------------------------------------------------------------+ //| Delete all pending orders | //+------------------------------------------------------------------+ void DeleteAllPendingBuyOrders(void) { int totalOrders = OrdersTotal(); for(int i = OrdersTotal()-1; i >= 0; i--) { ulong ticket = OrderGetTicket(i); string symbol = OrderGetString(ORDER_SYMBOL); if(OrderGetInteger(ORDER_MAGIC) == InpMagic && OrderGetInteger(ORDER_TYPE) == ORDER_TYPE_BUY_LIMIT && symbol == _Symbol) { trade.OrderDelete(ticket); } } } //+------------------------------------------------------------------+ //| Delete all pending orders | //+------------------------------------------------------------------+ void DeleteAllPendingSellOrders(void) { int totalOrders = OrdersTotal(); for(int i = OrdersTotal()-1; i >= 0; i--) { ulong ticket = OrderGetTicket(i); string symbol = OrderGetString(ORDER_SYMBOL); if(OrderGetInteger(ORDER_MAGIC) == InpMagic && OrderGetInteger(ORDER_TYPE) == ORDER_TYPE_SELL_LIMIT && symbol == _Symbol) { trade.OrderDelete(ticket); } } } //+------------------------------------------------------------------+ void OpenNewTrades() { double bid = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_BID), _Digits); double ask = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_ASK), _Digits); double tpBuy, tpSell, buyLimitPrice, sellLimitPrice; double lotSizeMultiplier = 2; int gridDistanceMultiplier = 1; // Calculate take profit for initial buy and sell positions tpBuy = bid + TakeProfitPoints * _Point; tpSell = ask - TakeProfitPoints * _Point; // Place initial buy position with take profit trade.Buy(FirstLot, _Symbol, bid, 0, tpBuy); // Place initial sell position with take profit trade.Sell(FirstLot, _Symbol, ask, 0, tpSell); // Place buy limit orders for(int i = 0; i < GridLevelBuy; i++) { buyLimitPrice = ask - (GridDistancePoints * (i + 1)) * _Point; tpBuy = buyLimitPrice + TakeProfitPoints * _Point; double lotSize = FirstLot * lotSizeMultiplier; trade.BuyLimit(lotSize, buyLimitPrice, _Symbol, 0, tpBuy); lotSizeMultiplier *= 2; // Double lot size multiplier for next iteration // Update gridDistanceMultiplier logic if(i >= 3) { if(i % 2 == 1) // For odd iterations gridDistanceMultiplier *= 3; else // For even iterations gridDistanceMultiplier += GridDistancePoints; } } // Reset variables for placing sell limit orders lotSizeMultiplier = 2; gridDistanceMultiplier = 1; // Place sell limit orders for(int i = 0; i < GridLevelSell; i++) { sellLimitPrice = bid + (GridDistancePoints * (i + 1)) * _Point; tpSell = sellLimitPrice - TakeProfitPoints * _Point; double lotSize = FirstLot * lotSizeMultiplier; trade.SellLimit(lotSize, sellLimitPrice, _Symbol, 0, tpSell); lotSizeMultiplier *= 2; // Double lot size multiplier for next iteration // Update gridDistanceMultiplier logic if(i >= 3) { if(i % 2 == 1) // For odd iterations gridDistanceMultiplier *= 3; else // For even iterations gridDistanceMultiplier += GridDistancePoints; } } } void closeLastTrades() { int totalPositions = PositionsTotal(); for (int i = 0; i < totalPositions; i++) { ulong ticket = PositionGetTicket(i); string symbol = PositionGetString(POSITION_SYMBOL); if (PositionGetInteger(POSITION_MAGIC) == InpMagic && symbol == _Symbol && (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY || PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)) { trade.PositionClose(ticket); Sleep(60000); } } }
As you can see from the code above, it's a martingale system that opens several grids and I use the OnTradeTransaction to delete the remaining pending orders as soon as TP is hit. I have discovered that sometimes a position will have made a profit only for the market to reverse so apart from the onTradeTransaction closing the trading session, I want to have another function that checks the equity with every tick and then closes the trades as soon as the set profit is hit. After which the EA should reset, open new trades and use the current balance as starting equity for the new set of trades
Hi
You can try something like this:
void OnTick() { if(AccountInfoDouble(ACCOUNT_EQUITY)-AccountInfoDouble(ACCOUNT_BALANCE)>=CHOSEN_PROFIT_TO_CLOSE){ //close all posiotions and orders //CloseAllBuyPositions, CloseAllSellPositions, DeleteAllBuy.... return; } if(PositionsTotal() == 0 && OrdersTotal() == 0) { Sleep(2000); OpenNewTrades(); } if(PositionsTotal() >= 1 && OrdersTotal() == 0) { closeLastTrades(); } }
Simply check if profit was reached and close all trades. You can add here some time “block” after closing or not – then new trades will open normally just after closing (if EA allows).
Best Regards
Hi
You can try something like this:
Simply check if profit was reached and close all trades. You can add here some time “block” after closing or not – then new trades will open normally just after closing (if EA allows).
Best Regards
Thank you all for your assistance. Much love.

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Dear All,
I am still a beginner in MQL5. I want to add a function of pausing my EA for a specified time after a set profit target is made. I am totally clueless as to how to achieve this. For instance, I want to place several buy trades at different times and I want the EA to keep monitoring my current account balance and to close all positions as soon as my current account balance exceeds my initial account balance by a specified amount. Please somebody, help