Exemple #1
0
        public static object GetReturns(object[,] Data, object IsDataReversedOpt, object UseBPReturnsOpt, object NumDaysReturnsOpt)
        {
            double[,] data = Utils.GetMatrix <double>(Data);
            bool reverse        = Utils.GetOptionalParameter(IsDataReversedOpt, false);
            bool useBPReturns   = Utils.GetOptionalParameter(UseBPReturnsOpt, false);
            int  numDaysReturns = Utils.GetOptionalParameter(NumDaysReturnsOpt, 1);

            int nAssets = data.GetLength(1), nDates = data.GetLength(0);

            double[,] ret = new double[nDates, nAssets];
            for (int i = 0; i < nAssets; ++i)
            {
                double[] temp = BasicAnalytics.CalculateReturns((reverse ? data.Column(i).Reverse().ToArray() : data.Column(i)), numDaysReturns, useBPReturns);
                if (reverse)
                {
                    temp = temp.Reverse().ToArray();
                }

                for (int j = 0; j < nDates; ++j)
                {
                    ret[j, i] = temp[j];
                }
            }

            return(ret);
        }
Exemple #2
0
        public static object GetCombinedPriceSeries(object[,] PriceSeries, object[] NAVWeightsOpt, object IsDataReversedOpt)
        {
            bool reversed = Utils.GetOptionalParameter(IsDataReversedOpt, false);

            double[,] data = Utils.GetMatrix <double>(PriceSeries);
            double[] weights;

            int nAssets = data.GetLength(1) - 0;

            if (NAVWeightsOpt == null || NAVWeightsOpt[0] is ExcelMissing || NAVWeightsOpt[0] is ExcelEmpty)
            {
                weights = new double[nAssets];
                for (int i = 0; i < weights.Length; ++i)
                {
                    weights[i] = 1;
                }
            }
            else
            {
                weights = Utils.GetVector <double>(NAVWeightsOpt);
            }

            if (weights.Length == 1)
            {
                double weight = weights[0];

                weights = new double[data.GetLength(1)];
                for (int i = 0; i < weights.Length; ++i)
                {
                    weights[i] = weight;
                }
            }

            int nDates = data.GetLength(0);

            double[] combinedData = new double[nDates];
            for (int i = 0; i < nAssets; ++i)
            {
                double[] temp = BasicAnalytics.CalculateReturns((reversed ? data.Column(i).Reverse().ToArray() : data.Column(i)), 1, false);

                for (int j = 0; j < nDates; ++j)
                {
                    combinedData[j] += weights[i] * temp[j];
                }
            }

            double[] combinedPrices = new double[nDates];
            combinedPrices[0] = 1;
            for (int i = 1; i < nDates; ++i)
            {
                combinedPrices[i] = combinedPrices[i - 1] * (1 + combinedData[i]);
            }

            if (reversed)
            {
                combinedPrices = combinedPrices.Reverse().ToArray();
            }
            return(combinedPrices.ToRange());
        }
Exemple #3
0
        public static object CalculatePortfolioVol(object[] Weights, object[,] PriceLevels,
                                                   object IsDataReversedOpt, object NumDaysReturnsOpt, object AnnualisationFactorOpt)
        {
            double[] weights = Utils.GetVector <double>(Weights);
            double[,] prices = Utils.GetMatrix <double>(PriceLevels);
            bool reverse        = Utils.GetOptionalParameter(IsDataReversedOpt, false);
            int  numDaysReturns = Utils.GetOptionalParameter(NumDaysReturnsOpt, 1);
            int  annualFactor   = Utils.GetOptionalParameter(AnnualisationFactorOpt, 252);

            int nAssets = weights.Length;
            int nDates  = prices.GetLength(0);

            if (reverse)
            {
                double[,] temp = new double[nDates, nAssets];
                for (int i = 0; i < nAssets; ++i)
                {
                    var rev = prices.Column(i).Reverse().ToArray();
                    for (int j = 0; j < nDates; ++j)
                    {
                        temp[j, i] = rev[j];
                    }
                }

                prices = temp;
            }

            double[,] returns = new double[nDates, nAssets];
            for (int i = 0; i < nAssets; ++i)
            {
                double[] ret = BasicAnalytics.CalculateReturns(prices.Column(i), numDaysReturns, false);
                for (int j = 0; j < nDates; ++j)
                {
                    returns[j, i] = ret[j];
                }
            }

            double[,] covar = returns.Covariance();

            double portfolioVol = Math.Sqrt(weights.Multiply(covar).InnerProduct(weights) * annualFactor / numDaysReturns);

            return(portfolioVol);
        }
Exemple #4
0
        public static object Sharpe(object[] PriceSeries, int SharpeWindow, object DataIsReversedOpt,
                                    object NumDaysReturnsOpt, object UseBPReturnsOpt, object AnnualisationFactorOpt,
                                    object ReturnZScoreOpt, object ZWindowOpt, object ExponentialSharpeOpt)
        {
            double[] prices         = Utils.GetVector <double>(PriceSeries);
            bool     dataIsReversed = Utils.GetOptionalParameter(DataIsReversedOpt, false);

            if (dataIsReversed)
            {
                prices = prices.Reverse().ToArray();
            }

            int  numDaysReturns      = Utils.GetOptionalParameter(NumDaysReturnsOpt, 1);
            bool useBPReturns        = Utils.GetOptionalParameter(UseBPReturnsOpt, false);
            int  annualisationFactor = Utils.GetOptionalParameter(AnnualisationFactorOpt, 1);
            bool returnZScore        = Utils.GetOptionalParameter(ReturnZScoreOpt, false);
            int  ZWindow             = Utils.GetOptionalParameter(ZWindowOpt, 252);
            bool exponentialSharpe   = Utils.GetOptionalParameter(ExponentialSharpeOpt, false);

            double[] r = BasicAnalytics.GetSharpe(prices, numDaysReturns, useBPReturns, SharpeWindow, annualisationFactor,
                                                  returnZScore, ZWindow, exponentialSharpe);
            return(r.ToRange());
        }