Lean
$LEAN_TAG$
LogReturn.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
18
namespace
QuantConnect.Indicators
19
{
20
/// <summary>
21
/// Represents the LogReturn indicator (LOGR)
22
/// - log returns are useful for identifying price convergence/divergence in a given period
23
/// - logr = log (current price / last price in period)
24
/// </summary>
25
public
class
LogReturn
:
WindowIndicator
<IndicatorDataPoint>,
IIndicatorWarmUpPeriodProvider
26
{
27
/// <summary>
28
/// Required period, in data points, for the indicator to be ready and fully initialized.
29
/// </summary>
30
public
int
WarmUpPeriod
=>
Period
;
31
32
/// <summary>
33
/// Initializes a new instance of the LogReturn class with the specified name and period
34
/// </summary>
35
/// <param name="name">The name of this indicator</param>
36
/// <param name="period">The period of the LOGR</param>
37
public
LogReturn
(
string
name,
int
period)
38
: base(name, period)
39
{
40
}
41
42
/// <summary>
43
/// Initializes a new instance of the LogReturn class with the default name and period
44
/// </summary>
45
/// <param name="period">The period of the SMA</param>
46
public
LogReturn
(
int
period)
47
: base($
"LOGR({period})"
, period)
48
{
49
}
50
51
/// <summary>
52
/// Computes the next value for this indicator from the given state.
53
/// - logr = log (current price / last price in period)
54
/// </summary>
55
/// <param name="window">The window of data held in this indicator</param>
56
/// <param name="input">The input value to this indicator on this time step</param>
57
/// <returns>A new value for this indicator</returns>
58
protected
override
decimal
ComputeNextValue
(
IReadOnlyWindow<IndicatorDataPoint>
window,
IndicatorDataPoint
input)
59
{
60
var valuef = input;
61
62
var value0 = window.
Samples
<= window.
Size
63
? window[window.
Count
- 1]
64
: window.
MostRecentlyRemoved
;
65
var result = Math.Log((
double
)(valuef.Value.SafeDivision(value0.Value)));
66
if
(result == Double.NegativeInfinity || result == Double.PositiveInfinity)
67
{
68
return
0;
69
}
70
71
return
result.SafeDecimalCast();
72
}
73
}
74
}
Indicators
LogReturn.cs
Generated by
1.8.17