Lean
$LEAN_TAG$
HeikinAshi.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
QuantConnect
.
Data
.
Market
;
18
19
namespace
QuantConnect.Indicators
20
{
21
/// <summary>
22
/// This indicator computes the Heikin-Ashi bar (HA)
23
/// The Heikin-Ashi bar is calculated using the following formulas:
24
/// HA_Close[0] = (Open[0] + High[0] + Low[0] + Close[0]) / 4
25
/// HA_Open[0] = (HA_Open[1] + HA_Close[1]) / 2
26
/// HA_High[0] = MAX(High[0], HA_Open[0], HA_Close[0])
27
/// HA_Low[0] = MIN(Low[0], HA_Open[0], HA_Close[0])
28
/// </summary>
29
public
class
HeikinAshi
:
BarIndicator
,
IIndicatorWarmUpPeriodProvider
30
{
31
/// <summary>
32
/// Gets the Heikin-Ashi Open
33
/// </summary>
34
public
IndicatorBase<IndicatorDataPoint>
Open
{
get
; }
35
36
/// <summary>
37
/// Gets the Heikin-Ashi High
38
/// </summary>
39
public
IndicatorBase<IndicatorDataPoint>
High
{
get
; }
40
41
/// <summary>
42
/// Gets the Heikin-Ashi Low
43
/// </summary>
44
public
IndicatorBase<IndicatorDataPoint>
Low
{
get
; }
45
46
/// <summary>
47
/// Gets the Heikin-Ashi Close
48
/// </summary>
49
public
IndicatorBase<IndicatorDataPoint>
Close
{
get
; }
50
51
/// <summary>
52
/// Gets the Heikin-Ashi Volume
53
/// </summary>
54
public
IndicatorBase<IndicatorDataPoint>
Volume
{
get
; }
55
56
/// <summary>
57
/// Initializes a new instance of the <see cref="HeikinAshi"/> class using the specified name.
58
/// </summary>
59
/// <param name="name">The name of this indicator</param>
60
public
HeikinAshi
(
string
name)
61
: base(name)
62
{
63
Open
=
new
Identity
(name +
"_Open"
);
64
High
=
new
Identity
(name +
"_High"
);
65
Low
=
new
Identity
(name +
"_Low"
);
66
Close
=
new
Identity
(name +
"_Close"
);
67
Volume
=
new
Identity
(name +
"_Volume"
);
68
}
69
70
/// <summary>
71
/// Initializes a new instance of the <see cref="HeikinAshi"/> class.
72
/// </summary>
73
public
HeikinAshi
()
74
: this(
"HA"
)
75
{
76
}
77
78
/// <summary>
79
/// Gets a flag indicating when this indicator is ready and fully initialized
80
/// </summary>
81
public
override
bool
IsReady
=> Samples > 1;
82
83
/// <summary>
84
/// Required period, in data points, for the indicator to be ready and fully initialized.
85
/// </summary>
86
public
int
WarmUpPeriod
=> 2;
87
88
/// <summary>
89
/// Computes the next value of this indicator from the given state
90
/// </summary>
91
/// <param name="input">The input given to the indicator</param>
92
/// <returns> A new value for this indicator </returns>
93
protected
override
decimal
ComputeNextValue
(
IBaseDataBar
input)
94
{
95
if
(!
IsReady
)
96
{
97
Open
.Update(input.
Time
, (input.
Open
+ input.
Close
) / 2);
98
Close
.Update(input.
Time
, (input.
Open
+ input.
High
+ input.
Low
+ input.
Close
) / 4);
99
High
.Update(input.
Time
, input.
High
);
100
Low
.Update(input.
Time
, input.
Low
);
101
}
102
else
103
{
104
Open
.Update(input.
Time
, (
Open
.Current.Value +
Close
.Current.Value) / 2);
105
Close
.Update(input.
Time
, (input.
Open
+ input.
High
+ input.
Low
+ input.
Close
) / 4);
106
High
.Update(input.
Time
, Math.Max(input.
High
, Math.Max(
Open
.Current.Value,
Close
.Current.Value)));
107
Low
.Update(input.
Time
, Math.Min(input.
Low
, Math.Min(
Open
.Current.Value,
Close
.Current.Value)));
108
}
109
110
var volume = 0.0m;
111
if
(input is
TradeBar
)
112
{
113
volume = ((
TradeBar
) input).Volume;
114
}
115
else
if
(input is
RenkoBar
)
116
{
117
volume = ((
RenkoBar
) input).Volume;
118
}
119
Volume
.Update(input.
Time
, volume);
120
121
return
Close
.Current.Value;
122
}
123
124
/// <summary>
125
/// Resets this indicator to its initial state
126
/// </summary>
127
public
override
void
Reset
()
128
{
129
Open
.Reset();
130
High
.Reset();
131
Low
.Reset();
132
Close
.Reset();
133
Volume
.Reset();
134
base.Reset();
135
}
136
}
137
}
Indicators
HeikinAshi.cs
Generated by
1.8.17