# EasyLanguage & PowerLanguage Tutorial – Lesson 02: Coding A Moving Average

## Creating the first real indicator and expanding the basics

After you familiarized yourself with the PowerLanguage Editor in the previous PowerLanguage tutorial – lesson 01 we will now build up on this foundation. In case you haven’t read the last lesson, I would suggest doing that first as it can help you with understanding this lesson, too. Let’s start with today’s lesson now.

Open the PowerLanguage Editor and create a new Indicator study. I will name mine ABC_PowerLanguage Lesson 02 – Moving Average so I can find it easily within my editor later. The name is totally up to you of course and you could even change it later. As the last part of the indicator name suggests, we will create and plot a moving average today. You have probably seen a moving average on a chart before or remember the term average from math. The main use for averages is as a filter to smooth the data you input. The image displays a 200 period simple moving average that gives a very smooth outcome. The downside to this smoothness is that you introduce more lag. This means the average becomes less responsive to changes in price. If you take a look at the next image you will see how different the behavior of a 200 period simple moving average is when you compare it to the green 10 period average. The latter is much quicker in responding to price changes, but in turn there is a lot more “noise” in the average. There are many different types of averages that mainly vary in the impact each data point has on the outcome of the average. A 200 period simple moving average will simply compute a summation of the last 200 data points and divide it by 200. The result is an average which gives each data point the same influence (the same value) on the outcome. The first bar and the last bar that are part of the average are both weighted the same for the outcome.
Two other prominent and commonly used averages are the Exponential Moving Average and the Weighted Moving Average. Both have higher weighting factors for the more recent data points. In a weighted moving average the weighting will decrease in arithmetical progression. For the exponential average it will decrease exponentially, hence the name. This will be as theoretically as it will get for today. If you want to read some more details about averages, you can start with this Wikipedia article. For further understanding of this lesson you won’t need this additional information though.

Let’s start with coding our average. Our indicator should not only calculate an average, but it should output the result to a chart. EasyLanguage has the “Plot” reserved word for that and we will use it to do that. Before you start with programming something it’s always a good idea to take a step back and think about what you are trying to accomplish and how you are going to do it.
As this study is not very complex, there are only a few things to think through. When studies get more complex you can save a lot of time with good planning upfront.

The goal is a study that calculates and plots a simple moving average.
We want to be able to change the length for the average with an input so it’s easy to customize.
For the average we need to sum the amount of values correlating to the length input. We don’t want to write code for every possible length input for the summation. This means the code needs to be able to calculate all possible length inputs on its own. Do you already have an idea how we could accomplish this?

The answer is that we need an iteration statement that can be executed repeatedly each bar for a specific number of times (our length input). I know this sounds complicated, but it will be quite simple. We will use the “for loop” for this task. This loop repeats one or more statements for a user defined, specific number of iterations. EasyLanguage code is executed from top to bottom and usually from left to right. Once one code line is executed, the next line is executed and so on. In case the code line is the beginning of a loop, the code lines within the loop will be executed for the specified amount. Only when the loop is finished the next code line after the loop is executed.
A for loop looks and works the following way:
A numerical variable will be incremented (or decremented) with every cycle through the loop from its start value to its end value.
This image displays a basic for loop with a numeric counter variable (ii in this case) and the initial value of 0. The iterations will be done ten times until the counter has reached the value of 9. Then the loop block is executed the last time and exited. You don’t have to increment the counter value yourself, the loop code takes care of that. The current counter value will be stored in the counter variable. So you can access it for every loop cycle and use it for your calculations. This will come in handy for calculating our average. The for loop can also decrement the counter with every iteration. The initial value in this example is 9, but the loop is executed ten times until it is exited, too. The counter simply decreases with every iteration by one until it reaches 0. In Easylanguage you can reference data-related reserved words, variables and functions from a previous bar very easy. Using a number within square brackets following the reserved word, calculation or variable will return the value for this particular bar. The number grows from the current bar (which you reference with ) in increments of one. When you want to store the value of the previous bar’s close within a variable called PrevCloseValue you can do it like this: