public async Task Is_Swaps_Correct()
        {
            _bestPriceConsumer.SendEvent(this, new BestPriceChangeEventArgs(new InstrumentBidAskPair {
                Instrument = "EURUSD", Bid = 1.02M, Ask = 1.04M
            }));

            var instrumentContract = new TradingInstrumentContract
            {
                TradingConditionId  = MarginTradingTestsUtils.TradingConditionId,
                Instrument          = "EURUSD",
                LeverageInit        = 100,
                LeverageMaintenance = 150,
                SwapLong            = 100,
                SwapShort           = 100
            };

            Mock.Get(_tradingInstruments).Setup(s => s.List(It.IsAny <string>()))
            .ReturnsAsync(new List <TradingInstrumentContract> {
                instrumentContract
            });

            await _accountAssetsManager.UpdateTradingInstrumentsCacheAsync();



            var dayPosition = new Position(Guid.NewGuid().ToString("N"), 0, "EURUSD", 20, Accounts[0].Id,
                                           MarginTradingTestsUtils.TradingConditionId, Accounts[0].BaseAssetId, null, MatchingEngineConstants.DefaultMm,
                                           new DateTime(2017, 01, 01, 20, 50, 0), "OpenTrade", OrderType.Market, 20, 1, 1, "USD", 1,
                                           new List <RelatedOrderInfo>(), "LYKKETEST", OriginatorType.Investor, "", "EURUSD", FxToAssetPairDirection.Straight, "");

            dayPosition.SetCommissionRates(100, 0, 0, 1);

            dayPosition.StartClosing(new DateTime(2017, 01, 02, 20, 50, 0), PositionCloseReason.Close,
                                     OriginatorType.Investor, "");
            dayPosition.Close(new DateTime(2017, 01, 02, 20, 50, 0), MatchingEngineConstants.DefaultMm, 2, 1, 1, OriginatorType.Investor,
                              PositionCloseReason.Close, "", "CloseTrade");

            var swapsForDay = _swapService.GetSwaps(dayPosition);

            var twoDayPosition = new Position(Guid.NewGuid().ToString("N"), 0, "EURUSD", 20, Accounts[0].Id,
                                              MarginTradingTestsUtils.TradingConditionId, Accounts[0].BaseAssetId, null, MatchingEngineConstants.DefaultMm,
                                              new DateTime(2017, 01, 01, 20, 50, 0), "OpenTrade", OrderType.Market, 20, 1, 1, "USD", 1,
                                              new List <RelatedOrderInfo>(), "LYKKETEST", OriginatorType.Investor, "", "EURUSD", FxToAssetPairDirection.Straight, "");

            twoDayPosition.SetCommissionRates(100, 0, 0, 1);

            twoDayPosition.StartClosing(new DateTime(2017, 01, 03, 20, 50, 0), PositionCloseReason.Close,
                                        OriginatorType.Investor, "");
            twoDayPosition.Close(new DateTime(2017, 01, 03, 20, 50, 0), MatchingEngineConstants.DefaultMm, 2, 1, 1, OriginatorType.Investor,
                                 PositionCloseReason.Close, "", "CloseTrade");

            var swapsFor2Days = _swapService.GetSwaps(twoDayPosition);

            Assert.AreEqual(5.69863014m, swapsForDay);
            Assert.AreEqual(11.39726027m, swapsFor2Days);
        }
        private void ValidateId(string tradingConditionId, string assetPairId, TradingInstrumentContract contract)
        {
            if (contract?.TradingConditionId != tradingConditionId)
            {
                throw new ArgumentException("TradingConditionId must match with contract tradingConditionId");
            }

            if (contract?.Instrument != assetPairId)
            {
                throw new ArgumentException("AssetPairId must match with contract instrument");
            }
        }
        public async Task <TradingInstrumentContract> Update(string tradingConditionId, string assetPairId,
                                                             [FromBody] TradingInstrumentContract instrument)
        {
            await ValidateTradingInstrument(instrument);

            ValidateId(tradingConditionId, assetPairId, instrument);

            await _tradingInstrumentsRepository.UpdateAsync(
                _convertService.Convert <TradingInstrumentContract, TradingInstrument>(instrument));

            await _eventSender.SendSettingsChangedEvent($"{Request.Path}",
                                                        SettingsChangedSourceType.TradingInstrument, GetIdContractSerialized(instrument));

            return(instrument);
        }
        public async Task <TradingInstrumentContract> Insert([FromBody] TradingInstrumentContract instrument)
        {
            await ValidateTradingInstrument(instrument);

            if (!await _tradingInstrumentsRepository.TryInsertAsync(
                    _convertService.Convert <TradingInstrumentContract, TradingInstrument>(instrument)))
            {
                throw new ArgumentException($"Trading instrument with tradingConditionId {instrument.TradingConditionId}" +
                                            $"and assetPairId {instrument.Instrument} already exists");
            }

            await _eventSender.SendSettingsChangedEvent($"{Request.Path}",
                                                        SettingsChangedSourceType.TradingInstrument, GetIdContractSerialized(instrument));

            return(instrument);
        }
        private async Task ValidateTradingInstrument(TradingInstrumentContract instrument)
        {
            if (instrument == null)
            {
                throw new ArgumentNullException("instrument", "Model is incorrect");
            }

            if (string.IsNullOrWhiteSpace(instrument?.TradingConditionId))
            {
                throw new ArgumentNullException(nameof(instrument.TradingConditionId), "TradingConditionId must be set");
            }

            if (string.IsNullOrWhiteSpace(instrument.Instrument))
            {
                throw new ArgumentNullException(nameof(instrument.Instrument), "Instrument must be set");
            }

            if (await _tradingConditionsRepository.GetAsync(instrument.TradingConditionId) == null)
            {
                throw new InvalidOperationException($"Trading condition {instrument.TradingConditionId} does not exist");
            }

            if (await _assetPairsRepository.GetAsync(instrument.Instrument) == null)
            {
                throw new InvalidOperationException($"Asset pair {instrument.Instrument} does not exist");
            }

            if (instrument.LeverageInit <= 0)
            {
                throw new InvalidOperationException($"LeverageInit must be greather then zero");
            }

            if (instrument.LeverageMaintenance <= 0)
            {
                throw new InvalidOperationException($"LeverageMaintenance must be greather then zero");
            }

            if (await _assetsRepository.GetAsync(instrument.CommissionCurrency) == null)
            {
                throw new InvalidOperationException($"Commission currency {instrument.CommissionCurrency} does not exist");
            }
        }
