{
"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",
" Open | \n",
" High | \n",
" Low | \n",
" Close | \n",
" Volume | \n",
" Dividends | \n",
" Stock Splits | \n",
"
\n",
" \n",
" Date | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" 2000-08-30 00:00:00-04:00 | \n",
" 0.879 | \n",
" 0.887 | \n",
" 0.8770 | \n",
" 0.8850 | \n",
" 2886 | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
" 2000-08-31 00:00:00-04:00 | \n",
" 0.885 | \n",
" 0.888 | \n",
" 0.8800 | \n",
" 0.8850 | \n",
" 1095 | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
" 2000-09-01 00:00:00-04:00 | \n",
" 0.878 | \n",
" 0.889 | \n",
" 0.8780 | \n",
" 0.8890 | \n",
" 3449 | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
" 2000-09-05 00:00:00-04:00 | \n",
" 0.896 | \n",
" 0.907 | \n",
" 0.8950 | \n",
" 0.9060 | \n",
" 1397 | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
" 2000-09-06 00:00:00-04:00 | \n",
" 0.905 | \n",
" 0.906 | \n",
" 0.8975 | \n",
" 0.9015 | \n",
" 1195 | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
"
\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",
" Open | \n",
" High | \n",
" Low | \n",
" Close | \n",
" Volume | \n",
" Dividends | \n",
" Stock Splits | \n",
"
\n",
" \n",
" \n",
" \n",
" 2000-08-30 | \n",
" 0.879 | \n",
" 0.887 | \n",
" 0.8770 | \n",
" 0.8850 | \n",
" 2886 | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
" 2000-08-31 | \n",
" 0.885 | \n",
" 0.888 | \n",
" 0.8800 | \n",
" 0.8850 | \n",
" 1095 | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
" 2000-09-01 | \n",
" 0.878 | \n",
" 0.889 | \n",
" 0.8780 | \n",
" 0.8890 | \n",
" 3449 | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
" 2000-09-05 | \n",
" 0.896 | \n",
" 0.907 | \n",
" 0.8950 | \n",
" 0.9060 | \n",
" 1397 | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
" 2000-09-06 | \n",
" 0.905 | \n",
" 0.906 | \n",
" 0.8975 | \n",
" 0.9015 | \n",
" 1195 | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
"
\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
}