daily update

This commit is contained in:
David Brazda
2024-10-21 20:57:56 +02:00
parent 132172855a
commit e3da60c647
196 changed files with 1722489 additions and 1134 deletions

View File

@ -0,0 +1,272 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "9bcf2ee3",
"metadata": {},
"source": [
"<div style=\"background-color:#000;\"><img src=\"pqn.png\"></img></div>"
]
},
{
"cell_type": "markdown",
"id": "a5b4c5df",
"metadata": {},
"source": [
"This code retrieves historical stock price data for Netflix from Yahoo Finance and performs statistical analysis. It calculates the rolling z-score of the closing prices over a 30-day window, allowing for the detection of significant deviations from the mean. The z-score is then plotted and its distribution visualized using a histogram. Additionally, it computes the minimum percentage change in closing prices over a 30-day rolling window and visualizes it. This is useful for identifying extreme price movements and understanding the stock's volatility."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "50d5e330",
"metadata": {},
"outputs": [],
"source": [
"import yfinance as yf"
]
},
{
"cell_type": "markdown",
"id": "393360c8",
"metadata": {},
"source": [
"Download historical stock price data for Netflix from Yahoo Finance"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b89ccf32",
"metadata": {
"lines_to_next_cell": 1
},
"outputs": [],
"source": [
"data = yf.download(\"NFLX\", start=\"2020-01-01\", end=\"2022-06-30\")"
]
},
{
"cell_type": "markdown",
"id": "87c13794",
"metadata": {},
"source": [
"Define a function to calculate the z-score for a given chunk of data"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2b6152e4",
"metadata": {
"lines_to_next_cell": 1
},
"outputs": [],
"source": [
"def z_score(chunk):\n",
" \"\"\"Calculate z-score for a given chunk.\n",
" \n",
" Parameters\n",
" ----------\n",
" chunk : pd.Series\n",
" A series of stock prices or values.\n",
" \n",
" Returns\n",
" -------\n",
" float\n",
" The z-score of the last value in the chunk.\n",
" \n",
" Notes\n",
" -----\n",
" This method computes the z-score, which is the number \n",
" of standard deviations a value is from the mean.\n",
" \"\"\"\n",
" return (chunk[-1] - chunk.mean()) / chunk.std()"
]
},
{
"cell_type": "markdown",
"id": "d92d5b53",
"metadata": {},
"source": [
"Calculate the rolling z-score of the closing prices over a 30-day window"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9c3b8292",
"metadata": {},
"outputs": [],
"source": [
"rolled = data.Close.rolling(window=30).apply(z_score)"
]
},
{
"cell_type": "markdown",
"id": "f0c0ce32",
"metadata": {},
"source": [
"Plot the rolling z-score to visualize deviations from the mean"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5c0f3295",
"metadata": {},
"outputs": [],
"source": [
"rolled.plot()"
]
},
{
"cell_type": "markdown",
"id": "5fc5a361",
"metadata": {},
"source": [
"Plot a histogram of the rolling z-score to understand its distribution"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "979cf4f9",
"metadata": {},
"outputs": [],
"source": [
"rolled.hist(bins=20)"
]
},
{
"cell_type": "markdown",
"id": "1acedb50",
"metadata": {},
"source": [
"Find the minimum z-score value to identify significant deviations"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "127b0f43",
"metadata": {},
"outputs": [],
"source": [
"rolled.min()"
]
},
{
"cell_type": "markdown",
"id": "f13fc8f4",
"metadata": {},
"source": [
"Calculate the percentage change from the closing price on 20 April 2022"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "502e5d91",
"metadata": {},
"outputs": [],
"source": [
"(226.19 - 348.61) / 348.61"
]
},
{
"cell_type": "markdown",
"id": "da9e48db",
"metadata": {},
"source": [
"Calculate the minimum percentage change in closing prices over a 30-day rolling window"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5c98be53",
"metadata": {},
"outputs": [],
"source": [
"min_pct_change = (\n",
" data\n",
" .Close\n",
" .pct_change()\n",
" .rolling(window=30)\n",
" .min()\n",
")"
]
},
{
"cell_type": "markdown",
"id": "47be23a9",
"metadata": {},
"source": [
"Plot the minimum percentage change to visualize extreme price movements"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e1fabbf2",
"metadata": {},
"outputs": [],
"source": [
"min_pct_change.plot()"
]
},
{
"cell_type": "markdown",
"id": "3c0c5951",
"metadata": {},
"source": [
"Plot a histogram of the minimum percentage change to understand its distribution"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a1697d6b",
"metadata": {},
"outputs": [],
"source": [
"min_pct_change.hist(bins=20)"
]
},
{
"cell_type": "markdown",
"id": "d97d6472",
"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"
},
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}