public static object EWMAVol(object[] Data, double DecayFactor, object FullVolSeriesOpt, object ReverseDataOpt) { bool fullSeries = Utils.GetOptionalParameter(FullVolSeriesOpt, false); bool reverseData = Utils.GetOptionalParameter(ReverseDataOpt, false); double[] data = Utils.GetVector <double>(Data); if (reverseData) { data = data.Reverse().ToArray(); } EWVol ew = new EWVol(DecayFactor); double[,] ret = new double[data.Length, 1]; for (int i = 0; i < ret.Length; ++i) { int index = (reverseData ? ret.Length - i - 1 : i); ret[index, 0] = ew.Update(data[i]); } if (fullSeries) { return(ret); } else { return(reverseData ? ret[0, 0] : ret[data.Length - 1, 0]); } }
public static double[] GetSharpe(double[] prices, int numDaysReturns, bool useBPReturns, int SharpeWindow, int annualisationFactor, bool returnZScore, int ZWindow, bool exponentialSharpe) { double[] ret = CalculateReturns(prices, numDaysReturns, useBPReturns); int nRet = prices.Length; CircularBuffer <double> returns = new CircularBuffer <double>(SharpeWindow); CircularBuffer <double> sharpes = new CircularBuffer <double>(ZWindow); double decayFactor = 1 - 2.0 / (SharpeWindow + 1); EMA r_ema = new EMA(decayFactor); EWVol v_ema = new EWVol(decayFactor); for (int i = 0; i < nRet; ++i) { returns.Insert(ret[i]); if (returns.Full) { double sharpe = 0; if (!exponentialSharpe) { double r_t = (prices[i] - prices[i - SharpeWindow + 1]) / prices[i - SharpeWindow + 1] / SharpeWindow; sharpe = r_t / returns.SD() * Math.Sqrt(annualisationFactor); } else { r_ema.Update(ret[i]); v_ema.Update(ret[i]); sharpe = r_ema.Value / v_ema.Value * Math.Sqrt(annualisationFactor); } sharpes.Insert(sharpe); } } double[] r = new double[2]; r[0] = sharpes.Last(); r[1] = sharpes.Last() / sharpes.SD(); return(r); }