Deal handeling is wrong!

 

Hi guys,


I am trying to find initial Balance of account working with Deals history and simply made a code to take and Print Deals data like this:

void FindInitialBalance()
{
   if(!HistorySelect(EAStartTime, TimeCurrent())) {initialBalance=0; return;}

   datetime Day0 = iTime(Symbol(), PERIOD_D1, 0);
   ulong posticket;
   CurrentProfit=0;

   int tot = HistoryDealsTotal()-1;
   //for (int i=0; i<=tot; i++)
   for (int i=tot; i>=0; i--)
      {
      double PL = Dealinfo.Profit() + Dealinfo.Swap() + Dealinfo.Commission();
      if (Dealinfo.SelectByIndex(i))
         {
         if (i==1)
            initialBalance = PL;
         else
            {
            posticket = Dealinfo.PositionId();
            bool Vio = ViolatedPos(posticket);
            
            if ((PL<0 && Vio) || (PL>0 && !Vio))
               CurrentProfit += PL;
            }
         
         Print("Deal ticket: ", Dealinfo.Ticket(), "     Pos. Ticket: ",posticket,"    Desc.: ",Dealinfo.TypeDescription(),"    PL: ",PL);   
         }
      else
         {
         CurrentProfit=0;
         PrintFormat("%i  HistoryDealSelect(%I64u) failed. Error %d",i, Dealinfo.Ticket(), GetLastError());
         return;
         }
      }
   
   Print("Current Profit: ", CurrentProfit,"    Ini.Balance: ", initialBalance);
   return;   
}

the code output issues (referring to attached image):

1- initial Balance is not correct. As per deal description balance has description like "Balance type" but as we see its value is zero which is wrong!

2-Balance ticket is changing when i run code per each tick. in image it is zero but it changes between 0, 2, 30.

3- Balance index in current index order (from higher value (tot) to zero) is zero which is correct but for reverse index order (from zero to tot) it is in index 1 but it has to be on index zero again. index one is a normal order which is weird.


 
Mahdi Ebrahimzadeh:

Hi guys,


I am trying to find initial Balance of account working with Deals history and simply made a code to take and Print Deals data like this:

the code output issues (referring to attached image):

1- initial Balance is not correct. As per deal description balance has description like "Balance type" but as we see its value is zero which is wrong!

2-Balance ticket is changing when i run code per each tick. in image it is zero but it changes between 0, 2, 30.

3- Balance index in current index order (from higher value (tot) to zero) is zero which is correct but for reverse index order (from zero to tot) it is in index 1 but it has to be on index zero again. index one is a normal order which is weird.


Articles

Orders, Positions and Deals in MetaTrader 5

MetaQuotes, 2011.02.01 16:13

Creating a robust trading robot cannot be done without an understanding of the mechanisms of the MetaTrader 5 trading system. The client terminal receives the information about the positions, orders, and deals from the trading server. To handle this data properly using the MQL5, it's necessary to have a good understanding of the interaction between the MQL5-program and the client terminal.

Specifically, see the section entitled Obtaining information on deals from the history.

 

if you want to get the initial balance of the account, it is simply accessible 

AccountInfoDouble(ACCOUNT_BALANCE);


make a global variable, so that you can still continue to access the initial balance in OnTick while you define the initial balance in the initializer (because that makes sense)

double initial_balance;

int OnInit()
{
    trade.SetExpertMagicNumber(expert_magic);    
    
    initial_balance = AccountInfoDouble(ACCOUNT_BALANCE);
         
    return(INIT_SUCCEEDED);
}

void OnTick()
{

double my_initial_balance = initial_balance;

Comment("Initial balance: ", my_initial_balance);
}

there is no need to access deal history, this is only for past deals

 
Conor Mcnamara #:

if you want to get the initial balance of the account, it is simply accessible 


make a global variable, so that you can still continue to access the initial balance in OnTick while you define the initial balance in the initializer (because that makes sense)

there is no need to access deal history, this is only for past deals

If I'm not mistaken, the OP is trying to retroactively calculate the starting balance of the account after trades have closed (for what purpose, I have no idea).

 
Ryan L Johnson #:

Specifically, see the section entitled Obtaining information on deals from the history.

yes i read it, the issue is i am asking for data before selecting deal.

double PL = Dealinfo.Profit() + Dealinfo.Swap() + Dealinfo.Commission();
      if (Dealinfo.SelectByIndex(i))
         {
        ...
        }

PL has to be inside of if box.

 
Conor Mcnamara #:

if you want to get the initial balance of the account, it is simply accessible 


make a global variable, so that you can still continue to access the initial balance in OnTick while you define the initial balance in the initializer (because that makes sense)

there is no need to access deal history, this is only for past deals

thanks your reply,

this↓

AccountInfoDouble(ACCOUNT_BALANCE);

just calls for CURRENT account balance not initial balance.


make a global variable, so that you can still continue to access the initial balance in OnTick while you define the initial balance in the initializer (because that makes sense)

this↑ will not work if you add EA after you do couple of trades (changes account balance) and initial balance changes before attaching EA to chart. then we need to look back to first DEAL marked as BALANCE TYPE to find out account initial balance.

 
Ryan L Johnson #:

If I'm not mistaken, the OP is trying to retroactively calculate the starting balance of the account after trades have closed (for what purpose, I have no idea).

yes you are right. i need initial balance of Account from first date it is generated, actually First Deposit.

code is corrected thank you.

 
Conor Mcnamara #:

Please think about it...when you get the "current" balance in the OnInit for the first time running the EA, what balance do you think you will get? You will get your initial deposit.

Running the function call in OnInit, is totally different to running it in OnTick. Only in OnTick you will get the updated balance.

I do not think so.

you can check it yourself too.

even if you call account balance in oninit it will not send you initial balance. it always will send current account balance.


for example if you want to draw account balance during past account life time. you will need to start from initial account balance. and dedicated function does not prepare any time dependent data. it always prepares current balance.

 
Conor Mcnamara #:

Please think about it...when you get the "current" balance in the OnInit, what balance do you think you will get? You will get your initial deposit. Each time you start your EA, you will have a new initial deposit, that is the reality of it.

I think we're embarking on a discussion about the meaning of words here. "Initial balance" might not have been the best choice of words in the OP, but "First Deposit" clearly means account opening deposit. I interpreted the apparent intent of the code differently is all.

 

I do not know how reliable this solution is but it is a concise one:

int OnInit()
  {
//---
   Print(FindInitialBalance());
//---
   return(INIT_SUCCEEDED);
  }

double FindInitialBalance()
{
   if(HistorySelect(0, TimeCurrent()))
   {
      ulong ticket = HistoryDealGetTicket(0);
      return HistoryDealGetDouble(ticket, DEAL_PROFIT);
   }
   return 0;
}

It assumes that the first deal in history is always of type DEAL_BALANCE(deposit).

 
Ryan L Johnson #:

I think we're embarking on a discussion about the meaning of words here. "Initial balance" might not have been the best choice of words in the OP, but "First Deposit" clearly means account opening deposit. I interpreted the apparent intent of the code differently is all.

Yea I saw that, I deleted my reply then when I realized he was looking to retrieve first deposit only