HistoryPositionInfo version 2 – library MetaTrader 5

Version 1.0: HistoryPositionInfo.

New in version 2:

The GetProfitInPoints function is still available, but it is recommended to use the new GetProfit function:

   bool              GetProfit(double &profit_in_points,
                               double &commission,
                               double &swap,
                               double &profit_in_money);

The GetProfit function returns the following information about a position, in addition to the profit in points: commission, swap and profit in money.

The CHistoryPositionInfo class is designed for getting the profit of a position in points, commission, swap and profit in money based on the trading history.

Class Description 

The CHistoryPositionInfo class allows getting the profit of a position in points, commission, swap and profit in money based on the trading history. 

Declaration

class CHistoryPositionInfo : public CArrayObj

Header

#include <HistorySelect_\HistoryPositionInfo.mqh>

Class methods by groups

Initialization
Init Initialization using a position ID
Access to the calculation result   
GetProfitInPoints is an obsolete method. It is recommended to use GetProfit Getting the profit of a position in points
GetProfit Getting the profit of a position in points, commission, swap and profit in money
Setting parameters  
LogErrors Enable/disable logging of errors
PrintDeals(const bool value)  Enable/disable deal print mode
Printing deals  
PrintDeals(void) Printing the deals that formed the position

An example of using the CHistoryPositionInfo class: requesting a five-day trading history, forming an array of all found position identifiers, then printing the following information in a loop: the profit of a position in points, commission, swap and profit in money based on the trading history.

//+------------------------------------------------------------------+
//|                                      TestHistoryPositionInfo.mq5 |
//|                              Copyright © 2017, Vladimir Karputov |
//|                                            |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2017, Vladimir Karputov"
#property link      ""
#property version   "2.000"
#property script_show_inputs
//---
input datetime from_date   = __DATE__-60*60*24*5;  // from date (current time - 5 days)
input datetime to_date     =__DATE__+60*60*24;     // to date (current time + 1 days)
input bool     log_errors  = true;                 // logging errors
input bool     print_deals = true;                 // print deals
#include <HistorySelect_\HistoryPositionInfo.mqh>
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   long arr_tickets[];
//--- request trade history 
   HistorySelect(from_date,to_date);
//---
   uint     total=HistoryDealsTotal();
   ulong    ticket=0;
   long     position_id=0;
//--- for all deals 
   for(uint i=0;i<total;i++)
     {
      //--- try to get deals ticket 
      if((ticket=HistoryDealGetTicket(i))>0)
        {
         //--- get deals properties 
         position_id=HistoryDealGetInteger(ticket,DEAL_POSITION_ID);

         int arr_size=ArraySize(arr_tickets);
         bool search=false;
         for(int ii=0;ii<arr_size;ii++)
           {
            if(arr_tickets[ii]==position_id)
              {
               search=true;
               continue;
              }
           }
         if(!search)
           {
            ArrayResize(arr_tickets,arr_size+1,100);
            arr_tickets[arr_size]=position_id;
           }
        }
     }
//---
   int arr_size=ArraySize(arr_tickets);
   for(int i=0;i<arr_size;i++)
     {
      Print("");
      Print("Search deals with POSITION_IDENTIFIER ",IntegerToString(arr_tickets[i]));

      CHistoryPositionInfo HistoryPositionInfo;
      //--- init position identifier
      HistoryPositionInfo.Init(arr_tickets[i]);
      //--- los errors
      HistoryPositionInfo.LogErrors(log_errors);
      //--- print deals
      HistoryPositionInfo.PrintDeals(print_deals);
      //---
      double profit_in_points=0.0;
      double commission=0.0;
      double swap=0.0;
      double profit=0.0;

      if(HistoryPositionInfo.GetProfit(profit_in_points,commission,swap,profit))
         Print("Profit in points ",DoubleToString(profit_in_points,1),
               ", commission ",DoubleToString(commission,2),
               ", swap ",DoubleToString(swap,2),
               ", profit ",DoubleToString(profit,2));
     }
  }
//+------------------------------------------------------------------+



📈 ROBOTFX MetaTrader Expert Advisors and Indicators to maximize profits and minimize the risks