コード例 #1
0
        static async void SR_CalculateSharpeforPortfolio(Portfolio currentportfolio, List <company> companies)
        {
            //*********************************************************************************************************
            //First, we need to fetch pricing data for all the companies in the portfolio
            // Typically, this is done with perhaps a 3 year horizon of daily prices, but we will do monthly over 5 years to start
            //*********************************************************************************************************

            Console.WriteLine("Calculating sharpe for: {0}", currentportfolio.name);

            //This list will hold all of our expected returns for all the companies in our portfolio
            List <double> expectedreturnforallcompanies = new List <double>();
            //This list will hold all of our standard deviations for all the companies in our portfolio
            List <double> stddevforallcompanies = new List <double>();
            //This list will hold all of the raw pricing data for later covariance calculations
            List <List <double> > pricingdataforcovariance = new List <List <double> >();

            List <double> covariances = new List <double>();

            //var pricingdata = new Tuple<double, double, List<double>>();



            //We need to iterate through all the tickers in our portfolio, and add that expected return to our list above
            for (var i = 0; i < currentportfolio.contents.Count(); i++)
            {
                //Get pricing data for that company
                Console.WriteLine("Fetching pricing data for: {0}", currentportfolio.contents[i].tickersymbol);
                Tuple <double, double, List <double> > pricingdata = await SR_FetchPricingDataandExpectedReturn(currentportfolio.contents[i].tickersymbol);

                //grab values out of Tuple and assign to local variables
                double expectedreturnforsinglecompany = pricingdata.Item2;
                double stddevforcompany = pricingdata.Item1;

                //Add values to their appropriate lists for later use
                expectedreturnforallcompanies.Add(expectedreturnforsinglecompany);
                stddevforallcompanies.Add(stddevforcompany);
                pricingdataforcovariance.Add(pricingdata.Item3);
            }

            //Now we need to calculate the sample covariance for each pair of companies in our portfolio
            for (var i = 0; i < pricingdataforcovariance.Count() - 1; i++)
            {
                for (var j = i + 1; j < pricingdataforcovariance.Count(); j++)
                {
                    double temp = Covariance(pricingdataforcovariance[i], pricingdataforcovariance[j]);
                    Console.WriteLine("Comparing {0} & {1}", i, j);
                    covariances.Add(temp);
                }
            }

            // Sharpe Ratio calculation now that we have all of our pieces! :)

            // Sharpe Ratio (p) = (Expected Return(P) - Risk Free Rate)/Standard Deviation(P)

            // 1) Lets get Expected return of portfolio based on expected returns of assets (calculated already) times the
            // weight that each asset holds ((number of shares * current price of stock)/total value of portfolio)

            // a) Make a list to hold our weights
            List <double> weights             = new List <double>();
            double        totalportfoliovalue = 0;

            for (var i = 0; i < currentportfolio.contents.Count(); i++)
            {
                int numshares = currentportfolio.contents[i].numshares;
                for (var j = 0; j < companies.Count(); j++)
                {
                    if (currentportfolio.contents[i].tickersymbol.Equals(companies[j].tickersymbol))
                    {
                        totalportfoliovalue = totalportfoliovalue + (numshares * companies[j].currentprice);
                        weights.Add(numshares * companies[j].currentprice);
                    }
                }
            }

            //NOTE: there is likely a much more efficient way to do this, perhaps calculate total portfolio value earlier?

            for (var i = 0; i < weights.Count(); i++)
            {
                weights[i] = weights[i] / totalportfoliovalue;
            }

            double portfolioexpectedreturn = 0;

            for (var i = 0; i < weights.Count(); i++)
            {
                portfolioexpectedreturn = portfolioexpectedreturn + (weights[i] * expectedreturnforallcompanies[i]);
            }

            // 2) Now lets get standard deviation of the portfolio!
        }
コード例 #2
0
        static async Task <List <Portfolio> > FetchPortfoliosandContents()
        {
            var client = new HttpClient();
            List <Portfolio> portfolioList = new List <Portfolio>();
            string           Url           = "http://web.engr.oregonstate.edu/~jonesty/api.php/PortfoliosInvestments";

            string content = await client.GetStringAsync(Url);

            JArray portfoliolistC = JArray.Parse(content);


            if (portfoliolistC.Count == 0)
            {
                Console.WriteLine("null returned");
                return(null);
                //Debug.WriteLine("null returned");
            }

            else
            {
                //Console.WriteLine(portfoliolistC);
                for (var i = 0; i < portfoliolistC.Count; i++)

                {
                    //Debug.WriteLine((string)portfolios[i]["username"]);
                    Portfolio         tempportfolio     = new Portfolio();
                    List <Investment> portfoliocontents = new List <Investment>();
                    Investment        tempinvestment    = new Investment();
                    tempportfolio.name = (string)portfoliolistC[i]["portfolioname"];
                    //tempportfolio.totalvalue = (int)portfoliolistC[i]["totalvalue"];
                    tempportfolio.contents = portfoliocontents;
                    if (!portfolioList.Contains(tempportfolio))
                    {
                        //Console.WriteLine("New Portfolio Scanned, adding to list");
                        tempinvestment.tickersymbol  = (string)portfoliolistC[i]["tickersymbol"];
                        tempinvestment.numshares     = (int)portfoliolistC[i]["numshares"];
                        tempinvestment.purchaseprice = (int)portfoliolistC[i]["pricepurchased"];



                        tempportfolio.contents.Add(tempinvestment);
                        //tempportfolio.totalvalue = (int)portfoliolistC[i]["totalvalue"];
                        //Console.WriteLine(tempportfolio.totalvalue);
                        portfolioList.Add(tempportfolio);
                    }
                    else
                    {
                        for (var j = 0; j < portfolioList.Count; j++)
                        {
                            if (portfolioList[j].name == tempportfolio.name)
                            {
                                //Console.WriteLine("wooooooo");
                                //Console.WriteLine((string)portfoliolistC[i]["tickersymbol"]);
                                tempinvestment.tickersymbol  = (string)portfoliolistC[i]["tickersymbol"];
                                tempinvestment.numshares     = (int)portfoliolistC[i]["numshares"];
                                tempinvestment.purchaseprice = (int)portfoliolistC[i]["pricepurchased"];



                                portfolioList[j].contents.Add(tempinvestment);
                            }
                        }
                    }
                    //tickerList.Add(portfoliolistC[i]["tickersymbol"]);
                    //Console.Write(tickerList[j].name);
                }
                //Console.Write()

                /*for (var k = 0; k<portfolioList.Count; k++)
                 * {
                 *  for(var h =0; h<portfolioList[k].contents.Count; h++)
                 *  {
                 *      Console.WriteLine(portfolioList[k].contents[h].tickersymbol);
                 *      Console.WriteLine(portfolioList[k].contents[h].numshares);
                 *      Console.WriteLine(portfolioList[k].contents[h].purchaseprice);
                 *  }
                 *  Console.WriteLine(portfolioList[k].contents[0].tickersymbol);
                 * }*/
                return(portfolioList);
            }
        }