6.2 KiB
This code downloads historical stock data for specified tickers and time periods using yfinance. It then visualizes the data using mplfinance, producing various types of financial charts such as candlestick, line, and Renko charts. The code also demonstrates how to plot moving averages and how to include volume in the charts. This is useful for technical analysis and understanding price movements over different timeframes.
import yfinance as yf import mplfinance as mpf import warnings
warnings.filterwarnings('ignore')
Download historical stock data for Apple (AAPL) from yfinance for the specified date range
data = yf.download("AAPL", start="2022-01-01", end="2022-06-30")
Plot the downloaded data using a default chart type
mpf.plot(data)
Plot the data using a candlestick chart
mpf.plot(data, type="candle")
Plot the data using a line chart
mpf.plot(data, type="line")
Plot the data using a Renko chart
mpf.plot(data, type="renko")
Plot the data using an OHLC chart with a 15-day moving average
mpf.plot(data, type="ohlc", mav=15)
Plot the data using a candlestick chart with moving averages of 7, 14, and 21 days
mpf.plot(data, type="candle", mav=(7, 14, 21))
Plot the candlestick chart with moving averages and volume
mpf.plot(data, type="candle", mav=(7, 14, 21), volume=True)
Plot the candlestick chart with moving averages, volume, and show non-trading periods
mpf.plot( data, type="candle", mav=(7, 14, 21), volume=True, show_nontrading=True )
Download intraday stock data for Palantir (PLTR) with 1-minute intervals over the last 5 days
intraday = yf.download(tickers="PLTR", period="5d", interval="1m")
Select the last 100 rows of intraday data for plotting
iday = intraday.iloc[-100:, :]
Plot the selected intraday data using a candlestick chart with 7 and 12-period moving averages and volume
mpf.plot(iday, type="candle", mav=(7, 12), volume=True)
PyQuant News is where finance practitioners level up with Python for quant finance, algorithmic trading, and market data analysis. Looking to get started? Check out the fastest growing, top-selling course to get started with Python for quant finance. For educational purposes. Not investment advise. Use at your own risk.
