Article-15346-MQL5-Trade-Mo.../Accounts.mqh

199 lines
16 KiB
MQL5
Raw Permalink Normal View History

2026-03-23 13:19:06 +07:00
<EFBFBD><EFBFBD>//+------------------------------------------------------------------+
//| Accounts.mqh |
//| Copyright 2024, MetaQuotes Ltd. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, MetaQuotes Ltd."
#property link "https://www.mql5.com"
#property version "1.00"
#include "Account.mqh"
//+------------------------------------------------------------------+
//| ;0AA-:>;;5:F8O 0::0C=B>2 |
//+------------------------------------------------------------------+
class CAccounts : public CObject
{
private:
CArrayObj m_list; // !?8A>: >1J5:B>2-0::0C=B>2
CAccount m_tmp; // @5<5==K9 >1J5:B-0::0C=B 4;O ?>8A:0
protected:
//--- !>740QB =>2K9 >1J5:B-0::0C=B 8 4>102;O5B 53> 2 A?8A>:
CAccount *Add(const long login, const string server);
public:
//--- !>740QB =>2K9 >1J5:B-0::0C=B
bool Create(const long login, const string server);
//--- >72@0I05B C:070B5;L =0 C:070==K9 >1J5:B-0::0C=B ?> (1) ;>38=C 8 A5@25@C, (2) 8=45:AC 2 A?8A:5
CAccount *Get(const long login, const string server);
CAccount *Get(const int index) const { return this.m_list.At(index); }
//--- 1J548=O5B A?8A:8 ?>78F89 0::0C=B>2 8 2>72@0I05B >1I89
CArrayObj *GetCommonPositionsList(void);
//--- >72@0I05B A?8A>: ?>78F89 C:070==>3> 0::0C=B0
CArrayObj *GetAccountPositionsList(const long login, const string server);
//--- >72@0I05B :>;8G5AB2> E@0=8<KE 0::0C=B>2
int Total(void) const { return this.m_list.Total(); }
//--- 1=>2;O5B A?8A:8 ?>78F89 C:070==>3> 0::0C=B0
bool PositionsRefresh(const long login, const string server);
//--- >=AB@C:B>@/45AB@C:B>@
CAccounts();
~CAccounts();
};
//+------------------------------------------------------------------+
//| >=AB@C:B>@ |
//+------------------------------------------------------------------+
CAccounts::CAccounts()
{
this.m_list.Sort();
}
//+------------------------------------------------------------------+
//| 5AB@C:B>@ |
//+------------------------------------------------------------------+
CAccounts::~CAccounts()
{
this.m_list.Clear();
}
//+------------------------------------------------------------------+
//| !>740QB =>2K9 >1J5:B-0::0C=B 8 4>102;O5B 53> 2 A?8A>: |
//+------------------------------------------------------------------+
CAccount *CAccounts::Add(const long login,const string server)
{
//--- !>740Q< =>2K9 >1J5:B-0::0C=B
CAccount *account=new CAccount(login, server);
if(account==NULL)
return NULL;
//--- A;8 A>740==K9 >1J5:B =5 4>102;5= 2 A?8A>: - C40;O5< 53> 8 2>72@0I05< NULL
if(!this.m_list.Add(account))
{
delete account;
return NULL;
}
//--- >72@0I05< C:070B5;L =0 A>740==K9 >1J5:B
return account;
}
//+------------------------------------------------------------------+
//| !>740QB =>2K9 >1J5:B-0::0C=B |
//+------------------------------------------------------------------+
bool CAccounts::Create(const long login,const string server)
{
//--- > 2@5<5==K9 >1J5:B-0::0C=B CAB0=02;8205< ;>38= 8 A5@25@
this.m_tmp.SetLogin(login);
this.m_tmp.SetServer(server);
//--- !?8A:C >1J5:B>2-0::0C=B>2 CAB0=02;8205< D;03 A>@B8@>20==>3> A?8A:0 8
//--- ?>;CG05< 8=45:A >1J5:B0 2 A?8A:5, 8<5NI53> B5 65 ;>38= 8 A5@25@, GB> 8 C 2@5<5==>3> >1J5:B0
this.m_list.Sort();
int index=this.m_list.Search(&this.m_tmp);
//--- >72@0I05< D;03 CA?5H=>3> 4>102;5=8O >1J5:B0 2 A?8A>: (@57C;LB0B @01>BK <5B>40 Add), ;81> false, 5A;8 >1J5:B 2 A?8A:5 C65 5ABL
return(index==WRONG_VALUE ? this.Add(login, server)!=NULL : false);
}
//+------------------------------------------------------------------+
//| >72@0I05B C:070B5;L =0 C:070==K9 >1J5:B-0::0C=B |
//+------------------------------------------------------------------+
CAccount *CAccounts::Get(const long login,const string server)
{
//--- > 2@5<5==K9 >1J5:B-0::0C=B CAB0=02;8205< ;>38= 8 A5@25@
this.m_tmp.SetLogin(login);
this.m_tmp.SetServer(server);
//--- !?8A:C >1J5:B>2-0::0C=B>2 CAB0=02;8205< D;03 A>@B8@>20==>3> A?8A:0 8
//--- ?>;CG05< 8=45:A >1J5:B0 2 A?8A:5, 8<5NI53> B5 65 ;>38= 8 A5@25@, GB> 8 C 2@5<5==>3> >1J5:B0
this.m_list.Sort();
int index=this.m_list.Search(&this.m_tmp);
//--- >72@0I05< C:070B5;L =0 >1J5:B 2 A?8A:5 ?> 8=45:AC, ;81> NULL, 5A;8 8=45:A @025= -1
return this.m_list.At(index);
}
//+------------------------------------------------------------------+
//| 1=>2;O5B A?8A:8 ?>78F89 C:070==>3> 0::0C=B0 |
//+------------------------------------------------------------------+
bool CAccounts::PositionsRefresh(const long login, const string server)
{
//--- >;CG05< C:070B5;L =0 >1J5:B-0::0C=B A C:070==K<8 ;>38=>< 8 A5@25@><
CAccount *account=this.Get(login, server);
if(account==NULL)
return false;
//--- A;8 ?>;CG5==K9 >1J5:B - =5 B5:CI89 0::0C=B,
if(account.Login()!=::AccountInfoInteger(ACCOUNT_LOGIN) || account.Server()!=::AccountInfoString(ACCOUNT_SERVER))
{
//--- A>>1I05<, GB> >1=>2;5=85 40==KE =5 B5:CI53> 0::0C=B0 ?@8254QB : =5:>@@5:B=K< 40==K< 8 2>72@0I05< false
::Print("Error. Updating the list of positions for a non-current account will result in incorrect data.");
return false;
}
//--- >72@0I05< @57C;LB0B >1=>2;5=8O 40==KE B5:CI53> 0::0C=B0
return account.PositionsRefresh();
}
//+------------------------------------------------------------------+
//| 1J548=O5B A?8A:8 ?>78F89 0::0C=B>2 8 2>72@0I05B >1I89 |
//+------------------------------------------------------------------+
CArrayObj *CAccounts::GetCommonPositionsList(void)
{
//--- !>740Q< =>2K9 A?8A>: 8 A1@0AK205< 4;O =53> D;03 C?@02;5=8O ?0<OBLN
CArrayObj *list=new CArrayObj();
if(list==NULL)
return NULL;
list.FreeMode(false);
//---  F8:;5 ?> A?8A:C 0::0C=B>2
int total=this.m_list.Total();
for(int i=0; i<total; i++)
{
//--- ?>;CG05< >G5@54=>9 >1J5:B-0::0C=B
CAccount *account=this.m_list.At(i);
if(account==NULL)
continue;
//--- >;CG05< A?8A>: 70:@KBKE ?>78F89 0::0C=B0
CArrayObj *src=account.GetPositionsList();
if(src==NULL)
continue;
//--- A;8 MB> ?5@2K9 0::0C=B 2 A?8A:5
if(i==0)
{
//--- :>?8@C5< 2 =>2K9 A?8A>: M;5<5=BK 87 A?8A:0 ?>78F89 0::0C=B0
if(!list.AssignArray(src))
{
delete list;
return NULL;
}
}
//--- A;8 MB> =5 ?5@2K9 0::0C=B 2 A?8A:5
else
{
//--- 4>102;O5< 2 :>=5F =>2>3> A?8A:0 M;5<5=BK 87 A?8A:0 ?>78F89 0::0C=B0
if(!list.AddArray(src))
continue;
}
}
//--- B?@02;O5< =>2K9 A?8A>: 2 E@0=8;8I5
if(!ListStorage.Add(list))
{
delete list;
return NULL;
}
//--- >72@0I05< C:070B5;L =0 A>740==K9 8 70?>;=5==K9 A?8A>:
return list;
}
//+------------------------------------------------------------------+
//| >72@0I05B A?8A>: ?>78F89 C:070==>3> 0::0C=B0 |
//+------------------------------------------------------------------+
CArrayObj *CAccounts::GetAccountPositionsList(const long login,const string server)
{
CAccount *account=this.Get(login, server);
return(account!=NULL ? account.GetPositionsList() : NULL);
}
//+------------------------------------------------------------------+