Beispiel #1
0
        public void TestGetPrice()
        {
            var strikePrice         = 100;
            var underlyingPrice     = 90;
            var daysUntilExpiration = 230 / 365.0;
            var volatility          = 0.5;
            var interestRate        = 0.1;
            var d1 = 0.09176057;
            var d2 = -0.30514527;

            var cdfComputer = Substitute.For <INormalCdfComputer>();

            cdfComputer.Compute(Arg.Is <double>(c => Math.Abs(Math.Round(c, 8) - d1) < double.Epsilon)).Returns(0.5365558638);
            cdfComputer.Compute(Arg.Is <double>(c => Math.Abs(Math.Round(c, 8) + d1) < double.Epsilon)).Returns(1 - 0.5365558638);
            cdfComputer.Compute(Arg.Is <double>(c => Math.Abs(Math.Round(c, 8) - d2) < double.Epsilon)).Returns(0.3801277569);
            cdfComputer.Compute(Arg.Is <double>(c => Math.Abs(Math.Round(c, 8) + d2) < double.Epsilon)).Returns(1 - 0.3801277569);

            var callParameter = new PricerParameter(OptionType.Call, underlyingPrice, strikePrice, daysUntilExpiration, interestRate, volatility);
            var putParameter  = new PricerParameter(OptionType.Put, underlyingPrice, strikePrice, daysUntilExpiration, interestRate, volatility);
            var pricer        = new Pricer(cdfComputer);

            var callPrice = Math.Round(pricer.GetPrice(callParameter), 4);
            var putPrice  = Math.Round(pricer.GetPrice(putParameter), 4);

            Assert.That(callPrice, Is.EqualTo(12.5987));
            Assert.That(putPrice, Is.EqualTo(16.4917));
        }
        public void Pricer_UsesSinglePricingStrategy()
        {
            var pricer = new Pricer(new DummyOffer());
            var basket = CreateBasket('A', 'B', 'C', 'D');

            var price = pricer.GetPrice(basket);

            price.ShouldBe(6M);
        }
        public void Pricer_Throws_WhenNoStrategy()
        {
            var pricer = new Pricer();
            var basket = CreateBasket('A', 'B', 'C');

            Should.Throw <InvalidOperationException>(() => {
                pricer.GetPrice(basket);
            });
        }
Beispiel #4
0
        public void IfTempLessThan40ThrowExceptionIcecreamShopIsclosed()
        {
            DateTime date        = new DateTime();
            var      temperature = 37;

            //var actual = pricer.GetPrice(date, input);
            //Console.WriteLine(actual);
            Assert.Throws <System.ArgumentException>(() => pricer.GetPrice(date, temperature));
        }
        public void MultiBuyOffer_PicksSpecifiedNumberOfSKU()
        {
            var pricer = new Pricer(
                new MultiBuyOffer(new SKU('A'), 3, 10M),
                new SingleItemOffer(new SKU('A'), 4M)
                );

            var price = pricer.GetPrice(CreateBasket('A', 'A', 'A', 'A'));

            price.ShouldBe(14M);
        }
        public void Pricer_GetsLatestOffers()
        {
            var offers = new List <IOffer>(new[] {
                new SingleItemOffer(new SKU('A'), 3M),
                new SingleItemOffer(new SKU('B'), 1M)
            });

            var pricer = new Pricer(() => offers);

            var basket = CreateBasket('A', 'B', 'A');

            var price1 = pricer.GetPrice(basket);

            price1.ShouldBe(7M);

            offers.Insert(0, new MultiBuyOffer(new SKU('A'), 2, 5M));

            var price2 = pricer.GetPrice(basket);

            price2.ShouldBe(6M);
        }
Beispiel #7
0
 private void GetQuote()
 {
     try
     {
         var price = _pricer.GetPrice(new PricerParameter(OptionType, UnderlyingPrice, Strike, DaysUntilExpiration / 365.0,
                                                          RiskFreeInterestRateInPercent / 100, VolatilityInPercent / 100));
         Result.Value = price.ToString("0.0000");
     }
     catch (Exception)
     {
         Result.Value = "Internal Error";
     }
 }
        public void Pricer_SortsSKUs()
        {
            var pricer = new Pricer(         //below acts as spy
                new AdHocOffer(x => {
                x.SKUs.ShouldBeInAnyOrder(); //the direction of the ordering doesn't matter to the strategies,
                                             //as long as they get chance to greedily consume grouped SKUs (which any ordering will do)
                x.SKUs.Clear();
                return(true);
            }));

            var basket = CreateBasket('D', 'A', 'C', 'D');

            pricer.GetPrice(basket);
        }
        public void Pricer_UsesMultiplePricingStrategies()
        {
            var pricer = new Pricer(
                CreateOffer('A', 1),
                CreateOffer('B', 2),
                CreateOffer('C', 3),
                CreateOffer('D', 4)
                );

            var basket = CreateBasket('A', 'A', 'C', 'D');

            var price = pricer.GetPrice(basket);

            price.ShouldBe(9);
        }
Beispiel #10
0
        public Response <Price> Price(Models.GetPriceRequest request)
        {
            // in real life scenario Pricer should be injected
            var    pricerService = new Pricer();
            var    response      = new Response <Price>();
            double spot;

            try
            {
                var tradingDates = pricerService.GetTradingDates();
                var price        = pricerService.GetPrice(new GetPriceRequest
                {
                    IsAdvised = request.IsAdvised
                });

                spot = price.Strike;
                // some fancy logic based on downstream response
                if (spot > 1)
                {
                    spot += 0.05;
                    response.Messages.Add(new Message {
                        StatusCode = StatusCodes.Warning, Text = "Spot was adjusted"
                    });
                }
                response.Result = new Price
                {
                    TradingDates = new TradingDates
                    {
                        MaturityDate   = tradingDates.MaturityDate,
                        SettlementDate = tradingDates.SettlementDate
                    },
                    Spot = spot,
                    Id   = price.Id
                };
            }
            catch (Exception ex)
            {
                response.Messages.Add(new Message {
                    StatusCode = StatusCodes.Error, Text = "Could not retrieve the price"
                });
            }

            return(response);
        }