Lean  $LEAN_TAG$
TargetDownsideDeviation.cs
1 /*
2  * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3  * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14 */
15 
16 using System;
17 using System.Linq;
18 
20 {
21  /// <summary>
22  /// This indicator computes the n-period target downside deviation. The target downside deviation is defined as the
23  /// root-mean-square, or RMS, of the deviations of the realized return’s underperformance from the target return
24  /// where all returns above the target return are treated as underperformance of 0.
25  ///
26  /// Reference: https://www.cmegroup.com/education/files/rr-sortino-a-sharper-ratio.pdf
27  /// </summary>
29  {
30  /// <summary>
31  /// Minimum acceptable return (MAR) for target downside deviation calculation
32  /// </summary>
33  private readonly double _minimumAcceptableReturn;
34 
35  /// <summary>
36  /// Initializes a new instance of the TargetDownsideDeviation class with the specified period and
37  /// minimum acceptable return.
38  ///
39  /// The target downside deviation is defined as the root-mean-square, or RMS, of the deviations of
40  /// the realized return’s underperformance from the target return where all returns above the target
41  /// return are treated as underperformance of 0.
42  /// </summary>
43  /// <param name="period">The sample size of the target downside deviation</param>
44  /// <param name="minimumAcceptableReturn">Minimum acceptable return (MAR) for target downside deviation calculation</param>
45  public TargetDownsideDeviation(int period, double minimumAcceptableReturn = 0)
46  : this($"TDD({period},{minimumAcceptableReturn})", period, minimumAcceptableReturn)
47  {
48  _minimumAcceptableReturn = minimumAcceptableReturn;
49  }
50 
51  /// <summary>
52  /// Initializes a new instance of the TargetDownsideDeviation class with the specified period and
53  /// minimum acceptable return.
54  ///
55  /// The target downside deviation is defined as the root-mean-square, or RMS, of the deviations of
56  /// the realized return’s underperformance from the target return where all returns above the target
57  /// return are treated as underperformance of 0.
58  /// </summary>
59  /// <param name="name">The name of this indicator</param>
60  /// <param name="period">The sample size of the target downside deviation</param>
61  /// <param name="minimumAcceptableReturn">Minimum acceptable return (MAR) for target downside deviation calculation</param>
62  public TargetDownsideDeviation(string name, int period, double minimumAcceptableReturn = 0)
63  : base(name, period)
64  {
65  }
66 
67  /// <summary>
68  /// Computes the next value of this indicator from the given state
69  /// </summary>
70  /// <param name="window">The window for the input history</param>
71  /// <param name="input">The input given to the indicator</param>
72  /// <returns>A new value for this indicator</returns>
74  {
75  var avg = window.Select(x => Math.Pow(Math.Min(0, (double)x.Value - _minimumAcceptableReturn), 2)).Average();
76  return Math.Sqrt(avg).SafeDecimalCast();
77  }
78  }
79 }