public PricingResults PriceResults(IOption option, Dictionary <string, decimal> priceMarket, DateTime dateDebut, IDataFeedProvider dataFeedProvider) { Pricer pricer = new Pricer(); int length = priceMarket.Count; double[] spot = new double[length]; for (int i = 0; i < spot.Length; i++) { spot[i] = (double)priceMarket.ElementAt(i).Value; } if (option is VanillaCall) { VanillaCall optionVanilla = (VanillaCall)option; PricingResults priceDelta = pricer.PriceCall(optionVanilla, dateDebut, dataFeedProvider.NumberOfDaysPerYear, spot[0], volatilite); return(priceDelta); } else { BasketOption optionBasket = (BasketOption)option; double[] volatilities = new double[optionBasket.Weights.Length]; for (int i = 0; i < optionBasket.Weights.Length; i++) { volatilities[i] = volatilite; } PricingResults priceDelta = pricer.PriceBasket(optionBasket, dateDebut, dataFeedProvider.NumberOfDaysPerYear, spot, volatilities, matrixCorrelation); return(priceDelta); } }
public List <Portefeuille> getPortefeuillesCouverture(List <DataFeed> listDataFeed, List <PricingResults> ListePricingResult) { List <Portefeuille> listePortefeuille = new List <Portefeuille>(); IEnumerator <PricingResults> enumPR = ListePricingResult.GetEnumerator(); int ind = 0; if (listDataFeed.Count < oObservation) { throw new InvalidOperationException("Il y a moins de données que nécessaire à l'estimation."); } while (ind < oObservation) { ind++; listDataFeed.RemoveAt(0); } IEnumerator <DataFeed> enumLDF = listDataFeed.GetEnumerator(); bool estDebut = true; double valeur = 0; double ancienneValeur = 0; PricingResults ancienPR = null; DataFeed ancienDF = null; string sousJacent = oShares[0].Id; int waitForRebalancing = oRebalancement; double[] currentDelta = new double[oShares.Length]; while (enumPR.MoveNext() && enumLDF.MoveNext()) { PricingResults pr = (PricingResults)enumPR.Current; DataFeed df = (DataFeed)enumLDF.Current; if (estDebut) { //calcul de PI0 valeur = (double)pr.Price; estDebut = false; currentDelta = pr.Deltas; } else { if (waitForRebalancing == 0) { valeur = produitScalaire(currentDelta, df.PriceList) + (ancienneValeur - produitScalaire(currentDelta, ancienDF.PriceList)) * Math.Exp(tauxSR / businessDays); waitForRebalancing = oRebalancement; currentDelta = ancienPR.Deltas; } else { valeur = produitScalaire(currentDelta, df.PriceList) + (ancienneValeur - produitScalaire(currentDelta, ancienDF.PriceList)) * Math.Exp(tauxSR / businessDays); waitForRebalancing--; } } ancienPR = pr; ancienDF = df; ancienneValeur = valeur; Portefeuille port = new Portefeuille(df.Date, valeur); listePortefeuille.Add(port); } return(listePortefeuille); }
public List <PricingResults> pricingUntilMaturity(List <DataFeed> listDataFeed) { if (oName.Equals(null) || oShares.Equals(null) || oMaturity == null || oStrike.Equals(null) || listDataFeed.Count == 0) { throw new NullReferenceException(); // TODO pls check if correct } List <PricingResults> listPrix = new List <PricingResults>(); List <DataFeed> listdf = new List <DataFeed>(listDataFeed); //This line wad added. oSpot = new double[oShares.Length]; BasketOption bask_o = new BasketOption(oName, oShares, oWeights, oMaturity, oStrike); while (listdf.Count > oObservation) { calculVolatility(listdf); for (int myShare = 0; myShare < oShares.Length; myShare++) { oSpot[myShare] = (double)listdf[oObservation].PriceList[oShares[myShare].Id]; } PricingResults pr = basketPricer.PriceBasket(bask_o, listdf[oObservation].Date, businessDays, oSpot, oVolatility, matriceCorr); if (listPrix.Count > 0) { var prev = listPrix.Last(); if (Math.Abs(prev.Price - pr.Price) > 3.5) { var i = 0; } } listPrix.Add(pr); listdf.RemoveAt(0); } return(listPrix); }
public void Actual(DataTestCase testCase) { Data.InitMarketData(testCase.Vol, testCase.Rate, testCase.EvalDate); var pricingCfg = new PricingConfiguration(testCase.GreekTypes); var pricer = new Pricer(); PricingResults expectedResults = null; var actualResults = pricer.Results(testCase.Option, pricingCfg); var error = string.Empty; try { using (StreamReader file = File.OpenText(@"C:\Users\AHMED\source\repos\ConsoleApp1\ResultPricing.text")) { JsonSerializer serializer = new JsonSerializer(); expectedResults = (PricingResults)serializer.Deserialize(file, typeof(PricingResults)); } } catch (Exception e) { Assert.Fail(error = string.Format("Not Deserialize result: {0}", e.Message)); } if (actualResults.CompareResult(expectedResults)) { Assert.Pass("No regression detected"); } else { // save ecart Assert.Fail("regression detected"); } }
public Balancement(IDataFeedProvider dataFeedProvider, IOption option, DateTime dateTmpDebut, int plageEstimation, int periodeRebalancement) { var dateDebut = new DateTime(dateTmpDebut.Year, dateTmpDebut.Month, dateTmpDebut.Day); var priceList = dataFeedProvider.GetDataFeed(option, dateDebut); payoff = payOffaMaturite(option, priceList); pricer = new Pricer(); hedge = new List <decimal>(); priceOption = new List <double>(); dates = new List <DateTime>(); Dictionary <string, double> compo = new Dictionary <string, double> { }; volatilite = Estimateur.Volatilite(priceList, plageEstimation, dateDebut.AddDays(plageEstimation), option, dataFeedProvider); matrixCorrelation = Estimateur.getCorrMatrix(priceList, plageEstimation, dateDebut.AddDays(plageEstimation)); Estimateur.dispMatrix(matrixCorrelation); PricingResults priceDelta = PriceResults(option, priceList[0].PriceList, dateDebut, dataFeedProvider); int i = 0; foreach (string id in option.UnderlyingShareIds) { compo[id] = priceDelta.Deltas[i]; i += 1; } DateTime oldBalancement = dateDebut; portfolio = new Portfolio(Convert.ToDecimal(priceDelta.Price), compo, priceList[0].PriceList); priceOption.Add(priceDelta.Price); hedge.Add(portfolio.portfolioValue); dates.Add(priceList[0].Date); foreach (DataFeed priceAsset_t in priceList.Skip(1)) { if (DayCount.CountBusinessDays(oldBalancement, priceAsset_t.Date) >= periodeRebalancement || priceAsset_t == priceList.Last()) { priceDelta = PriceResults(option, priceAsset_t.PriceList, dateDebut, dataFeedProvider); priceOption.Add(priceDelta.Price); portfolio.UpdatePortfolioValue(priceAsset_t, dataFeedProvider.NumberOfDaysPerYear, oldBalancement); i = 0; foreach (string id in option.UnderlyingShareIds) { compo[id] = priceDelta.Deltas[i]; i += 1; } hedge.Add(portfolio.portfolioValue); dates.Add(priceAsset_t.Date); portfolio.UpdateLiquidity(priceAsset_t); portfolio.UpdateCompo(compo); oldBalancement = priceAsset_t.Date; } } }
public static bool CompareResult(this PricingResults actual, PricingResults expected) { if ((expected.Mtm == actual.Mtm) && actual.Results.CompareGreek(expected.Results)) { return(true); } else { return(false); } }
public override CompletePricingResults getPricingResults(List <DataFeed> pricingData) { double[,] underlyingSpots = ConvertDecimalArray.listDataFeedToDoubleArray(pricingData); double[] volatility = StatComputing.volatilitiesComputing(underlyingSpots); Pricer pricer = new Pricer(); double spot = underlyingSpots[underlyingSpots.GetLength(0) - 1, 0]; PricingResults pricingResults = pricer.Price((VanillaCall)opt, pricingData.Last().Date, 365, spot, volatility.First()); CompletePricingResults completePricingResults = new CompletePricingResults(pricingResults, new double[] { spot }); return(completePricingResults); }
public void TestMethod1() { BasketPricingModel bpm = new BasketPricingModel(); bpm.oMaturity = new DateTime(2015, 8, 20); bpm.oName = "test"; Share share = new Share("ACCOR SA", "AC FP "); bpm.oShares = new Share[1]; bpm.oShares[0] = share; bpm.oStrike = 45.5; DataGestion dg = new DataGestion(); DateTime date = new DateTime(2013, 8, 21); List <DataFeed> ldf = dg.getListDataField(date, bpm.oMaturity, bpm.oShares); PricingResults pR = bpm.getPayOff(ldf); Console.WriteLine(pR.Price); }
public override CompletePricingResults getPricingResults(List <DataFeed> pricingData) { double[,] underlyingSpots = ConvertDecimalArray.listDataFeedToDoubleArray(pricingData); double[,] myCorrelationMatrix = StatComputing.correlationMatrix(underlyingSpots); double[] volatility = StatComputing.volatilitiesComputing(underlyingSpots); Pricer pricer = new Pricer(); int nShares = underlyingSpots.GetLength(1); double[] spots = new double[nShares]; for (int i = 0; i < nShares; i++) { spots[i] = underlyingSpots[nShares - 1, i]; } PricingResults pricingResults = pricer.Price((BasketOption)opt, pricingData.Last().Date, 365, spots, volatility, myCorrelationMatrix);; CompletePricingResults completePricingResults = new CompletePricingResults(pricingResults, spots); return(completePricingResults); }
public Balancement(DataFeed data) { DateTime dateDebut = DateTime.Now; VanillaCall option = new VanillaCall("Vanilla Call", new Share("VanillaShare", "1"), new DateTime(2019, 12, 04), 8); var priceList = dataFeedProvider.GetDataFeed(option, dateDebut); //Je pense en fait que la fonction Pricer c'est la fonction doit être un fonction qui renvoit pricedelta et du //coup c'est la bas qu'on differencie les baskets des vanilles pricer = new Pricer(); PricingResults priceDelta = pricer.PriceCall(option, dateDebut, dataFeedProvider.NumberOfDaysPerYear, Convert.ToDouble(priceList[0].PriceList[option.UnderlyingShareIds[1]]), 0.25); Dictionary <string, double> compo = new Dictionary <string, double> { }; int i = 1; foreach (string id in option.UnderlyingShareIds) { compo[id] = priceDelta.Deltas[i]; i += 1; } // Le premier argument de portefeuille doit être le prix de l'option en 0; //Le deuxième la compo //le troisième le prix des actifs en 0 List <decimal> priceOption = new List <decimal> { Convert.ToDecimal(priceDelta.Price) }; Portfolio portfolio = new Portfolio(priceOption, compo, priceList[0].PriceList); //Traitement des données foreach (DataFeed priceAsset_t in priceList.Skip(1)) { priceDelta = pricer.PriceCall(option, dateDebut, dataFeedProvider.NumberOfDaysPerYear, Convert.ToDouble(priceAsset_t.PriceList[option.UnderlyingShareIds[1]]), 0.25); //updateCompo foreach (string id in option.UnderlyingShareIds) { compo[id] = priceDelta.Deltas[i]; i += 1; } portfolio.UpdatePortfolioValue(priceAsset_t, dataFeedProvider.NumberOfDaysPerYear, periodeRebalancement); portfolio.UpdateCompo(compo); } }
public void TestMethod1() { VanillaCallPricingModel vcpm = new VanillaCallPricingModel(); vcpm.oMaturity = new DateTime(2015, 8, 20); vcpm.oName = "test"; Share share = new Share("ACCOR SA", "AC FP "); vcpm.oShares = new Share[1]; vcpm.oShares[0] = share; vcpm.oStrike = 45.5; vcpm.oSpot = new double[1]; vcpm.oSpot[0] = 48; DataGestion dg = new DataGestion(); DateTime date = new DateTime(2013, 8, 20); List <DataFeed> ldf = dg.getListDataField(date, vcpm.oMaturity, vcpm.oShares); PricingResults pR = vcpm.getPayOff(ldf); Console.WriteLine(pR.Price); }
public static void Main(string[] args) { int strike = 9; double volatility = 0.4; DateTime maturity = new DateTime(2019, 09, 04); DateTime beginDate = new DateTime(2018, 09, 04); int totalDays = DayCount.CountBusinessDays(beginDate, maturity); Share[] share = { new Share("Sfr", "1254798") }; if (share.Length == 1) { VanillaCall callOption = new VanillaCall("Vanille", share[0], maturity, strike); /* Simulated data feeds : */ SimulatedDataFeedProvider simulator = new SimulatedDataFeedProvider(); List <DataFeed> dataFeeds = simulator.GetDataFeed(callOption, beginDate); int numberOfDaysPerYear = simulator.NumberOfDaysPerYear; /* Portfolio initialisation : */ Portfolio portfolio = new Portfolio(); portfolio.PortfolioComposition = new Dictionary <String, double>(); Pricer pricer = new Pricer(); double spot = (double)dataFeeds[0].PriceList[share[0].Id]; PricingResults pricingResults = pricer.PriceCall(callOption, beginDate, totalDays, spot, volatility); double callPrice = pricingResults.Price; portfolio.FirstPortfolioValue = callPrice; portfolio.CurrentPortfolioValue = callPrice; portfolio.CurrentDate = beginDate; double previousDelta = pricingResults.Deltas[0]; portfolio.PortfolioComposition.Add(share[0].Id, previousDelta); double payoff = 0; double[] optionValue = new Double[totalDays]; double[] portfolioValue = new Double[totalDays]; optionValue[0] = callPrice; portfolioValue[0] = portfolio.CurrentPortfolioValue; int indexArrays = 1; /* Skip the first day : because it's already initialized*/ foreach (DataFeed data in dataFeeds.Skip(1)) { if (data != dataFeeds.Last()) { portfolio.updateValue(spot, data, pricer, callOption, volatility, numberOfDaysPerYear); spot = updateSpot(data, share[0]); double pricing = getPricingResult(data, callOption, pricer, volatility, numberOfDaysPerYear); /* Fill arrays of optionValue and portfolioValue */ optionValue[indexArrays] = pricing; portfolioValue[indexArrays] = portfolio.CurrentPortfolioValue; Console.WriteLine("Valeur option = " + optionValue[indexArrays]); Console.WriteLine("Valeur portefeuille = " + portfolioValue[indexArrays]); } /* For the last day : */ else { portfolio.CurrentDate = data.Date; payoff = callOption.GetPayoff(data.PriceList); } indexArrays++; } /* Partie traçage de courbes */ using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\ensimag\Desktop\WriteLines.txt")) { for (int index = 0; index < totalDays; index++) { // If the line doesn't contain the word 'Second', write the line to the file. file.WriteLine(optionValue[index]); file.WriteLine(portfolioValue[index]); } } //double valuePortfolio = (portfolio.currentPortfolioValue - payoff) / portfolio.firstPortfolioValue; //Console.WriteLine("Valeur = " + valuePortfolio); Portfolio portefolioTest = new Portfolio(); double finalportfolioValue = portfolio.updatePortfolio(pricer, callOption, dataFeeds, numberOfDaysPerYear, share[0], totalDays, volatility, beginDate); Console.WriteLine("Valeur = " + finalportfolioValue); } }
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); }
// Création et initialisation du portefeuille de couverture de l'option public HedgingPortfolio createPortfolio(Option option, PricingResults pricingResults, List<DataFeed> dataFeedList, DateTime date) { System.Collections.Generic.Dictionary<string, double> sharesQuantities = new System.Collections.Generic.Dictionary<string, double>(); double[] shareSpots = Utilities.shareSpots(dataFeedList, date); double portfolioSharesValue = 0; for (int i = 0; i < pricingResults.Deltas.Length; i++) { sharesQuantities.Add(option.UnderlyingShareIds[i], pricingResults.Deltas[i]); portfolioSharesValue += pricingResults.Deltas[i] * shareSpots[i]; } double riskFreeRateInvestment = pricingResults.Price - portfolioSharesValue; HedgingPortfolio portfolio = new HedgingPortfolio(sharesQuantities, riskFreeRateInvestment); return portfolio; }
public CompletePricingResults(PricingResults res, double[] spots) { Deltas = res.Deltas; Price = res.Price; Spots = spots; }