Example #1
0
        internal void FreeEquity_MessageCorrect_ReturnsTrue()
        {
            // Arrange
            var account = StubAccountProvider.Create();
            var message = new AccountStateEvent(
                new AccountId("FXCM", "123456789", "SIMULATED"),
                Currency.USD,
                Money.Create(150000m, Currency.AUD),
                Money.Create(150000m, Currency.AUD),
                Money.Zero(Currency.AUD),
                Money.Zero(Currency.AUD),
                Money.Create(2000m, Currency.AUD),
                decimal.Zero,
                string.Empty,
                Guid.NewGuid(),
                this.clock.TimeNow());

            // Act
            account.Apply(message);

            var result = account.FreeEquity;

            // Assert
            Assert.Equal(150000m - 2000m, result.Value);
        }
        internal void CanSerializeAndDeserialize_AccountStateEvent()
        {
            // Arrange
            var accountEvent = new AccountStateEvent(
                new AccountId("FXCM", "D123456", "SIMULATED"),
                Currency.USD,
                Money.Create(100000, Currency.USD),
                Money.Create(100000, Currency.USD),
                Money.Zero(Currency.USD),
                Money.Zero(Currency.USD),
                Money.Zero(Currency.USD),
                0m,
                "N",
                Guid.NewGuid(),
                StubZonedDateTime.UnixEpoch());

            // Act
            var packed   = this.serializer.Serialize(accountEvent);
            var unpacked = (AccountStateEvent)this.serializer.Deserialize(packed);

            // Assert
            Assert.Equal("FXCM-D123456-SIMULATED", accountEvent.AccountId.Value);
            Assert.Equal(accountEvent, unpacked);
            this.Output.WriteLine(Convert.ToBase64String(packed));
        }
Example #3
0
        internal void Update_MessageCorrect_EqualsUpdatedStatusValues()
        {
            // Arrange
            var account = StubAccountProvider.Create();
            var message = new AccountStateEvent(
                new AccountId("FXCM", "123456789", "SIMULATED"),
                Currency.AUD,
                Money.Create(150000m, Currency.AUD),
                Money.Create(150000m, Currency.AUD),
                Money.Zero(Currency.AUD),
                Money.Zero(Currency.AUD),
                Money.Zero(Currency.AUD),
                decimal.Zero,
                string.Empty,
                Guid.NewGuid(),
                this.clock.TimeNow());

            // Act
            account.Apply(message);

            // Assert
            Assert.Equal(150000m, account.CashBalance.Value);
            Assert.Equal(150000m, account.CashStartDay.Value);
            Assert.Equal(0m, account.CashActivityDay.Value);
            Assert.Equal(0m, account.MarginRatio);
            Assert.Equal(0m, account.MarginUsedMaintenance.Value);
            Assert.Equal(0m, account.MarginUsedLiquidation.Value);
            Assert.Equal(string.Empty, account.MarginCallStatus);
        }
Example #4
0
        internal void UpdateAccount_WhenAccountExists_CorrectlyUpdatesAccount()
        {
            // Arrange
            var account = StubAccountProvider.Create();

            this.database.UpdateAccount(account);

            var message = new AccountStateEvent(
                new AccountId("FXCM", "123456789", "SIMULATED"),
                Currency.AUD,
                Money.Create(150000m, Currency.AUD),
                Money.Create(150000m, Currency.AUD),
                Money.Zero(Currency.AUD),
                Money.Zero(Currency.AUD),
                Money.Zero(Currency.AUD),
                decimal.Zero,
                string.Empty,
                Guid.NewGuid(),
                StubZonedDateTime.UnixEpoch());

            account.Apply(message);

            // Act
            this.database.UpdateAccount(account);

            // Assert
            Assert.True(true); // Does not throw
        }
Example #5
0
        public void OnMessage(CollateralReport message)
        {
            Debug.NotNull(this.tradingGateway, nameof(this.tradingGateway));

            var inquiryId     = message.GetField(Tags.CollRptID);
            var accountNumber = message.GetField(Tags.Account);

            this.Logger.LogDebug($"{Received}{Fix} {nameof(CollateralReport)}(InquiryId={inquiryId}, AccountNumber={accountNumber}).");

            var cashBalance           = message.GetDecimal(Tags.CashOutstanding);
            var cashStartDay          = message.GetDecimal(Tags.StartCash);
            var cashDaily             = message.GetDecimal(FxcmTags.CashDaily);
            var marginUsedMaintenance = message.GetDecimal(FxcmTags.UsedMarginMaintenance);
            var marginUsedLiq         = message.GetDecimal(FxcmTags.UsedMarginLiquidation);
            var marginRatio           = message.GetDecimal(Tags.MarginRatio);
            var marginCallStatus      = message.GetField(FxcmTags.MarginCall);

            var accountEvent = new AccountStateEvent(
                this.accountId,
                this.accountCurrency,
                Money.Create(cashBalance, this.accountCurrency),
                Money.Create(cashStartDay, this.accountCurrency),
                Money.Create(cashDaily, this.accountCurrency),
                Money.Create(marginUsedLiq, this.accountCurrency),
                Money.Create(marginUsedMaintenance, this.accountCurrency),
                marginRatio,
                marginCallStatus,
                this.NewGuid(),
                this.TimeNow());

            this.tradingGateway?.Send(accountEvent);
        }
        internal void LoadAccountsCache_WhenAccountInDatabase_CorrectlyCachesAccount()
        {
            // Arrange
            var account = StubAccountProvider.Create();

            this.database.UpdateAccount(account);

            var message = new AccountStateEvent(
                new AccountId("FXCM", "123456789", "SIMULATED"),
                Currency.AUD,
                Money.Create(150000m, Currency.AUD),
                Money.Create(150000m, Currency.AUD),
                Money.Zero(Currency.AUD),
                Money.Zero(Currency.AUD),
                Money.Zero(Currency.AUD),
                decimal.Zero,
                string.Empty,
                Guid.NewGuid(),
                StubZonedDateTime.UnixEpoch());

            account.Apply(message);
            this.database.UpdateAccount(account);

            this.database.ClearCaches();

            // Act
            this.database.LoadAccountsCache();

            // Assert
            Assert.Equal(account.Id, this.database.GetAccountIds().FirstOrDefault());
        }
 private void OnSessionAccountStateUpdated(AccountStateEvent accountState)
 {
     SendOutMessage(
         this
         .CreatePortfolioChangeMessage(accountState.AccountId.To <string>())
         .Add(PositionChangeTypes.CurrentPrice, accountState.Balance)
         .Add(PositionChangeTypes.VariationMargin, accountState.Margin)
         .Add(PositionChangeTypes.UnrealizedPnL, accountState.UnrealisedProfitAndLoss));
 }
Example #8
0
        /// <summary>
        /// обновились портфели
        /// </summary>
        private void ClientOnUpdatePortfolios(AccountStateEvent accountState)
        {
            var portfolioInfo = new List <Portfolio>();

            foreach (var accountStateWallet in accountState.Wallets)
            {
                portfolioInfo.Add(new Portfolio()
                {
                    Number       = accountStateWallet.Key,
                    ValueCurrent = accountStateWallet.Value,
                });
            }

            PortfolioEvent?.Invoke(portfolioInfo);
        }
Example #9
0
        private void OnAccountStateUpdated(AccountStateEvent accountState)
        {
            if (_session == null)
            {
                return;
            }

            AccountInfo.Currency = _session.AccountDetails.Currency;
            AccountInfo.UserName = _session.AccountDetails.Username;
            AccountInfo.ID       = _session.AccountDetails.AccountId.ToString();
            AccountInfo.Balance  = accountState.Balance;
            AccountInfo.Margin   = accountState.Margin;
            AccountInfo.Profit   = accountState.UnrealisedProfitAndLoss;
            AccountInfo.Equity   = AccountInfo.Balance + AccountInfo.Profit;
            OnAccountStateChanged();
        }
Example #10
0
        public static Account ZeroCash()
        {
            var @event = new AccountStateEvent(
                new AccountId("IB", "123456789", "SIMULATED"),
                Currency.USD,
                Money.Zero(Currency.USD),
                Money.Zero(Currency.USD),
                Money.Zero(Currency.USD),
                Money.Zero(Currency.USD),
                Money.Zero(Currency.USD),
                decimal.Zero,
                string.Empty,
                Guid.NewGuid(),
                StubZonedDateTime.UnixEpoch());

            return(new Account(@event));
        }
Example #11
0
        private void OnMessage(AccountStateEvent @event)
        {
            this.EventCount++;
            this.Logger.LogInformation(LogId.Trading, $"{Received}{Event} {@event}.");

            var account = this.database.GetAccount(@event.AccountId);

            if (account is null)
            {
                account = new Account(@event);
                this.database.AddAccount(account);
            }
            else
            {
                account.Apply(@event);
                this.database.UpdateAccount(account);
            }

            this.SendToEventPublisher(@event);
        }
		private void OnSessionAccountStateUpdated(AccountStateEvent accountState)
		{
			SendOutMessage(
				this
					.CreatePortfolioChangeMessage(accountState.AccountId.To<string>())
						.Add(PositionChangeTypes.CurrentPrice, accountState.Balance)
						.Add(PositionChangeTypes.VariationMargin, accountState.Margin)
						.Add(PositionChangeTypes.UnrealizedPnL, accountState.UnrealisedProfitAndLoss));
		}
 /// <summary>
 /// обновились портфели
 /// </summary>
 public void OnAccountStateEvent(AccountStateEvent accountStateEvent)
 {
     UpdatePortfolios?.Invoke(accountStateEvent);
 }
Example #14
0
 private void OnEvent(AccountStateEvent @event)
 {
     this.Publish($"{nameof(Event)}:{Account}:{@event.AccountId.Value}", @event);
 }
Example #15
0
 private void OnMessage(AccountStateEvent @event)
 {
     this.ReceivedObjects.Add(@event);
 }
Example #16
0
 private void OnMessage(AccountStateEvent @event)
 {
     this.SendToBus(@event);
 }