Beispiel #6
0
        private static async Task CheckTradingInstrumentsApiWorking(IHttpClientGenerator clientGenerator)
        {
            var tradingInstrument = new TradingInstrumentContract
            {
                TradingConditionId  = "t1",
                Instrument          = "BTCUSD",
                LeverageInit        = 1,
                LeverageMaintenance = 1,
                SwapLong            = 1,
                SwapShort           = 1,
                Delta                = 1,
                DealMinLimit         = 1,
                DealMaxLimit         = 1,
                PositionLimit        = 1,
                ShortPosition        = true,
                LiquidationThreshold = 1000,
                CommissionRate       = 1,
                CommissionMin        = 1,
                CommissionMax        = 1,
                CommissionCurrency   = "1",
            };

            var tradingInstrumentApiClient = clientGenerator.Generate <ITradingInstrumentsApi>();
            await tradingInstrumentApiClient.List(null).Dump();

            await tradingInstrumentApiClient.Insert(tradingInstrument).Dump();

            tradingInstrument.LeverageInit = 2;
            await tradingInstrumentApiClient.Update("t1", "BTCUSD", tradingInstrument).Dump();

            await tradingInstrumentApiClient.AssignCollection("t1", new[] { "EURUSD", "EURCHF", "BTCUSD" }).Dump();

            foreach (var tradingInstrumentContract in await tradingInstrumentApiClient.List(null))
            {
                await tradingInstrumentApiClient.Delete(tradingInstrumentContract.TradingConditionId,
                                                        tradingInstrumentContract.Instrument);
            }
        }