How to start with MQL5 - page 6

 
Hi, Vladimir
I have a pending order and when it is executed and closed
I want to open a pending order at the exact same price as the previous closed order .. please what function I use to execute the order (MQL5)
thank you so much ..
 
Ahmadahmad654 :
Hi, Vladimir
I have a pending order and when it is executed and closed
I want to open a pending order at the exact same price as the previous closed order .. please what function I use to execute the order (MQL5)
thank you so much ..

An order can only be pending. Therefore, the order cannot be closed - the order can only be deleted.


As I understand it: you are placing a pending order (its price is Price # 1). Then it works -> a position appears. Then the position is closed and you want to place a pending order again at the price Price # 1? Did I understand you correctly?

 
Yes .. after activating the order and closing it on take profit
 
Ahmadahmad654 :
Yes .. after activating the order and closing it on take profit

In this case, you need to work in steps. Step one: catch the moment of closing the transaction.

Transactions can be (by type of entry / exit): ENUM_DEAL_ENTRY

ENUM_DEAL_ENTRY

Identifier

Description

DEAL_ENTRY_IN

Entry in

DEAL_ENTRY_OUT

Entry out

DEAL_ENTRY_INOUT

Reverse

DEAL_ENTRY_OUT_BY

Close a position by an opposite one


We need to catch the deal DEAL_ENTRY_OUT - make it easy in OnTradeTransaction. Procedure: catches the transaction TRADE_TRANSACTION_DEAL_ADD (adding a deal to history ) and see that it is a deal with type DEAL_ENTRY_OUT

Code example:

//+------------------------------------------------------------------+
//|                                       Example DEAL_ENTRY_OUT.mq5 |
//|                        Copyright 2020, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
/*
   barabashkakvn Trading engine 3.124
*/
#include <Trade\SymbolInfo.mqh>
#include <Trade\DealInfo.mqh>
//---
CSymbolInfo    m_symbol;                     // object of CSymbolInfo class
CDealInfo      m_deal;                       // object of CDealInfo class
//--- input parameters
input ulong    InpMagic             = 200;         // Magic number
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   if(!m_symbol.Name(Symbol())) // sets symbol name
     {
      Print(__FILE__," ",__FUNCTION__,", ERROR: CSymbolInfo.Name");
      return(INIT_FAILED);
     }
//---
   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)
     {
      if(HistoryDealSelect(trans.deal))
         m_deal.Ticket(trans.deal);
      else
         return;
      if(m_deal.Symbol()==m_symbol.Name() && m_deal.Magic()==InpMagic)
        {
         if(m_deal.DealType()==DEAL_TYPE_BUY || m_deal.DealType()==DEAL_TYPE_SELL)
           {
            if(m_deal.Entry()==DEAL_ENTRY_OUT)
              {
               Print(m_deal.Time()," | ",m_deal.Symbol()," | ",m_deal.Price());
              }
           }
        }
     }
  }
//+------------------------------------------------------------------+


Please practice starting this advisor.

Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Deal Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Deal Properties
  • www.mql5.com
A deal is the reflection of the fact of a trade operation execution based on an order that contains a trade request. Each trade is described by properties that allow to obtain information about it. In order to read values of properties, functions of the Identifier of a position, in the opening, modification or closing of which this deal took...
 

OK..
Step 1 # Open and execute a pending order at  price# 1
Step 2 #: Close the position at price# 2 (m_deal.Price ())
Step 3 # Open a new pending order at a price #1
Please help me in a step#3

thank you very much.. best wishes

 
Ahmadahmad654 :

OK..
Step 1 # Open and execute a pending order at  price# 1
Step 2 #: Close the position at price# 2 (m_deal.Price ())
Step 3 # Open a new pending order at a price #1
Please help me in a step#3

thank you very much.. best wishes

Yes, of course I will help.

You just say: Have you figured out how the code works in ? Do you understand how to catch a deal closing?

How to start with MQL5
How to start with MQL5
  • 2020.06.24
  • www.mql5.com
This thread discusses MQL5 code examples. There will be examples of how to get data from indicators, how to program advisors...
 

Good day, Mr. Vladimir

The code #54 gives us the transaction information that was closed

But m_deal.Price () == the deal's closing price
Where can I find the deal opening price?
Best regards

 
Ahmadahmad654 :

Good day, Mr. Vladimir

The code #54 gives us the transaction information that was closed

But m_deal.Price () == the deal's closing price
Where can I find the deal opening price ?
Best regards

When a pending order is triggered, a deal of the type 'DEAL_ENTRY_IN' appears.

We’ll slightly change the OnTradeTransaction procedure, now we can catch two deals: enter the market (DEAL_ENTRY_IN) and exit the market (DEAL_ENTRY_OUT).

//+------------------------------------------------------------------+
//| 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);
      else
         return;
      if(m_deal.Symbol()==m_symbol.Name() && m_deal.Magic()==InpMagic)
        {
         if(m_deal.DealType()==DEAL_TYPE_BUY || m_deal.DealType()==DEAL_TYPE_SELL)
           {
            if(m_deal.Entry()==DEAL_ENTRY_IN)
              {
               string entry="in";
               Print(m_deal.Time()," | ",m_deal.Symbol()," | ",entry," | ",m_deal.Price());
              }
            if(m_deal.Entry()==DEAL_ENTRY_OUT)
              {
               string entry="out";
               Print(m_deal.Time()," | ",m_deal.Symbol()," | ",entry," | ",m_deal.Price());
              }
           }
        }
     }
  }
 
Thank you very much for helping me
 
Ahmadahmad654 :
Thank you very much for helping me

This is only one way - catching online transactions.

And there is at least a way to work with trading history. You can determine what led to the 'OUT' deal - Stop Loss triggering or Take Profit triggering ...

But at this stage, I think you just need to use the idea from  

How to start with MQL5
How to start with MQL5
  • 2020.06.24
  • www.mql5.com
This thread discusses MQL5 code examples. There will be examples of how to get data from indicators, how to program advisors...
Reason: