{ "cells": [ { "cell_type": "code", "execution_count": 13, "id": "initial_id", "metadata": { "collapsed": true, "ExecuteTime": { "end_time": "2024-03-08T19:24:48.711659Z", "start_time": "2024-03-08T19:24:48.709724Z" } }, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np" ] }, { "cell_type": "code", "outputs": [ { "data": { "text/plain": " Unnamed: 0 Name Ticker\n0 0 20 Microns Ltd. 20MICRONS\n1 1 360 One Wam Ltd. 360ONE\n2 2 3M India Ltd. 3MINDIA\n3 3 3P Land Holdings Ltd. 3PLAND\n4 4 3i Infotech Ltd. 3IINFOLTD", "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
Unnamed: 0NameTicker
0020 Microns Ltd.20MICRONS
11360 One Wam Ltd.360ONE
223M India Ltd.3MINDIA
333P Land Holdings Ltd.3PLAND
443i Infotech Ltd.3IINFOLTD
\n
" }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.read_csv(\"Data/Company List.csv\")\n", "df.head()" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2024-03-08T19:24:48.887880Z", "start_time": "2024-03-08T19:24:48.881294Z" } }, "id": "ff8275fdbab66ba3", "execution_count": 14 }, { "cell_type": "code", "outputs": [], "source": [ "l = (df[\"Ticker\"]+'.NS')[:5].to_list()\n", "l.append(\"BHUSANSTL.NS\")" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2024-03-08T19:24:49.073847Z", "start_time": "2024-03-08T19:24:49.071352Z" } }, "id": "8c0fc5687a75e887", "execution_count": 15 }, { "cell_type": "code", "outputs": [ { "data": { "text/plain": "['20MICRONS.NS',\n '360ONE.NS',\n '3MINDIA.NS',\n '3PLAND.NS',\n '3IINFOLTD.NS',\n 'BHUSANSTL.NS']" }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2024-03-08T19:24:49.617566Z", "start_time": "2024-03-08T19:24:49.614957Z" } }, "id": "f564268b1875dee3", "execution_count": 16 }, { "cell_type": "code", "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "[*********************100%%**********************] 1 of 1 completed\n", "[*********************100%%**********************] 1 of 1 completed\n", "[*********************100%%**********************] 1 of 1 completed\n", "[*********************100%%**********************] 1 of 1 completed\n", "[*********************100%%**********************] 1 of 1 completed\n", "[*********************100%%**********************] 1 of 1 completed\n", "\n", "1 Failed download:\n", "['BHUSANSTL.NS']: Exception('%ticker%: 1d data not available for startTime=-2208971040 and endTime=1709874000. Only 100 years worth of day granularity data are allowed to be fetched per request.')\n" ] } ], "source": [ "import yfinance as yf\n", "\n", "com_df = pd.DataFrame()\n", "\n", "for i in l:\n", " com_data = yf.download(i, start=\"1900-01-01\", end=\"2024-03-08\")\n", " \n", " if (com_data.empty == False):\n", " com_df[i] = pd.Series(com_data['Adj Close'], index=com_data.index)" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2024-03-08T19:24:53.664589Z", "start_time": "2024-03-08T19:24:51.930859Z" } }, "id": "6f4b522412812a96", "execution_count": 17 }, { "cell_type": "code", "outputs": [ { "data": { "text/plain": " 20MICRONS.NS 360ONE.NS 3MINDIA.NS 3PLAND.NS 3IINFOLTD.NS\nDate \n2008-10-06 14.736081 NaN 1210.735474 14.001053 521.733643\n2008-10-07 13.198785 NaN 1143.373535 14.646491 481.067078\n2008-10-08 11.639526 NaN 1053.750000 15.391228 432.082306\n2008-10-10 10.190077 NaN 1002.206726 12.312982 408.052063\n2008-10-13 10.826957 NaN 980.954834 10.922807 478.294373", "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
20MICRONS.NS360ONE.NS3MINDIA.NS3PLAND.NS3IINFOLTD.NS
Date
2008-10-0614.736081NaN1210.73547414.001053521.733643
2008-10-0713.198785NaN1143.37353514.646491481.067078
2008-10-0811.639526NaN1053.75000015.391228432.082306
2008-10-1010.190077NaN1002.20672612.312982408.052063
2008-10-1310.826957NaN980.95483410.922807478.294373
\n
" }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "com_df.head()" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2024-03-08T19:23:54.775082Z", "start_time": "2024-03-08T19:23:54.770174Z" } }, "id": "ff14463f49d5d8f4", "execution_count": 9 }, { "cell_type": "code", "outputs": [], "source": [], "metadata": { "collapsed": false }, "id": "162b263160a53be2" }, { "cell_type": "code", "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "[*********************100%%**********************] 1 of 1 completed\n", "[*********************100%%**********************] 1 of 1 completed\n", "[*********************100%%**********************] 1 of 1 completed\n", "\n", "1 Failed download:\n", "['BHUSANSTL.NS']: Exception('%ticker%: No price data found, symbol may be delisted (1d 2023-01-01 -> 2024-01-01)')\n", "[*********************100%%**********************] 1 of 1 completed" ] }, { "name": "stdout", "output_type": "stream", "text": [ " AAPL GOOG MSFT\n", "Date \n", "2023-01-03 00:00:00 124.216301 89.699997 237.036026\n", "2023-01-04 00:00:00 125.497498 88.709999 226.667297\n", "2023-01-05 00:00:00 124.166634 86.769997 219.949371\n", "2023-01-06 00:00:00 128.735229 88.160004 222.541565\n", "2023-01-09 00:00:00 129.261612 88.800003 224.708313\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "import yfinance as yf\n", "import pandas as pd\n", "\n", "def fetch_adj_close(ticker):\n", " try:\n", " data = yf.download(ticker, start=\"2023-01-01\", end=\"2024-01-01\")\n", " adj_close = data['Adj Close'] # Extracting adjusted close prices\n", " adj_close = pd.DataFrame(adj_close) # Convert Series to DataFrame\n", " adj_close.columns = [ticker] # Rename column to ticker symbol\n", " return adj_close\n", " except Exception as e:\n", " print(f\"Error fetching data for {ticker}: {e}\")\n", " return None\n", "\n", "# List of tickers you want to fetch data for\n", "tickers = [\"AAPL\", \"GOOG\", \"BHUSANSTL.NS\", \"MSFT\"]\n", "\n", "# Create an empty DataFrame to store the adjusted close prices\n", "adj_close_df = pd.DataFrame()\n", "\n", "for ticker in tickers:\n", " data = fetch_adj_close(ticker)\n", " if data is not None:\n", " adj_close_df = pd.concat([adj_close_df, data], axis=1) # Concatenate the data to the DataFrame\n", "\n", "# Set the date as the index\n", "adj_close_df.index.name = 'Date'\n", "\n", "# Drop columns with any NaN values (i.e., for invalid tickers)\n", "adj_close_df.dropna(axis=1, how='any', inplace=True)\n", "\n", "print(adj_close_df.head())\n" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2024-03-08T23:14:00.320054Z", "start_time": "2024-03-08T23:14:00.259300Z" } }, "id": "fdb2a5f6391b0ca2", "execution_count": 5 }, { "cell_type": "code", "outputs": [], "source": [], "metadata": { "collapsed": false }, "id": "b34afac23ee07c5e" } ], "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 }