Exemplo n.º 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);
        }
Exemplo n.º 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); }
        }
Exemplo n.º 3
0
 public void FillTable(double[,] finalArray)
 {
     double[] average = CalculationClass.AverageProfitability(finalArray);
     for (int i = 0; i < selectedAssets.Length; i++)
     {
         for (int j = 0; j < i + 1; j++)
         {
             double result = Math.Round(CalculationClass.Cor(finalArray, average, i, j), 4);
             dataGridView[j + 1, i].Value = result;
             if (result == 1)
             {
                 dataGridView[j + 1, i].Style.BackColor = Color.IndianRed;
             }
             else
             {
                 if (result < 0)
                 {
                     dataGridView[j + 1, i].Style.BackColor = Color.LightGreen;
                 }
                 else
                 {
                     dataGridView[j + 1, i].Style.BackColor = Color.Coral;
                 }
             }
         }
     }
 }
Exemplo n.º 4
0
        public static void VolatilityResult(Asset a, double[] data)
        {
            double[] vol = data;

            double[] vol_t = CalculationClass.Vol_t(vol);
            double   avVol = CalculationClass.AverageVol(vol_t);

            a.Volatility = Math.Round(CalculationClass.Volatility(vol_t, avVol), 5) * 100;
        }
Exemplo n.º 5
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);
        }
Exemplo n.º 6
0
        private void CorrelationPortfolio(Asset[] allAssets, int count, int period)
        {
            ParallelOptions parOpts = new ParallelOptions();

            parOpts.CancellationToken = cancelToken.Token;
            try
            {
                parOpts.CancellationToken.ThrowIfCancellationRequested();

                double[,] finalArray = ClassAssistant.ReadDataForCorrelation(allAssets, Program.path + "\\data.txt", period); //ClassAssistant.ReadData(allAssets, path);
                parOpts.CancellationToken.ThrowIfCancellationRequested();

                double[] average = CalculationClass.AverageProfitability(finalArray);
                parOpts.CancellationToken.ThrowIfCancellationRequested();
                for (int i = count; i < allAssets.Length; i++)
                {
                    double sum = 0;
                    for (int j = 0; j < count; j++)
                    {
                        sum += CalculationClass.Cor(finalArray, average, i, j);
                    }
                    allAssets[i].Correlation = Math.Round(sum / count, 2);
                    parOpts.CancellationToken.ThrowIfCancellationRequested();
                }
            }
            catch (IOException ex)
            {
                string[] exception = ex.Message.Split(' ');
                //  DialogResult dr = MessageBox.Show("Ошибка при считывании файла " + exception[exception.Length - 1] + ". Замените этот актив или попробуйте еще раз.\nПовторить попытку?",
                //       "Ошибка!", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
                // MessageBox.Show(ex.Message);
                counter++;
                if (counter == 3)
                {
                    MessageBox.Show("Ошибка при расчете корреляции!", "Ошибка!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    counter = 0;
                }
                else
                {
                    CorrelationPortfolio(allAssets, count, period);
                }
            }
            catch (OperationCanceledException ex) { throw ex; }
            catch (Exception ex)
            {
                string[]     exception = ex.Message.Split(' ');
                DialogResult dr        = MessageBox.Show(ex.Message, "Ошибка!", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
                if (dr == System.Windows.Forms.DialogResult.Retry)
                {
                    CorrelationPortfolio(allAssets, count, period);
                }
            }
        }