コード例 #1
0
        public double PortfolioValue(BasketOption optionBasket, Share[] sharesBasket, int totalDays, double[] volatility, double[,] correlationMatrix, DateTime beginDate)
        {
            this.basket = optionBasket;
            SimulatedDataFeedProvider simulator        = new SimulatedDataFeedProvider();
            List <DataFeed>           simulationBasket = simulator.GetDataFeed(optionBasket, beginDate);
            int numberDaysPerYear = simulator.NumberOfDaysPerYear;

            int size = sharesBasket.Length;                                 // Number of Shares

            double[] spotsPrev = new double[size];                          // Array that will stock the spots values of the last rebalancing
            spotsPrev = fillSpots(simulationBasket[0], sharesBasket, size); // We initialize the array with the spots of the first day of analysis

            /* Calculation of the option price and the initial composition of the portfolio */
            Pricer         pricer        = new Pricer();
            PricingResults pricesResults = pricer.PriceBasket(optionBasket, beginDate, numberDaysPerYear, spotsPrev, volatility, correlationMatrix);
            double         priceBasket   = pricesResults.Price;

            double[] deltaPrev = new double[size];
            deltaPrev = pricesResults.Deltas;
            double cashRiskFreePrev = priceBasket - dotArrays(deltaPrev, spotsPrev, size);

            this.portfolioValue  = priceBasket;
            this.basketPriceInit = priceBasket;

            int index = 0;

            double[] delta             = new double[size];
            double[] spots             = new double[size];
            double   cashRiskFree      = 0;
            double   variationCashRisk = 0;
            double   freeRate          = 0;
            double   cashRisk          = 0;
            DateTime today;

            double[] optionValue    = new double[totalDays];
            double[] portfolioValue = new double[totalDays];
            double   payoff         = 0;

            /* Arrays used for the plot */
            optionValue[0]    = pricesResults.Price;
            portfolioValue[0] = this.portfolioValue;

            foreach (DataFeed data in simulationBasket)
            {
                if (index != 0 && index != totalDays)
                {
                    cashRisk          = 0;
                    variationCashRisk = 0;
                    today             = data.Date;

                    /* Update spots */
                    spots = fillSpots(data, sharesBasket, size);

                    /* Update priceResults */
                    pricesResults = pricer.PriceBasket(optionBasket, today, numberDaysPerYear, spots, volatility, correlationMatrix);

                    /* Update deltas */
                    delta = pricesResults.Deltas;

                    /* Update cashRisk */
                    cashRisk = dotArrays(delta, spots, size);

                    variationCashRisk = dotArrays(minusArrays(deltaPrev, delta, size), spots, size);
                    freeRate          = RiskFreeRateProvider.GetRiskFreeRateAccruedValue(1 / numberDaysPerYear);

                    /* Update cashRiskFree */
                    cashRiskFree = variationCashRisk + cashRiskFreePrev * freeRate;

                    /* Update portfolioValue */
                    this.portfolioValue = cashRiskFree + cashRisk;

                    /* Memorize the delta and the cashRiskFree calculated for the next balancing */
                    deltaPrev        = delta;
                    cashRiskFreePrev = cashRiskFree;

                    optionValue[index]    = pricesResults.Price;
                    portfolioValue[index] = this.portfolioValue;
                }
                else if (index == totalDays)
                {
                    payoff = optionBasket.GetPayoff(data.PriceList);
                }

                index++;
            }

            /* Partie traçage de courbes */
            using (System.IO.StreamWriter file =
                       new System.IO.StreamWriter(@"C:\Users\ensimag\Desktop\PortfolioBasketOption.txt"))
            {
                for (int i = 0; i < totalDays; i++)
                {
                    // If the line doesn't contain the word 'Second', write the line to the file.
                    file.WriteLine(optionValue[i]);
                    file.WriteLine(portfolioValue[i]);
                }
            }

            return((this.portfolioValue - payoff) / this.basketPriceInit);
        }