Expert Advisors • Indicators • Scripts • Libraries

MQL.RobotFX.org is the biggest collection of MetaTrader expert advisors (MT5 & MT4), indicators, scripts and libraries that can be used to improve trading results, minimize risks or simply automate trading tasks

MetaTrader 5 Libraries | CHistoryPositionInfo Class

MetaTrader Experts, Indicators, Scripts and Libraries

CHistoryPositionInfo class provides an easy access to the closed position properties.

The class has a similar interface to CPositionInfo class of the standard library.

class CHistoryPositionInfo : public CObject    {  public:                       CHistoryPositionInfo(void);                      ~CHistoryPositionInfo(void);     //--- methods of access to protected data     ulong             Ticket(void)           const { return(m_curr_ticket); }     //--- fast access methods to the integer position properties     datetime          TimeOpen(void);     ulong             TimeOpenMsc(void);     datetime          TimeClose(void);     ulong             TimeCloseMsc(void);     ENUM_POSITION_TYPE PositionType(void);     string            TypeDescription(void);     long              Magic(void);     long              Identifier(void);     ENUM_DEAL_REASON  OpenReason(void);     ENUM_DEAL_REASON  CloseReason(void);     //--- fast access methods to the double position properties     double            Volume(void);     double            PriceOpen(void);     double            StopLoss(void) const;     double            TakeProfit(void) const;     double            PriceClose(void);     double            Commission(void);     double            Swap(void);     double            Profit(void);     //--- fast access methods to the string position properties     string            Symbol(void);     string            OpenComment(void);     string            CloseComment(void);     string            OpenReasonDescription(void);     string            CloseReasonDescription(void);     string            DealTickets(const string separator = " ");     //--- info methods     string            FormatType(string &str,const uint type) const;     string            FormatReason(string &str,const uint reason) const;     //--- methods for select position     bool              HistorySelect(datetime from_date,datetime to_date);     int               PositionsTotal(void) const;     bool              SelectByTicket(const ulong ticket);     bool              SelectByIndex(const int index);    };  

Here is a sample code showing how to use the class in your code

#include <CHistoryPositionInfo.mqh>  //+------------------------------------------------------------------+  //|                                                                  |  //+------------------------------------------------------------------+  void OnStart()    {  //--- variable to hold the history position info     CHistoryPositionInfo hist_position;    //--- Retrieve the history of closed positions for the specified period     if(!hist_position.HistorySelect(0,TimeCurrent()))       {        Alert("CHistoryPositionInfo::HistorySelect() failed!");        return;       }    //--- now process the list of closed positions     int total = hist_position.PositionsTotal();     for(int i = 0; i < total; i++)       {        //--- Select a closed position by its index in the list        if(hist_position.SelectByIndex(i))          {           ulong    ticket            = hist_position.Ticket();           datetime time_open         = hist_position.TimeOpen();           ulong    time_open_msc     = hist_position.TimeOpenMsc();           datetime time_close        = hist_position.TimeClose();           ulong    time_close_msc    = hist_position.TimeCloseMsc();           long     type              = hist_position.PositionType();           string   type_desc         = hist_position.TypeDescription();           long     magic             = hist_position.Magic();           long     pos_id            = hist_position.Identifier();           double   volume            = hist_position.Volume();           double   price_open        = hist_position.PriceOpen();           double   price_sl          = hist_position.StopLoss();           double   price_tp          = hist_position.TakeProfit();           double   price_close       = hist_position.PriceClose();           double   commission        = hist_position.Commission();           double   swap              = hist_position.Swap();           double   profit            = hist_position.Profit();           string   symbol            = hist_position.Symbol();           string   open_comment      = hist_position.OpenComment();           string   close_comment     = hist_position.CloseComment();           string   open_reason_desc  = hist_position.OpenReasonDescription();           string   close_reason_desc = hist_position.CloseReasonDescription();           string   deal_tickets      = hist_position.DealTickets(",");           //---           int      deals_count       = HistoryDealsTotal();   // of the selected position           int      orders_count      = HistoryOrdersTotal();  // of the selected position          }       }  //---     Print("Total closed positions = ",hist_position.PositionsTotal());    }  //+------------------------------------------------------------------+  

Note: when using the methods HistorySelect() and SelectByIndex(), the list of positions is ordered by time of closing (not by the opening times).

This means the history of closed positions (data rows) is ordered by Close Time to help calculate the running  Balance, correctly.

27683