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:
We want to build our average using the Close for the last X bars. Where X is an input to allow for more flexibility. You already know that we want to use a loop for that and we just found out how we can reference Close values for the previous bars. This should be enough to write the code for the main part of our indicator. Let’s continue by creating the input and variable sections. You might recall from the last lesson that using meaningful variable names is a good coding practice and can save you a lot of troubles later.
We need to declare one input so we are able to change the length for our average on the chart. Besides that we want one variable that holds the summation, one to hold the counter value and a last one to store the average value. For outputting the value on the chart we will use the reserved word Plot. This is followed by a number so you are able to distinguish between different plots. Which is needed as you can use up to 999 plots in Multicharts. The plot reserved word can have several parameters like color, plot size and some more. We will keep it simple here and use Plot1 with just two parameters – the first for the numerical expression to be plotted and a second one for the name we want to assign to the plot. The final code will look something like this:
After compiling this code we are almost ready to load our indicator to a chart in Multicharts. Let’s just take a look at the properties of the indicator first. You can find them under -> File -> Properties or by clicking on the Properties symbol in the menu (it should be the one left to Compile). Under the Style tab you can change the color, line style and thickness for the plot you created. If you go to the properties tab there are several options to set or check, but for now you might only want to make sure the option “Same As Symbol” is checked. This will make sure the indicator is applied directly on your chart rather than a subchart.
Now you are ready to apply the indicator to a chart of your choice. When you have a chart open in the Multicharts main window you can simply insert the indicator to this chart.
When the indicator is applied the outcome should be similar to the above screenshot. However this doesn’t seem right as this doesn’t look like a moving average at all. The price series is almost a flat line and the plot coming from our indicator is only rising. With the E-Mini S&P 500 being in the area of 1’800 a 10 bar moving average value for this market of 1’952’647 is obviously not correct. This points towards a problem in our calculations. Do you have an idea what the code is missing? It actually is just a little, but very important detail we forgot to add. We need to add something in front of the for loop. The loop simply keeps on adding the values for the previous ten bars with every new bar. This is fine and we want it to do exactly this, but we don’t want it to add the new values to the old values. In other words you need to make sure CloseValueSum doesn’t still hold the old values when the for loop starts. With adding one line to the code the outcome is exactly what we wanted to achieve.
We can also change the indicator’s appearance on the chart. Using the style tab under “Format Study” we can alter the visual outcome like line style, color and thickness. Under the “Inputs” tab you will find the input you created and the default setting for the length. By loading a second instance of the study and using a different color and length you can confirm that the study gives a different outcome with a different length input.
If you are having trouble finding the correct fix feel free to contact us with your solution and we will try to help you in a timely manner. I am afraid just asking for the solution won’t work though, you need at least be able to show that you put some effort into finding the solution, too. As a last hint you can take a look at other average indicators or functions and find some inspiration for the missing link there. I hope you enjoyed this Powerlanguage tutorial lesson and I am looking forward to working with you in the next one.