/// <summary> /// Balance transaction and OrderClosed consistency /// </summary> /// <param name="isSql">Use SQL database or Azure database</param> /// <param name="from">Start Date</param> /// <param name="to">End Date</param> /// <returns></returns> public async Task <IEnumerable <IBalanceAndOrderClosedCheckResult> > CheckBalanceAndOrderClosed(bool isSql, DateTime?from, DateTime?to) { await _log.WriteInfoAsync("CheckBalanceAndOrderClosed", null, $"Started Check IsSql={isSql} From [{from?.ToString("u")}] To [{to?.ToString("u")}]"); var accountTransactionRepo = _repositoryManager.GetAccountTransactionsReport(isSql); var tradingPositionRepo = _repositoryManager.GetTradingPosition(isSql); var accountTransactions = (await accountTransactionRepo.GetAsync(from, to)) .ToList(); // TradePositionReportClosed records with Counterparty ID = LykkeHedgingService should be excluded from the check var tradingPositionsClosed = (await tradingPositionRepo.GetClosedAsync(from, to)) .Where(m => m.TakerCounterpartyId != Constants.HedgingServiceName) .ToList(); var result = new List <BalanceAndOrderClosedCheckResult>(); // There should be a single record in MarginAccountTransactionReport with type "OrderClosed". // No double records should exist on either side (MarginAccountTransactionReport) result.AddRange(tradingPositionsClosed.CheckTransactionCount(accountTransactions)); // Amount of transaction should match PnL of closed position. result.AddRange(tradingPositionsClosed.CheckTransactionPnL(accountTransactions)); // ClientID should match result.AddRange(tradingPositionsClosed.CheckClientId(accountTransactions)); // AccountID should match result.AddRange(tradingPositionsClosed.CheckAccountId(accountTransactions)); // DateTime should be similar (with difference no more than 1 minute result.AddRange(tradingPositionsClosed.CheckDate(accountTransactions)); // No orphaned MarginAccountTransactionReport records should exist // No double records should exist on either side (TradingPositionClosed) result.AddRange(tradingPositionsClosed.CheckPositionCount(accountTransactions)); await _log.WriteInfoAsync("CheckBalanceAndOrderClosed", null, $"Check finished with {result.Count} errors"); return(result); }