How do I delete open positions on take profit and add trade comments for EA in MQL5

 

Hi, I am still quite new to MQL5 programming and I am stuck writing the code for a grid EA. Basically, I want the EA to do the following:

Step 1. If there are no open positions and pending orders, the EA should call the OpenNewTrade() function

Step 2. While there are open positions or pending orders, the EA should close all other sell positions and delete all buy limit orders whenever a sell position hits take profit. Also, it should close all other buy positions and delete all sell limit orders whenever a buy position hits take profit.

Step 3. If step 2 above is properly executed, there will be no more open positions and pending orders, therefore, step 1 gets called again in a loop.


PROBLEM

==========

All functions work fine except that the function to close positions as in step 2 above isn't working properly so I don't know if there is an error in the logic of the code. For instance, whenever a sell position hits take profit, the buy limit orders are deleted correctly but the other sell position(s) gets deleted along with buy position(s). this is not supposed to be so as I want the EA to function strictly according to step 2. Below is the code for the EA:

#include <Trade\PositionInfo.mqh>
#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>
#include <Trade\AccountInfo.mqh>
#include <Trade\DealInfo.mqh>
#include <Trade\OrderInfo.mqh>
#include <Expert\Money\MoneyFixedMargin.mqh>
//-------
CPositionInfo  m_position;
CTrade         trade;
CSymbolInfo    m_symbol;
CAccountInfo   m_account;
CDealInfo      m_deal;
COrderInfo     m_order;
CMoneyFixedMargin *m_money;
//--------
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(m_need_close_all_buy)
           {
            CloseAllBuyPositions();
           }
         //----
         if(m_need_close_all_sell)
           {
             CloseAllSellPositions();
           }
         //---
         if(m_need_delete_all_buy)
           {
              DeleteAllPendingBuyOrders();
           }
         //---
         if(m_need_delete_all_sell)
           {
              DeleteAllPendingSellOrders();
           }
         //---
         if(PositionsTotal() == 0 && OrdersTotal() == 0)
            {
               OpenNewTrades();
            } 
   }
//+------------------------------------------------------------------+
//| 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)
                      {
                          // Close all sell positions
                          m_need_close_all_sell=true;//---------------------------------------------This is the line of code that wont execute properly
                          // Delete all pending buy orders
                          m_need_delete_all_buy=true;
                      }
                  }
              }
              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)
                      {
                          // Close all buy positions
                          m_need_close_all_buy=true;//---------------------------------------------This is the line of code that wont execute properly
                          // Delete all pending sell orders
                          m_need_delete_all_sell=true;
                      }
                  }
              }            
          }
     }
  }
//+------------------------------------------------------------------+
//| Close all positions                                              |
//+------------------------------------------------------------------+
void CloseAllBuyPositions(void)
  {
   int totalPositions = PositionsTotal();
       for(int i = PositionsTotal()-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 = PositionsTotal()-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);
               }
            }
  }
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;

    // Calculate take profit for initial buy and sell positions
    tpBuy = ask + TakeProfitPoints * _Point;
    tpSell = bid - TakeProfitPoints * _Point;

    // Place initial buy position with take profit
    trade.Buy(FirstLot, _Symbol, ask, 0, tpBuy);
    
    // Place initial sell position with take profit
    trade.Sell(FirstLot, _Symbol, bid, 0, tpSell);

    // Place buy limit orders
    for(int i = 0; i < GridLevelBuy; i++)
    {
        buyLimitPrice = ask - (GridDistancePoints * (i + 1)) * _Point;
        tpBuy = buyLimitPrice + TakeProfitPoints * _Point;
        trade.BuyLimit(FirstLot * MathPow(2, i), buyLimitPrice, _Symbol, 0, tpBuy);
    }

    // Place sell limit orders
    for(int i = 0; i < GridLevelSell; i++)
    {
        sellLimitPrice = bid + (GridDistancePoints * (i + 1)) * _Point;
        tpSell = sellLimitPrice - TakeProfitPoints * _Point;
        trade.SellLimit(FirstLot * MathPow(2, i), sellLimitPrice, _Symbol, 0, tpSell);
    }
   }
How to Order a Trading Robot in MQL5 and MQL4
How to Order a Trading Robot in MQL5 and MQL4
  • www.mql5.com
"Freelance" is the largest freelance service for ordering MQL4/MQL5 trading robots and technical indicators. Hundreds of professional developers are ready to develop a custom trading application for the MetaTrader 4/5 terminal.
 
Mo Isiak:

Hi, I am still quite new to MQL5 programming and I am stuck writing the code for a grid EA. Basically, I want the EA to do the following:

Step 1. If there are no open positions and pending orders, the EA should call the OpenNewTrade() function

Step 2. While there are open positions or pending orders, the EA should close all other sell positions and delete all buy limit orders whenever a sell position hits take profit. Also, it should close all other buy positions and delete all sell limit orders whenever a buy position hits take profit.

Step 3. If step 2 above is properly executed, there will be no more open positions and pending orders, therefore, step 1 gets called again in a loop.


PROBLEM

==========

All functions work fine except that the function to close positions as in step 2 above isn't working properly so I don't know if there is an error in the logic of the code. For instance, whenever a sell position hits take profit, the buy limit orders are deleted correctly but the other sell position(s) gets deleted along with buy position(s). this is not supposed to be so as I want the EA to function strictly according to step 2. Below is the code for the EA:

Please help correct this code to function as described in step 2 above. Thank you very much in advance.

Also I don't know how to have the EA set its name as trade comments. It will be nice if you could help me with it as I will like the trades from this EA differentiated from trades placed by other EAs. Thank you.