Lean
$LEAN_TAG$
OnBalanceVolume.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
QuantConnect
.
Data
.
Market
;
17
18
namespace
QuantConnect.Indicators
19
{
20
/// <summary>
21
/// This indicator computes the On Balance Volume (OBV).
22
/// The On Balance Volume is calculated by determining the price of the current close price and previous close price.
23
/// If the current close price is equivalent to the previous price the OBV remains the same,
24
/// If the current close price is higher the volume of that day is added to the OBV, while a lower close price will
25
/// result in negative value.
26
/// </summary>
27
public
class
OnBalanceVolume
:
TradeBarIndicator
,
IIndicatorWarmUpPeriodProvider
28
{
29
private
TradeBar
_previousInput;
30
31
/// <summary>
32
/// Initializes a new instance of the Indicator class using the specified name.
33
/// </summary>
34
public
OnBalanceVolume
()
35
: base(
"OBV"
)
36
{
37
}
38
39
/// <summary>
40
/// Initializes a new instance of the Indicator class using the specified name.
41
/// </summary>
42
/// <param name="name">The name of this indicator</param>
43
public
OnBalanceVolume
(
string
name)
44
: base(name)
45
{
46
}
47
48
/// <summary>
49
/// Gets a flag indicating when this indicator is ready and fully initialized
50
/// </summary>
51
public
override
bool
IsReady
=> _previousInput !=
null
;
52
53
/// <summary>
54
/// Required period, in data points, for the indicator to be ready and fully initialized.
55
/// </summary>
56
public
int
WarmUpPeriod
=> 1;
57
58
/// <summary>
59
/// Computes the next value of this indicator from the given state
60
/// </summary>
61
/// <param name="input">The input given to the indicator</param>
62
/// <returns> A new value for this indicator </returns>
63
protected
override
decimal
ComputeNextValue
(
TradeBar
input)
64
{
65
var obv = Current.Value;
66
67
if
(_previousInput !=
null
)
68
{
69
if
(input.
Value
> _previousInput.
Value
)
70
{
71
obv += input.
Volume
;
72
Update(input);
73
}
74
else
if
(input.
Value
< _previousInput.
Value
)
75
{
76
obv -= input.
Volume
;
77
Update(input);
78
}
79
}
80
else
81
{
82
obv = input.
Volume
;
83
Update(input);
84
}
85
86
_previousInput = input;
87
return
obv;
88
}
89
90
/// <summary>
91
/// Resets this indicator to its initial state
92
/// </summary>
93
public
override
void
Reset
()
94
{
95
_previousInput =
null
;
96
base.Reset();
97
}
98
}
99
}
Indicators
OnBalanceVolume.cs
Generated by
1.8.17