Files
strategy-lab/to_explore/pyquantnews/94_MPLFinanceForStockCharts.ipynb
David Brazda e3da60c647 daily update
2024-10-21 20:57:56 +02:00

6.2 KiB

No description has been provided for this image

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.

In [ ]:
import yfinance as yf
import mplfinance as mpf
import warnings
In [ ]:
warnings.filterwarnings('ignore')

Download historical stock data for Apple (AAPL) from yfinance for the specified date range

In [ ]:
data = yf.download("AAPL", start="2022-01-01", end="2022-06-30")

Plot the downloaded data using a default chart type

In [ ]:
mpf.plot(data)

Plot the data using a candlestick chart

In [ ]:
mpf.plot(data, type="candle")

Plot the data using a line chart

In [ ]:
mpf.plot(data, type="line")

Plot the data using a Renko chart

In [ ]:
mpf.plot(data, type="renko")

Plot the data using an OHLC chart with a 15-day moving average

In [ ]:
mpf.plot(data, type="ohlc", mav=15)

Plot the data using a candlestick chart with moving averages of 7, 14, and 21 days

In [ ]:
mpf.plot(data, type="candle", mav=(7, 14, 21))

Plot the candlestick chart with moving averages and volume

In [ ]:
mpf.plot(data, type="candle", mav=(7, 14, 21), volume=True)

Plot the candlestick chart with moving averages, volume, and show non-trading periods

In [ ]:
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

In [ ]:
intraday = yf.download(tickers="PLTR", period="5d", interval="1m")

Select the last 100 rows of intraday data for plotting

In [ ]:
iday = intraday.iloc[-100:, :]

Plot the selected intraday data using a candlestick chart with 7 and 12-period moving averages and volume

In [ ]:
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.