OPEN-SOURCE SCRIPT
MAMA [DCAUT]

█ MAMA (MESA Adaptive Moving Average) [DCAUT]
📊 OVERVIEW
The MESA Adaptive Moving Average (MAMA) represents an advanced implementation of John F. Ehlers' adaptive moving average system using the Hilbert Transform Discriminator. This indicator automatically adjusts to market cycles, providing superior responsiveness compared to traditional fixed-period moving averages while maintaining smoothness.
MAMA dynamically calculates two lines: the fast-adapting MAMA line and the following FAMA (Following Adaptive Moving Average) line. The system's core strength lies in its ability to automatically detect and adapt to the dominant market cycle, reducing lag during trending periods while providing stability during consolidation phases.
🎯 CORE CONCEPTS
Signal Interpretation:
• MAMA above FAMA: Indicates bullish trend momentum with the fast line leading upward movement
• MAMA below FAMA: Suggests bearish trend momentum with the fast line leading downward movement
• Golden Cross: MAMA crossing above FAMA signals potential upward momentum shift
• Death Cross: MAMA crossing below FAMA indicates potential downward momentum shift
• Line Convergence: MAMA and FAMA approaching each other suggests trend consolidation or potential reversal
Primary Applications:
• Trend Following: Enhanced responsiveness to trend changes compared to traditional moving averages
• Crossover Signals: MAMA/FAMA crossovers for identifying potential entry and exit points
• Cycle Analysis: Automatic adaptation to market's dominant cycle characteristics
• Reduced Lag: Minimized delay in trend detection while maintaining signal smoothness
📐 MATHEMATICAL FOUNDATION
Hilbert Transform Discriminator Technology:
The MAMA system employs John F. Ehlers' Hilbert Transform Discriminator, a sophisticated signal processing technique borrowed from telecommunications engineering. The Hilbert Transform creates a complex representation of the price series by generating a 90-degree phase-shifted version of the original signal, enabling precise cycle measurement.
The discriminator analyzes the instantaneous phase relationships between the original price series and its Hilbert Transform counterpart. This mathematical relationship reveals the dominant cycle period present in the market data at each point in time, forming the foundation for adaptive smoothing.
Instantaneous Period Calculation:
The algorithm computes the instantaneous period using the arctangent of the ratio between the Hilbert Transform and the original price series. This calculation produces a real-time measurement of the market's dominant cycle, typically ranging from short-term noise cycles to longer-term trend cycles.
The instantaneous period measurement undergoes additional smoothing to prevent erratic behavior from single-bar anomalies. This smoothed period value becomes the basis for calculating the adaptive alpha coefficient that controls the moving average's responsiveness.
Dynamic Alpha Coefficient System:
The adaptive alpha calculation represents the core mathematical innovation of MAMA. The alpha coefficient is derived from the instantaneous period measurement and constrained within the user-defined fast and slow limits.
The mathematical relationship converts the measured cycle period into an appropriate smoothing factor: shorter detected cycles result in higher alpha values (increased responsiveness), while longer cycles produce lower alpha values (increased stability). This creates an automatic adaptation mechanism that responds to changing market conditions.
MAMA/FAMA Calculation Process:
The MAMA line applies the dynamically calculated alpha coefficient to an exponential moving average formula: MAMA = alpha × Price + (1 - alpha) × MAMA[1]. The FAMA line then applies a secondary smoothing operation to the MAMA line, creating a following average that provides confirmation signals.
This dual-line approach ensures that the fast-adapting MAMA line captures trend changes quickly, while the FAMA line offers a smoother confirmation signal, reducing the likelihood of acting on temporary price fluctuations.
Cycle Detection Mechanism:
The underlying cycle detection employs quadrature components derived from the Hilbert Transform to measure both amplitude and phase characteristics of price movements. This allows the system to distinguish between genuine trend changes and temporary price noise, automatically adjusting the smoothing intensity accordingly.
The mathematical framework ensures that during strong trending periods with clear directional movement, the algorithm reduces smoothing to minimize lag. Conversely, during consolidation phases with mixed signals, increased smoothing helps filter out false breakouts and whipsaws.
📋 PARAMETER CONFIGURATION
Source Selection Strategy:
• HL2 (High+Low)/2 (Default): Recommended for cycle analysis as it represents the midpoint of each period's trading range, reducing impact of opening gaps and closing spikes
• Close Price: Traditional choice reflecting final market sentiment, suitable for end-of-day analysis
• HLC3 (High+Low+Close)/3: Balanced approach incorporating range information with closing emphasis
• OHLC4 (Open+High+Low+Close)/4: Most comprehensive price representation for complete market view
Fast Limit Configuration (Default 0.5):
Controls the maximum responsiveness of the adaptive system. Higher values increase sensitivity to recent price changes but may introduce more noise. This parameter sets the upper bound for the dynamic alpha calculation.
Slow Limit Configuration (Default 0.05):
Determines the minimum responsiveness, providing stability during uncertain market conditions. Lower values increase smoothing but may cause delayed signals. This parameter sets the lower bound for the dynamic alpha calculation.
Parameter Relationship Considerations:
The fast and slow limits work together to define the adaptive range. The wider the range between these limits, the more dramatic the adaptation between trending and consolidating market conditions. Different market characteristics may benefit from different parameter configurations, requiring individual testing and validation.
📊 COLOR CODING SYSTEM
Line Visualization:
• Green Line (MAMA): The fast-adapting moving average that responds quickly to price changes
• Red Line (FAMA): The following adaptive moving average that provides confirmation signals
The fixed color scheme provides consistent visual identification of each line, enabling clear differentiation between the fast-adapting MAMA and the following FAMA throughout all market conditions.
💡 CORE VALUE PROPOSITION
Advantages Over Traditional Moving Averages:
• Cycle Adaptation: Automatically adjusts to market's dominant cycle rather than using fixed periods
• Reduced Lag: Faster response to genuine trend changes while filtering market noise
• Mathematical Foundation: Based on advanced signal processing techniques from telecommunications engineering
• Dual-Line System: Provides both fast adaptation (MAMA) and confirmation (FAMA) in one indicator
Comparative Performance Characteristics:
Unlike fixed-period moving averages that apply the same smoothing regardless of market conditions, MAMA adapts its behavior based on current market cycle characteristics. This may help reduce whipsaws during consolidation periods while maintaining responsiveness during trending phases.
Usage Considerations:
This indicator is designed for technical analysis purposes. The adaptive nature means that parameter optimization should consider the specific characteristics of the asset and timeframe being analyzed. Like all technical indicators, MAMA should be used as part of a comprehensive analysis approach rather than as a standalone signal generator.
Alert Functionality:
The indicator includes alert conditions for MAMA/FAMA crossovers, enabling automated notification of potential momentum shifts. These alerts can assist in timing analysis but should be combined with other forms of market analysis for decision-making purposes.
📊 OVERVIEW
The MESA Adaptive Moving Average (MAMA) represents an advanced implementation of John F. Ehlers' adaptive moving average system using the Hilbert Transform Discriminator. This indicator automatically adjusts to market cycles, providing superior responsiveness compared to traditional fixed-period moving averages while maintaining smoothness.
MAMA dynamically calculates two lines: the fast-adapting MAMA line and the following FAMA (Following Adaptive Moving Average) line. The system's core strength lies in its ability to automatically detect and adapt to the dominant market cycle, reducing lag during trending periods while providing stability during consolidation phases.
🎯 CORE CONCEPTS
Signal Interpretation:
• MAMA above FAMA: Indicates bullish trend momentum with the fast line leading upward movement
• MAMA below FAMA: Suggests bearish trend momentum with the fast line leading downward movement
• Golden Cross: MAMA crossing above FAMA signals potential upward momentum shift
• Death Cross: MAMA crossing below FAMA indicates potential downward momentum shift
• Line Convergence: MAMA and FAMA approaching each other suggests trend consolidation or potential reversal
Primary Applications:
• Trend Following: Enhanced responsiveness to trend changes compared to traditional moving averages
• Crossover Signals: MAMA/FAMA crossovers for identifying potential entry and exit points
• Cycle Analysis: Automatic adaptation to market's dominant cycle characteristics
• Reduced Lag: Minimized delay in trend detection while maintaining signal smoothness
📐 MATHEMATICAL FOUNDATION
Hilbert Transform Discriminator Technology:
The MAMA system employs John F. Ehlers' Hilbert Transform Discriminator, a sophisticated signal processing technique borrowed from telecommunications engineering. The Hilbert Transform creates a complex representation of the price series by generating a 90-degree phase-shifted version of the original signal, enabling precise cycle measurement.
The discriminator analyzes the instantaneous phase relationships between the original price series and its Hilbert Transform counterpart. This mathematical relationship reveals the dominant cycle period present in the market data at each point in time, forming the foundation for adaptive smoothing.
Instantaneous Period Calculation:
The algorithm computes the instantaneous period using the arctangent of the ratio between the Hilbert Transform and the original price series. This calculation produces a real-time measurement of the market's dominant cycle, typically ranging from short-term noise cycles to longer-term trend cycles.
The instantaneous period measurement undergoes additional smoothing to prevent erratic behavior from single-bar anomalies. This smoothed period value becomes the basis for calculating the adaptive alpha coefficient that controls the moving average's responsiveness.
Dynamic Alpha Coefficient System:
The adaptive alpha calculation represents the core mathematical innovation of MAMA. The alpha coefficient is derived from the instantaneous period measurement and constrained within the user-defined fast and slow limits.
The mathematical relationship converts the measured cycle period into an appropriate smoothing factor: shorter detected cycles result in higher alpha values (increased responsiveness), while longer cycles produce lower alpha values (increased stability). This creates an automatic adaptation mechanism that responds to changing market conditions.
MAMA/FAMA Calculation Process:
The MAMA line applies the dynamically calculated alpha coefficient to an exponential moving average formula: MAMA = alpha × Price + (1 - alpha) × MAMA[1]. The FAMA line then applies a secondary smoothing operation to the MAMA line, creating a following average that provides confirmation signals.
This dual-line approach ensures that the fast-adapting MAMA line captures trend changes quickly, while the FAMA line offers a smoother confirmation signal, reducing the likelihood of acting on temporary price fluctuations.
Cycle Detection Mechanism:
The underlying cycle detection employs quadrature components derived from the Hilbert Transform to measure both amplitude and phase characteristics of price movements. This allows the system to distinguish between genuine trend changes and temporary price noise, automatically adjusting the smoothing intensity accordingly.
The mathematical framework ensures that during strong trending periods with clear directional movement, the algorithm reduces smoothing to minimize lag. Conversely, during consolidation phases with mixed signals, increased smoothing helps filter out false breakouts and whipsaws.
📋 PARAMETER CONFIGURATION
Source Selection Strategy:
• HL2 (High+Low)/2 (Default): Recommended for cycle analysis as it represents the midpoint of each period's trading range, reducing impact of opening gaps and closing spikes
• Close Price: Traditional choice reflecting final market sentiment, suitable for end-of-day analysis
• HLC3 (High+Low+Close)/3: Balanced approach incorporating range information with closing emphasis
• OHLC4 (Open+High+Low+Close)/4: Most comprehensive price representation for complete market view
Fast Limit Configuration (Default 0.5):
Controls the maximum responsiveness of the adaptive system. Higher values increase sensitivity to recent price changes but may introduce more noise. This parameter sets the upper bound for the dynamic alpha calculation.
Slow Limit Configuration (Default 0.05):
Determines the minimum responsiveness, providing stability during uncertain market conditions. Lower values increase smoothing but may cause delayed signals. This parameter sets the lower bound for the dynamic alpha calculation.
Parameter Relationship Considerations:
The fast and slow limits work together to define the adaptive range. The wider the range between these limits, the more dramatic the adaptation between trending and consolidating market conditions. Different market characteristics may benefit from different parameter configurations, requiring individual testing and validation.
📊 COLOR CODING SYSTEM
Line Visualization:
• Green Line (MAMA): The fast-adapting moving average that responds quickly to price changes
• Red Line (FAMA): The following adaptive moving average that provides confirmation signals
The fixed color scheme provides consistent visual identification of each line, enabling clear differentiation between the fast-adapting MAMA and the following FAMA throughout all market conditions.
💡 CORE VALUE PROPOSITION
Advantages Over Traditional Moving Averages:
• Cycle Adaptation: Automatically adjusts to market's dominant cycle rather than using fixed periods
• Reduced Lag: Faster response to genuine trend changes while filtering market noise
• Mathematical Foundation: Based on advanced signal processing techniques from telecommunications engineering
• Dual-Line System: Provides both fast adaptation (MAMA) and confirmation (FAMA) in one indicator
Comparative Performance Characteristics:
Unlike fixed-period moving averages that apply the same smoothing regardless of market conditions, MAMA adapts its behavior based on current market cycle characteristics. This may help reduce whipsaws during consolidation periods while maintaining responsiveness during trending phases.
Usage Considerations:
This indicator is designed for technical analysis purposes. The adaptive nature means that parameter optimization should consider the specific characteristics of the asset and timeframe being analyzed. Like all technical indicators, MAMA should be used as part of a comprehensive analysis approach rather than as a standalone signal generator.
Alert Functionality:
The indicator includes alert conditions for MAMA/FAMA crossovers, enabling automated notification of potential momentum shifts. These alerts can assist in timing analysis but should be combined with other forms of market analysis for decision-making purposes.
סקריפט קוד פתוח
ברוח TradingView אמיתית, היוצר של הסקריפט הזה הפך אותו לקוד פתוח, כך שסוחרים יכולים לבדוק ולאמת את הפונקציונליות שלו. כל הכבוד למחבר! למרות שאתה יכול להשתמש בו בחינם, זכור שפרסום מחדש של הקוד כפוף לכללי הבית שלנו.
כתב ויתור
המידע והפרסומים אינם אמורים להיות, ואינם מהווים, עצות פיננסיות, השקעות, מסחר או סוגים אחרים של עצות או המלצות שסופקו או מאושרים על ידי TradingView. קרא עוד בתנאים וההגבלות.
סקריפט קוד פתוח
ברוח TradingView אמיתית, היוצר של הסקריפט הזה הפך אותו לקוד פתוח, כך שסוחרים יכולים לבדוק ולאמת את הפונקציונליות שלו. כל הכבוד למחבר! למרות שאתה יכול להשתמש בו בחינם, זכור שפרסום מחדש של הקוד כפוף לכללי הבית שלנו.
כתב ויתור
המידע והפרסומים אינם אמורים להיות, ואינם מהווים, עצות פיננסיות, השקעות, מסחר או סוגים אחרים של עצות או המלצות שסופקו או מאושרים על ידי TradingView. קרא עוד בתנאים וההגבלות.