/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using Deedle;
using QuantConnect.Packets;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
namespace QuantConnect.Report.ReportElements
{
///
/// Class for render the Sharpe Ratio statistic for a report
///
public class SharpeRatioReportElement : ReportElement
{
///
/// The number of trading days per year to get better result of statistics
///
private double _tradingDaysPerYear;
///
/// Live result object
///
protected LiveResult LiveResult { get; }
///
/// Backtest result object
///
protected BacktestResult BacktestResult { get; }
///
/// Sharpe Ratio from a backtest
///
public virtual decimal? BacktestResultValue => BacktestResult?.TotalPerformance?.PortfolioStatistics?.SharpeRatio;
///
/// Estimate the sharpe ratio of the strategy.
///
/// Name of the widget
/// Location of injection
/// Backtest result object
/// Live result object
/// The number of trading days per year to get better result of statistics
public SharpeRatioReportElement(string name, string key, BacktestResult backtest, LiveResult live, int tradingDaysPerYear)
{
LiveResult = live;
BacktestResult = backtest;
Name = name;
Key = key;
_tradingDaysPerYear = Convert.ToDouble(tradingDaysPerYear, CultureInfo.InvariantCulture);
}
///
/// The generated output string to be injected
///
public override string Render()
{
if (LiveResult == null)
{
Result = BacktestResultValue;
return BacktestResultValue?.ToString("F1") ?? "-";
}
var equityPoints = ResultsUtil.EquityPoints(LiveResult);
var performance = DeedleUtil.PercentChange(new Series(equityPoints).ResampleEquivalence(date => date.Date, s => s.LastValue()));
if (performance.ValueCount == 0)
{
return "-";
}
var sixMonthsAgo = performance.LastKey().AddDays(-180);
var trailingPerformance = performance.Where(series => series.Key >= sixMonthsAgo && series.Key.DayOfWeek != DayOfWeek.Saturday && series.Key.DayOfWeek != DayOfWeek.Sunday)
.Values
.ToList();
var annualStandardDeviation = trailingPerformance.Count < 7 ? 0 : GetAnnualStandardDeviation(trailingPerformance, _tradingDaysPerYear);
if (annualStandardDeviation <= 0)
{
return "-";
}
var annualPerformance = Statistics.Statistics.AnnualPerformance(trailingPerformance, _tradingDaysPerYear);
var liveResultValue = Statistics.Statistics.SharpeRatio(annualPerformance, annualStandardDeviation, 0.0);
Result = liveResultValue;
return liveResultValue.ToString("F2");
}
///
/// Get annual standard deviation
///
/// The performance for the last period
/// The number of trading days per year to get better result of statistics
/// Annual standard deviation.
public virtual double GetAnnualStandardDeviation(List trailingPerformance, double tradingDaysPerYear)
{
return Statistics.Statistics.AnnualStandardDeviation(trailingPerformance, tradingDaysPerYear);
}
}
}