Strategy Creation

What are Moving Averages and How to Use Them in Tuned Script

Mr. Plippy

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.

MA_example.PNG

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:

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

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

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

//Signal Push
    // NEVER_ORDER means that no orders will be placed
    out(NEVER_ORDER)

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

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

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

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

//Signal Push
    // NEVER_ORDER means that no orders will be placed
    out(NEVER_ORDER)
Moving_Average_Plot.PNG

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:

//Inputs
    // 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)

//indicator
    // 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
    // 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.

Combined_MA.PNG

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.

All investing involves risk, including the possible loss of all the money you invest, and past performance does not guarantee future performance. Historical returns expected returns, and probability projections are provided for informational and illustrative purposes, and may not reflect actual future performance.

Tuned is not a broker-dealer, exchange, custodian, wallet provider, or counterparty. Investing is highly speculative and volatile. Tuned is only suitable for investors who fully understand the risk of loss and may experience large drawdowns. Investors should never invest more than they can afford to lose.