286 lines
6.2 KiB
Plaintext
286 lines
6.2 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "464b6cd8",
|
|
"metadata": {},
|
|
"source": [
|
|
"<div style=\"background-color:#000;\"><img src=\"pqn.png\"></img></div>"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "e3bcc3f9",
|
|
"metadata": {},
|
|
"source": [
|
|
"This code downloads historical stock data for specified tickers and time periods using yfinance. \n",
|
|
"It then visualizes the data using mplfinance, producing various types of financial charts \n",
|
|
"such as candlestick, line, and Renko charts. The code also demonstrates how to plot moving \n",
|
|
"averages and how to include volume in the charts. This is useful for technical analysis \n",
|
|
"and understanding price movements over different timeframes."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "b6f07ce9",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import yfinance as yf\n",
|
|
"import mplfinance as mpf\n",
|
|
"import warnings"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "1343f752",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"warnings.filterwarnings('ignore')"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "8a8960a3",
|
|
"metadata": {},
|
|
"source": [
|
|
"Download historical stock data for Apple (AAPL) from yfinance for the specified date range"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "14bc43b3",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"data = yf.download(\"AAPL\", start=\"2022-01-01\", end=\"2022-06-30\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "7ded8f61",
|
|
"metadata": {},
|
|
"source": [
|
|
"Plot the downloaded data using a default chart type"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "17fa0ce1",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"mpf.plot(data)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "1c0611a0",
|
|
"metadata": {},
|
|
"source": [
|
|
"Plot the data using a candlestick chart"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "4e278a86",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"mpf.plot(data, type=\"candle\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "e285c032",
|
|
"metadata": {},
|
|
"source": [
|
|
"Plot the data using a line chart"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "9214073c",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"mpf.plot(data, type=\"line\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "61bbcc00",
|
|
"metadata": {},
|
|
"source": [
|
|
"Plot the data using a Renko chart"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "0f63a046",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"mpf.plot(data, type=\"renko\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "5865feca",
|
|
"metadata": {},
|
|
"source": [
|
|
"Plot the data using an OHLC chart with a 15-day moving average"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "7a36a88c",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"mpf.plot(data, type=\"ohlc\", mav=15)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "e8b33855",
|
|
"metadata": {},
|
|
"source": [
|
|
"Plot the data using a candlestick chart with moving averages of 7, 14, and 21 days"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "3bf408dd",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"mpf.plot(data, type=\"candle\", mav=(7, 14, 21))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "622d2692",
|
|
"metadata": {},
|
|
"source": [
|
|
"Plot the candlestick chart with moving averages and volume"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "82641833",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"mpf.plot(data, type=\"candle\", mav=(7, 14, 21), volume=True)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "f62ec2a9",
|
|
"metadata": {},
|
|
"source": [
|
|
"Plot the candlestick chart with moving averages, volume, and show non-trading periods"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "ad60039d",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"mpf.plot(\n",
|
|
" data, \n",
|
|
" type=\"candle\", \n",
|
|
" mav=(7, 14, 21), \n",
|
|
" volume=True, \n",
|
|
" show_nontrading=True\n",
|
|
")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "72374bfa",
|
|
"metadata": {},
|
|
"source": [
|
|
"Download intraday stock data for Palantir (PLTR) with 1-minute intervals over the last 5 days"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "600c9dbc",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"intraday = yf.download(tickers=\"PLTR\", period=\"5d\", interval=\"1m\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "fc78ad0c",
|
|
"metadata": {},
|
|
"source": [
|
|
"Select the last 100 rows of intraday data for plotting"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "27e18b45",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"iday = intraday.iloc[-100:, :]"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "017866b3",
|
|
"metadata": {},
|
|
"source": [
|
|
"Plot the selected intraday data using a candlestick chart with 7 and 12-period moving averages and volume"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "502d5882",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"mpf.plot(iday, type=\"candle\", mav=(7, 12), volume=True)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "76dc895f",
|
|
"metadata": {},
|
|
"source": [
|
|
"<a href=\"https://pyquantnews.com/\">PyQuant News</a> 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 <a href=\"https://gettingstartedwithpythonforquantfinance.com/\">get started with Python for quant finance</a>. For educational purposes. Not investment advise. Use at your own risk."
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"jupytext": {
|
|
"cell_metadata_filter": "-all",
|
|
"main_language": "python",
|
|
"notebook_metadata_filter": "-all"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|