Supported Indicators
Money Flow Index
Introduction
The Money Flow Index (MFI) is an oscillator that uses both price and volume to measure buying and selling pressure Typical Price = (High + Low + Close)/3 Money Flow = Typical Price x Volume Positive Money Flow = Sum of the money flows of all days where the typical price is greater than the previous day's typical price Negative Money Flow = Sum of the money flows of all days where the typical price is less than the previous day's typical price Money Flow Ratio = (14-period Positive Money Flow)/(14-period Negative Money Flow) Money Flow Index = 100 x Positive Money Flow / ( Positive Money Flow + Negative Money Flow)
To view the implementation of this indicator, see the LEAN GitHub repository.
Using MFI Indicator
To create an automatic indicator for MoneyFlowIndex, call the MFImfi helper method from the QCAlgorithm class. The MFImfi method creates a MoneyFlowIndex object, hooks it up for automatic updates, and returns it so you can used it in your algorithm. In most cases, you should call the helper method in the Initializeinitialize method.
public class MoneyFlowIndexAlgorithm : QCAlgorithm
{
private Symbol _symbol;
private MoneyFlowIndex _mfi;
public override void Initialize()
{
_symbol = AddEquity("SPY", Resolution.Daily).Symbol;
_mfi = MFI(_symbol, 20);
}
public override void OnData(Slice data)
{
if (_mfi.IsReady)
{
// The current value of _mfi is represented by itself (_mfi)
// or _mfi.Current.Value
Plot("MoneyFlowIndex", "mfi", _mfi);
// Plot all properties of abands
Plot("MoneyFlowIndex", "positivemoneyflow", _mfi.PositiveMoneyFlow);
Plot("MoneyFlowIndex", "negativemoneyflow", _mfi.NegativeMoneyFlow);
}
}
} class MoneyFlowIndexAlgorithm(QCAlgorithm):
def initialize(self) -> None:
self._symbol = self.add_equity("SPY", Resolution.DAILY).symbol
self._mfi = self.mfi(self._symbol, 20)
def on_data(self, slice: Slice) -> None:
if self._mfi.is_ready:
# The current value of self._mfi is represented by self._mfi.current.value
self.plot("MoneyFlowIndex", "mfi", self._mfi.current.value)
# Plot all attributes of self._mfi
self.plot("MoneyFlowIndex", "positive_money_flow", self._mfi.positive_money_flow.current.value)
self.plot("MoneyFlowIndex", "negative_money_flow", self._mfi.negative_money_flow.current.value)For more information about this method, see the QCAlgorithm classQCAlgorithm class.
You can manually create a MoneyFlowIndex indicator, so it doesn't automatically update. Manual indicators let you update their values with any data you choose.
Updating your indicator manually enables you to control when the indicator is updated and what data you use to update it. To manually update the indicator, call the Updateupdate method. The indicator will only be ready after you prime it with enough data.
public class MoneyFlowIndexAlgorithm : QCAlgorithm
{
private Symbol _symbol;
private MoneyFlowIndex _moneyflowindex;
public override void Initialize()
{
_symbol = AddEquity("SPY", Resolution.Daily).Symbol;
_moneyflowindex = new MoneyFlowIndex(20);
}
public override void OnData(Slice data)
{
if (data.Bars.TryGetValue(_symbol, out var bar))
_moneyflowindex.Update(bar);
if (_moneyflowindex.IsReady)
{
// The current value of _moneyflowindex is represented by itself (_moneyflowindex)
// or _moneyflowindex.Current.Value
Plot("MoneyFlowIndex", "moneyflowindex", _moneyflowindex);
// Plot all properties of abands
Plot("MoneyFlowIndex", "positivemoneyflow", _moneyflowindex.PositiveMoneyFlow);
Plot("MoneyFlowIndex", "negativemoneyflow", _moneyflowindex.NegativeMoneyFlow);
}
}
} class MoneyFlowIndexAlgorithm(QCAlgorithm):
def initialize(self) -> None:
self._symbol = self.add_equity("SPY", Resolution.DAILY).symbol
self._moneyflowindex = MoneyFlowIndex(20)
def on_data(self, slice: Slice) -> None:
bar = slice.bars.get(self._symbol)
if bar:
self._moneyflowindex.update(bar)
if self._moneyflowindex.is_ready:
# The current value of self._moneyflowindex is represented by self._moneyflowindex.current.value
self.plot("MoneyFlowIndex", "moneyflowindex", self._moneyflowindex.current.value)
# Plot all attributes of self._moneyflowindex
self.plot("MoneyFlowIndex", "positive_money_flow", self._moneyflowindex.positive_money_flow.current.value)
self.plot("MoneyFlowIndex", "negative_money_flow", self._moneyflowindex.negative_money_flow.current.value)For more information about this indicator, see its referencereference.
Indicator History
To get the historical data of the MoneyFlowIndex indicator, call the IndicatorHistoryself.indicator_history method. This method resets your indicator, makes a history request, and updates the indicator with the historical data. Just like with regular history requests, the IndicatorHistoryindicator_history method supports time periods based on a trailing number of bars, a trailing period of time, or a defined period of time. If you don't provide a resolution argument, it defaults to match the resolution of the security subscription.
public class MoneyFlowIndexAlgorithm : QCAlgorithm
{
private Symbol _symbol;
private MoneyFlowIndex _mfi;
public override void Initialize()
{
_symbol = AddEquity("SPY", Resolution.Daily).Symbol;
_mfi = MFI(_symbol, 20);
var indicatorHistory = IndicatorHistory(_mfi, _symbol, 100, Resolution.Minute);
var timeSpanIndicatorHistory = IndicatorHistory(_mfi, _symbol, TimeSpan.FromDays(10), Resolution.Minute);
var timePeriodIndicatorHistory = IndicatorHistory(_mfi, _symbol, new DateTime(2024, 7, 1), new DateTime(2024, 7, 5), Resolution.Minute);
// Access all attributes of indicatorHistory
var positiveMoneyFlow = indicatorHistory.Select(x => ((dynamic)x).PositiveMoneyFlow).ToList();
var negativeMoneyFlow = indicatorHistory.Select(x => ((dynamic)x).NegativeMoneyFlow).ToList();
}
} class MoneyFlowIndexAlgorithm(QCAlgorithm):
def initialize(self) -> None:
self._symbol = self.add_equity("SPY", Resolution.DAILY).symbol
self._mfi = self.mfi(self._symbol, 20)
indicator_history = self.indicator_history(self._mfi, self._symbol, 100, Resolution.MINUTE)
timedelta_indicator_history = self.indicator_history(self._mfi, self._symbol, timedelta(days=10), Resolution.MINUTE)
time_period_indicator_history = self.indicator_history(self._mfi, self._symbol, datetime(2024, 7, 1), datetime(2024, 7, 5), Resolution.MINUTE)
# Access all attributes of indicator_history
indicator_history_df = indicator_history.data_frame
positive_money_flow = indicator_history_df["positivemoneyflow"]
negative_money_flow = indicator_history_df["negativemoneyflow"]