Skip to content

Instantly share code, notes, and snippets.

@tycoi2005
Last active November 24, 2024 15:43
Show Gist options
  • Save tycoi2005/d6943b755dc7058e588dd66dbfb23d72 to your computer and use it in GitHub Desktop.
Save tycoi2005/d6943b755dc7058e588dd66dbfb23d72 to your computer and use it in GitHub Desktop.
correlation usdx, usdvnd and vnindex
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"[*********************100%***********************] 3 of 3 completed\n",
"/var/folders/zh/g_gf_j_s13l4g_rmzlh9125w0000gn/T/ipykernel_14810/560431944.py:17: FutureWarning: The default fill_method='pad' in DataFrame.pct_change is deprecated and will be removed in a future version. Either fill in any non-leading NA values prior to calling pct_change or specify 'fill_method=None' to not fill NA values.\n",
" returns = data.pct_change().dropna()\n",
"[*********************100%***********************] 3 of 3 completed\n",
"/var/folders/zh/g_gf_j_s13l4g_rmzlh9125w0000gn/T/ipykernel_14810/560431944.py:17: FutureWarning: The default fill_method='pad' in DataFrame.pct_change is deprecated and will be removed in a future version. Either fill in any non-leading NA values prior to calling pct_change or specify 'fill_method=None' to not fill NA values.\n",
" returns = data.pct_change().dropna()\n",
"[*********************100%***********************] 3 of 3 completed"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Correlation Matrix: interval 1d\n",
" USDX USD_VND VNINDEX\n",
"USDX 1.000000 -0.008557 -0.001278\n",
"USD_VND -0.008557 1.000000 -0.003327\n",
"VNINDEX -0.001278 -0.003327 1.000000\n",
"Correlation Matrix: interval 1wk\n",
" USDX USD_VND VNINDEX\n",
"USDX 1.000000 -0.000555 -0.000372\n",
"USD_VND -0.000555 1.000000 -0.025753\n",
"VNINDEX -0.000372 -0.025753 1.000000\n",
"Correlation Matrix: interval 1mo\n",
" USDX USD_VND VNINDEX\n",
"USDX 1.000000 -0.230952 -0.275351\n",
"USD_VND -0.230952 1.000000 0.247131\n",
"VNINDEX -0.275351 0.247131 1.000000\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\n"
]
}
],
"source": [
"import yfinance as yf\n",
"import pandas as pd\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"\n",
"intervals = [\"1d\", \"1wk\",\"1mo\"]\n",
"\n",
"for interval in intervals:\n",
" # Define the tickers\n",
" tickers = {'USDX': 'DX-Y.NYB', 'USD_VND': 'VND=X', \"VNINDEX\": \"0P0000HY8X.VN\"}\n",
"\n",
" # Fetch data from Yahoo Finance\n",
" data = yf.download(list(tickers.values()), start='2020-01-01', end='2023-01-01', interval=interval)['Adj Close']\n",
" data.columns = list(tickers.keys())\n",
"\n",
" # Calculate daily returns\n",
" returns = data.pct_change().dropna()\n",
"\n",
" # Calculate the correlation matrix of returns\n",
" correlation_matrix = returns.corr()\n",
"\n",
" # Print the correlation matrix\n",
" print(\"Correlation Matrix: interval \", interval)\n",
" print(correlation_matrix)\n",
"\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.13.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment