示例#1
0
        } // End Drawdown:

        /// <summary>
        /// Get the Sharpe Ratio of this period:
        /// </summary>
        /// <param name="equity">Equity of this period.</param>
        /// <param name="averageAnnualGrowth">Percentage annual growth</param>
        /// <param name="rounding">decimal rounding</param>
        /// <param name="startingCash">Starting cash for the conversion to percentages</param>
        /// <returns>decimal sharpe.</returns>
        public static decimal SharpeRatio(SortedDictionary <DateTime, decimal> equity, SortedDictionary <DateTime, decimal> performance, decimal startingCash, decimal averageAnnualGrowth, int rounding = 1)
        {
            //Initialise
            decimal sharpe = 0;

            try {
                decimal[]      equityCash       = equity.Values.ToArray();
                decimal[]      dailyPerformance = performance.Values.ToArray();
                List <decimal> equityPercent    = new List <decimal>();

                //Sharpe = Mean Daily Performance * Sqrt (252) / StdDeviation of Returns
                decimal averageDailyPerformance    = dailyPerformance.Average();
                decimal standardDeviationOfReturns = QCMath.StandardDeviation(dailyPerformance);

                Log.Trace("Avg Daily Performance: " + averageDailyPerformance + " Std Dev: " + standardDeviationOfReturns.ToString());

                if (standardDeviationOfReturns > 0)
                {
                    sharpe = (averageDailyPerformance * Convert.ToDecimal(Math.Sqrt(252))) / standardDeviationOfReturns;
                }

                Log.Trace("SHARPE RATIO: " + sharpe.ToString());
            } catch (Exception err) {
                Log.Error("Statistics.SharpeRatio(): " + err.Message);
            }

            if (sharpe > 10)
            {
                sharpe = Math.Round(sharpe, 0);
            }
            else if (sharpe > 0 & sharpe < 10)
            {
                sharpe = Math.Round(sharpe, 1);
            }
            else if (sharpe < 0)
            {
                sharpe = Math.Round(sharpe, 1);
            }

            return(sharpe);
        }
示例#2
0
            /********************************************************
             * CLASS VARIABLES
             *********************************************************/

            /********************************************************
             * CLASS METHODS
             *********************************************************/
            /// <summary>
            /// Find the standard deviation of a STArray data set
            /// Formula found from here : http://www.johndcook.com/variance.gif
            /// </summary>
            /// <param name="dX">STArray VEctor</param>
            /// <returns>decimal dVariance of the Vector</returns>
            public static decimal GetVariance(IList <decimal> dX)
            {
                /*
                 *                 1     |                              |
                 *      s^2 = ---------- | n*sum( Xi^2 )  - sum(Xi) ^ 2 |
                 *             n(n - 1)  |                              |
                 *
                 */

                decimal dN     = dX.Count;
                decimal dSumXX = QCMath.VectorSquaredSum(dX);
                decimal dSumX2 = (decimal)System.Math.Pow((double)dX.Sum(), 2);

                decimal dVariance = (1 / (dN * (dN - 1))) * ((dN * dSumXX) - dSumX2);

                if (dVariance < 0)
                {
                    //Log.Error("QCMath: GetVariance(): Error calculating variance");
                    //Normally occurs when vector is identical, eg, 30 rows of $50.1, Variance=0:

                    dVariance = 0;
                }
                return(dVariance);
            }