mirror of
https://github.com/norandom/trading_analysis.git
synced 2024-11-21 20:53:44 +00:00
373 lines
10 KiB
Plaintext
373 lines
10 KiB
Plaintext
{
|
|
"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": [
|
|
"<div>\n",
|
|
"<style scoped>\n",
|
|
" .dataframe tbody tr th:only-of-type {\n",
|
|
" vertical-align: middle;\n",
|
|
" }\n",
|
|
"\n",
|
|
" .dataframe tbody tr th {\n",
|
|
" vertical-align: top;\n",
|
|
" }\n",
|
|
"\n",
|
|
" .dataframe thead th {\n",
|
|
" text-align: right;\n",
|
|
" }\n",
|
|
"</style>\n",
|
|
"<table border=\"1\" class=\"dataframe\">\n",
|
|
" <thead>\n",
|
|
" <tr style=\"text-align: right;\">\n",
|
|
" <th></th>\n",
|
|
" <th>Open</th>\n",
|
|
" <th>High</th>\n",
|
|
" <th>Low</th>\n",
|
|
" <th>Close</th>\n",
|
|
" <th>Volume</th>\n",
|
|
" <th>Dividends</th>\n",
|
|
" <th>Stock Splits</th>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>Date</th>\n",
|
|
" <th></th>\n",
|
|
" <th></th>\n",
|
|
" <th></th>\n",
|
|
" <th></th>\n",
|
|
" <th></th>\n",
|
|
" <th></th>\n",
|
|
" <th></th>\n",
|
|
" </tr>\n",
|
|
" </thead>\n",
|
|
" <tbody>\n",
|
|
" <tr>\n",
|
|
" <th>2000-08-30 00:00:00-04:00</th>\n",
|
|
" <td>0.879</td>\n",
|
|
" <td>0.887</td>\n",
|
|
" <td>0.8770</td>\n",
|
|
" <td>0.8850</td>\n",
|
|
" <td>2886</td>\n",
|
|
" <td>NaN</td>\n",
|
|
" <td>NaN</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>2000-08-31 00:00:00-04:00</th>\n",
|
|
" <td>0.885</td>\n",
|
|
" <td>0.888</td>\n",
|
|
" <td>0.8800</td>\n",
|
|
" <td>0.8850</td>\n",
|
|
" <td>1095</td>\n",
|
|
" <td>NaN</td>\n",
|
|
" <td>NaN</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>2000-09-01 00:00:00-04:00</th>\n",
|
|
" <td>0.878</td>\n",
|
|
" <td>0.889</td>\n",
|
|
" <td>0.8780</td>\n",
|
|
" <td>0.8890</td>\n",
|
|
" <td>3449</td>\n",
|
|
" <td>NaN</td>\n",
|
|
" <td>NaN</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>2000-09-05 00:00:00-04:00</th>\n",
|
|
" <td>0.896</td>\n",
|
|
" <td>0.907</td>\n",
|
|
" <td>0.8950</td>\n",
|
|
" <td>0.9060</td>\n",
|
|
" <td>1397</td>\n",
|
|
" <td>NaN</td>\n",
|
|
" <td>NaN</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>2000-09-06 00:00:00-04:00</th>\n",
|
|
" <td>0.905</td>\n",
|
|
" <td>0.906</td>\n",
|
|
" <td>0.8975</td>\n",
|
|
" <td>0.9015</td>\n",
|
|
" <td>1195</td>\n",
|
|
" <td>NaN</td>\n",
|
|
" <td>NaN</td>\n",
|
|
" </tr>\n",
|
|
" </tbody>\n",
|
|
"</table>\n",
|
|
"</div>"
|
|
],
|
|
"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": [
|
|
"<div>\n",
|
|
"<style scoped>\n",
|
|
" .dataframe tbody tr th:only-of-type {\n",
|
|
" vertical-align: middle;\n",
|
|
" }\n",
|
|
"\n",
|
|
" .dataframe tbody tr th {\n",
|
|
" vertical-align: top;\n",
|
|
" }\n",
|
|
"\n",
|
|
" .dataframe thead th {\n",
|
|
" text-align: right;\n",
|
|
" }\n",
|
|
"</style>\n",
|
|
"<table border=\"1\" class=\"dataframe\">\n",
|
|
" <thead>\n",
|
|
" <tr style=\"text-align: right;\">\n",
|
|
" <th></th>\n",
|
|
" <th>Open</th>\n",
|
|
" <th>High</th>\n",
|
|
" <th>Low</th>\n",
|
|
" <th>Close</th>\n",
|
|
" <th>Volume</th>\n",
|
|
" <th>Dividends</th>\n",
|
|
" <th>Stock Splits</th>\n",
|
|
" </tr>\n",
|
|
" </thead>\n",
|
|
" <tbody>\n",
|
|
" <tr>\n",
|
|
" <th>2000-08-30</th>\n",
|
|
" <td>0.879</td>\n",
|
|
" <td>0.887</td>\n",
|
|
" <td>0.8770</td>\n",
|
|
" <td>0.8850</td>\n",
|
|
" <td>2886</td>\n",
|
|
" <td>NaN</td>\n",
|
|
" <td>NaN</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>2000-08-31</th>\n",
|
|
" <td>0.885</td>\n",
|
|
" <td>0.888</td>\n",
|
|
" <td>0.8800</td>\n",
|
|
" <td>0.8850</td>\n",
|
|
" <td>1095</td>\n",
|
|
" <td>NaN</td>\n",
|
|
" <td>NaN</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>2000-09-01</th>\n",
|
|
" <td>0.878</td>\n",
|
|
" <td>0.889</td>\n",
|
|
" <td>0.8780</td>\n",
|
|
" <td>0.8890</td>\n",
|
|
" <td>3449</td>\n",
|
|
" <td>NaN</td>\n",
|
|
" <td>NaN</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>2000-09-05</th>\n",
|
|
" <td>0.896</td>\n",
|
|
" <td>0.907</td>\n",
|
|
" <td>0.8950</td>\n",
|
|
" <td>0.9060</td>\n",
|
|
" <td>1397</td>\n",
|
|
" <td>NaN</td>\n",
|
|
" <td>NaN</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>2000-09-06</th>\n",
|
|
" <td>0.905</td>\n",
|
|
" <td>0.906</td>\n",
|
|
" <td>0.8975</td>\n",
|
|
" <td>0.9015</td>\n",
|
|
" <td>1195</td>\n",
|
|
" <td>NaN</td>\n",
|
|
" <td>NaN</td>\n",
|
|
" </tr>\n",
|
|
" </tbody>\n",
|
|
"</table>\n",
|
|
"</div>"
|
|
],
|
|
"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": 16,
|
|
"id": "46d35140195ce652",
|
|
"metadata": {
|
|
"ExecuteTime": {
|
|
"end_time": "2024-03-18T11:27:06.711138Z",
|
|
"start_time": "2024-03-18T11:27:06.675173Z"
|
|
},
|
|
"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[[\"Open\", \"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
|
|
}
|