public override void Start() { _console.WriteLine("Starting InitAccountsCache"); var accounts = _accountsApi.List().GetAwaiter().GetResult() .Select(Convert).ToDictionary(x => x.Id); //TODO: think about approach //ApplyMarginFreezing(accounts); _accountsCacheService.InitAccountsCache(accounts); _console.WriteLine($"Finished InitAccountsCache. Count: {accounts.Count}"); base.Start(); }
private async Task <Dictionary <string, MarginTradingAccount> > GetAccounts() { var accountsTask = _accountsApi.List(); var onDate = _systemClock.UtcNow.UtcDateTime.Date; var balanceChangesTask = _accountBalanceHistoryApi.ByDate(onDate, onDate.AddDays(1)); await Task.WhenAll(accountsTask, balanceChangesTask); var accounts = await accountsTask; var balanceChanges = await balanceChangesTask; var result = accounts.Select(Convert).ToDictionary(x => x.Id); result.ForEach(x => { var account = x.Value; if (balanceChanges.ContainsKey(x.Key)) { var accountBalanceChanges = balanceChanges[x.Key]; var firstBalanceChange = accountBalanceChanges.OrderBy(b => b.ChangeTimestamp).FirstOrDefault(); account.TodayStartBalance = firstBalanceChange != null ? firstBalanceChange.Balance - firstBalanceChange.ChangeAmount : account.Balance; account.TodayRealizedPnL = accountBalanceChanges.GetTotalByType(AccountBalanceChangeReasonTypeContract.RealizedPnL); account.TodayUnrealizedPnL = accountBalanceChanges.GetTotalByType(AccountBalanceChangeReasonTypeContract.UnrealizedDailyPnL); account.TodayDepositAmount = accountBalanceChanges.GetTotalByType(AccountBalanceChangeReasonTypeContract.Deposit); account.TodayWithdrawAmount = accountBalanceChanges.GetTotalByType(AccountBalanceChangeReasonTypeContract.Withdraw); account.TodayCommissionAmount = accountBalanceChanges.GetTotalByType(AccountBalanceChangeReasonTypeContract.Commission); account.TodayOtherAmount = accountBalanceChanges.Where(x => !new[] { AccountBalanceChangeReasonTypeContract.RealizedPnL, // AccountBalanceChangeReasonTypeContract.UnrealizedDailyPnL, // TODO: why not (copied from account management)? AccountBalanceChangeReasonTypeContract.Deposit, AccountBalanceChangeReasonTypeContract.Withdraw, AccountBalanceChangeReasonTypeContract.Commission, }.Contains(x.ReasonType)).Sum(x => x.ChangeAmount); } else { account.TodayStartBalance = account.Balance; } }); return(result); }