{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import yfinance as yf" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Stocks" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "[*********************100%***********************] 1 of 1 completed\n" ] }, { "data": { "text/plain": [ "Date\n", "2012-05-18 38.050667\n", "2012-05-21 33.870369\n", "2012-05-22 30.854580\n", "2012-05-23 31.849892\n", "2012-05-24 32.875061\n", " ... \n", "2020-01-03 207.691162\n", "2020-01-06 211.602722\n", "2020-01-07 212.060562\n", "2020-01-08 214.210419\n", "2020-01-09 217.275986\n", "Name: META, Length: 1923, dtype: float64" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ticker = \"META\"\n", "start_date = \"2010-01-01\"\n", "end_date = \"2020-01-10\"\n", "\n", "data = yf.download(ticker, start=start_date, end=end_date) # [\"Close\"][ticker]\n", "data[\"Close\"][ticker]" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1923,)" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data[\"Close\"][ticker].shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Gold" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "[*********************100%***********************] 1 of 1 completed\n" ] }, { "data": { "text/plain": [ "Date\n", "2010-01-04 1117.699951\n", "2010-01-05 1118.099976\n", "2010-01-06 1135.900024\n", "2010-01-07 1133.099976\n", "2010-01-08 1138.199951\n", " ... \n", "2020-01-03 1549.199951\n", "2020-01-06 1566.199951\n", "2020-01-07 1571.800049\n", "2020-01-08 1557.400024\n", "2020-01-09 1551.699951\n", "Name: GC=F, Length: 2519, dtype: float64" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ticker = \"GC=F\"\n", "data = yf.download(ticker, start=start_date, end=end_date)\n", "data[\"Close\"][ticker]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
OpenHighLowCloseVolumeDividendsStock Splits
Date
2012-05-18 00:00:00-04:0041.85274744.78891037.82174638.0506675735764000.00.0
2012-05-21 00:00:00-04:0036.35863836.48802932.84519833.8703651681927000.00.0
2012-05-22 00:00:00-04:0032.45703033.43243330.79486430.8545821017866000.00.0
2012-05-23 00:00:00-04:0031.22284832.34754631.21289431.849892736000000.00.0
2012-05-24 00:00:00-04:0032.79543433.05421331.62096932.875057502372000.00.0
\n", "
" ], "text/plain": [ " Open High Low Close \\\n", "Date \n", "2012-05-18 00:00:00-04:00 41.852747 44.788910 37.821746 38.050667 \n", "2012-05-21 00:00:00-04:00 36.358638 36.488029 32.845198 33.870365 \n", "2012-05-22 00:00:00-04:00 32.457030 33.432433 30.794864 30.854582 \n", "2012-05-23 00:00:00-04:00 31.222848 32.347546 31.212894 31.849892 \n", "2012-05-24 00:00:00-04:00 32.795434 33.054213 31.620969 32.875057 \n", "\n", " Volume Dividends Stock Splits \n", "Date \n", "2012-05-18 00:00:00-04:00 573576400 0.0 0.0 \n", "2012-05-21 00:00:00-04:00 168192700 0.0 0.0 \n", "2012-05-22 00:00:00-04:00 101786600 0.0 0.0 \n", "2012-05-23 00:00:00-04:00 73600000 0.0 0.0 \n", "2012-05-24 00:00:00-04:00 50237200 0.0 0.0 " ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Get data for Gold futures\n", "gold = yf.Ticker(\"GC=F\")\n", "\n", "# Get historical data (e.g., for the last month)\n", "gold_data = gold.history(period=\"1000mo\")\n", "\n", "# Print the data\n", "(gold_data.head())" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(6185,)" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gold_data[\"Close\"].shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## " ] } ], "metadata": { "kernelspec": { "display_name": "return-on-investment", "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.12.9" } }, "nbformat": 4, "nbformat_minor": 2 }