---
title: What is and how to use Moving Averages in Tuned Script
slug: what-is-and-how-to-use-moving-averages-in-tuned-script
createdAt: 2022-06-16T18:36:58.000Z
---

## What Is a Moving Average (MA)?

In statistics, a moving average is a calculation used to analyse 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.

![](https://api.archbee.com/api/optimize/sLyofYuHQt3L9DD4FlP8D/VzQoZCPA8oE-k57-7cj1e_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?

:::CodeblockTabs
TunedScript

```kotlin
alma() (Arnaud Legoux Moving Average)
```
:::

:::CodeblockTabs
TunedScript

```kotlin
dema() (Double Exponential Moving Average)
```
:::

:::CodeblockTabs
TunedScript

```kotlin
ema() (Exponential Moving Average)
```
:::

:::CodeblockTabs
TunedScript

```kotlin
hullma() (Hull Moving Average)
```
:::

:::CodeblockTabs
TunedScript

```kotlin
kama() (Kaufman Adaptive Moving Average)
```
:::

:::CodeblockTabs
TunedScript

```kotlin
mama() (MESA Adaptive Moving Average)
```
:::

:::CodeblockTabs
TunedScript

```kotlin
sma() (Simple Moving Average)
```
:::

:::CodeblockTabs
TunedScript

```kotlin
swma() (Symmetrically Weighted Moving Average)
```
:::

:::CodeblockTabs
TunedScript

```kotlin
tema() (Triple Exponential Moving Average)
```
:::

:::CodeblockTabs
TunedScript

```kotlin
trima() (Triangular Moving Average)
```
:::

:::CodeblockTabs
TunedScript

```kotlin
vwma() (Volume Weighted Moving Average)
```
:::

:::CodeblockTabs
TunedScript

```kotlin
wma() (Weighted Moving Average)
```
:::

All of the above Moving Averages you will be able to find in our [Docs](https://www.tuned.com/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.&#x20;
Lets start with writing a small section of code to use the Exponential Moving Average

:::CodeblockTabs
TunedScript

```kotlin
//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

:::CodeblockTabs
TunedScript

```kotlin
//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)
```
:::

![](https://api.archbee.com/api/optimize/sLyofYuHQt3L9DD4FlP8D/4u5z9E9icdNaiDqmixeez_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


:::CodeblockTabs
TunedScript

```kotlin
//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.


![](https://api.archbee.com/api/optimize/sLyofYuHQt3L9DD4FlP8D/at8OKOHdVt79D-oNQUMcj_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!



