Example #1
0
 public ActionResult AddAsset(Asset asset)
 {
     Portfolio portfolio = db.Portfolios.Find(asset.PortfolioID);
     if (ModelState.IsValid && portfolio != null)
     {
         asset.Prices = getQuotes(asset.Symbol);
         asset.Portfolio = portfolio;
         db.Assets.Add(asset);
         db.SaveChanges();
     }
     return AssetList(asset.PortfolioID);
 }
Example #2
0
        private Asset calculateAssetStats(Asset asset, DateTime startDate, bool meanRateMethodIsSimple, bool expectedRateMethodIsCAPM, double riskFreeRate, double MRP)
        {
            List<AssetPrice> queriedPrices = asset.Prices.Where(p => p.Date >= startDate).ToList();
            queriedPrices.RemoveAt(0);
            //asset.Prices = queriedPrices;

            List<AssetPrice> riskFreeRates = getQuotes("^IRX").Where(p => p.Date >= startDate).ToList();
            riskFreeRates.RemoveAt(0);
            double meanRiskFreeRate = riskFreeRates.Sum(p => p.ClosePrice) / (riskFreeRates.Count);

            List<AssetPrice> marketRates = getQuotes("SPY").Where(p => p.Date >= startDate).ToList();
            double expectedValue_Asset = calculateExpectedValue(queriedPrices, riskFreeRates, meanRateMethodIsSimple);
            double expectedValue_Market = calculateExpectedValue(marketRates, riskFreeRates, meanRateMethodIsSimple);
            double variance_Asset = calculateRiskFreeVariance(queriedPrices, riskFreeRates, expectedValue_Asset, meanRateMethodIsSimple);
            double variance_Market = calculateRiskFreeVariance(marketRates, riskFreeRates, expectedValue_Market, meanRateMethodIsSimple);
            double covariance = calculateCovariance(queriedPrices, marketRates, riskFreeRates, expectedValue_Asset, expectedValue_Market, meanRateMethodIsSimple);

            asset.DailyMeanRate = calculateDailyMeanRate(queriedPrices, meanRateMethodIsSimple);

            asset.DailyVariance = calculateVariance(queriedPrices, asset.DailyMeanRate, meanRateMethodIsSimple);
            asset.AnnualizedVariance = asset.DailyVariance * 252;

            asset.DailyStandardDeviation = Math.Sqrt(asset.DailyVariance);
            asset.AnnualizedStandardDeviation = asset.DailyStandardDeviation * Math.Sqrt(252);

            asset.Covariance = covariance;
            asset.SharpeRatio = (expectedValue_Asset * queriedPrices.Count) / Math.Sqrt((variance_Asset * Math.Pow(queriedPrices.Count, 2)) / 252);
            asset.Beta = covariance / variance_Market;
            asset.HistoricalCorrelation = covariance / (Math.Sqrt(variance_Asset) * Math.Sqrt(variance_Market));

            if (expectedRateMethodIsCAPM)
                asset.AnnualizedMeanRate = riskFreeRate + (asset.Beta * MRP);
            else
                asset.AnnualizedMeanRate = asset.DailyMeanRate * 252;

            return asset;
        }