What Is a Moving Average (MA)?

In statistics, a moving average is a calculation used to analyze data points by creating a series of averages of different subsets of the full data set. In finance/crypto, a moving average (MA) is an indicator that is commonly used in technical analysis. The reason for calculating the moving average of a stock/crypto asset is to help smooth out the price data by creating a constantly updated average price.

By calculating the moving average, the impacts of random, short-term fluctuations on the price of an asset over a specified time-frame are mitigated.


Key points about Moving Averages

  1. A Moving Average is often used to smooth out price data.
  2. Moving Averages are commonly used to gauge trend direction.
  3. There are many types of Moving Averages, each with varying results/calculations.

What type of Moving Averages are Supported on Tuned?

  • alma() (Arnaud Legoux Moving Average)
  • dema() (Double Exponential Moving Average)
  • ema() (Exponential Moving Average)
  • hullma() (Hull Moving Average)
  • kama() (Kaufman Adaptive Moving Average)
  • mama() (MESA Adaptive Moving Average)
  • sma() (Simple Moving Average)
  • swma() (Symmetrically Weighted Moving Average)
  • tema() (Triple Exponential Moving Average)
  • trima() (Triangular Moving Average)
  • vwma() (Volume Weighted Moving Average)
  • wma() (Weighted Moving Average)

All of the above Moving Averages you will be able to find in our Docs

How to add a Moving Average to my script?

Below you will find a short example of using 2 different Moving Averages in your script. Lets start with writing a small section of code to use the Exponential Moving Average:

    // Create an input variable using type Integer to use as the EMA's length
    emaLen = intInput("EMA Length", 21) 

    // Define your EMA
    EMA = ema(close, emaLen) 

    // Plot the EMA
    plot(EMA, "EMA Plot", "#004BEC", 2)

//Signal Push
    // NEVER_ORDER means that no orders will be placed

The second example will show how to use the Hull Moving Average:

    // Create an input variable for type Integer to use as the HullMA's length
    hmaLen = intInput("HullMA Length", 55) 

    // Define your HullMA
    HullMA = hullma(close, hmaLen) 

    // Plot the HullMA
    plot(HullMA, "HullMA Plot", "#004BEC", 2)

//Signal Push
    // NEVER_ORDER means that no orders will be placed

The above image is how the plot should look on the chart. The blue line is your Moving Average in this example.

How to combine these 2 Moving Averages?

So now you know how to write a simple script to generate a Moving Average, what’s next?
Combining them together will allow you to generate buy and sell signals based off of the Moving Averages.
The most common way to do this would be to use a faster Moving Average cross over or under a slower Moving Average.

Here is how to do this:

    // Create an input variable for type Integer to use as the HullMA's length
    emaLen = intInput("EMA Length Fast MA", 13)
    hmaLen = intInput("HullMA Length Slow MA", 100)

    // Define your Moving averages
    ema = ema(close, emaLen) // This is your EMA
    HullMA = hullma(close, hmaLen) // This is your HullMA

//Entry and exit conditions
    // Define the entry condition of Fast crossing over Slow
    buy = crossover(ema, HullMA)

    // Define the exit condition of Fast crossing under Slow
    sell = crossunder(ema, HullMA)

    // Plot the Moving Averages
    plot(ema, "EMA Plot", "#00ff00", 2) // Green line is your EMA
    plot(HullMA, "HullMA Plot", "#004BEC", 2) // Blue line is HullMA

//Signal Push
    // Here you will output your entry and exit signals
    out(signalIf(buy, sell))

Below is a screenshot of how it should look on your chart.


So now you have learnt what a Moving Average is, how to use it and how to put it in a script and generate signals from it.

Have fun scripting!

Disclaimer: The opinions expressed here are for general informational purposes only and are not intended to provide specific advice or recommendations for any specific security or investment product. You should never invest money that you cannot afford to lose. Before trading using complex financial products, please ensure to understand the risks involved. Past performance is no guarantee of future results.

Supercharge your trading

Sign up for our waitlist to get early access to trading strategies developed by quantitative traders.

Get Started