OPEN-SOURCE SCRIPT

Adaptive Trend Lines [MAMA and FAMA]

Updated my previous algo on the Adaptive Trend lines, however I have added new functionalities and sorted out the settings.

You can now switch between normalized and non-normalized settings, the colors have also been updated and look much better.

The MAMA and FAMA
These indicators was originally developed by John F. Ehlers (Stocks & Commodities V. 19:10: MESA Adaptive Moving Averages). Everget wrote the initial functions for these in pine script. I have simply normalized the indicators and chosen to use the Laplace transformation instead of the hilbert transformation

How the Indicator Works:
The indicator employs a series of complex calculations, but we'll break it down into key steps to understand its functionality:

LaplaceTransform: Calculates the Laplace distribution for the given src input. The Laplace distribution is a continuous probability distribution, also known as the double exponential distribution. I use this because of the assymetrical return profile

MESA Period: The indicator calculates a MESA period, which represents the dominant cycle length in the price data. This period is continuously adjusted to adapt to market changes.

InPhase and Quadrature Components: The InPhase and Quadrature components are derived from the Hilbert Transform output. These components represent different aspects of the price's cyclical behavior.

Homodyne Discriminator: The Homodyne Discriminator is a phase-sensitive technique used to determine the phase and amplitude of a signal. It helps in detecting trend changes.

Alpha Calculation: Alpha represents the adaptive factor that adjusts the sensitivity of the indicator. It is based on the MESA period and the phase of the InPhase component. Alpha helps in dynamically adjusting the indicator's responsiveness to changes in market conditions.

MAMA and FAMA Calculation: The MAMA and FAMA values are calculated using the adaptive factor (alpha) and the input price data. These values are essentially adaptive moving averages that aim to capture the current trend more effectively than traditional moving averages.

But Omar, why would anyone want to use this?
The MAMA and FAMA lines offer benefits:
The indicator offers a distinct advantage over conventional moving averages due to its adaptive nature, which allows it to adjust to changing market conditions. This adaptability ensures that investors can stay on the right side of the trend, as the indicator becomes more responsive during trending periods and less sensitive in choppy or sideways markets.

One of the key strengths of this indicator lies in its ability to identify trends effectively by combining the MESA and MAMA techniques. By doing so, it efficiently filters out market noise, making it highly valuable for trend-following strategies. Investors can rely on this feature to gain clearer insights into the prevailing trends and make well-informed trading decisions.

This indicator is primarily suppoest to be used on the big timeframes to see which trend is prevailing, however I am not against someone using it on a timeframe below the 1D, just be careful if you are using this for modern portfolio theory, this is not suppoest to be a mid-term component, but rather a long term component that works well with proper use of detrended fluctuation analysis.

Dont hesitate to ask me if you have any questions

Again, I want to give credit to Everget and ChartPrime!

Code explanation as required by House Rules:

input.float: Used to create input fields for the user to set the fastLimit and slowLimit values.
input: General function to get user inputs, like the data source (close price) used for calculations.

input.int: Creates an input field for the normalization period.
input.bool: Allows the user to toggle normalization on or off.

Color settings in the code:

Constants and functions

math.pi: Represents the mathematical constant π (pi).
laplace: A function that applies the Laplace transform to the source data.
_computeComponent: Computes a component of the data using the Laplace transform.
_smoothComponent: Smooths data by averaging the current value with the previous one (nz function is used to handle null values).

Alpha function:

_computeAlpha: Calculates the adaptive alpha value based on the fastLimit and slowLimit. This value is crucial for determining the MAMA and FAMA lines.

Calculating MAMA and FAMA:

Normalization:

ta.lowest and ta.highest: Find the lowest and highest values of mama_fama_diff over the normalization period.
The oscillator is normalized to a range, making it easier to compare over different periods.

And finally, the plotting:

Example of Normalized settings:

Example for setup:
Try to make sure the lower timeframe follows the higher timeframe if you take a trade based on this indicator!
Join my community where i share signals and my secret algos:

Telegram:
t.me/ikkeomarfree

I can also pass funded accounts for you!
\$50,000 accounts

Just dm my Telegram @IkkeOmar

สคริปต์โอเพนซอร์ซ

ด้วยเจตนารมณ์หลักของ TradingView ผู้เขียนสคริปต์นี้ได้เผยแพร่เป็นโอเพนซอร์ส เพื่อให้เทรดเดอร์สามารถเข้าใจและตรวจสอบได้ ต้องขอบคุณผู้เขียน! ที่ให้คุณใช้ได้ฟรี แต่การนำโค้ดนี้ไปใช้ในการเผยแพร่ซ้ำจะต้องอยู่ภายใต้ กฎระเบียบการใช้งาน คุณสามารถตั้งเป็นรายการโปรดเพื่อใช้บนชาร์ตได้