Use:
//+------------------------------------------------------------------+ //| ProjectName | //| Copyright 2020,CompanyName | //| http://www.companyname.net | //+------------------------------------------------------------------+ #include <Trade\DealInfo.mqh> CDealInfo deal; //+------------------------------------------------------------------+ //| Class DealsStatistics | //+------------------------------------------------------------------+ class DealsStatistics { public : double profit[]; double neatProfit[]; double balance[]; int dealsTotal; public : //+------------------------------------------------------------------+ //| Deals Statistics | //+------------------------------------------------------------------+ void DealsStatistics() { datetime end=TimeCurrent(); datetime start=0 ; HistorySelect(start,end); dealsTotal=HistoryDealsTotal(); ArrayResize(profit,dealsTotal,0); ArrayResize(neatProfit,dealsTotal,0); ArrayResize(balance,dealsTotal,0); } ~DealsStatistics() { ArrayFree(profit); ArrayFree(neatProfit); ArrayFree(balance); } //+------------------------------------------------------------------+ //| Get Data | //+------------------------------------------------------------------+ void GetData() { for(int i=0;i<dealsTotal;i++) { deal.SelectByIndex(i); profit[i]=deal.Profit(); neatProfit[i]=deal.Profit()+deal.Commission()+deal.Swap(); if(i>0) balance[i]=balance[i-1]+neatProfit[i]; else balance[i]=neatProfit[i]; int d=0; } } //+------------------------------------------------------------------+ //| Get Initial Balance | //+------------------------------------------------------------------+ double GetInitialBalance() { return profit[ 0 ]; } }; //--- DealsStatistics *dStatistics; //--- int OnStart(void) { dStatistics= new DealsStatistics(); dStatistics.GetData(); double initialBalance =dStatistics.GetInitialBalance(); double currentBalance =dStatistics.balance[dStatistics.dealsTotal- 1 ]; Comment( " \nHistoryDealsTotal=", dStatistics.dealsTotal, " \nInitialBalance=", initialBalance, " \ncurrentBalance=", currentBalance ); delete dStatistics; ChartRedraw(); return 0 ; } //+------------------------------------------------------------------+
Files:
Script_1.mq5
6 kb
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
I am trying to calculate the current balance using CDealInfo and HistorySelect. But every time I run the script I get a different results. What am I doing wrong?
Thank you so much!!