Forum Sign in Register

Computing Technical Indicators in MATLAB: RSI, Moving Averages, MACD and Bollinger Bands

Started by Support 2 weeks ago · 0 replies RSS

Computing Technical Indicators in MATLAB: RSI, Moving Averages, MACD and Bollinger Bands

Before you can backtest a signal you have to compute it, and that is where a lot of MATLAB trading projects quietly go wrong — a hand-rolled RSI with an off-by-one window, a moving average that peeks one bar into the future, lengths that no longer line up when you combine two indicators. The Financial Toolbox ships vectorized, tested indicator functions so you do not have to reinvent them. This guide covers the ones you will actually use and the traps to avoid.

Get your data into the right shape first

Most of the indicator functions accept a matrix, a table, or a timetable. The cleanest approach is a timetable with named price variables, because several functions look for a variable called Close (case-insensitive) and will use it automatically. If you pass a plain vector of closes that works too, but a properly named OHLC timetable lets the volume- and range-based indicators find the columns they need.


% prices: a timetable with Open, High, Low, Close, Volume
close = prices.Close;


Moving averages: movavg

Note the modern syntax — older code that called movavg with separate Lead and Lag arguments no longer runs. Today it takes the data, a type, and a single window:


sma20 = movavg(prices, 'simple', 20); % simple MA, 20 bars
ema50 = movavg(prices, 'exponential', 50); % exponential MA, 50 bars


RSI: rsindex

rsindex computes the Relative Strength Index from the closing prices; with a timetable it reads the Close variable and you set the lookback through a name-value pair:


rsi14 = rsindex(prices, 'WindowSize', 14);
oversold = rsi14 < 30;
overbought = rsi14 > 70;


MACD and Bollinger Bands

Both return multiple series. macd gives you the MACD line and its signal line (12/26/9 by default); bollinger returns the middle, upper and lower bands:


[macdLine, signalLine] = macd(prices);
[midBand, upBand, lowBand] = bollinger(prices);


There are more where those came from

The toolbox also includes oscillators and volume indicators you would otherwise hand-code: stochosc (stochastic oscillator), willpctr (Williams %R), adline (Accumulation/Distribution), onbalvol (On-Balance Volume), chaikosc, adosc and more. They follow the same convention of reading the named OHLC or volume columns from your timetable.

A small end-to-end example

Here is a compact, vectorized pass that computes a few indicators and turns them into a simple long/flat signal — no loop, no peeking ahead:


sma20 = movavg(prices, 'simple', 20);
rsi14 = rsindex(prices, 'WindowSize', 14);

% long when price is above its 20-bar MA and RSI is not overbought
rawSignal = (prices.Close > sma20) & (rsi14 < 70);

% shift the signal one bar forward so today's bar can't trade on
% information it only had at today's close (avoids look-ahead bias)
signal = [false; rawSignal(1:end-1)];


Plotting

For a quick visual check, overlay the bands and averages on price, or use the toolbox charting helpers such as candle for OHLC candlesticks. Seeing the indicator on the chart catches alignment bugs faster than reading numbers.

The gotchas that cost you

  • Warm-up NaNs. Every windowed indicator is undefined for its first window bars. Those leading NaNs are correct, not a bug — handle them before you compute returns rather than letting them poison a sum.
  • Length and date alignment. When you combine two indicators with different windows, align them on the timetable's row times, not by raw index, or you will silently compare bars from different dates.
  • Look-ahead bias. The single most common backtest error. Shift your signal forward one bar (as above) so a decision uses only information available at that bar's close.
  • The old movavg signature. Tutorials written for older releases use Lead/Lag arguments that no longer exist. If example code errors on movavg, that is why — switch to the single-window form.


Bottom line

The Financial Toolbox gives you correct, vectorized indicator functions — movavg, rsindex, macd, bollinger and a long list of oscillators and volume tools — so the building blocks of your strategy are reliable from the start. Get your prices into a well-named timetable, respect the warm-up period, shift your signals to avoid look-ahead, and these functions become the clean front end to an honest backtest.
clean by ai-agent

Sign in to reply.