Questions from Beginners MQL5 MT5 MetaTrader 5 - page 697

 
yaaarik777:

Good day everyone! Can you please advise how to set the deletion of an order in an EA?

The situation is as follows:

We place 2 pending orders in different directions, as soon as one of them triggers, the other is removed and is no longer exposed.

I would be very grateful for any help.

Thank you.

There are several ways to do this. One of them is usingOnTradeTransaction.

In catching the transaction

TRADE_TRANSACTION_DEAL_ADD

Add the transaction to the history. Performed as a result of order execution or account balance transactions.

and make sure that this transaction is an entry into the market:

DEAL_ENTRY_IN

Market entry


//+------------------------------------------------------------------+
//| 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)
     {
      long     deal_entry        =0;
      long     deal_type         =0;
      string   deal_symbol       ="";
      long     deal_magic        =0;
      if(HistoryDealSelect(trans.deal))
        {
         deal_entry=HistoryDealGetInteger(trans.deal,DEAL_ENTRY);
         deal_type=HistoryDealGetInteger(trans.deal,DEAL_TYPE);
         deal_symbol=HistoryDealGetString(trans.deal,DEAL_SYMBOL);
         deal_magic=HistoryDealGetInteger(trans.deal,DEAL_MAGIC);
        }
      else
         return;
      if(deal_symbol==Symbol() && deal_magic==m_magic)
         if(deal_entry==DEAL_ENTRY_IN)
           {
            CloseAll();
           }
     }
  }

This transaction will take place after the pending order has triggered and the trade will be logged in the history.

Once we have caught this transaction, we only need to delete all of our remaining pending orders:

//+------------------------------------------------------------------+
//| Close All Orders                                                 |
//+------------------------------------------------------------------+
void CloseAll()
  {
   Print(__FUNCTION__);
   for(int i=OrdersTotal()-1;i>=0;i--) // returns the number of open positions
      if(m_order.SelectByIndex(i))
         if(m_order.Symbol()==Symbol() && m_order.Magic()==m_magic)
            m_trade.OrderDelete(m_order.Ticket());
  }

Common EA code (only here I have specified magic equal to "0" - you should set your own magic):

//+------------------------------------------------------------------+
//|                                       There will be only one.mq5 |
//|                              Copyright © 2016, Vladimir Karputov |
//|                                           http://wmua.ru/slesar/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2016, Vladimir Karputov"
#property link      "http://wmua.ru/slesar/"
#property version   "1.00"
#include <Trade\Trade.mqh>
#include <Trade\OrderInfo.mqh>
CTrade         m_trade;                      // trading object
COrderInfo     m_order;                      // pending orders object
//---
ulong          m_magic=0;                // magic number
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---

  }
//+------------------------------------------------------------------+
//| 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)
     {
      long     deal_entry        =0;
      long     deal_type         =0;
      string   deal_symbol       ="";
      long     deal_magic        =0;
      if(HistoryDealSelect(trans.deal))
        {
         deal_entry=HistoryDealGetInteger(trans.deal,DEAL_ENTRY);
         deal_type=HistoryDealGetInteger(trans.deal,DEAL_TYPE);
         deal_symbol=HistoryDealGetString(trans.deal,DEAL_SYMBOL);
         deal_magic=HistoryDealGetInteger(trans.deal,DEAL_MAGIC);
        }
      else
         return;
      if(deal_symbol==Symbol() && deal_magic==m_magic)
         if(deal_entry==DEAL_ENTRY_IN)
           {
            CloseAll();
           }
     }
  }
//+------------------------------------------------------------------+
//| Close All Orders                                                 |
//+------------------------------------------------------------------+
void CloseAll()
  {
   Print(__FUNCTION__);
   for(int i=OrdersTotal()-1;i>=0;i--) // returns the number of open positions
      if(m_order.SelectByIndex(i))
         if(m_order.Symbol()==Symbol() && m_order.Magic()==m_magic)
            m_trade.OrderDelete(m_order.Ticket());
  }
//+------------------------------------------------------------------+
 

Another question about the OnDeinit event:

this code will work:

void OnDeinit(const int reason)

{

if(reason==REASON_REMOVE)

{

code to close the position(if any)

}

}

Generally, we need to know if it is possible to close the position in OnDeinit event handling

 

Good afternoon experts, please share your experiences.

There is an array of values, it reflects some object/field, i.e. if there is a task to designate this object/field in some way, what are the possible actions.

For example, would it be appropriate to put this array into Kohonen map network, if the volume of the array can reach thousands of cells of x and y?

Maybe you can suggest how to stick one of the two-dimensional arrays (with large dimensionality) into a Kohonen network.

And whether Kohon is suitable for analysis - recursion of arrays (arrays in an array).

Or maybe someone can help put the right questions


1 1 1 1 1 2 4 6 8
1 1 1 1 2 3 5 7 9
1 1 1 1 3 4 6 8 10
1 1 1 2 4 5 7 9 11
1 1 1 3 5 6 8 10 12
1 1 1 4 6 7 9 11 13
1 1 1 5 7 8 10 12 14
1 1 2 6 8 9 11 13 15
1 1 3 7 9 10 12 14 16
1 1 4 8 10 11 13 15 17
1 2 5 9 11 12 14 16 18
1 3 6 10 12 13 15 17 19
1 4 7 11 13 14 16 18 20
1 5 8 12 14 15 17 19 21
1 6 9 13 15 16 18 20 22
2 7 10 14 16 17 19 21 1
3 8 11 15 17 18 20 22 1
4 9 12 16 18 19 21 1 1
5 10 13 17 19 20 22 1 1
6 11 14 18 20 21 1 1 1
7 12 15 19 21 22 1 1 1
8 13 16 20 22 1 1 1 1
9 14 17 21 1 1 1 1 1
10 15 18 22 1 1 1 1 1
 
