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  protected readonly Identity Price;
33  protected readonly Identity Volume;
34  protected CompositeIndicator VWAP;
35 
36  /// <summary>
37  /// Initializes a new instance of the VWAP class with the default name and period
38  /// </summary>
39  /// <param name="period">The period of the VWAP</param>
41  : this($"VWAP({period})", period)
42  {
43  }
44 
45  /// <summary>
46  /// Initializes a new instance of the VWAP class with a given name and period
47  /// </summary>
48  /// <param name="name">string - the name of the indicator</param>
49  /// <param name="period">The period of the VWAP</param>
50  public VolumeWeightedAveragePriceIndicator(string name, int period)
51  : base(name)
52  {
53  _period = period;
54 
55  Price = new Identity("Price");
56  Volume = new Identity("Volume");
57 
58  // This class will be using WeightedBy indicator extension
59  VWAP = Price.WeightedBy(Volume, period);
60  }
61 
62  /// <summary>
63  /// Gets a flag indicating when this indicator is ready and fully initialized
64  /// </summary>
65  public override bool IsReady => VWAP.IsReady;
66 
67  /// <summary>
68  /// Required period, in data points, for the indicator to be ready and fully initialized.
69  /// </summary>
70  public int WarmUpPeriod => _period;
71 
72  /// <summary>
73  /// Resets this indicator to its initial state
74  /// </summary>
75  public override void Reset()
76  {
77  Price.Reset();
78  Volume.Reset();
79  VWAP.Reset();
80  base.Reset();
81  }
82 
83  /// <summary>
84  /// Computes the next value of this indicator from the given state
85  /// </summary>
86  /// <param name="input">The input given to the indicator</param>
87  /// <returns>A new value for this indicator</returns>
88  protected override decimal ComputeNextValue(TradeBar input)
89  {
90  Price.Update(input.EndTime, GetTimeWeightedAveragePrice(input));
91  Volume.Update(input.EndTime, input.Volume);
92  return VWAP.Current.Value;
93  }
94 
95  /// <summary>
96  /// Gets an estimated average price to use for the interval covered by the input trade bar.
97  /// </summary>
98  /// <param name="input">The current trade bar input</param>
99  /// <returns>An estimated average price over the trade bar's interval</returns>
100  protected virtual decimal GetTimeWeightedAveragePrice(TradeBar input)
101  {
102  return (input.Open + input.High + input.Low + input.Value) / 4;
103  }
104  }
105 }