Watch how to download trading robots for free
Find us on Telegram!
Join our fan page
Interesting script?
So post a link to it -
let others appraise it
You liked the script? Try it in the MetaTrader 5 terminal
Libraries

HistoryPositionInfo version 2 - library for MetaTrader 5

Views:
7919
Rating:
(31)
Published:
2017.08.10 12:56
\MQL5\Include\HistorySelect_\
Need a robot or indicator based on this code? Order it on Freelance Go to Freelance

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
InitInitialization using a position ID
Access to the calculation result  
GetProfitInPoints is an obsolete method. It is recommended to use GetProfitGetting the profit of a position in points
GetProfitGetting the profit of a position in points, commission, swap and profit in money
Setting parameters 
LogErrorsEnable/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 |
//|                                           http://wmua.ru/slesar/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2017, Vladimir Karputov"
#property link      "http://wmua.ru/slesar/"
#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));
     }
  }
//+------------------------------------------------------------------+



Translated from Russian by MetaQuotes Ltd.
Original code: https://www.mql5.com/ru/code/18721

Get_Rich_or_Die_Trying_GBP Get_Rich_or_Die_Trying_GBP

Trades on PERIOD_M1 (M1). Calculates bullish and bearish candlesticks.

Trailing_Profit Trailing_Profit

Closing all open positions with the specified profit percent.

VWAP Custom Position VWAP Custom Position

Standard VWAP (Volume Weighted Average Price) calculation, but with configurable start position.

SaveTicks SaveTicks

This utility program records tick quotes in the CSV and BIN format. A flexible set of symbols is available for writing.