DEV Community

Cover image for Efficient way to calculate a moving average
Darko
Darko

Posted on

Efficient way to calculate a moving average

The moving average figure is part of the most popular trading indicators. It provide reliable and important information about the state of the market we are tracking.

Benefits of moving averages

Instead of tracking the price movement only by the latest price, which is only one point, the moving average is tracking the average movement in a full period, taking into consideration the last n points.

If you’re calculating daily average, that is the average price movement in the last 24 hours, this is taking into consideration all price points from the last 24 hours.
This makes the moving average a more reliable indicator of the price movement that is not easily swayed by one or two trades.

Usages

The moving average is part of many trading strategies. One of the most common signals is tracking two distinct moving average windows and detecting the points of their interceptions.

For example tracking the short 5 days and the long 30 days averages. When the short moving average crosses the long moving average that indicates these is real price movement in the market.

Calculating Moving Average
Example: Let’s consider having the following 5 prices, one per day, that we want to calculate moving average.

Day1: 5000
Day2: 5390
Day3: 5500
Day4: 5400
Day5: 5700

If we are calculating moving average with size of 5, we would calculate the average from all 5 days from above:
Sum = 5000 + 5390 + 5500 + 5400 + 5700 = 25000
Count = 5
Moving average = 25000 / 5 = 5000

When we receive a price for Day6 we would need to sum the day prices between Day2 and Day6 and again divide by 5 to keep the moving average moving forward.

The Efficient approach

At BitcoinAverage we are keeping moving averages from 24h up to 1 full year. In this case we have a lot of price points to sum up which is quite inefficient considering that only few price points come in and few expire.

There is also another challenge which is that sometimes there could be 5 new price point per calculation and other times 10, so the count of the numbers need to be adjusted as well.

The approach we came up with is the following:

Keep a moving sum and moving count of all price points
When new price points arrive, update the sum and the counter
When price points expire, remove them from sum and decrease counter
To calculate the average just divide the moving sum by the moving counter.

Example

Let’s start with the price points: 5001, 5002, 5003, 5004.

We keep the sum and the count of these numbers:

Sum = 20010

Count = 4

Then we receive two new numbers: 5010 and 5008, now are full list looks like this:

5001, 5002, 5003, 5004, 5010, 5008.

If we are keeping a moving average based on the number of price points, in this case 4, then we need to include the two new numbers and remove the two oldest ones.

What we do is just subtract from the sum the expired ones and add in the new ones.

Sum = 20010 – 5001 – 5002 + 5010 + 5008 = 2025

The counter stays the same, 4.

However most often you will need to keep a moving average based on a time window, for example 24hours. And you will be calculating the average on a different interval than the prices are ariving.

For example new price points could be arriving every second, but the average will be recalculated every minute. This is the case with any trading market, there are new prices every second.

So at the point of updating the moving average you could have 15 new prices and only 12 expiring prices, which would alter your counter value.

The first thing to do is to keep your price points sorted by their time of arrival, the easiest format to keep track of arrival time is by timestamp,

Timestamp for a specific date is a number that represents the number of seconds passed since 1970 until that date and time.

Without further due here is a complete gist example in Python:

Discussion (0)