Vladimir Karputov:

There are several ways of doing this. One is to useOnTradeTransaction.

In we catch the transaction

TRADE_TRANSACTION_DEAL_ADD

Add the transaction to the history. Performed as a result of order execution or account balance transactions.

and make sure that this transaction is an entry into the market:

DEAL_ENTRY_IN

Market entry


//+------------------------------------------------------------------+
//| 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)
     {
      long     deal_entry        =0;
      long     deal_type         =0;
      string   deal_symbol       ="";
      long     deal_magic        =0;
      if(HistoryDealSelect(trans.deal))
        {
         deal_entry=HistoryDealGetInteger(trans.deal,DEAL_ENTRY);
         deal_type=HistoryDealGetInteger(trans.deal,DEAL_TYPE);
         deal_symbol=HistoryDealGetString(trans.deal,DEAL_SYMBOL);
         deal_magic=HistoryDealGetInteger(trans.deal,DEAL_MAGIC);
        }
      else
         return;
      if(deal_symbol==Symbol() && deal_magic==m_magic)
         if(deal_entry==DEAL_ENTRY_IN)
           {
            CloseAll();
           }
     }
  }

This transaction will take place after the pending order has triggered and the trade will be logged in the history.

Once we have caught this transaction, we only need to delete all outstanding pending orders:

//+------------------------------------------------------------------+
//| Close All Orders                                                 |
//+------------------------------------------------------------------+
void CloseAll()
  {
   Print(__FUNCTION__);
   for(int i=OrdersTotal()-1;i>=0;i--) // returns the number of open positions
      if(m_order.SelectByIndex(i))
         if(m_order.Symbol()==Symbol() && m_order.Magic()==m_magic)
            m_trade.OrderDelete(m_order.Ticket());
  }

Generic EA code (only here I have specified magic equal to "0" - you should set your own magic):

//+------------------------------------------------------------------+
//|                                       There will be only one.mq5 |
//|                              Copyright © 2016, Vladimir Karputov |
//|                                           http://wmua.ru/slesar/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2016, Vladimir Karputov"
#property link      "http://wmua.ru/slesar/"
#property version   "1.00"
#include <Trade\Trade.mqh>
#include <Trade\OrderInfo.mqh>
CTrade         m_trade;                      // trading object
COrderInfo     m_order;                      // pending orders object
//---
ulong          m_magic=0;                // magic number
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---

  }
//+------------------------------------------------------------------+
//| 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)
     {
      long     deal_entry        =0;
      long     deal_type         =0;
      string   deal_symbol       ="";
      long     deal_magic        =0;
      if(HistoryDealSelect(trans.deal))
        {
         deal_entry=HistoryDealGetInteger(trans.deal,DEAL_ENTRY);
         deal_type=HistoryDealGetInteger(trans.deal,DEAL_TYPE);
         deal_symbol=HistoryDealGetString(trans.deal,DEAL_SYMBOL);
         deal_magic=HistoryDealGetInteger(trans.deal,DEAL_MAGIC);
        }
      else
         return;
      if(deal_symbol==Symbol() && deal_magic==m_magic)
         if(deal_entry==DEAL_ENTRY_IN)
           {
            CloseAll();
           }
     }
  }
//+------------------------------------------------------------------+
//| Close All Orders                                                 |
//+------------------------------------------------------------------+
void CloseAll()
  {
   Print(__FUNCTION__);
   for(int i=OrdersTotal()-1;i>=0;i--) // returns the number of open positions
      if(m_order.SelectByIndex(i))
         if(m_order.Symbol()==Symbol() && m_order.Magic()==m_magic)
            m_trade.OrderDelete(m_order.Ticket());
  }
//+------------------------------------------------------------------+

Thank you, but how should it be implemented in MQL4? I apologize for not specifying right away.

 
yaaarik777:

Thank you, but how do I format it for MQL4? I apologize for not being more specific right away.

You don't. I do not provide consultations on the old language and terminal. Ask your questions about the old terminal in the section:MQL4 and MetaTrader 4 (22693)
 

Why do I catch an error in the stock and futures market and everything is fine in forex and commodities?

failed sell limit 1.00 USDRUB_TOM at 63.8679 sl: 63.8829 tp: 63.8329 [Invalid expiration]

 
-Aleks-:

Why do I catch an error in the stock and futures market and everything is fine in forex and commodities?

failed sell limit 1.00 USDRUB_TOM at 63.8679 sl: 63.8829 tp: 63.8329 [Invalid expiration]

you need to add a parameter

request.type_time=ORDER_TIME_DAY;
 

Hello colleagues, I have a question about mql5:

How to calculate the profit on positions opened today (if the position was opened yesterday and closed today, then this position is not taken into account) ????

 
Dmitry Melnichenko:

Hello colleagues, I have a question about mql5:

How to calculate the profit on positions opened today (if the position was opened yesterday and closed today, then this position is not taken into account) ????

Obviously by comparing the time the position was opened with the time of the beginning of the day.
 
Vitalie Postolache:
Obviously, by comparing the time the position was opened with the time of the beginning of the day.
But if the position was opened yesterday and closed today, then the history will show a Transaction with the opening time today, it just closes yesterday's position! Or am I wrong?
Reason: