Supported Indicators

Mesa Adaptive Moving Average

Introduction

Implements the Mesa Adaptive Moving Average (MAMA) indicator along with the following FAMA (Following Adaptive Moving Average) as a secondary indicator. The MAMA adjusts its smoothing factor based on the market's volatility, making it more adaptive than a simple moving average.

To view the implementation of this indicator, see the LEAN GitHub repository.

Using MAMA Indicator

To create an automatic indicator for MesaAdaptiveMovingAverage, call the MAMAmama helper method from the QCAlgorithm class. The MAMAmama method creates a MesaAdaptiveMovingAverage 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 MesaAdaptiveMovingAverageAlgorithm : QCAlgorithm
{
    private Symbol _symbol;
    private MesaAdaptiveMovingAverage _mama;

    public override void Initialize()
    {
        _symbol = AddEquity("SPY", Resolution.Daily).Symbol;
        _mama = MAMA(_symbol, 0.5m, 0.05m);
    }

    public override void OnData(Slice data)
    {

        if (_mama.IsReady)
        {
            // The current value of _mama is represented by itself (_mama)
            // or _mama.Current.Value
            Plot("MesaAdaptiveMovingAverage", "mama", _mama);
            // Plot all properties of abands
            Plot("MesaAdaptiveMovingAverage", "fama", _mama.Fama);
        }
    }
}
class MesaAdaptiveMovingAverageAlgorithm(QCAlgorithm):
    def initialize(self) -> None:
        self._symbol = self.add_equity("SPY", Resolution.DAILY).symbol
        self._mama = self.mama(self._symbol, 0.5, 0.05)

    def on_data(self, slice: Slice) -> None:

        if self._mama.is_ready:
            # The current value of self._mama is represented by self._mama.current.value
            self.plot("MesaAdaptiveMovingAverage", "mama", self._mama.current.value)
            # Plot all attributes of self._mama
            self.plot("MesaAdaptiveMovingAverage", "fama", self._mama.fama.current.value)

For more information about this method, see the QCAlgorithm classQCAlgorithm class.

You can manually create a MesaAdaptiveMovingAverage 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 MesaAdaptiveMovingAverageAlgorithm : QCAlgorithm
{
    private Symbol _symbol;
    private MesaAdaptiveMovingAverage _mesaadaptivemovingaverage;

    public override void Initialize()
    {
        _symbol = AddEquity("SPY", Resolution.Daily).Symbol;
        _mesaadaptivemovingaverage = new MesaAdaptiveMovingAverage(0.5m, 0.05m);
    }

    public override void OnData(Slice data)
    {
        if (data.Bars.TryGetValue(_symbol, out var bar))
            _mesaadaptivemovingaverage.Update(bar);

        if (_mesaadaptivemovingaverage.IsReady)
        {
            // The current value of _mesaadaptivemovingaverage is represented by itself (_mesaadaptivemovingaverage)
            // or _mesaadaptivemovingaverage.Current.Value
            Plot("MesaAdaptiveMovingAverage", "mesaadaptivemovingaverage", _mesaadaptivemovingaverage);
            // Plot all properties of abands
            Plot("MesaAdaptiveMovingAverage", "fama", _mesaadaptivemovingaverage.Fama);
        }
    }
}
class MesaAdaptiveMovingAverageAlgorithm(QCAlgorithm):
    def initialize(self) -> None:
        self._symbol = self.add_equity("SPY", Resolution.DAILY).symbol
        self._mesaadaptivemovingaverage = MesaAdaptiveMovingAverage(0.5, 0.05)

    def on_data(self, slice: Slice) -> None:
        bar = slice.bars.get(self._symbol)
        if bar:
            self._mesaadaptivemovingaverage.update(bar)

        if self._mesaadaptivemovingaverage.is_ready:
            # The current value of self._mesaadaptivemovingaverage is represented by self._mesaadaptivemovingaverage.current.value
            self.plot("MesaAdaptiveMovingAverage", "mesaadaptivemovingaverage", self._mesaadaptivemovingaverage.current.value)
            # Plot all attributes of self._mesaadaptivemovingaverage
            self.plot("MesaAdaptiveMovingAverage", "fama", self._mesaadaptivemovingaverage.fama.current.value)

For more information about this indicator, see its referencereference.

Visualization

The following plot shows values for some of the MesaAdaptiveMovingAverage indicator properties:

MesaAdaptiveMovingAverage line plot.

Indicator History

To get the historical data of the MesaAdaptiveMovingAverage 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 MesaAdaptiveMovingAverageAlgorithm : QCAlgorithm
{
    private Symbol _symbol;
    private MesaAdaptiveMovingAverage _mama;

    public override void Initialize()
    {
        _symbol = AddEquity("SPY", Resolution.Daily).Symbol;
        _mama = MAMA(_symbol, 0.5m, 0.05m);

        var indicatorHistory = IndicatorHistory(_mama, _symbol, 100, Resolution.Minute);
        var timeSpanIndicatorHistory = IndicatorHistory(_mama, _symbol, TimeSpan.FromDays(10), Resolution.Minute);
        var timePeriodIndicatorHistory = IndicatorHistory(_mama, _symbol, new DateTime(2024, 7, 1), new DateTime(2024, 7, 5), Resolution.Minute);

        // Access all attributes of indicatorHistory
        var fama = indicatorHistory.Select(x => ((dynamic)x).Fama).ToList();
    }
}
class MesaAdaptiveMovingAverageAlgorithm(QCAlgorithm):
    def initialize(self) -> None:
        self._symbol = self.add_equity("SPY", Resolution.DAILY).symbol
        self._mama = self.mama(self._symbol, 0.5, 0.05)

        indicator_history = self.indicator_history(self._mama, self._symbol, 100, Resolution.MINUTE)
        timedelta_indicator_history = self.indicator_history(self._mama, self._symbol, timedelta(days=10), Resolution.MINUTE)
        time_period_indicator_history = self.indicator_history(self._mama, 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
        fama = indicator_history_df["fama"]

You can also see our Videos. You can also get in touch with us via Discord.

Did you find this page helpful?

Contribute to the documentation: