Lean  $LEAN_TAG$
VolumeWeightedAveragePriceIndicator.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 
18 
20 {
21  /// <summary>
22  /// Volume Weighted Average Price (VWAP) Indicator:
23  /// It is calculated by adding up the dollars traded for every transaction (price multiplied
24  /// by number of shares traded) and then dividing by the total shares traded for the day.
25  /// </summary>
27  {
28  /// <summary>
29  /// In this VWAP calculation, typical price is defined by (O + H + L + C) / 4
30  /// </summary>
31  private readonly int _period;
32 
33  /// <summary>
34  /// Indentity indicator for price
35  /// </summary>
36  protected Identity Price { get; }
37 
38  /// <summary>
39  /// Identity indicator for volume
40  /// </summary>
41  protected Identity Volume { get; }
42 
43  /// <summary>
44  /// Volume Weighted Average Price
45  /// </summary>
46  protected CompositeIndicator VWAP { get; }
47 
48  /// <summary>
49  /// Initializes a new instance of the VWAP class with the default name and period
50  /// </summary>
51  /// <param name="period">The period of the VWAP</param>
53  : this($"VWAP({period})", period)
54  {
55  }
56 
57  /// <summary>
58  /// Initializes a new instance of the VWAP class with a given name and period
59  /// </summary>
60  /// <param name="name">string - the name of the indicator</param>
61  /// <param name="period">The period of the VWAP</param>
62  public VolumeWeightedAveragePriceIndicator(string name, int period)
63  : base(name)
64  {
65  _period = period;
66 
67  Price = new Identity("Price");
68  Volume = new Identity("Volume");
69 
70  // This class will be using WeightedBy indicator extension
71  VWAP = Price.WeightedBy(Volume, period);
72  }
73 
74  /// <summary>
75  /// Gets a flag indicating when this indicator is ready and fully initialized
76  /// </summary>
77  public override bool IsReady => VWAP.IsReady;
78 
79  /// <summary>
80  /// Required period, in data points, for the indicator to be ready and fully initialized.
81  /// </summary>
82  public int WarmUpPeriod => _period;
83 
84  /// <summary>
85  /// Resets this indicator to its initial state
86  /// </summary>
87  public override void Reset()
88  {
89  Price.Reset();
90  Volume.Reset();
91  VWAP.Reset();
92  base.Reset();
93  }
94 
95  /// <summary>
96  /// Computes the next value of this indicator from the given state
97  /// </summary>
98  /// <param name="input">The input given to the indicator</param>
99  /// <returns>A new value for this indicator</returns>
100  protected override decimal ComputeNextValue(TradeBar input)
101  {
102  Price.Update(input.EndTime, GetTimeWeightedAveragePrice(input));
103  Volume.Update(input.EndTime, input.Volume);
104  return VWAP.Current.Value;
105  }
106 
107  /// <summary>
108  /// Gets an estimated average price to use for the interval covered by the input trade bar.
109  /// </summary>
110  /// <param name="input">The current trade bar input</param>
111  /// <returns>An estimated average price over the trade bar's interval</returns>
112  protected virtual decimal GetTimeWeightedAveragePrice(TradeBar input)
113  {
114  return (input.Open + input.High + input.Low + input.Value) / 4;
115  }
116  }
117 }