- Moving Averages (SMA & EMA)
Simple Moving Average (SMA)
The Simple Moving Average (SMA) is the average of a security’s price over a specific time period. It’s one of the most commonly used technical indicators to determine trends.
Exponential Moving Average (EMA)
The Exponential Moving Average (EMA) gives more weight to recent prices and reacts faster to price changes compared to the Simple Moving Average (SMA).
- Relative Strength Index (RSI)
RSI is a momentum oscillator that measures the speed and change of price movements. It ranges from 0 to 100 and is typically used to identify overbought or oversold conditions.
Step-by-Step VBA Code Explanation
The following code will calculate:
- SMA: Simple Moving Average.
- EMA: Exponential Moving Average.
- RSI: Relative Strength Index.
We’ll assume you have historical price data in an Excel worksheet in the following format:
- Column A: Date
- Column B: Closing Price (the security’s price)
Excel Sheet Layout Example:
| Date | Closing Price |
| 01/01/2025 | 100 |
| 01/02/2025 | 105 |
| 01/03/2025 | 103 |
| 01/04/2025 | 110 |
| … | … |
Excel VBA Code:
Sub CalculateTechnicalIndicators()
' Define worksheet and range
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Sheet1")
' Define variables
Dim lastRow As Long
Dim i As Long
Dim period As Integer
Dim sma As Double, ema As Double, rs As Double
Dim gains As Double, losses As Double
Dim avgGain As Double, avgLoss As Double
Dim rsi As Double
' Find the last row of data
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
' Set period for SMA, EMA, and RSI
period = 14 ' 14-period for SMA, EMA, and RSI
' Adding Headers for SMA, EMA, and RSI columns
ws.Cells(1, 3).Value = "SMA"
ws.Cells(1, 4).Value = "EMA"
ws.Cells(1, 5).Value = "RSI"
' Calculate SMA (Simple Moving Average)
For i = period To lastRow
sma = Application.WorksheetFunction.Average(ws.Range("B" & i - period + 1 & ":B" & i))
ws.Cells(i, 3).Value = sma
Next i
' Calculate EMA (Exponential Moving Average)
ema = ws.Cells(period, 3).Value ' Start EMA with the first SMA value
Dim multiplier As Double
multiplier = 2 / (period + 1)
For i = period + 1 To lastRow
ema = (ws.Cells(i, 2).Value - ema) * multiplier + ema
ws.Cells(i, 4).Value = ema
Next i
' Calculate RSI (Relative Strength Index)
For i = period + 1 To lastRow
gains = 0
losses = 0
' Calculate the average gains and losses
For j = i - period + 1 To i
If ws.Cells(j + 1, 2).Value > ws.Cells(j, 2).Value Then
gains = gains + (ws.Cells(j + 1, 2).Value - ws.Cells(j, 2).Value)
Else
losses = losses + (ws.Cells(j, 2).Value - ws.Cells(j + 1, 2).Value)
End If
Next j
' Average Gain and Loss
avgGain = gains / period
avgLoss = losses / period
If avgLoss = 0 Then
rsi = 100 ' If there are no losses, RSI is 100
Else
' Calculate RS (Relative Strength) and RSI
rs = avgGain / avgLoss
rsi = 100 - (100 / (1 + rs))
End If
' Output RSI to the worksheet
ws.Cells(i, 5).Value = rsi
Next i
MsgBox "Technical Indicators Calculated Successfully!", vbInformation
End Sub
Explanation of the Code
- Initial Setup
- The ws variable represents the worksheet where the price data resides (adjust the sheet name as needed).
- We define variables like lastRow to get the last row of the data, period for the moving average and RSI period (commonly 14), and other variables to hold intermediate results.
- SMA Calculation
- For each row from the period to lastRow, we calculate the Simple Moving Average by averaging the closing prices in the last period number of rows. This is done using the Application.WorksheetFunction.Average method.
- EMA Calculation
- The Exponential Moving Average (EMA) starts with the first SMA value and then uses the multiplier (2 / (period + 1)) to calculate the next EMA values. This gives more weight to the recent closing prices.
- RSI Calculation
- For RSI, we calculate the average gain and average loss for the previous period number of closing prices. If there are no losses in the period, RSI is set to 100. Otherwise, the Relative Strength (RS) is calculated as the average gain divided by the average loss, and RSI is computed using the formula RSI = 100 – (100 / (1 + RS)).
How to Use the Code
- Open Excel and press Alt + F11 to open the VBA editor.
- In the VBA editor, click Insert > Module to create a new module.
- Paste the code into the module.
- Go back to the Excel worksheet and press Alt + F8, select CalculateTechnicalIndicators, and click Run.
This will calculate and output the following columns:
- SMA in Column C
- EMA in Column D
- RSI in Column E
Conclusion
This VBA code demonstrates how to implement some of the most commonly used technical analysis indicators in Excel. You can modify this code to incorporate additional indicators or adjust the period lengths based on your trading strategy.