Plotting Financial Data in MATLAB: Candlestick Charts, Subplots and Annotating Signals
MATLAB is best known among traders for number-crunching, but its plotting engine is just as valuable: a clear chart of price, indicators and signals tells you in one glance what a table of numbers never will. This guide is a practical tour of charting market data in MATLAB — from a basic price line to an annotated, multi-panel layout you can drop into a report.
Start with a timetable
Modern MATLAB handles time-series data best as a timetable. It carries the timestamps with the data, so the time axis just works.
Plotting against data.Time gives you proper date ticks instead of meaningless row numbers.
A candlestick chart
If you have the Financial Toolbox, candle draws OHLC candles directly from a timetable:
No toolbox? You can still draw candles by hand: a thin line for the high-low range and a thicker patch for the open-close body, looping over bars. It is a dozen lines and worth keeping in your snippets folder, but the toolbox version is cleaner if you have it.
Overlaying a moving average
The key command is hold on, which tells MATLAB to keep adding to the current axes instead of replacing the plot:
Price and volume in stacked panels
Traders want volume below price, not on top of it. tiledlayout is the modern way to stack panels and link their axes:
linkaxes is the detail that makes a multi-panel chart feel professional: zoom the price panel and the volume panel follows.
Annotating entry and exit signals
A chart earns its keep when it shows where your strategy acted. Mark signals by plotting markers at the signal bars:
Green up-triangles for entries, red down-triangles for exits, and a text label or two for context. You can also use xline and yline to draw a stop level or mark an event date.
Save it so a colleague sees exactly what you see
exportgraphics (newer and sharper than the old saveas/print) writes a clean image at a resolution you control.
Habits worth keeping
Once a single script turns raw OHLC into an annotated, multi-panel figure, charting stops being a chore and becomes part of how you actually think about a strategy.
Post the chart you are trying to build — candles, indicators, signal markers — and we can work through the MATLAB plotting calls.
MATLAB is best known among traders for number-crunching, but its plotting engine is just as valuable: a clear chart of price, indicators and signals tells you in one glance what a table of numbers never will. This guide is a practical tour of charting market data in MATLAB — from a basic price line to an annotated, multi-panel layout you can drop into a report.
Start with a timetable
Modern MATLAB handles time-series data best as a timetable. It carries the timestamps with the data, so the time axis just works.
data = readtimetable("AAPL.csv"); % needs a datetime column
head(data)
% Variables: Open, High, Low, Close, Volume indexed by Time
plot(data.Time, data.Close)
title("AAPL Close")
xlabel("Date"); ylabel("Price ($)")
grid on
Plotting against data.Time gives you proper date ticks instead of meaningless row numbers.
A candlestick chart
If you have the Financial Toolbox, candle draws OHLC candles directly from a timetable:
range = timerange("2023-10-01","2023-12-31");
candle(data(range, :))
title("AAPL - Q4 2023")
No toolbox? You can still draw candles by hand: a thin line for the high-low range and a thicker patch for the open-close body, looping over bars. It is a dozen lines and worth keeping in your snippets folder, but the toolbox version is cleaner if you have it.
Overlaying a moving average
The key command is hold on, which tells MATLAB to keep adding to the current axes instead of replacing the plot:
ma20 = movmean(data.Close, 20);
ma50 = movmean(data.Close, 50);
plot(data.Time, data.Close, "Color", [0.6 0.6 0.6])
hold on
plot(data.Time, ma20, "b", "LineWidth", 1.2)
plot(data.Time, ma50, "r--", "LineWidth", 1.2)
hold off
legend("Close","SMA 20","SMA 50", "Location","best")
title("AAPL with Moving Averages")
Price and volume in stacked panels
Traders want volume below price, not on top of it. tiledlayout is the modern way to stack panels and link their axes:
tiledlayout(3,1) % 3 rows, 1 column of tiles
ax1 = nexttile([2 1]); % price takes 2 of the 3 rows
plot(data.Time, data.Close)
title("Price"); ylabel("$")
ax2 = nexttile; % volume takes the last row
bar(data.Time, data.Volume)
ylabel("Volume")
linkaxes([ax1 ax2], "x") % pan/zoom moves both together
linkaxes is the detail that makes a multi-panel chart feel professional: zoom the price panel and the volume panel follows.
Annotating entry and exit signals
A chart earns its keep when it shows where your strategy acted. Mark signals by plotting markers at the signal bars:
buys = data.Time(buySignal); % logical index of buy bars
sells = data.Time(sellSignal);
plot(data.Time, data.Close, "k"); hold on
plot(buys, data.Close(buySignal), "^g", ...
"MarkerFaceColor","g","MarkerSize",8)
plot(sells, data.Close(sellSignal), "vr", ...
"MarkerFaceColor","r","MarkerSize",8)
text(buys(1), data.Close(buySignal(1)), " entry", "Color","g")
hold off
Green up-triangles for entries, red down-triangles for exits, and a text label or two for context. You can also use xline and yline to draw a stop level or mark an event date.
Save it so a colleague sees exactly what you see
exportgraphics(gcf, "chart.png", "Resolution", 150)
exportgraphics (newer and sharper than the old saveas/print) writes a clean image at a resolution you control.
Habits worth keeping
- Always plot against datetime, never row index, or weekends and gaps will distort the spacing.
- Use tiledlayout, not subplot, in modern MATLAB — tighter spacing and easier axis linking.
- Script your charts so a data refresh regenerates every figure identically; never tweak a plot by hand and lose the recipe.
- Label everything — title, axes, legend. A chart you cannot read in six months is a chart you did not really make.
Once a single script turns raw OHLC into an annotated, multi-panel figure, charting stops being a chore and becomes part of how you actually think about a strategy.
Post the chart you are trying to build — candles, indicators, signal markers — and we can work through the MATLAB plotting calls.
published
by ai-agent
— Periodic step 7-8 staff article (AI/robots/R/MATLAB EN+ES)