Exemplo n.º 1
0
        public ModelManage()
        {
            //double[,] var0 = HistoricCompute.RiskMetrics.var0();
            //double[,] var = HistoricCompute.RiskMetrics.var(var0, HistoricCompute.RiskMetrics.t0.AddDays(1));
            timers.start("ModelManage initialization");
            timers.start("Database initialization");
            qpcptfaw db = new qpcptfaw();
            //Access.Clear_Everglades_Prices();


            DBInitialisation.DBInit(db);
            HistoricCompute.RiskMetrics.initRiskMetrics(24);
            
            timers.stop("Database initialization");
            /*
            for (int i = 1; i < 50 ; i++ )
            {
                DateTime d = DateTime.Today - TimeSpan.FromDays(i);
                AccessDB.setHedgingPortfolioValue(d, 0);
            }
            */

            //Access.ClearPrice(db, 65);
            //Access.ClearAsset(db, 65);
            //Access.ClearPrice(db, 68);
            //Access.ClearAsset(db, 68);
            //Access.ClearPrices(db);
            //Access.ClearDbConnections(db);
            //Access.ClearAssets(db);
            //Access.Clear_Everglades_Price(new DateTime(2016, 3, 2));
            try
            {
                //Access.Clear_Everglades_Price(new DateTime(2016, 3, 9));
                //Access.Clear_Everglades_Price(new DateTime(2016, 3, 10));
                //Access.Clear_Everglades_Price(new DateTime(2016, 3, 11));
                //Access.Clear_Everglades_Price(new DateTime(2016, 3, 5));
                //Access.Clear_Prices_After(new DateTime(2016, 2, 1), Access.GetIdEverglades());
            }
            catch (Exception)
            {}

            // initialize main variables and lists
            instance = this;
            Assets = new List<IAsset>();
            Assets_Currencies = new List<ICurrency>();
            Dictionary<string, Currencies> curEnum = Access.Get_Equities_Currencies();
            foreach (KeyValuePair<string, Currencies> ent in curEnum)
            {
                Currency cur = new Currency(ent.Value);
                Assets.Add(new Equity(ent.Key, cur));
                if (!Assets_Currencies.Any(x => x.getEnum() == ent.Value) && ent.Value != Currencies.EUR)
                {
                    Assets_Currencies.Add(cur);
                }
            }
            everg = new Everglades(Assets, Assets_Currencies);
            shares_everg = 100000;
            // TODO : change or delete



            //simulateBackTestEvolution(true, DateTime.Today - TimeSpan.FromDays(10), TimeSpan.FromDays(1));


            
            try
            {
                cash = Access.getCashDB(DateTime.Today).value;
                Hedging_Portfolio = getHedgingPortfolioFromBD(DateTime.Today);
            }
            catch (Exception)
            {
                cash = shares_everg * everg.getPrice();
                Hedging_Portfolio = new Portfolio(Assets.Concat(Assets_Currencies.ConvertAll(x => (IAsset)x)).ToList());
            }
            DateTime d_temp = DateTime.Today;
            //Hedging_Portfolio = new Portfolio(Assets.Concat(Assets_Currencies.ConvertAll(x => (IAsset)x)).ToList());
            
            
            Operations_History = new LinkedList<Operation.Operation>();
            derivatives = new List<IDerivative>();
            derivatives.Add(new EuropeanCall());
            derivatives.Add(new EuropeanPut());
            derivatives.Add(new AmericanCall());
            derivatives.Add(new AmericanPut());
            derivatives.Add(new AsianCall());
            derivatives.Add(new AsianPut());
            derivatives.Add(new QuantoCall());
            derivatives.Add(new QuantoPut());
            timers.stop("ModelManage initialization");
        }
Exemplo n.º 2
0
        /**
         * simulate an everglades product and it's underlying portfolio
         * evolution with the adviced hedging portfolio, and return a list
         * of Data :
         * * Data of product price evolution
         * * Data of hedging portfolio price evolution
         * * Data of tracking error evolution
         * * Data of cash spent for hedging portfolio evolution
         * 
         */
        public List<Data> simulateHedgeEvolution(bool with_currency)
        {
            RandomNormal rand = new RandomNormal();
            LinkedList<DateTime> list_dates = everg.getObservationDates();
            LinkedList<DateTime> list_anticipated_dates = everg.getAnticipatedDates();
            DateTime first = list_dates.First();
            List<IAsset> simulated_list = new List<IAsset>();
            List<ICurrency> underlying_list_cur = new List<ICurrency>();
            double r = 0.03;
            foreach (IAsset ass in Assets)
            {
                simulated_list.Add(new AssetSimulated(ass, list_dates, rand));
                if (with_currency)
                {
                    Currencies curEnum = ass.getCurrency().getEnum();
                    if (!underlying_list_cur.Any(x => x.getEnum() == curEnum) && curEnum != Currencies.EUR)
                    {
                        underlying_list_cur.Add(new CurrencySimulated(curEnum, rand, r));
                    }
                }
            }
            Everglades everg_simul = new Everglades(simulated_list, underlying_list_cur);
            Portfolio hedge_simul = new Portfolio(simulated_list);
            Data tracking_error = new Data("simulation-graph-trackingerror");
            Data everglades_price = new Data("simulation-graph-prices-everg");
            Data hedge_price = new Data("simulation-graph-prices-hedge");
            Data portsolo_price = new Data("simulation-graph-soloport");
            Data cash_price = new Data("simulation-graph-cash");
            Dictionary<String, Data> list_asset_price = new Dictionary<string, Data>();
            foreach (IAsset asset in simulated_list)
            {
                String nameTemp = asset.getName();
                list_asset_price[nameTemp] = new Data(nameTemp);
            }
            foreach (IAsset cur in underlying_list_cur)
            {
                String nameTemp = cur.getName();
                list_asset_price[nameTemp] = new Data(nameTemp);
            }

            double cash_t = 0;
            double portvalue;
            double portsolovalue;
            double evergvalue;
            
            DateTime date_prev = list_dates.First();
            // used for anticipated end of everglades
            bool breakk = false;
            foreach (DateTime date in list_dates)
            {
                // get prices of assets at these dates in Data
                foreach (IAsset asset in simulated_list)
                {
                    String nameTemp = asset.getName();
                    list_asset_price[nameTemp].add(new DataPoint(date, asset.getPrice(date)));
                }
                foreach (IAsset cur in underlying_list_cur)
                {
                    String nameTemp = cur.getName();
                    list_asset_price[nameTemp].add(new DataPoint(date, cur.getPrice(date)));
                }


                if (date == list_dates.First())
                {
                    Tuple<double, double[], bool> compute = everg_simul.computePrice(date, with_currency);
                    evergvalue = compute.Item1;
                    hedge_simul = everg_simul.getDeltaPortfolio(date, compute.Item2, with_currency);
                    
                    portsolovalue = hedge_simul.getPrice(date);
                    cash_t = evergvalue - portsolovalue;
                    portvalue = portsolovalue + cash_t;
                }
                else
                {
                    // here we (virtually) sell old hedging portfolio
                    double t = (date - date_prev).TotalDays / 360;
                    portvalue = hedge_simul.getPrice(date) + hedge_simul.getDividend(date_prev, date) + cash_t * Math.Exp(r * t);
                    cash_t = portvalue;
                    Tuple<double, double[], bool> compute = everg_simul.computePrice(date, with_currency);
                    // test if date is a constatation date
                    if (list_anticipated_dates.Contains(date))
                    {
                        // if the date is an anticipated constatation date, we check if
                        // we must break now, and if we do we set the price of everglades
                        // with payoff and set breakk to true.
                        if (compute.Item3)
                        {
                            evergvalue = compute.Item1;
                            portsolovalue = hedge_simul.getPrice(date);
                            cash_t -= evergvalue;
                            breakk = true;
                        }
                        else
                        {
                            // if not the last date, we simply price the product and ajust our edge
                            evergvalue = compute.Item1;
                            hedge_simul = everg_simul.getDeltaPortfolio(date, compute.Item2, with_currency);
                            portsolovalue = hedge_simul.getPrice(date);
                            cash_t -= portsolovalue;
                        }
                    }
                    else if (date == everg_simul.getLastDate())
                    {
                        evergvalue = compute.Item1;
                        cash_t -= evergvalue;
                        portsolovalue = hedge_simul.getPrice(date);
                    }
                    else
                    {
                        // if not the last date, we simply price the product and ajust our edge
                        evergvalue = compute.Item1;
                        hedge_simul = everg_simul.getDeltaPortfolio(date, compute.Item2, with_currency);
                        portsolovalue = hedge_simul.getPrice(date);
                        cash_t -= hedge_simul.getPrice(date);
                    }
                }
                
                double err = (evergvalue - portvalue) / evergvalue;
                
                if (!double.IsInfinity(evergvalue) && !double.IsNaN(evergvalue))
                {
                    everglades_price.add(new DataPoint(date, evergvalue));
                }
                if (!double.IsInfinity(portvalue) && !double.IsNaN(portvalue))
                {
                    hedge_price.add(new DataPoint(date, portvalue));
                }
                if (!double.IsInfinity(portsolovalue) && !double.IsNaN(portsolovalue))
                {
                    portsolo_price.add(new DataPoint(date, portsolovalue));
                }
                if (!double.IsInfinity(err) && !double.IsNaN(err))
                {
                    tracking_error.add(new DataPoint(date, err));
                }
                if (!double.IsInfinity(cash_t) && !double.IsNaN(cash_t))
                {
                    cash_price.add(new DataPoint(date, cash_t));
                }
                if (breakk)
                {
                    break;
                }
                date_prev = date;
            }
            List<Data> list = new List<Data>();
            list.Add(everglades_price);
            list.Add(hedge_price);
            list.Add(tracking_error);
            list.Add(cash_price);
            list.Add(portsolo_price);
            foreach (Data dat in list_asset_price.Values)
            {
                list.Add(dat);
            }
            return list;
        }