please help

 

hi,

can someone please help me with this:

I'm trying to get the correct account balance at the start of the month (1st),

the code so far does give me the correct balance when the month changes to a new month,

 but the problem is when the EA is loaded the start balance of the month is reflected as the current balance at the time of load/reload

#include <Trade\Trade.mqh>

// Constants for deal types
#define DEAL_BALANCE     0
#define DEAL_CREDIT      1
#define DEAL_CHARGE      2
#define DEAL_CORRECTION  3
#define DEAL_BONUS       4
#define DEAL_COMMISSION  5
#define DEAL_COMMISSION_DAILY  6
#define DEAL_COMMISSION_MONTHLY  7
#define DEAL_COMMISSION_AGENT_DAILY  8
#define DEAL_COMMISSION_AGENT_MONTHLY  9
#define DEAL_INTEREST    10
#define DEAL_MARGIN_LEVEL  11
#define DEAL_MARGIN      12
#define DEAL_BUY         13
#define DEAL_SELL        14
#define DEAL_SELL_CANCELED 15
#define DEAL_BUY_CANCELED  16

double GetStartOfMonthBalance()
{
    datetime now = TimeCurrent();
    int year = TimeYear(now);
    int month = TimeMonth(now);
    datetime startOfMonth = StringToTime(IntegerToString(year) + "." + IntegerToString(month) + ".01 00:00");

    double startOfMonthBalance = AccountInfoDouble(ACCOUNT_BALANCE);

    // Adjust for deals (deposits and withdrawals) before the start of the month
    for (int i = 0; i < HistoryDealsTotal(); i++)
    {
        ulong ticket = HistoryDealGetTicket(i);
        if (ticket > 0)
        {
            datetime dealTime = HistoryDealGetInteger(ticket, DEAL_TIME);
            double dealAmount = HistoryDealGetDouble(ticket, DEAL_PROFIT);
            int dealType = HistoryDealGetInteger(ticket, DEAL_TYPE);

            if (dealTime >= startOfMonth)
                continue;

            if (dealType == DEAL_BALANCE || dealType == DEAL_CREDIT)
            {
                if (dealAmount > 0) // Deposit
                {
                    startOfMonthBalance -= dealAmount;
                }
                else // Withdrawal
                {
                    startOfMonthBalance += -dealAmount;
                }
            }
        }
    }

    return startOfMonthBalance;
}

//+------------------------------------------------------------------+
//| Custom functions to get the year and month from a datetime value |
//+------------------------------------------------------------------+
int TimeYear(datetime value)
{
    MqlDateTime dt;
    TimeToStruct(value, dt);
    return dt.year;
}

int TimeMonth(datetime value)
{
    MqlDateTime dt;
    TimeToStruct(value, dt);
    return dt.mon;
}

//+------------------------------------------------------------------+
Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Account Properties
Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Account Properties
  • www.mql5.com
To obtain information about the current account there are several functions: AccountInfoInteger() , AccountInfoDouble() and AccountInfoString() ...
 
Justin Govender:

hi,

can someone please help me with this:

I'm trying to get the correct account balance at the start of the month (1st),

the code so far does give me the correct balance when the month changes to a new month,

 but the problem is when the EA is loaded the start balance of the month is reflected as the current balance at the time of load/reload

account balance is a real time thing, it is not recorded as a time series, so whenever you request the balance it is of the moment you requested it.

 
Justin Govender:
            if (dealTime >= startOfMonth)                 continue;
            if (dealTime >= startOfMonth)
                continue;

just a suggestion, assuming that your idea worked, wouldnt this be "dealTime <= startOfMonth ??

 
Michael Charles Schefe #:

just a suggestion, assuming that your idea worked, wouldnt this be "dealTime <= startOfMonth ??

hi

thanks a lot for helping.

I tried it but it didn't help.


thank u kindly