Latest posts

    Bitcoin – The bottom is not in! 15k or 10k (careful)

    March 23, 2023

    Bitcoin – 10% crash, pullback is very likely!

    March 21, 2023

    Ethereum – Trendline from 2015 is breaking down! (unbeliveable)

    March 20, 2023
    Telegram RSS
    • My TradingView
    • Best way to trade futures
    • Best way to trade spot
    • The holy grail of trading
    • The game changer
    Telegram RSS
    Tolberti
    • Home
    • Education
      1. Motivation & Strategy
      2. Technical Basics
      3. Candlesticks
      4. Chart patterns
      5. Indicators
      6. FIbonacci
      7. Elliott wave
      8. Harmonic patterns
      9. View All

      What is drawdown in Trading

      December 2, 2022

      Trading Psychology and its Importance – Tips For a Winning Mindset

      November 2, 2022

      As a trader you need to stay disciplined!

      November 2, 2022

      How to Create a Successful Trading Plan – Step-By-Step Guide

      November 2, 2022

      Price action trading strategies that you need to know

      November 2, 2022

      Support and Resistance – A basic concept of technical analysis

      November 2, 2022

      Technical analysis explained – What is it and how does it work?

      November 2, 2022

      The most common mistakes technical analysts make

      November 2, 2022

      Candlestick Patterns – Complete Guide to Bearish and Bullish Candlesticks

      November 2, 2022

      Rising three methods

      August 11, 2022

      Falling three methods

      August 11, 2022

      Spinning top

      August 11, 2022

      Rising & Falling Wedge

      January 20, 2023

      Bullish Pennant

      January 20, 2023

      Bull Flag

      January 20, 2023

      Head and Shoulders

      January 20, 2023

      OBV – On-Balance Volume

      December 2, 2022

      Market Profile – A Complete Guide

      November 2, 2022

      Ichimoku cloud

      August 18, 2022

      Moving average (MA)

      August 18, 2022

      Fibonacci and Market Analysis

      August 13, 2022

      4 mistakes that Traders should avoid while Trading with Fibonacci

      August 13, 2022

      Why Fibonacci Series is important for Technical Analysis

      August 13, 2022

      A complete overview on trend and theory of retracement

      August 13, 2022

      Elliott Wave Cheat Sheet: All You Need

      August 14, 2022

      Elliott Wave Theory: Complex guide

      August 14, 2022

      Elliott Wave: All About How To Count

      August 14, 2022

      Leading And Ending Diagonal: How To Trade Them

      August 14, 2022

      Alternate bat pattern

      October 1, 2022

      The List of All Known Harmonic Patterns & Ratios

      October 1, 2022

      Cypher pattern

      September 1, 2022

      ABCD pattern

      August 13, 2022

      Rising & Falling Wedge

      January 20, 2023

      Bullish Pennant

      January 20, 2023

      Bull Flag

      January 20, 2023

      Head and Shoulders

      January 20, 2023
    • Technical analysis

      Bitcoin – The bottom is not in! 15k or 10k (careful)

      March 23, 2023

      Bitcoin – 10% crash, pullback is very likely!

      March 21, 2023

      Ethereum – Trendline from 2015 is breaking down! (unbeliveable)

      March 20, 2023

      Bitcoin – 100% probability to go down to 20k!

      March 17, 2023

      Bitcoin – Last crash before a pump! (must see)

      March 12, 2023
    • Info
      • Crypto news
      • Crypto converter
      • Crypto exchanges
      • My TradingView
      • Best way to trade futures
      • Best way to trade spot
    • Contact
    • My books
    • Join premium channel
    Tolberti
    Home»Education»Indicators»Moving average (MA)
    Indicators

    Moving average (MA)

    By TolbertiAugust 18, 2022No Comments
    Facebook Twitter Telegram WhatsApp Reddit Pinterest Email
    Share
    Facebook Twitter Telegram WhatsApp Reddit Pinterest Email

    Simple Moving Averages

    Moving averages help us confirm and ride the trend. They are the most known technical indicator and this is because of their simplicity and their proven track record of adding value to the analyses. We can use them to find support and resistance levels, stops and targets, and to understand the underlying trend. This versatility makes them an indispensable tool in our trading arsenal.

    EURUSD with its 200-period Simple moving average. (Image by Author)

    As the name suggests, this is your plain simple mean that is used everywhere in statistics and basically any other part in our lives. It is simply the total values of the observations divided by the number of observations. Mathematically speaking, it can be written down as:

    We can see that the moving average is providing decent dynamic support and resistance levels from where we can place our orders in case the market goes down there.

    def ma(Data, lookback, what, where):
        
      for i in range(len(Data)):
          try:
            Data[i, where] = (Data[i - lookback + 1:i + 1, what].mean())
            
                except IndexError:
                    pass
        return Data
    • Main strength: Followed heavily by traders across the world thus benefitting from the fact that reactions are more likely to occur than on other moving averages.
    • Main weakness: Lagging and slow to react to quick moves.

    Exponential Moving Averages

    Another even more dynamic moving average is the exponential one. Its idea is to give more weight to the more recent values so that it reduces the lag between the price and the average.

    EURUSD with its 200-period Exponential moving average. (Image by Author)

    Notice how the exponential moving average is closer to prices than the simple one when the trend is strong. This is because it gives more weight to the latest values so that the average does not stay very far. To code a function in Python that outputs this type of average, you can use the below snippet:

    def ema(Data, alpha, lookback, what, where):
        
        # alpha is the smoothing factor
        # window is the lookback period
        # what is the column that needs to have its average calculated
        # where is where to put the exponential moving average
        
        alpha = alpha / (lookback + 1.0)
        beta  = 1 - alpha
        
        # First value is a simple SMA
        Data = ma(Data, lookback, what, where)
        
        # Calculating first EMA
        Data[lookback + 1, where] = (Data[lookback + 1, what] * alpha) + (Data[lookback, where] * beta)        # Calculating the rest of EMA
        for i in range(lookback + 2, len(Data)):
          try:
            Data[i, where] = (Data[i, what] * alpha) + (Data[i - 1, where] * beta)
            
                except IndexError:
                    pass
        return Data
    • Main strength: Quick to react to sudden moves and followed by traders across the world even though a little less than the Simple Moving Averages.
    • Main weakness: As it takes into account the latest data more than the previous ones, it may miss the overall trend

    Smoothed Moving Averages

    This moving average takes into account the general picture and is less impacted by recent movements. It’s my favourite trend-following indicator. Mathematically speaking, it can be found by simply multiplying the Days variable in the EMA function by 2 and subtract 1. This means that to transform an exponential moving average into a smoothed one, we follow this equation in python language, that transforms the exponential moving average into a smoothed one:

    smoothed = (exponential * 2) - 1 # From exponential to smoothed
    EURUSD with its 100-period Smoothed Moving Average. (Image by Author)
    • Main strength: Works well for dynamic support and resistance levels. Also, a component of the well known Relative Strength Index.
    • Main weakness: Less followed by traders and slow to react to sudden changes.

    Linear-Weighted Moving Averages

    Also referred to as a linear-weighted moving average, the weighted moving average is a simple moving average that places more weight on recent data. The most recent observation has the biggest weight and each one prior to it has a progressively decreasing weight. Intuitively, it has less lag than the other moving averages but it is also the least used, and hence, what it gains in lag reduction, it loses in popularity.

    Mathematically speaking, it can be written down as:

    EURUSD hourly data with a 200-period Weighted Moving Average. (Image by Author)

    Basically, if we have a dataset composed of two numbers [1, 2] and we want to calculate a linear weighted average, then we will do the following:

    • (2 x 2) + (1 x 1) = 5
    • 5 / 3 = 1.66

    This assumes a time series with the number 2 as being the most recent observation.

    USDCHF hourly data with a 200-period Weighted Moving Average. (Image by Author)
    import numpy as npdef lwma(Data, lookback):
        
        weighted = []
        for i in range(len(Data)):
                try:
                    total = np.arange(1, lookback + 1, 1)
                    
                    matrix = Data[i - lookback + 1: i + 1, 3:4]
                    matrix = np.ndarray.flatten(matrix)
                    matrix = total * matrix
                    wma = (matrix.sum()) / (total.sum())
                    weighted = np.append(weighted, wma)                  except ValueError:
                    pass
        
        Data = Data[lookback - 1:, ]
        weighted = np.reshape(weighted, (-1, 1)) 
        Data = np.concatenate((Data, weighted), axis = 1)   
        
        return Data# For this function to work, you need to have an OHLC array composed of the four usual columns, then you can use the below syntax to get a data array with the weighted moving average using the lookback you needmy_ohlc_data = lwma(my_ohlc_data, 20)
    • Main strength: Quick to react as it takes into account the latest values.
    • Main weakness: Not very followed and not very efficient in detecting support and resistance levels.

    Triangular Moving Averages

    What is called a Triangular Moving Average is simply the moving average of the moving average itself and this is to provide an extra layer of smoothing that can act as a dynamic support or resistance level. Surely it is not as reactive as the first moving average but in stable times, it can provide excellent reactionary levels.

    Notice how in the above plot of NZDUSD, the Triangular Moving Average in purple is provide better trading levels. The market seems to prefer this moving average more than the first one (100-period).

    NZDUSD with the Simple Moving Average (in blue) and the Triangular Moving Average (in purple). (Image by Author)

    The disadvantage of the Triangular Moving Average is that sometimes it tends to be far away from the market price when there is a strong trend. This situation makes it less useful to determine a close support or resistance level. The seen in the plot below on the USDCAD with the first bearish trend. Notice how the simple 100-period moving average is close to the market price and can provide dynamic reactionary levels but the Triangular Moving Average is quite far.

    NZDUSD with the Simple Moving Average (in blue) and the Triangular Moving Average (in purple). (Image by Author)

    To code the Triangular Moving Average, we simple write the ma function again but apply it on the moving average column.

    # Consider my_data is the OHLC data array with two extra free columns
    my_data = ma(my_data, lookback, closing_price, where_to_put_ma)
    my_data = ma(my_data, lookback, where_to_put_ma, where_to_put_tma)# the variable where_to_put_tma refers to the index of the column where you want to put the Triangular Moving Average
    NZDUSD with the Triangular Moving Average (in purple). (Image by Author)
    • Main strength: Acts relatively well as dynamic support and resistance levels.
    • Main weakness: Super slow to react as it an average over an average.

    Fibonacci Moving Averages

    The sequence follows this distinct pattern:

    The numbers are found by adding the previous two numbers behind them. In the case of 13, it is calculated as 8 + 5, hence the formula is:

    This gives us the intuition to code it using the following code:

    def Fibonacci(n):
       if n == 1:
          return 1
      
       elif n == 0:   
          return 0 
               
       else:                      
          return Fibonacci(n - 1) + Fibonacci(n - 2)

    The below function gives us the Fibonacci number according to its index. The index is simply the order of the numbers seen in the table below.

    Fibonacci Table. (Image by Author)

    Now, if we use the function and look at its results, we will understand better.

    fib(14)
    # Output: 377 fib(5)
    # Output: 5

    Now, from the exponential moving average seen above and the Fibonacci sequence, we will create the new indicator, the Fibonacci Moving Average.

    The Fibonacci Moving Average is an equally weighted exponential moving average using the lookbacks of selected Fibonacci numbers. Here is what I mean step by step:

    • We calculate exponential moving averages using the following lookbacks {2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597}.
    • We divide the sum of the exponential moving averages by their number. In our case, we will divide by 15.
    • Plot the Fibonacci Moving Average alongside the market price.
    EURUSD Daily data with the Fibonacci Moving Average. (Image by Author)
    EURCAD Daily data with the Fibonacci Moving Average. (Image by Author)

    It is clear that the FMA shows better reactionary levels especially when prices dip. The way to trade the FMA is simply by initiating a contrarian trade when markets approach it. For example, if the market is dropping and heading towards the FMA, we can think about a bullish reaction that could happen around that zone.

    def fibonnaci_moving_average(Data, fib_range, what, where):
        
        for i in range(3, fib_range):
            
            Data = adder(Data, 1)
            lookback = fib(i)
            Data = ema(Data, 2, lookback, what, -1)
        
            Data = adder(Data, 1)       for i in range(len(Data)):
            Data[i, -1] = np.sum(Data[i, where:where + 15])
            Data[i, -1] = Data[i, - 1] / 15
        
        return Data
    • Main strength: Acts extremely well as a dynamic support and resistance level.
    • Main weakness: Terrible in choppy markets and a little slow to react.

    Hull Moving Average

    The Hull Moving Average uses the Weighted Moving Average as building blocks and it is calculated following the below steps:

    • Choose a lookback period such as 20 or 100 and calculate the Weighted Moving Average of the closing price.
    • Divide the lookback period found in the first step and calculate the Weighted Moving Average of the closing price using this new lookback period. If the number cannot by divided by two, then take the closest number before the comma (e.g. a lookback of 15 can be 7 or 8 as the second lookback).
    • Multiply the second Weighted Moving Average by two and subtract from it the first Weighted Moving Average.
    • As a final step, take the square root of the first lookback (e.g. if you have chosen a lookback of 100, then the third lookback period is 10) and calculate the Weighted Moving Average on the latest result we have had in the third step. Be careful not to calculate it on market prices.

    Therefore, if we choose a lookback period of 100, we will calculate on 100 lookback period, then on 50, and finally, on 10 applied to the latest result.

    GBPUSD hourly data with the 100-period Hull Moving Average. (Image by Author)
    def hull_moving_average(Data, what, lookback, where):
        
        Data = lwma(Data, lookback, what)
        
        second_lookback = round((lookback / 2), 1)
        second_lookback = int(second_lookback) 
        
        Data = lwma(Data, second_lookback, what)
        
        Data = adder(Data, 1)
        Data[:, where + 2] = ((2 * Data[:, where + 1]) - Data[:, where])
        third_lookback = round(np.sqrt(lookback), 1)
        third_lookback = int(third_lookback)         Data = lwma(Data, third_lookback, where + 2)     return Data
    EURUSD hourly data with the 200-period Hull Moving Average. (Image by Author)

    Why do we say that the Hull Moving Average reduces lag? The answer comes mainly from the building blocks which are the weighted moving averages. They place more weights on more recent values. Furthermore, the lag is also reduced by offsetting one weighted moving average with the one that has half the lookback period. Finally, we take the square root of the lookback period and apply it on the moving average itself using the weighting method. This gives us a moving average close to the market price and serves as an early trend reversal indicator.

    There is a special trick I like to use to add a visual component and helps to know whether the moving average is tilting to the upside or to the downside. Notice that in the chart above, when the moving average is pointing upwards, it is painted in green and when it is pointing downwards, it is painted in red. The way to do this is to simply put the following conditions:

    • When the current moving average is greater than the one preceding it, then the next column should equal to the current moving average. Similarly, when the current moving average is less than the one preceding it, then the next column should equal to the current moving average divided by zero so that it gets an “inf” value in the array. This is so that when it is plotted on the chart, the “inf” values do not appear thus leaving the place for other values to be filled.
    • When the current moving average is less than the one preceding it, then the next column should equal to the current moving average. Similarly, when the current moving average is greater than the one preceding it, then the next column should equal to the current moving average divided by zero so that it gets an “inf” value in the array.

    The plotting function will be used on both new columns and we will choose our colors accordingly, giving us one moving average plot stemming from two columns, here is how to do this in Python:

    my_data[:, new_column1] = my_data[:, hull_average]
    my_data[:, new_column2] = my_data[:, hull_average]for i in range(len(my_data)):
        if my_data[i, hull_average] > my_data[i - 1, hull_average]:
            my_data[i, new_column1] = my_data[i, hull_average]
            my_data[i, new_column2] = my_data[i, hull_average] / 0
            
        if my_data[i, hull_average] < my_data[i - 1, hull_average]:
            my_data[i, new_column2] = my_data[i, hull_average]  
            my_data[i, new_column1] = my_data[i, hull_average] / 0 
            
    plt.plot(my_data[-1000:, new_column1], color = 'green')
    plt.plot(my_data[-1000:, new_column2], color = 'red')

    You can use this trick for pretty much any type of data.

    EURUSD hourly data with the 50-period Hull Moving Average. (Image by Author)
    • Main strength: Reacts well to moves due to its dynamic nature. Crossover strategies work well using it.
    • Main weakness: Does not provide good dynamic support and resistance levels.

    Kaufman’s Adaptive Moving Average

    The KAMA has been created to reduce the noise and whipsaw effects. It works the same as other moving averages do and follows the same intuition. The generation of false trading signals is one of the problems with moving averages and this is due to short-term sudden fluctuations that bias the calculation. The KAMA’s primary objective is to reduce as much noise as possible.

    The first concept we should measure is the Efficiency Ratio which is the absolute change of the current close relative to the change in the past 10 periods divided by a type of volatility calculated in a special manner. We can say that the Efficiency Ratio is the change divided by volatility.

    Then we calculate a smoothing constant based on the following formula:

    Finally, to calculate the KAMA we use the below formula:

    The calculation may seem complicated but it is easily automated and you do not have to think about it much. Let us see how to code it in Python and then proceed by seeing some examples.

    def kama(Data, what, where, change):
        
        # Change from previous period
        for i in range(len(Data)):
            Data[i, where] = abs(Data[i, what] - Data[i - 1, what])
        
        Data[0, where] = 0
        
        # Sum of changes
        for i in range(len(Data)):
            Data[i, where + 1] = (Data[i - change + 1:i + 1, where].sum())   
            
        # Volatility    
        for i in range(len(Data)):
            Data[i, where + 2] = abs(Data[i, 3] - Data[i - 10, 3])
            
        Data = Data[11:, ]
        
        # Efficiency Ratio
        Data[:, where + 3] = Data[:, where + 2] / Data[:, where + 1]
        
        for i in range(len(Data)):
            Data[i, where + 4] = np.square(Data[i, where + 3] * 0.6666666666666666667)
            
        for i in range(len(Data)):
            Data[i, where + 5] = Data[i - 1, where + 5] + (Data[i, where + 4] * (Data[i, 3] - Data[i - 1, where + 5]))
            Data[11, where + 5] = 0

    The below chart illustrates what the above function can give us. Notice the more stable tendency of the KAMA relative to other moving averages. It is an improvement with regards to whipsaws and false breaks.

    EURUSD with its 60-period Adaptive Moving Average. (Image by Author)
    • Main strength: Highly reactive to moves and adapted to fast markets.
    • Main weakness: Less efficient in detecting support and resistance levels.

    Conclusion

    It can be seen that there are a lot of types of moving averages and our job is to choose the ones that fits us the most as there is not really one that beats the others on all different spectrums. My personal preference is for a long-term simple moving average and for the default Fibonacci moving average but I will leave it to you find your preferred one.

     

    Share. Facebook Twitter Telegram WhatsApp Reddit Pinterest Email
    Previous ArticleParabolic SAR
    Next Article Ranges
    Tolberti
    • Website

    Professional trader and analyst. My specialization is in Elliott Wave Theory, Fibonacci tools, chart patterns, candlesticks, and price action. To analyze market structure, I use market profile and volume profile in my trading system. To analyze trends, I use trendlines, VWAP, and simple moving averages.

    Leave A Reply Cancel Reply

    Join premium channel
    Join Premium
    Random education
    Indicators

    MACD – Moving average convergence divergence

    TolbertiBy TolbertiAugust 16, 20220

    Are you an indicator trader? If yes, then you will enjoy this comprehensive guide to…

    Elliott wave

    Triangles (ABCDE)

    TolbertiBy TolbertiAugust 11, 20220

    Triangles are a correction five-wave pattern (marked as A-B-C-D-E), which is divided into five types.…

    Indicators

    VWAP – Volume-weighted average price

    TolbertiBy TolbertiAugust 17, 20220

    If you are wondering what the Volume Weighted Average Price (VWAP) is or how to…

    Elliott wave

    Extension wave

    TolbertiBy TolbertiAugust 11, 20220

    We can face with an extension in the most impulses in a position of motive…

    Chart patterns

    Tripple bottom

    TolbertiBy TolbertiJanuary 15, 20230

    What Is A Triple Bottom Chart Pattern? In technical analysis, a triple bottom is a…

    Random analysis
    Technical analysis

    Bitcoin – Last crash before a pump! (must see)

    TolbertiBy TolbertiMarch 12, 20232

    The price of bitcoin is currently near the 0.618 FIB. Usually it acts as a magnet when…

    Technical analysis

    Bitcoin – Secret Invisible Pattern!

    TolbertiBy TolbertiNovember 20, 20220

    This is a secret parallel channel that absolutely no one is talking about because it’s visible only…

    Technical analysis

    Synthetix SNX – Bullish head and shoulders! Elliott wave

    TolbertiBy TolbertiJuly 22, 20220

    SNX is ready for an upcoming uptrend! On the daily / 3D chart we can…

    Technical analysis

    Link – Trendline is breaking out! +60% right now

    TolbertiBy TolbertiSeptember 27, 20220

    Link looks pretty strong in the immediate short term, but for the long term I…

    Technical analysis

    Ethereum is bullish! | Bull flag is ready!

    TolbertiBy TolbertiDecember 10, 20220

    This bullish flag is absolutely ready for a breakout! I am bullish on ETH, and I opened a quick…

    Free technical analysis and education from a professional trader. Go to the "education" section and start learning immediately.

    💡Join my premium channel on Telegram (my trades, signals) - get access.

    ✅ 1 month: 49 USD
    ✅ 4 months: 99 USD
    ✅ 12 months: 149 USD
    ✅ Life Time: 249 USD

    Latest posts

    Bitcoin – The bottom is not in! 15k or 10k (careful)

    March 23, 2023

    Bitcoin – 10% crash, pullback is very likely!

    March 21, 2023

    Ethereum – Trendline from 2015 is breaking down! (unbeliveable)

    March 20, 2023
    Get Informed

    Subscribe to Updates

    Get the latest news about crypto, technical analysis and trading.

    Telegram RSS
    • Home
    • Education
    • Technical analysis
    • Info
    • Contact
    • Terms of use
    • My book
    • Join premium channel
    © 2023 Tolberti.com. All rights reserved.

    Type above and press Enter to search. Press Esc to cancel.

    This website uses cookies to give you the most relevant experience.
    Cookie SettingsAccept
    Manage consent

    Privacy Overview

    This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
    Necessary
    Always Enabled
    Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
    CookieDurationDescription
    cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
    cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
    cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
    cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
    cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
    viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
    Functional
    Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
    Performance
    Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
    Analytics
    Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
    Advertisement
    Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
    Others
    Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
    SAVE & ACCEPT