Пример #1
0
        public static Portfolio SinglePortfolio(double[,] array)
        {
            double[,] profit = CalculationClass.Profitability(array);

            double[] averageProfit = CalculationClass.AverageProfitability(profit);

            double[] profitSP = CalculationClass.ProfitabilitySP(profit);

            double averageProfitSP = profitSP.Average();

            double sum_d = 0;

            double[] beta = CalculationClass.Beta(profit, averageProfit, profitSP, averageProfitSP, out sum_d);

            double[] risk = CalculationClass.ResidualRisk(profit, averageProfit, profitSP, averageProfitSP, beta);

            double riskSP = CalculationClass.RiskSP(sum_d, array.GetLength(1));
            double R_sp   = CalculationClass.R_sp(profitSP, averageProfitSP);

            double[] w = new double[beta.Length];
            for (int i = 0; i < w.Length; i++)
            {
                w[i] = Math.Round(1.0 / w.Length, 4) * 100;
            }
            double profitSP_model = Math.Round(SolverClass.GetProfitSP(averageProfit, beta, R_sp), 4) * 100;
            double riskSP_model   = Math.Round(riskSP, 4) * 100;// Math.Round(SolverClass.GetRiskSP(beta, risk, riskSP), 4) * 100;

            Portfolio p = new Portfolio(w, profitSP_model, riskSP_model);

            p.StandDev = Math.Round(riskSP, 4) * 100;
            //   p.Cost = Math.Round(CalculationClass.PortfolioCost(array, p), 2);
            return(p);
        }
Пример #2
0
        // assets для Excel
        private static Portfolio DoCalculations(double[,] array, Asset[] assets, double valueRiskOrProf, bool forExcel, double[] constraints)
        {
            try
            {
                double[,] profit = CalculationClass.Profitability(array);

                double[] averageProfit = CalculationClass.AverageProfitability(profit);

                double[] profitSP = CalculationClass.ProfitabilitySP(profit);

                double averageProfitSP = profitSP.Average();

                double   sum_d = 0;
                double[] beta  = CalculationClass.Beta(profit, averageProfit, profitSP, averageProfitSP, out sum_d);

                double[] risk = CalculationClass.ResidualRisk(profit, averageProfit, profitSP, averageProfitSP, beta);

                double riskSP = CalculationClass.RiskSP(sum_d, array.GetLength(1));
                double R_sp   = CalculationClass.R_sp(profitSP, averageProfitSP);

                if (forExcel)
                {
                    WorkWithExcel.WriteFile(assets, averageProfit, beta, risk, 12, riskSP, R_sp, valueRiskOrProf);
                    return(null);
                }
                else
                {
                    return(SolverClass.DoOpt(averageProfit, beta, risk, valueRiskOrProf, riskSP, R_sp, constraints));//, out p);
                    //return p;
                }
            }
            catch (NullReferenceException) { return(null); }
        }
Пример #3
0
        public static List <Portfolio> DoListPortfolio(double[,] finalArray, Asset[] selectedAssets, double start, double finish, double step, double[] constraints)
        {
            List <Portfolio> results = new List <Portfolio>();
            double           r = 0, p = 0;
            bool             isCycle = false;

            while (start <= finish && !isCycle)
            {
                Portfolio result = ClassAssistant.DoCalculations(finalArray, selectedAssets, start, false, constraints);

                if (result.Profit == p && result.Risk == r)
                {
                    break;
                }
                else
                {
                    if (start > result.Profit)
                    {
                        break;
                    }
                    else
                    {
                        double t = Math.Truncate(result.Profit / step);
                        start = step * t + step;
                    }

                    p = result.Profit;
                    r = result.Risk;
                    double sum = 0;
                    for (int k = 0; k < result.ConvertPortfolio().Length; k++)
                    {
                        result.ConvertPortfolio()[k] = Math.Round(result.ConvertPortfolio()[k], 4) * 100;
                    }
                    result.Profit = Math.Round(result.Profit, 5) * 100;
                    result.Risk   = Math.Round(result.Risk, 4) * 100;
                    sum           = result.GetSum;
                    foreach (var res in results)
                    {
                        if (res.Profit == result.Profit && res.Risk == result.Risk)
                        {
                            isCycle = true;
                        }
                    }
                    if (Math.Abs(sum - 100) <= 0.1 && !isCycle)
                    {
                        double[,] profit = CalculationClass.Profitability(finalArray);
                        result.StandDev  = Math.Round(CalculationClass.StandardDeviation(profit, result), 4) * 100;

                        //  result.Cost = Math.Round(CalculationClass.PortfolioCost(finalArray, result), 2);

                        results.Add(result);
                    }
                }
            }
            return(results);
        }