Calculating the Exponential Moving Average in JavaScript

Anthony Lepore
CodeX
Published in
4 min readFeb 26, 2021

--

Last week, we began our building of market data indicators in JavaScript with the Simple Moving Average. We also added the helper formula extractData to quickly return which data key from our dataset object that we wanted to run calculations on (ie., the Open, High, Low, or Closing price). Today, as promised at the end of the previous post, we will explore the calculation for the Exponential Moving Average.

As we recall, the Simple Moving Average (SMA) takes in a series of price points across time, and returns the average of the last n data points. For example, the often-quoted 200 Day Moving Average simply takes the closing prices of a particular trading instrument for the last 200 days, and calculates the average of only those prices. It is called a moving average because tomorrow, when a new closing price is added to the dataset, the earliest data point from yesterday’s calculation will be removed. Therefore, tomorrow’s SMA will most likely be different from today’s SMA, but not by much. By plotting these points on a chart, we can quickly see if the SMA is moving up each day that we are in an uptrend, and conversely, if the SMA is sloping downward, we are in a downtrend. (Note: That was a very simplistic description of uptrend and downtrend since there are many more variables that one could use to determine that distinction, but the SMA slope…

--

--

Anthony Lepore
CodeX

Composer, playwright, designer for theater, jazz musician, philosopher, software engineer and technical writer for a FinTech firm in NYC.