• Sign up >>

Watch how to download trading robots for free

Interesting script?
So post a link to it -
let others appraise it

You liked the script? Try it in the MetaTrader 5 terminal

Libraries

CHistoryPositionInfo Class - library for MetaTrader 5

amrali

Views:
638
Rating:
votes: 7
Published:
2020.01.21 03:26
Updated:
2020.01.24 10:12

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.




3D Spiral Quotes 3D Spiral Quotes

Demonstration of Canvas's capabilities using 3D spiral quotes as an example.

Pseudo-Indicator with Asynchronous Multi-Threaded Calculations Demo Pseudo-Indicator with Asynchronous Multi-Threaded Calculations Demo

This is an indicator w/o buffers which demonstrates parallel multi-threaded calculations in chart objects hosting worker expert adviser.

3D Moving Average 3D Moving Average

The first really 3D indicator "Moving Average".

CSetFileReader CSetFileReader

Class to provide simple reading mechanism from MetaTrader set files.