Lean
$LEAN_TAG$
VariableIndexDynamicAverage.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
/// This indicator computes the n-period adaptive weighted moving average indicator.
22
/// VIDYAi = Pricei x F x ABS(CMOi) + VIDYAi-1 x (1 - F x ABS(CMOi))
23
/// where:
24
/// VIDYAi - is the value of the current period.
25
/// Pricei - is the source price of the period being calculated.
26
/// F = 2/(Period_EMA+1) - is a smoothing factor.
27
/// ABS(CMOi) - is the absolute current value of CMO.
28
/// VIDYAi-1 - is the value of the period immediately preceding the period being calculated.
29
/// </summary>
30
public
class
VariableIndexDynamicAverage
:
WindowIndicator
<IndicatorDataPoint>,
IIndicatorWarmUpPeriodProvider
31
{
32
private
decimal _vidya;
33
private
ChandeMomentumOscillator
_CMO;
34
private
readonly decimal _smoothingFactor;
35
36
/// <summary>
37
/// Initializes a new instance of the <see cref="VariableIndexDynamicAverage"/> class using the specified period.
38
/// </summary>
39
/// <param name="period">The period of the indicator</param>
40
public
VariableIndexDynamicAverage
(
int
period)
41
: this($
"VIDYA({period})"
, period)
42
{
43
}
44
45
/// <summary>
46
/// Initializes a new instance of the <see cref="VariableIndexDynamicAverage"/> class using the specified name and period.
47
/// </summary>
48
/// <param name="name">The name of this indicator</param>
49
/// <param name="period">The period of the indicator</param>
50
public
VariableIndexDynamicAverage
(
string
name,
int
period)
51
: base(name, period)
52
{
53
_CMO =
new
ChandeMomentumOscillator
(period);
54
_smoothingFactor = 2m / (period + 1);
55
}
56
57
/// <summary>
58
/// Gets a flag indicating when this indicator is ready and fully initialized
59
/// </summary>
60
public
override
bool
IsReady
=> Samples >
Period
;
61
62
/// <summary>
63
/// Required period, in data points, for the indicator to be ready and fully initialized.
64
/// </summary>
65
public
override
int
WarmUpPeriod
=>
Period
+ 1;
66
67
/// <summary>
68
/// Computes the next value of this indicator from the given state
69
/// </summary>
70
/// <param name="input">The input given to the indicator</param>
71
/// <param name="window">The window for the input history</param>
72
/// <returns>A new value for this indicator</returns>
73
protected
override
decimal
ComputeNextValue
(
IReadOnlyWindow<IndicatorDataPoint>
window,
IndicatorDataPoint
input)
74
{
75
_CMO.Update(input);
76
if
(!
IsReady
)
77
{
78
_vidya = input.
Value
;
79
return
0m;
80
}
81
var absCMO = Math.Abs(_CMO.Current.Value / 100);
82
_vidya = (input.
Value
* _smoothingFactor * absCMO) + (_vidya * (1 - _smoothingFactor * absCMO));
83
84
return
_vidya;
85
}
86
87
/// <summary>
88
/// Resets this indicator to its initial state
89
/// </summary>
90
public
override
void
Reset
()
91
{
92
_vidya = 0;
93
_CMO.
Reset
();
94
base.Reset();
95
}
96
}
97
}
Indicators
VariableIndexDynamicAverage.cs
Generated by
1.8.17