{ "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": 1, "id": "904104edfdda9e89", "metadata": { "ExecuteTime": { "end_time": "2024-03-15T12:53:50.140736Z", "start_time": "2024-03-15T12:53:49.262218Z" }, "collapsed": false }, "outputs": [], "source": [ "import yfinance as yf\n", "\n", "hg_f = yf.Ticker(\"HG=F\")" ] }, { "cell_type": "code", "execution_count": 5, "id": "f8531df814e529fb", "metadata": { "ExecuteTime": { "end_time": "2024-03-15T12:59:52.590852Z", "start_time": "2024-03-15T12:59:52.243074Z" }, "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']\n", "\n", "hist.to_csv(f'./Data_COPPER.csv')" ] }, { "cell_type": "code", "id": "de2634931db1a5d6", "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 }