public void TestInit()
 {
     using (DAL dal = new DAL())
     {
         List <Indice> indices = dal.GetIndices();
         Assert.IsNotNull(indices);
         Assert.AreEqual(6, indices.Count);
         Assert.AreEqual("estox", indices[0].Nom);
         MultiMondeParam param = dal.GetParams();
         Assert.AreEqual(1000, param.NbSamples);
     }
 }
        public ActionResult UpdatePortefeuille()
        {
            using (DAL dal = new DAL())
            {
                DateTime          currD        = dal.GetParams().CurrDate;
                double            portValue    = 0.0;
                double            optimumValue = 0.0;
                MultiMondeParam   param        = dal.GetParams();
                PortefeuilleIdeal IdealPort    = dal.getPortOpti();

                double        currTDC;
                double        currP;
                double        currZC;
                double        Tmoinst  = (param.EndDate - currD).TotalDays / 365.25;
                List <Indice> indList  = dal.GetIndices();
                double        tauxEuro = 0.0;
                foreach (Indice ind in indList)
                {
                    if (ind.Money == "eur")
                    {
                        tauxEuro = ind.InterestRateThisArea;
                        currTDC  = 1.0;
                    }
                    else
                    {
                        currTDC = dal.getSingleChange(currD, "EUR" + ind.Money.ToUpper());
                    }
                    currP  = dal.getSingleData(currD, ind.Nom.ToUpper()) / currTDC;
                    currZC = Math.Exp(-ind.InterestRateThisArea * Tmoinst) / currTDC;

                    portValue    += dal.getPortefeuilleCouverture().GetDelta(ind.Nom) * currP;
                    portValue    += dal.getPortefeuilleCouverture().GetDelta(ind.Money) * currZC;
                    optimumValue += IdealPort.GetDelta(ind.Nom) * currP;
                    optimumValue += IdealPort.GetDelta(ind.Money) * currZC;

                    dal.SetDelta(ind.Nom, IdealPort.GetDelta(ind.Nom));
                    dal.SetDelta(ind.Money, IdealPort.GetDelta(ind.Money));
                }

                double restant = portValue - optimumValue;
                restant *= Math.Exp(tauxEuro * Tmoinst);
                restant += IdealPort.GetDelta("eur");
                dal.SetDelta("eur", restant);

                return(PartialView("Couverture", dal.getPortefeuilleCouverture()));
            }
        }
        public unsafe ActionResult SetDate(MultiMondeParam m)
        {
            using (DAL dal = new DAL())
            {
                if (!ModelState.IsValid)
                {
                    ViewBag.MessageErreur = ModelState["CurrDate"].Errors[0].ErrorMessage;
                    return(PartialView("MultiMondeParam", dal.GetParams()));
                }
                dal.ChangeDate(m.CurrDate);

                MultiMondeParam param = dal.GetParams();
                double          t     = (param.CurrDate - param.Origin).TotalDays / 365.25;
                double          currTDC;
                double          currP;
                DateTime        currD         = param.CurrDate;
                List <Indice>   indList       = dal.GetIndices();
                int             i             = 0;
                double[]        currentPrices = new double[11];
                double[]        volatilities  = new double[11];
                double[]        interestRates = new double[6];
                double[]        correlations  = new double[11 * 11];

                double[] tmpcorr;
                foreach (Indice ind in indList)
                {
                    if (ind.Money != "eur")
                    {
                        currTDC = dal.getSingleChange(currD, "EUR" + ind.Money.ToUpper());
                        currentPrices[i + 5]            = currTDC;
                        volatilities[i + 5]             = ind.MoneyVol;
                        correlations[(i + 5) * (i + 5)] = 1.0;

                        tmpcorr = ind.getCorrTDC();
                        for (int j = 0; j < 11; ++j)
                        {
                            correlations[(i + 5) * 11 + j] = tmpcorr[j];
                        }
                    }
                    currP            = dal.getSingleData(currD, ind.Nom.ToUpper());
                    currentPrices[i] = currP;
                    volatilities[i]  = ind.Vol;
                    interestRates[i] = ind.InterestRateThisArea;

                    tmpcorr = ind.getCorrI();
                    for (int j = 0; j < 11; ++j)
                    {
                        correlations[i * 11 + j] = tmpcorr[j];
                    }

                    i++;
                }

                int nbRows = 1;

                foreach (DateTime constat in param.Constatations)
                {
                    if (DateTime.Compare(constat, currD) <= 0)
                    {
                        nbRows += 1;
                    }
                }
                double[] past = new double[11 * nbRows];



                int row = 0;
                i = 0;
                foreach (Indice ind in indList)
                {
                    if (ind.Money != "eur")
                    {
                        currTDC = dal.getSingleChange(param.Origin, "EUR" + ind.Money.ToUpper());
                        past[11 * row + i + 5] = currTDC;
                    }
                    currP = dal.getSingleData(param.Origin, ind.Nom.ToUpper());
                    past[11 * row + i] = currP;
                    i++;
                }
                foreach (DateTime constat in param.Constatations)
                {
                    row += 1;
                    if (DateTime.Compare(constat, currD) <= 0)
                    {
                        i = 0;
                        foreach (Indice ind in indList)
                        {
                            if (ind.Money != "eur")
                            {
                                currTDC = dal.getSingleChange(constat, "EUR" + ind.Money.ToUpper());
                                past[11 * row + i + 5] = currTDC;
                            }
                            currP = dal.getSingleData(constat, ind.Nom.ToUpper());
                            past[11 * row + i] = currP;
                            i++;
                        }
                    }
                }


                double price;
                double ic;
                PriceMultimonde2021Quanto(param.NbSamples, past, nbRows, t, currentPrices, volatilities, interestRates, correlations, &price, &ic);
                dal.setPrice(price);

                double tauxEuro;
                double currZC;
                double portValue = 0;
                double Tmoinst   = (param.EndDate - currD).TotalDays / 365.25;

                foreach (Indice ind in indList)
                {
                    if (ind.Money == "eur")
                    {
                        tauxEuro = ind.InterestRateThisArea;
                        currTDC  = 1.0;
                    }
                    else
                    {
                        currTDC = dal.getSingleChange(currD, "EUR" + ind.Money.ToUpper());
                    }
                    currP  = dal.getSingleData(currD, ind.Nom.ToUpper()) / currTDC;
                    currZC = Math.Exp(-ind.InterestRateThisArea * Tmoinst) / currTDC;

                    portValue += dal.getPortefeuilleCouverture().GetDelta(ind.Nom) * currP;
                    portValue += dal.getPortefeuilleCouverture().GetDelta(ind.Money) * currZC;
                }

                dal.setPnl(price - portValue);

                return(PartialView("MultiMondeParam", dal.GetParams()));
            }
        }
        public unsafe ActionResult CalculerDeltas()
        {
            using (DAL dal = new DAL())
            {
                CouvertureIdealeViewModel vm = new CouvertureIdealeViewModel();
                vm.IdealPort = new PortefeuilleIdeal();

                MultiMondeParam param = dal.GetParams();
                double          t     = (param.CurrDate - param.Origin).TotalDays / 365.25;
                double          currTDC;
                double          currP;
                DateTime        currD         = param.CurrDate;
                List <Indice>   indList       = dal.GetIndices();
                int             i             = 0;
                double[]        currentPrices = new double[11];
                double[]        volatilities  = new double[11];
                double[]        interestRates = new double[6];
                double[]        correlations  = new double[11 * 11];

                double[] tmpcorr;
                foreach (Indice ind in indList)
                {
                    if (ind.Money != "eur")
                    {
                        currTDC = dal.getSingleChange(currD, "EUR" + ind.Money.ToUpper());
                        currentPrices[i + 5]            = currTDC;
                        volatilities[i + 5]             = ind.MoneyVol;
                        correlations[(i + 5) * (i + 5)] = 1.0;

                        tmpcorr = ind.getCorrTDC();
                        for (int j = 0; j < 11; ++j)
                        {
                            correlations[(i + 5) * 11 + j] = tmpcorr[j];
                        }
                    }
                    currP            = dal.getSingleData(currD, ind.Nom.ToUpper());
                    currentPrices[i] = currP;
                    volatilities[i]  = ind.Vol;
                    interestRates[i] = ind.InterestRateThisArea;

                    tmpcorr = ind.getCorrI();
                    for (int j = 0; j < 11; ++j)
                    {
                        correlations[i * 11 + j] = tmpcorr[j];
                    }

                    i++;
                }

                int nbRows = 1;

                foreach (DateTime constat in param.Constatations)
                {
                    if (DateTime.Compare(constat, currD) <= 0)
                    {
                        nbRows += 1;
                    }
                }
                double[] past = new double[11 * nbRows];



                int row = 0;
                i = 0;
                foreach (Indice ind in indList)
                {
                    if (ind.Money != "eur")
                    {
                        currTDC = dal.getSingleChange(param.Origin, "EUR" + ind.Money.ToUpper());
                        past[11 * row + i + 5] = currTDC;
                    }
                    currP = dal.getSingleData(param.Origin, ind.Nom.ToUpper());
                    past[11 * row + i] = currP;
                    i++;
                }
                foreach (DateTime constat in param.Constatations)
                {
                    row += 1;
                    if (DateTime.Compare(constat, currD) <= 0)
                    {
                        i = 0;
                        foreach (Indice ind in indList)
                        {
                            if (ind.Money != "eur")
                            {
                                currTDC = dal.getSingleChange(constat, "EUR" + ind.Money.ToUpper());
                                past[11 * row + i + 5] = currTDC;
                            }
                            currP = dal.getSingleData(constat, ind.Nom.ToUpper());
                            past[11 * row + i] = currP;
                            i++;
                        }
                    }
                }

                IntPtr deltasPtr;

                DeltasMultimonde2021Quanto(param.NbSamples, past, nbRows, t, currentPrices, volatilities, interestRates, correlations, out deltasPtr);

                double[] deltas = new double[11];
                System.Runtime.InteropServices.Marshal.Copy(deltasPtr, deltas, 0, 11); //<- deltas contient maintenant les deltas

                String[] names = new String[11] {
                    "estoxx",
                    "sp500",
                    "n225",
                    "hang",
                    "ftse",
                    "asx",
                    "usd",
                    "jpy",
                    "hkd",
                    "gbp",
                    "aud"
                };

                for (int k = 0; k < 11; ++k)
                {
                    vm.IdealPort.SetDelta(names[k], deltas[k]);
                }

                double tauxEuro = 0;
                double currZC;
                double optimumValue = 0;
                double Tmoinst      = (param.EndDate - currD).TotalDays / 365.25;

                foreach (Indice ind in indList)
                {
                    if (ind.Money == "eur")
                    {
                        tauxEuro = ind.InterestRateThisArea;
                        currTDC  = 1.0;
                    }
                    else
                    {
                        currTDC = dal.getSingleChange(currD, "EUR" + ind.Money.ToUpper());
                        //currTDC = 0.5;
                    }
                    currP = dal.getSingleData(currD, ind.Nom.ToUpper()) / currTDC;
                    //currP = 100/currTDC;
                    currZC = Math.Exp(-ind.InterestRateThisArea * Tmoinst) / currTDC;

                    optimumValue += vm.IdealPort.GetDelta(ind.Nom) * currP;
                    optimumValue += vm.IdealPort.GetDelta(ind.Money) * currZC;
                }
                double price;
                double ic;
                PriceMultimonde2021Quanto(param.NbSamples, past, nbRows, t, currentPrices, volatilities, interestRates, correlations, &price, &ic);
                double restant = price - optimumValue;
                restant *= Math.Exp(tauxEuro * Tmoinst);
                restant += vm.IdealPort.GetDelta("eur");
                vm.IdealPort.SetDelta("eur", restant);
                dal.saveOpti(vm.IdealPort);

                return(PartialView("CouvertureIdeale", vm));

                //return Content("Deltas en cours d'implémentation");
            }
        }