{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "initial_id", "metadata": { "collapsed": true }, "outputs": [], "source": [ "%pip install \"yfinance[optional]\"==\"0.2.37\"" ] }, { "cell_type": "code", "execution_count": 7, "id": "904104edfdda9e89", "metadata": { "ExecuteTime": { "end_time": "2024-03-18T09:32:44.491420Z", "start_time": "2024-03-18T09:32:44.088451Z" }, "collapsed": false }, "outputs": [], "source": [ "import yfinance as yf\n", "\n", "hg_f = yf.Ticker(\"HG=F\")" ] }, { "cell_type": "code", "execution_count": 8, "id": "f8531df814e529fb", "metadata": { "ExecuteTime": { "end_time": "2024-03-18T09:32:47.062820Z", "start_time": "2024-03-18T09:32:44.497587Z" }, "collapsed": false }, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np\n", "\n", "# get historical market data\n", "hist = hg_f.history(period=\"100y\")\n", "\n", "# Replace 0 values with NaN in specific columns\n", "hist.replace({'Volume': {0: pd.NA}, 'Open': {0: pd.NA}}, inplace=True)\n", "hist.replace(0, np.nan, inplace=True) # Replace 0 with NaN\n", "\n", "columns = ['Open', 'High', 'Low', 'Close', 'Volume']" ] }, { "cell_type": "code", "execution_count": 9, "id": "de2634931db1a5d6", "metadata": { "ExecuteTime": { "end_time": "2024-03-18T09:32:47.077281Z", "start_time": "2024-03-18T09:32:47.066270Z" }, "collapsed": false }, "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
2000-08-30 00:00:00-04:000.8790.8870.87700.88502886NaNNaN
2000-08-31 00:00:00-04:000.8850.8880.88000.88501095NaNNaN
2000-09-01 00:00:00-04:000.8780.8890.87800.88903449NaNNaN
2000-09-05 00:00:00-04:000.8960.9070.89500.90601397NaNNaN
2000-09-06 00:00:00-04:000.9050.9060.89750.90151195NaNNaN
\n", "
" ], "text/plain": [ " Open High Low Close Volume Dividends \\\n", "Date \n", "2000-08-30 00:00:00-04:00 0.879 0.887 0.8770 0.8850 2886 NaN \n", "2000-08-31 00:00:00-04:00 0.885 0.888 0.8800 0.8850 1095 NaN \n", "2000-09-01 00:00:00-04:00 0.878 0.889 0.8780 0.8890 3449 NaN \n", "2000-09-05 00:00:00-04:00 0.896 0.907 0.8950 0.9060 1397 NaN \n", "2000-09-06 00:00:00-04:00 0.905 0.906 0.8975 0.9015 1195 NaN \n", "\n", " Stock Splits \n", "Date \n", "2000-08-30 00:00:00-04:00 NaN \n", "2000-08-31 00:00:00-04:00 NaN \n", "2000-09-01 00:00:00-04:00 NaN \n", "2000-09-05 00:00:00-04:00 NaN \n", "2000-09-06 00:00:00-04:00 NaN " ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hist.head()" ] }, { "cell_type": "code", "execution_count": 11, "id": "6da84391ad0032f6", "metadata": { "ExecuteTime": { "end_time": "2024-03-18T09:35:22.674361Z", "start_time": "2024-03-18T09:35:22.648963Z" }, "collapsed": false }, "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", "
OpenHighLowCloseVolumeDividendsStock Splits
2000-08-300.8790.8870.87700.88502886NaNNaN
2000-08-310.8850.8880.88000.88501095NaNNaN
2000-09-010.8780.8890.87800.88903449NaNNaN
2000-09-050.8960.9070.89500.90601397NaNNaN
2000-09-060.9050.9060.89750.90151195NaNNaN
\n", "
" ], "text/plain": [ " Open High Low Close Volume Dividends Stock Splits\n", "2000-08-30 0.879 0.887 0.8770 0.8850 2886 NaN NaN\n", "2000-08-31 0.885 0.888 0.8800 0.8850 1095 NaN NaN\n", "2000-09-01 0.878 0.889 0.8780 0.8890 3449 NaN NaN\n", "2000-09-05 0.896 0.907 0.8950 0.9060 1397 NaN NaN\n", "2000-09-06 0.905 0.906 0.8975 0.9015 1195 NaN NaN" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Convert the index to datetime if it's not already\n", "hist.index = pd.to_datetime(hist.index)\n", "\n", "# Strip the time and timezone, keeping only the date for the index\n", "hist.index = hist.index.date\n", "\n", "hist.head()" ] }, { "cell_type": "code", "execution_count": 15, "id": "46d35140195ce652", "metadata": { "ExecuteTime": { "end_time": "2024-03-18T09:41:02.403056Z", "start_time": "2024-03-18T09:41:02.371704Z" }, "collapsed": false }, "outputs": [], "source": [ "from datetime import datetime\n", "\n", "# Get the current date in 'YYYY-MM-DD' format\n", "current_date = datetime.now().strftime('%Y-%m-%d')\n", "\n", "# Create the file name with the current date\n", "file_name = f'./Data_COPPER_{current_date}.csv'\n", "\n", "hist = hist.round(3)\n", "\n", "# Save the DataFrame to CSV with the dynamic file name\n", "hist[[\"High\", \"Low\", \"Close\"]].to_csv(file_name)" ] }, { "cell_type": "code", "id": "3f6096fe43e72687", "metadata": { "collapsed": false }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.6" } }, "nbformat": 4, "nbformat_minor": 5 }