Esempio n. 1
0
        public async Task OvernightSwapCalculation_Success()
        {
            _bestPriceConsumer.SendEvent(this,
                                         new BestPriceChangeEventArgs(new InstrumentBidAskPair {
                Instrument = "BTCUSD", Bid = 9000M, Ask = 9010M
            }));

            await _accountAssetsRepository.AddOrReplaceAsync(new AccountAssetPair
            {
                TradingConditionId  = MarginTradingTestsUtils.TradingConditionId,
                BaseAssetId         = "USD",
                Instrument          = "BTCUSD",
                LeverageInit        = 100,
                LeverageMaintenance = 150,
                SwapLong            = 100,
                SwapShort           = 100,
                OvernightSwapLong   = 1,
                OvernightSwapShort  = 1
            });

            await _accountAssetsManager.UpdateAccountAssetsCache();

            var accountId = (await _fakeMarginTradingAccountsRepository.GetAllAsync("1"))
                            .First(x => x.ClientId == "1" && x.BaseAssetId == "USD").Id;

            _ordersCache.ActiveOrders.Add(new Order
            {
                Id                 = "1",
                AccountId          = accountId,
                Instrument         = "BTCUSD",
                ClientId           = "1",
                TradingConditionId = "1",
                AccountAssetId     = "USD",
                Volume             = 1,
                OpenDate           = new DateTime(2017, 01, 01, 20, 50, 0),
                CloseDate          = new DateTime(2017, 01, 02, 20, 50, 0),
                MatchedOrders      = new MatchedOrderCollection(new List <MatchedOrder> {
                    new MatchedOrder()
                    {
                        Volume = 1
                    }
                }),
                LegalEntity = "LYKKETEST",
            });

            var accountBalance = (await _fakeMarginTradingAccountsRepository.GetAsync(accountId)).Balance;

            _overnightSwapService.CalculateAndChargeSwaps();

            var calc = _overnightSwapCache.GetAll().First();

            Assert.AreEqual(24.68493151M, calc.Value);
            Assert.True(calc.IsSuccess);
            Assert.AreEqual(accountBalance - 24.68493151M, (await _fakeMarginTradingAccountsRepository.GetAsync(accountId)).Balance);
        }
Esempio n. 2
0
        public async Task Is_Swaps_Correct()
        {
            _bestPriceConsumer.SendEvent(this, new BestPriceChangeEventArgs(new InstrumentBidAskPair {
                Instrument = "EURUSD", Bid = 1.02M, Ask = 1.04M
            }));

            _accountAssetsRepository.AddOrReplaceAsync(new AccountAssetPair
            {
                TradingConditionId  = MarginTradingTestsUtils.TradingConditionId,
                BaseAssetId         = "USD",
                Instrument          = "EURUSD",
                LeverageInit        = 100,
                LeverageMaintenance = 150,
                SwapLong            = 100,
                SwapShort           = 100
            }).Wait();

            await _accountAssetsManager.UpdateAccountAssetsCache();

            var dayOrder = new Order
            {
                AccountAssetId = "USD",
                Instrument     = "EURUSD",
                Volume         = 20,
                OpenDate       = new DateTime(2017, 01, 01, 20, 50, 0),
                CloseDate      = new DateTime(2017, 01, 02, 20, 50, 0),
                MatchedOrders  = new MatchedOrderCollection(new List <MatchedOrder> {
                    new MatchedOrder()
                    {
                        Volume = 20
                    }
                }),
                SwapCommission = 100
            };

            var swapsForDay = _swapService.GetSwaps(dayOrder);

            var twoDayOrder = new Order
            {
                AccountAssetId = "USD",
                Instrument     = "EURUSD",
                Volume         = 20,
                OpenDate       = new DateTime(2017, 01, 01, 20, 50, 0),
                CloseDate      = new DateTime(2017, 01, 03, 20, 50, 0),
                MatchedOrders  = new MatchedOrderCollection(new List <MatchedOrder>()
                {
                    new MatchedOrder()
                    {
                        Volume = 20
                    }
                }),
                SwapCommission = 100
            };

            var swapsFor2Days = _swapService.GetSwaps(twoDayOrder);

            Assert.AreEqual(5.69863014m, swapsForDay);
            Assert.AreEqual(11.39726027m, swapsFor2Days);
        }
Esempio n. 3
0
        public async Task <IAccountAssetPair> AddOrReplaceAccountAssetAsync(IAccountAssetPair model)
        {
            await _pairsRepository.AddOrReplaceAsync(model);

            await UpdateAccountAssetsCache();

            await _clientNotifyService.NotifyTradingConditionsChanged(model.TradingConditionId);

            return(model);
        }
Esempio n. 4
0
        public async Task OvernightSwapCalculation_Success()
        {
            var dsMock = Mock.Get(Container.Resolve <IDateService>());

            dsMock.Setup(d => d.Now()).Returns(new DateTime(2017, 1, 2));

            await _accountAssetsRepository.AddOrReplaceAsync(new AccountAssetPair
            {
                TradingConditionId  = MarginTradingTestsUtils.TradingConditionId,
                BaseAssetId         = "USD",
                Instrument          = "BTCUSD",
                LeverageInit        = 100,
                LeverageMaintenance = 150,
                SwapLong            = 100,
                SwapShort           = 100,
                OvernightSwapLong   = 1,
                OvernightSwapShort  = 1
            });

            await _accountAssetsManager.UpdateAccountAssetsCache();

            var accountId = (await _fakeMarginTradingAccountsRepository.GetAllAsync("1"))
                            .First(x => x.ClientId == "1" && x.BaseAssetId == "USD").Id;

            _ordersCache.ActiveOrders.Add(new Order
            {
                Id                 = "1",
                AccountId          = accountId,
                Instrument         = "BTCUSD",
                ClientId           = "1",
                TradingConditionId = "1",
                AccountAssetId     = "USD",
                Volume             = 1,
                OpenDate           = new DateTime(2017, 01, 01, 20, 50, 0),
                MatchedOrders      = new MatchedOrderCollection(new List <MatchedOrder> {
                    new MatchedOrder()
                    {
                        Volume = 1
                    }
                }),
                LegalEntity = "LYKKETEST",
            });

            _ordersCache.ActiveOrders.Add(new Order
            {
                Id                 = "2",
                AccountId          = accountId,
                Instrument         = "BTCUSD",
                ClientId           = "1",
                TradingConditionId = "1",
                AccountAssetId     = "USD",
                Volume             = 1,
                OpenDate           = new DateTime(2017, 01, 02, 20, 50, 0),
                MatchedOrders      = new MatchedOrderCollection(new List <MatchedOrder> {
                    new MatchedOrder()
                    {
                        Volume = 1
                    }
                }),
                LegalEntity = "LYKKETEST",
            });

            const decimal swapValue = 0.00273973M;

            var initialAccountBalance = (await _fakeMarginTradingAccountsRepository.GetAsync(accountId)).Balance;

            _overnightSwapService.CalculateAndChargeSwaps();

            var calculations = _overnightSwapCache.GetAll();

            Assert.AreEqual(1, calculations.Count);             //only order 1 should be calculated
            var singleCalc = calculations.First();

            Assert.AreEqual(swapValue, singleCalc.Value);
            Assert.True(singleCalc.IsSuccess);
            Assert.AreEqual(initialAccountBalance - swapValue, (await _fakeMarginTradingAccountsRepository.GetAsync(accountId)).Balance);

            //move few hours later and calculate again
            dsMock.Setup(d => d.Now()).Returns(new DateTime(2017, 1, 2, 2, 0, 0));

            //nothing should change
            calculations = _overnightSwapCache.GetAll();
            Assert.AreEqual(1, calculations.Count);
            Assert.AreEqual(initialAccountBalance - swapValue, (await _fakeMarginTradingAccountsRepository.GetAsync(accountId)).Balance);

            _overnightSwapService.CalculateAndChargeSwaps();

            //move to next day and calculate again
            dsMock.Setup(d => d.Now()).Returns(new DateTime(2017, 1, 3));

            _overnightSwapService.CalculateAndChargeSwaps();

            calculations = _overnightSwapCache.GetAll();
            Assert.AreEqual(2, calculations.Count);             // both order 1 and order 2 should be calculated
            Assert.AreEqual(initialAccountBalance - swapValue * 3,
                            (await _fakeMarginTradingAccountsRepository.GetAsync(accountId)).Balance);
        }