- A Moving Average is often used to smooth out price data.
- Moving Averages are commonly used to gauge trend direction.
- There are many types of Moving Averages, each with varying results/calculations.
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
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)
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.
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!
5 Principles for Writing Your First Automated Trading Strategy
If you’re looking for massive profits in the market, you will have to build a…
The Pitfalls of Backtesting – How to Avoid Overfitting and Disappointment
Table of Contents: What is overfitting?How do I avoid overfitting? For the investors reading this…
The No-Code Strategy Template by Tuned
Out of 50,000 trading bots there’s a strong chance at least one is profitable. In…