Lean  $LEAN_TAG$
MoneyFlowIndex.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 
17 
19 {
20  /// <summary>
21  /// The Money Flow Index (MFI) is an oscillator that uses both price and volume to
22  /// measure buying and selling pressure
23  ///
24  /// Typical Price = (High + Low + Close)/3
25  /// Money Flow = Typical Price x Volume
26  /// Positive Money Flow = Sum of the money flows of all days where the typical
27  /// price is greater than the previous day's typical price
28  /// Negative Money Flow = Sum of the money flows of all days where the typical
29  /// price is less than the previous day's typical price
30  /// Money Flow Ratio = (14-period Positive Money Flow)/(14-period Negative Money Flow)
31  ///
32  /// Money Flow Index = 100 x Positive Money Flow / ( Positive Money Flow + Negative Money Flow)
33  /// </summary>
35  {
36  /// <summary>
37  /// The sum of positive money flow to compute money flow ratio
38  /// </summary>
40 
41  /// <summary>
42  /// The sum of negative money flow to compute money flow ratio
43  /// </summary>
45 
46  /// <summary>
47  /// The current and previous typical price is used to determine positive or negative money flow
48  /// </summary>
49  public decimal PreviousTypicalPrice { get; private set; }
50 
51  /// <summary>
52  /// Gets a flag indicating when this indicator is ready and fully initialized
53  /// </summary>
54  public override bool IsReady => PositiveMoneyFlow.IsReady && NegativeMoneyFlow.IsReady;
55 
56  /// <summary>
57  /// Required period, in data points, for the indicator to be ready and fully initialized.
58  /// </summary>
59  public int WarmUpPeriod { get; }
60 
61  /// <summary>
62  /// Resets this indicator to its initial state
63  /// </summary>
64  public override void Reset()
65  {
66  PreviousTypicalPrice = 0.0m;
67  PositiveMoneyFlow.Reset();
68  NegativeMoneyFlow.Reset();
69  base.Reset();
70  }
71 
72  /// <summary>
73  /// Initializes a new instance of the MoneyFlowIndex class
74  /// </summary>
75  /// <param name="period">The period of the negative and positive money flow</param>
76  public MoneyFlowIndex(int period)
77  : this($"MFI({period})", period)
78  {
79  }
80 
81  /// <summary>
82  /// Initializes a new instance of the MoneyFlowIndex class
83  /// </summary>
84  /// <param name="name">The name of this indicator</param>
85  /// <param name="period">The period of the negative and positive money flow</param>
86  public MoneyFlowIndex(string name, int period)
87  : base(name)
88  {
89  WarmUpPeriod = period;
90  PositiveMoneyFlow = new Sum(name + "_PositiveMoneyFlow", period);
91  NegativeMoneyFlow = new Sum(name + "_NegativeMoneyFlow", period);
92  }
93 
94  /// <summary>
95  /// Computes the next value of this indicator from the given state
96  /// </summary>
97  /// <param name="input">The input given to the indicator</param>
98  /// <returns>A new value for this indicator</returns>
99  protected override decimal ComputeNextValue(TradeBar input)
100  {
101  var typicalPrice = (input.High + input.Low + input.Close) / 3.0m;
102  var moneyFlow = typicalPrice * input.Volume;
103 
104  PositiveMoneyFlow.Update(input.Time, typicalPrice > PreviousTypicalPrice ? moneyFlow : 0.0m);
105  NegativeMoneyFlow.Update(input.Time, typicalPrice < PreviousTypicalPrice ? moneyFlow : 0.0m);
106  PreviousTypicalPrice = typicalPrice;
107 
108  var totalMoneyFlow = PositiveMoneyFlow.Current.Value + NegativeMoneyFlow.Current.Value;
109  if (totalMoneyFlow == 0.0m)
110  {
111  return 100.0m;
112  }
113 
114  return 100m * PositiveMoneyFlow.Current.Value / totalMoneyFlow;
115  }
116  }
117 }