Quant500 Academy

2. Candlesticks, moving averages and your first technical chart

Level 3: Market Data and APIs

A line chart connects closing prices, but it loses information: it does not show the range the price touched within each day. That is what candlesticks are for, the visual standard of any trading platform: each candle shows the open, the close, the high and the low of a period, and the color indicates whether that period closed up or down.

The mplfinance library is designed specifically to draw candlesticks from a DataFrame with the OHLC columns (Open, High, Low, Close) that you already have after downloading data with yfinance:

import mplfinance as mpf

mpf.plot(data[-50:], type="candle", volume=True)

data[-50:] selects only the last 50 days — with long series, drawing the entire history as candles usually becomes unreadable, so restricting the range is almost mandatory. volume=True adds a lower panel with volume bars, aligned with the dates of the main chart.

On top of that chart it is common to overlay moving averages, which smooth the daily noise of the price to reveal the underlying trend. A 100-day simple moving average, for example, is computed in a single line thanks to pandas:

data["sma_100"] = data["Adj Close"].rolling(window=100).mean()

rolling(window=100) creates a sliding window of 100 days; .mean() computes the average within that window for each row. The result is that the first 99 days are empty (there is not enough history to compute the average yet) and, from day 100 onwards, a line that is far smoother than the original price — the wider the window, the smoother the line and the later it reacts to a genuine change in trend.

mplfinance also lets you pass several moving averages directly as a parameter, without having to compute them by hand:

mpf.plot(data[-50:], type="candle", mav=(3, 6, 9), volume=True)

mav=(3, 6, 9) draws three simultaneous moving averages of 3, 6 and 9 periods over the candles. Seeing several averages of different lengths at once is the visual basis of one of the oldest strategies in technical analysis: the moving average crossover, which you will see formalized in the next module.

To go deeper into this lesson

The rest of Quant500

What you are learning, applied to the index every day:

The model portfolio, weekly in your inbox

Every Saturday, its week in one minute: how it went, the best and the worst, and which insiders bought with their own money. Plus, the day the model is rebuilt, which stocks come in and which go out. A simulation, not advice. At most one email a week, no ads, no account; one-click unsubscribe. Made by one person: Pol Gracia.

A model portfolio with simulated results, not a personalized recommendation. Your email is used for this only: privacy.

Behind the Academy and the rest of Quant500 there is one person: Pol Gracia Serrahima.