コード例 #1
0
        public async Task Handle(AccountChangedEvent e)
        {
            var(executionInfo, _) = await _operationExecutionInfoRepository.GetOrAddAsync(
                operationName : OperationName,
                operationId : e.OperationId,
                factory : () => new OperationExecutionInfo <OperationData>(
                    operationName: OperationName,
                    id: e.OperationId,
                    lastModified: _dateService.Now(),
                    data: new OperationData {
                State = OperationState.Initiated
            }
                    ));

            if (executionInfo.Data.SwitchState(OperationState.Initiated, OperationState.Finished))
            {
                var updatedAccount = Convert(e.Account, e.ChangeTimestamp);

                switch (e.EventType)
                {
                case AccountChangedEventTypeContract.Created:
                    _accountsCacheService.TryAddNew(MarginTradingAccount.Create(updatedAccount));
                    break;

                case AccountChangedEventTypeContract.Updated:
                {
                    var account = _accountsCacheService.TryGet(e.Account.Id);
                    if (await ValidateAccount(account, e) &&
                        await _accountsCacheService.UpdateAccountChanges(updatedAccount.Id,
                                                                         updatedAccount.TradingConditionId, updatedAccount.WithdrawTransferLimit,
                                                                         updatedAccount.IsDisabled, updatedAccount.IsWithdrawalDisabled, e.ChangeTimestamp, updatedAccount.AdditionalInfo))
                    {
                        _accountUpdateService.RemoveLiquidationStateIfNeeded(e.Account.Id,
                                                                             "Trading conditions changed");
                    }
                    break;
                }

                case AccountChangedEventTypeContract.BalanceUpdated:
                {
                    if (e.BalanceChange != null)
                    {
                        var account = _accountsCacheService.TryGet(e.Account.Id);
                        if (await ValidateAccount(account, e))
                        {
                            switch (e.BalanceChange.ReasonType)
                            {
                            case AccountBalanceChangeReasonTypeContract.Swap:
                                HandleSwap(e);
                                break;

                            case AccountBalanceChangeReasonTypeContract.Withdraw:
                                await _accountUpdateService.UnfreezeWithdrawalMargin(updatedAccount.Id,
                                                                                     e.BalanceChange.Id);

                                break;

                            case AccountBalanceChangeReasonTypeContract.UnrealizedDailyPnL:
                                HandleUnrealizedPnLTransaction(e);
                                break;

                            case AccountBalanceChangeReasonTypeContract.RealizedPnL:
                                await _accountUpdateService.UnfreezeUnconfirmedMargin(e.Account.Id,
                                                                                      e.BalanceChange.EventSourceId);

                                break;

                            case AccountBalanceChangeReasonTypeContract.Reset:
                                await HandleAccountReset(e);

                                break;
                            }

                            await _accountsCacheService.HandleBalanceChange(updatedAccount.Id,
                                                                            e.BalanceChange.Balance,
                                                                            e.BalanceChange.ChangeAmount,
                                                                            e.BalanceChange.ReasonType.ToType <AccountBalanceChangeReasonType>(),
                                                                            e.ChangeTimestamp);

                            _accountUpdateService.RemoveLiquidationStateIfNeeded(e.Account.Id,
                                                                                 "Balance updated");

                            _accountBalanceChangedEventChannel.SendEvent(this,
                                                                         new AccountBalanceChangedEventArgs(updatedAccount.Id));
                        }
                    }
                    else
                    {
                        _log.WriteWarning("AccountChangedEvent Handler", e.ToJson(), "BalanceChange info is empty");
                    }

                    break;
                }

                case AccountChangedEventTypeContract.Deleted:
                    //account deletion from cache is double-handled by CQRS flow
                    _accountsCacheService.Remove(e.Account.Id);
                    break;

                default:
                    await _log.WriteErrorAsync(nameof(AccountsProjection), nameof(AccountChangedEvent),
                                               e.ToJson(), new Exception("AccountChangedEventTypeContract was in incorrect state"));

                    break;
                }

                _chaosKitty.Meow(e.OperationId);

                await _operationExecutionInfoRepository.Save(executionInfo);
            }
        }
コード例 #2
0
        public void Check_Calculations_As_In_Excel_Document()
        {
            Accounts[0].Balance = 50000;
            _accountsCacheService.HandleBalanceChange(Accounts[0].Id, Accounts[0].Balance, 0, AccountBalanceChangeReasonType.RealizedPnL, DateTime.UtcNow);

            _bestPriceConsumer.SendEvent(this, new BestPriceChangeEventArgs(new InstrumentBidAskPair {
                Instrument = "EURUSD", Ask = 1.061M, Bid = 1.06M
            }));
            _bestPriceConsumer.SendEvent(this, new BestPriceChangeEventArgs(new InstrumentBidAskPair {
                Instrument = "BTCEUR", Ask = 1092M, Bid = 1091M
            }));
            _bestPriceConsumer.SendEvent(this, new BestPriceChangeEventArgs(new InstrumentBidAskPair {
                Instrument = "BTCUSD", Ask = 1001M, Bid = 1000M
            }));
            _fxRateCacheService.SetQuote(new InstrumentBidAskPair {
                Instrument = "EURUSD", Ask = 1.061M, Bid = 1.06M
            });
            _fxRateCacheService.SetQuote(new InstrumentBidAskPair {
                Instrument = "BTCEUR", Ask = 1092M, Bid = 1091M
            });
            _fxRateCacheService.SetQuote(new InstrumentBidAskPair {
                Instrument = "BTCUSD", Ask = 1001M, Bid = 1000M
            });

            var positions = new List <Position>
            {
                TestObjectsFactory.CreateOpenedPosition("EURUSD", Accounts[0],
                                                        MarginTradingTestsUtils.TradingConditionId, 100000, 1.05M),

                TestObjectsFactory.CreateOpenedPosition("EURUSD", Accounts[0],
                                                        MarginTradingTestsUtils.TradingConditionId, -200000, 1.04M),

                TestObjectsFactory.CreateOpenedPosition("EURUSD", Accounts[0],
                                                        MarginTradingTestsUtils.TradingConditionId, 50000, 1.061M),

                TestObjectsFactory.CreateOpenedPosition("BTCEUR", Accounts[0],
                                                        MarginTradingTestsUtils.TradingConditionId, 100, 1120, 1.06m)
            };

            foreach (var position in positions)
            {
                _ordersCache.Positions.Add(position);
            }

            positions[0].UpdateClosePrice(1.06M);
            positions[1].UpdateClosePrice(1.061M);
            positions[2].UpdateClosePrice(1.06M);
            positions[3].UpdateClosePrice(1091M);

            positions[3].UpdateCloseFxPrice(1.061M);

            var account = Accounts[0];

            Assert.AreEqual(50000, account.Balance);
            Assert.AreEqual(43673.1m, Math.Round(account.GetTotalCapital(), 5));
            Assert.AreEqual(33481.4m, Math.Round(account.GetFreeMargin(), 1));
            Assert.AreEqual(28385.6M, Math.Round(account.GetMarginAvailable(), 1));
            Assert.AreEqual(-6326.9M, Math.Round(account.GetPnl(), 5));
            Assert.AreEqual(10191.7M, Math.Round(account.GetUsedMargin(), 1));
            Assert.AreEqual(15287.5M, Math.Round(account.GetMarginInit(), 1));
        }