{
"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 Unnamed: 0 | \n Name | \n Ticker | \n
\n \n \n \n 0 | \n 0 | \n 20 Microns Ltd. | \n 20MICRONS | \n
\n \n 1 | \n 1 | \n 360 One Wam Ltd. | \n 360ONE | \n
\n \n 2 | \n 2 | \n 3M India Ltd. | \n 3MINDIA | \n
\n \n 3 | \n 3 | \n 3P Land Holdings Ltd. | \n 3PLAND | \n
\n \n 4 | \n 4 | \n 3i Infotech Ltd. | \n 3IINFOLTD | \n
\n \n
\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 20MICRONS.NS | \n 360ONE.NS | \n 3MINDIA.NS | \n 3PLAND.NS | \n 3IINFOLTD.NS | \n
\n \n Date | \n | \n | \n | \n | \n | \n
\n \n \n \n 2008-10-06 | \n 14.736081 | \n NaN | \n 1210.735474 | \n 14.001053 | \n 521.733643 | \n
\n \n 2008-10-07 | \n 13.198785 | \n NaN | \n 1143.373535 | \n 14.646491 | \n 481.067078 | \n
\n \n 2008-10-08 | \n 11.639526 | \n NaN | \n 1053.750000 | \n 15.391228 | \n 432.082306 | \n
\n \n 2008-10-10 | \n 10.190077 | \n NaN | \n 1002.206726 | \n 12.312982 | \n 408.052063 | \n
\n \n 2008-10-13 | \n 10.826957 | \n NaN | \n 980.954834 | \n 10.922807 | \n 478.294373 | \n
\n \n
\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
}