getting Last Closed Order in MQL5 - page 2

To add comments, please log in or register
Vladimir Karputov
Moderator
192645
Vladimir Karputov  
Amirfakhredin Ghanbari :


I propose a solution through 'OnTradeTransaction': as soon as the transaction 'TRADE_TRANSACTION_DEAL_ADD' is caught -> we turn to the trading history 'HistoryDealSelect' -> if this is the deal 'DEAL_TYPE_BUY' or 'DEAL_TYPE_SELL' -> print out the data:

//+------------------------------------------------------------------+
//|                                                    Last Deal.mq5 |
//|                              Copyright © 2020, Vladimir Karputov |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2020, Vladimir Karputov"
#property version   "1.000"
/*
   barabashkakvn Trading engine 3.118
*/
#include <Trade\DealInfo.mqh>
//---
CDealInfo      m_deal;                       // object of CDealInfo class
//+------------------------------------------------------------------+
//--- input parameters
input int      Input1=9;
//+------------------------------------------------------------------+
//| 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)
     {
      if(HistoryDealSelect(trans.deal))
         m_deal.Ticket(trans.deal);
      else
         return;
      if(m_deal.DealType()==DEAL_TYPE_BUY || m_deal.DealType()==DEAL_TYPE_SELL)
        {
         //---
         string deal_type=(m_deal.DealType()==DEAL_TYPE_BUY)?"buy":"sell";

         string deal_direction;
         switch(m_deal.Entry())
           {
            case  DEAL_ENTRY_IN:
               deal_direction="in";
               break;
            case  DEAL_ENTRY_OUT:
               deal_direction="in";
               break;
            default:
               deal_direction="-/-";
               break;
           }

         long digits=5;
         if(SymbolSelect(m_deal.Symbol(),true))
            digits=SymbolInfoInteger(m_deal.Symbol(),SYMBOL_DIGITS);

         string headline="| Time               | Symbol    | Deal      | Order     | Type      | Direction | Volume    | Price     |";
         string line=StringFormat("|%-19s | %-9s | %-9d | %-9d | %-9s | %-9.2s | %-9.2f | %-9."+IntegerToString(digits)+"f |",
                                  TimeToString(m_deal.Time(),TIME_DATE|TIME_SECONDS),
                                  m_deal.Symbol(),
                                  m_deal.Ticket(),
                                  m_deal.Order(),
                                  deal_type,
                                  deal_direction,
                                  m_deal.Volume(),
                                  m_deal.Price()
                                 );
         Print(headline+"\n"+line);
        }
     }
  }
//+------------------------------------------------------------------+


Result:

and


Files:
WindmillMQL
163
WindmillMQL  
Amirfakhredin Ghanbari:

seems i cant make this work all i get is this error below!


'HistoryDealGetInteger' - no one of the overloads can be applied to the function call


Can you try whether the following code works for you?

void playWithHistory(){
   HistorySelect(0,TimeCurrent());
   int TotalDeals = HistoryDealsTotal();
   datetime LatestDeal = 0;
   ulong LatestTicket = 0;
   for(int i = 0; i < TotalDeals;i++){
      ulong Ticket = HistoryDealGetTicket(i);
      if(HistoryDealGetInteger(Ticket,DEAL_ENTRY) == DEAL_ENTRY_OUT){
         datetime DealTime = HistoryDealGetInteger(Ticket,DEAL_TIME);
         if(DealTime > LatestDeal){
            LatestDeal = DealTime;
            LatestTicket = Ticket;
         }
      }
   }
   if(LatestTicket > 0){
      double LatestProfit = HistoryDealGetDouble(LatestTicket, DEAL_PROFIT);
      Alert(LatestTicket," Profit of latest deal: ",DoubleToString(LatestProfit,2));
   }
   else Alert("No ticket found.");
}//end function


This code is not optimised, just thrown together as experiment. I think it works for me.

12
To add comments, please log in or register