Exemple #1
0
        public async Task UpdateAsync(Index index)
        {
            if (!index.ValidateValue())
            {
                throw new InvalidOperationException("Invalid index value");
            }

            if (!index.ValidateWeights())
            {
                throw new InvalidOperationException("Invalid index weights");
            }

            IndexSettings indexSettings = await _indexSettingsService.GetByIndexAsync(index.Name);

            if (indexSettings == null)
            {
                throw new InvalidOperationException("Index settings not found");
            }

            IndexPrice indexPrice = await GetByIndexAsync(index.Name);

            if (indexPrice == null)
            {
                indexPrice = IndexPrice.Init(index.Name, index.Value, index.Timestamp, index.Weights);

                await _indexPriceRepository.InsertAsync(indexPrice);

                _log.InfoWithDetails("The index price initialized", indexPrice);
            }
            else
            {
                IndexSettlementPrice indexSettlementPrice = IndexSettlementPriceCalculator.Calculate(
                    index.Value, indexPrice.Value, indexSettings.Alpha, indexPrice.K, indexPrice.Price,
                    index.Timestamp, indexPrice.Timestamp, indexSettings.TrackingFee, indexSettings.PerformanceFee,
                    indexSettings.IsShort);

                indexPrice.Update(index.Value, index.Timestamp, indexSettlementPrice.Price, indexSettlementPrice.K,
                                  indexSettlementPrice.R, indexSettlementPrice.Delta, index.Weights);

                await _indexPriceRepository.UpdateAsync(indexPrice);

                _log.InfoWithDetails("The index price calculated", new
                {
                    indexPrice,
                    indexSettings
                });
            }

            _cache.Set(indexPrice);
        }
        public void Calculate_Expected_Price()
        {
            // arrange

            decimal  currentIndexValue  = 500;
            decimal  previousIndexValue = 400;
            decimal  alpha                  = .5m;
            decimal  previousK              = .001m;
            decimal  previousPrice          = 1000;
            DateTime currentIndexTimestamp  = DateTime.UtcNow;
            DateTime previousIndexTimestamp = DateTime.UtcNow.AddMinutes(-1);
            decimal  trackingFee            = .2m;
            decimal  performanceFee         = .2m;

            var expectedIndexSettlementPrice = new IndexSettlementPrice(
                1249.9995730872490702824580044m,
                125.0005m,
                0.25m,
                0.0000018973891608480064764218m);

            // act

            IndexSettlementPrice actualIndexSettlementPrice = IndexSettlementPriceCalculator.Calculate(
                currentIndexValue,
                previousIndexValue,
                alpha,
                previousK,
                previousPrice,
                currentIndexTimestamp,
                previousIndexTimestamp,
                trackingFee,
                performanceFee,
                false);

            // assert

            Assert.IsTrue(AreEqual(expectedIndexSettlementPrice, actualIndexSettlementPrice, 10),
                          "Wrong index settlement price");
        }
Exemple #3
0
        public void Calculate_Expected_Price()
        {
            // arrange

            decimal  currentIndexValue  = 500;
            decimal  previousIndexValue = 400;
            decimal  alpha                  = .5m;
            decimal  previousK              = .001m;
            decimal  previousPrice          = 1000;
            DateTime currentIndexTimestamp  = DateTime.UtcNow;
            DateTime previousIndexTimestamp = DateTime.UtcNow.AddMinutes(-1);
            decimal  trackingFee            = .2m;
            decimal  performanceFee         = .2m;

            var expectedIndexSettlementPrice = new IndexSettlementPrice(
                1249.9995719176186738968163368m,
                125.0005m,
                0.25m,
                0.0000019025875158548959918823m);

            // act

            IndexSettlementPrice actualIndexSettlementPrice = IndexSettlementPriceCalculator.Calculate(
                currentIndexValue,
                previousIndexValue,
                alpha,
                previousK,
                previousPrice,
                currentIndexTimestamp,
                previousIndexTimestamp,
                trackingFee,
                performanceFee,
                false);

            // assert

            Assert.IsTrue(AreEqual(expectedIndexSettlementPrice, actualIndexSettlementPrice, 10),
                          "Wrong index settlement price");
        }
 private static bool AreEqual(IndexSettlementPrice left, IndexSettlementPrice right, int accuracy)
 => Math.Round(left.Delta, accuracy) == Math.Round(right.Delta, accuracy) &&
 Math.Round(left.K, accuracy) == Math.Round(right.K, accuracy) &&
 Math.Round(left.R, accuracy) == Math.Round(right.R, accuracy) &&
 Math.Round(left.Price, accuracy) == Math.Round(right.Price, accuracy);