| //+------------------------------------------------------------------+
|
| //| Base.mqh |
|
| //| Copyright 2026, Niquel Mendoza. |
|
| //| https://www.mql5.com |
|
| //+------------------------------------------------------------------+
|
| #property copyright "Copyright 2026, Niquel Mendoza."
|
| #property link "https://www.mql5.com"
|
| #property strict
|
|
|
| #ifndef MQLARTICLES_ACCOUNT_MONEYHISTORY_BASE_MQH
|
| #define MQLARTICLES_ACCOUNT_MONEYHISTORY_BASE_MQH
|
|
|
| //+------------------------------------------------------------------+
|
| //| |
|
| //+------------------------------------------------------------------+
|
| #include <TSN\\Std\\Basics\\CompTime.mqh>
|
|
|
| //+------------------------------------------------------------------+
|
| //| |
|
| //+------------------------------------------------------------------+
|
| namespace TSN
|
| {
|
| //+------------------------------------------------------------------+
|
| //| Defines |
|
| //+------------------------------------------------------------------+
|
| const bool GetNetProfitOmitirDeal[18] =
|
| {
|
| false, //compra
|
| false, //venta
|
| true, //balance
|
| true, //credito
|
| true, //carga adicionales
|
| false, //correcion
|
| true, //bonos
|
| false, //comisiones adicional
|
| false, //comisions del dia
|
| false, //comision del mes
|
| false, //comosions agente dia
|
| false, //comosiion agenet mes,
|
| false, //interes
|
| false, //compra cancelada
|
| false, //ventan cancelada
|
| false, //dividendo
|
| false, //dividendo frankeado, con beneficions
|
| false //impuestos
|
| };
|
|
|
| //---
|
| #define TSNMQLAR_ACCOUNT_MH_MINDEAL (DEAL_TYPE_BUY)
|
| #define TSNMQLAR_ACCOUNT_MH_MAXDEAL (DEAL_TAX)
|
|
|
| //---
|
| // Basicos
|
| TSNSTD_STATIC_ASSERT(DEAL_TYPE_BUY == 0, mqlart_acc_mnh_buy)
|
| TSNSTD_STATIC_ASSERT(DEAL_TYPE_SELL == 1, mqlart_acc_mnh_sell)
|
| // En rango
|
| TSNSTD_STATIC_ASSERT(TSNMQLAR_ACCOUNT_MH_MINDEAL >= 0 && TSNMQLAR_ACCOUNT_MH_MAXDEAL < 18, mqlart_acc_mnh_inr)
|
|
|
| //--- atajos
|
| #define TSNMQLART_MONEYHIST_DEALIS_COMPRAVENTA(DEAL) ((DEAL)<2)
|
|
|
| //---
|
| /*
|
| Print(DEAL_ENTRY_IN);
|
| Print(DEAL_ENTRY_OUT);
|
| Print(DEAL_ENTRY_INOUT);
|
| Print(DEAL_ENTRY_OUT_BY);
|
| 2026.10.28 19:33:11.199 a (XAUUSD,MN1) 0
|
| 2026.10.28 19:33:11.199 a (XAUUSD,MN1) 1
|
| 2026.10.28 19:33:11.199 a (XAUUSD,MN1) 2
|
| 2026.10.28 19:33:11.199 a (XAUUSD,MN1) 3
|
| */
|
|
|
| //---
|
| TSNSTD_STATIC_ASSERT(DEAL_ENTRY_IN == 0, mqlart_acc_ms_d_e_in)
|
| TSNSTD_STATIC_ASSERT(DEAL_ENTRY_OUT == 1, mqlart_acc_ms_d_e_out)
|
| TSNSTD_STATIC_ASSERT(DEAL_ENTRY_INOUT == 2, mqlart_acc_ms_d_e_inout)
|
| TSNSTD_STATIC_ASSERT(DEAL_ENTRY_OUT_BY == 3, mqlart_acc_ms_d_e_outby)
|
|
|
| //+------------------------------------------------------------------+
|
| //| |
|
| //+------------------------------------------------------------------+
|
| class CHistoryMoneyUtils
|
| {
|
| public:
|
| CHistoryMoneyUtils(void) {}
|
| ~CHistoryMoneyUtils(void) {}
|
| //---
|
| //---
|
| static double GetPositionCommission(ulong ticket);
|
| static double GetTotalPositionProfitNoCurrent(ulong position_ticket);
|
|
|
| //---
|
| static inline ulong GetOrderMagic(const ulong ticket);
|
|
|
| //---
|
| static double GetNetProfitSince(bool include_all_magic, ulong specific_magic, datetime start_date);
|
| };
|
|
|
| //+------------------------------------------------------------------+
|
| //| |
|
| //+------------------------------------------------------------------+
|
| static double CHistoryMoneyUtils::GetPositionCommission(ulong ticket)
|
| {
|
| // Verificar si la posición existe y obtener su tiempo de apertura
|
| if(!PositionSelectByTicket(ticket))
|
| return 0.0;
|
| const datetime open_time = (datetime)PositionGetInteger(POSITION_TIME);
|
| // Usar un rango de tiempo estrecho: desde el tiempo de apertura hasta 1 segundo después
|
| if(!HistorySelect(open_time, open_time + 1))
|
| return 0.0;
|
| // Obtener el primer deal del historial
|
| const ulong deal_ticket = HistoryDealGetTicket(0);
|
| if(deal_ticket == 0)
|
| {
|
| return 0.0;
|
| }
|
| // Verificar si el deal corresponde a la posición y es de entrada
|
| if(HistoryDealGetInteger(deal_ticket, DEAL_POSITION_ID) == ticket)
|
| {
|
| return HistoryDealGetDouble(deal_ticket, DEAL_COMMISSION);
|
| }
|
| return 0.0;
|
| }
|
|
|
| //+------------------------------------------------------------------+
|
| static double CHistoryMoneyUtils::GetTotalPositionProfitNoCurrent(ulong position_ticket)
|
| {
|
| //---
|
| double total_profit = 0.0;
|
|
|
| //---
|
| if(HistorySelectByPosition(position_ticket))
|
| {
|
| const int deals_count = HistoryDealsTotal();
|
| for(int i = 0; i < deals_count; i++)
|
| {
|
| const ulong deal_ticket = HistoryDealGetTicket(i);
|
| if(deal_ticket <= 0)
|
| continue;
|
| const uint entry = (uint)HistoryDealGetInteger(deal_ticket, DEAL_ENTRY);
|
| // in = 0 - 2 = numero grande
|
| // out = 1 - 2 = numero grande
|
| // inout = 2-2 = 0
|
| // out_by = 3-2 = 1
|
| if((entry - 2u) > 0) // solo filtramos inout
|
| {
|
| total_profit += HistoryDealGetDouble(deal_ticket, DEAL_PROFIT) +
|
| HistoryDealGetDouble(deal_ticket, DEAL_SWAP) +
|
| HistoryDealGetDouble(deal_ticket, DEAL_COMMISSION);
|
| }
|
| }
|
| }
|
| return total_profit;
|
| }
|
|
|
| //+------------------------------------------------------------------+
|
| static inline ulong CHistoryMoneyUtils::GetOrderMagic(const ulong ticket)
|
| {
|
| HistoryOrderSelect(ticket);
|
| return HistoryOrderGetInteger(ticket, ORDER_MAGIC);
|
| }
|
|
|
| //+------------------------------------------------------------------+
|
| static double CHistoryMoneyUtils::GetNetProfitSince(bool include_all_magic, ulong specific_magic, datetime start_date)
|
| {
|
| //---
|
| double total_net_profit = 0.0; // Initialize the total net profit
|
|
|
| //---
|
| if(start_date > 0 && start_date != D'1971.01.01 00:00')
|
| {
|
| ResetLastError(); // Reset any previous errors
|
| if(!HistorySelect(start_date, TimeCurrent()))
|
| {
|
| Print("Error when selecting orders: ", _LastError);
|
| return 0.00; // Exit if unable to select the history
|
| }
|
|
|
| //---
|
| const int total_deals = HistoryDealsTotal(); // Count total deals in the history
|
| for(int i = 0; i < total_deals; i++)
|
| {
|
| //---
|
| const ulong deal_ticket = HistoryDealGetTicket(i); // Get the deal ticket
|
|
|
| //---
|
| if(GetNetProfitOmitirDeal[HistoryDealGetInteger(deal_ticket, DEAL_TYPE)])
|
| continue;
|
|
|
| //---
|
| if(!include_all_magic && HistoryDealGetInteger(deal_ticket, DEAL_MAGIC) != specific_magic)
|
| continue;
|
|
|
| //---
|
| const double deal_profit = HistoryDealGetDouble(deal_ticket, DEAL_PROFIT);
|
| const double deal_commission = HistoryDealGetDouble(deal_ticket, DEAL_COMMISSION);
|
| const double deal_swap = HistoryDealGetDouble(deal_ticket, DEAL_SWAP);
|
| total_net_profit += (deal_profit + deal_commission + deal_swap);
|
| }
|
| }
|
|
|
| //---
|
| return total_net_profit;
|
| }
|
|
|
|
|
| //---
|
| }
|
| //+------------------------------------------------------------------+
|
| #endif // MQLARTICLES_ACCOUNT_MONEYHISTORY_BASE_MQH
|