You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"Requirement already satisfied: yfinance in /usr/local/lib/python3.10/dist-packages (0.2.27)\n",
77
+
"Requirement already satisfied: pandas>=1.3.0 in /usr/local/lib/python3.10/dist-packages (from yfinance) (1.5.3)\n",
78
+
"Requirement already satisfied: numpy>=1.16.5 in /usr/local/lib/python3.10/dist-packages (from yfinance) (1.23.5)\n",
79
+
"Requirement already satisfied: requests>=2.31 in /usr/local/lib/python3.10/dist-packages (from yfinance) (2.31.0)\n",
80
+
"Requirement already satisfied: multitasking>=0.0.7 in /usr/local/lib/python3.10/dist-packages (from yfinance) (0.0.11)\n",
81
+
"Requirement already satisfied: lxml>=4.9.1 in /usr/local/lib/python3.10/dist-packages (from yfinance) (4.9.3)\n",
82
+
"Requirement already satisfied: appdirs>=1.4.4 in /usr/local/lib/python3.10/dist-packages (from yfinance) (1.4.4)\n",
83
+
"Requirement already satisfied: pytz>=2022.5 in /usr/local/lib/python3.10/dist-packages (from yfinance) (2022.7.1)\n",
84
+
"Requirement already satisfied: frozendict>=2.3.4 in /usr/local/lib/python3.10/dist-packages (from yfinance) (2.3.8)\n",
85
+
"Requirement already satisfied: beautifulsoup4>=4.11.1 in /usr/local/lib/python3.10/dist-packages (from yfinance) (4.11.2)\n",
86
+
"Requirement already satisfied: html5lib>=1.1 in /usr/local/lib/python3.10/dist-packages (from yfinance) (1.1)\n",
87
+
"Requirement already satisfied: soupsieve>1.2 in /usr/local/lib/python3.10/dist-packages (from beautifulsoup4>=4.11.1->yfinance) (2.4.1)\n",
88
+
"Requirement already satisfied: six>=1.9 in /usr/local/lib/python3.10/dist-packages (from html5lib>=1.1->yfinance) (1.16.0)\n",
89
+
"Requirement already satisfied: webencodings in /usr/local/lib/python3.10/dist-packages (from html5lib>=1.1->yfinance) (0.5.1)\n",
90
+
"Requirement already satisfied: python-dateutil>=2.8.1 in /usr/local/lib/python3.10/dist-packages (from pandas>=1.3.0->yfinance) (2.8.2)\n",
91
+
"Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests>=2.31->yfinance) (3.2.0)\n",
92
+
"Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests>=2.31->yfinance) (3.4)\n",
93
+
"Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests>=2.31->yfinance) (1.26.16)\n",
94
+
"Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests>=2.31->yfinance) (2023.7.22)\n"
95
+
]
96
+
}
97
+
],
98
+
"source": [
99
+
"!pip install yfinance"
100
+
]
101
+
},
102
+
{
103
+
"cell_type": "markdown",
104
+
"source": [
105
+
"First, we need to install the ifinance API, which is not available on the current environment."
106
+
],
107
+
"metadata": {
108
+
"id": "juvNLW-bVDey"
109
+
}
110
+
},
111
+
{
112
+
"cell_type": "code",
113
+
"source": [
114
+
"import pandas as pd\n",
115
+
"import yfinance as yf\n",
116
+
"import datetime as datetime"
117
+
],
118
+
"metadata": {
119
+
"id": "Oq31gZLQVK7T"
120
+
},
121
+
"execution_count": 15,
122
+
"outputs": []
123
+
},
124
+
{
125
+
"cell_type": "markdown",
126
+
"source": [
127
+
"Here we import Pandas for data manipulation, yfinance for the API (to get real-time data), and the datetime library. Even the datetime is the built-in library in Python, it is still a good practice to import it for readability and maintenance of the code."
"We need two first key points of time: the start date and the end date, which can be referred to the open and close date of the stock market.\n",
149
+
"\n",
150
+
"There is the \"pd.dateoffset()\" function in Pandas, which is used to create a constant increment object of period of time for adding or subtracting.\n",
151
+
"\n",
152
+
"The code line `start_date = datetime.now() - pd.dateoffset(months=3)` creates a timestamp object called \"start_date\".\n",
153
+
"The second one creates the datetime object called \"end_date\"."
154
+
],
155
+
"metadata": {
156
+
"id": "O5KdPVYBWTvP"
157
+
}
158
+
},
159
+
{
160
+
"cell_type": "code",
161
+
"source": [
162
+
"tickers = ['AAPL', 'MSFT', 'NFLX', 'GOOG']"
163
+
],
164
+
"metadata": {
165
+
"id": "ph1lyoTqY_D2"
166
+
},
167
+
"execution_count": 20,
168
+
"outputs": []
169
+
},
170
+
{
171
+
"cell_type": "markdown",
172
+
"source": [
173
+
"Create the list of stocks (abbreviation, 4 letters)."
174
+
],
175
+
"metadata": {
176
+
"id": "CzdpEipmZDDz"
177
+
}
178
+
},
179
+
{
180
+
"cell_type": "code",
181
+
"source": [
182
+
"df_list = []"
183
+
],
184
+
"metadata": {
185
+
"id": "6bRcvMRzZXtW"
186
+
},
187
+
"execution_count": 22,
188
+
"outputs": []
189
+
},
190
+
{
191
+
"cell_type": "markdown",
192
+
"source": [
193
+
"Create an empty list to get the data of 4 stocks above."
194
+
],
195
+
"metadata": {
196
+
"id": "p3RTA08RZegw"
197
+
}
198
+
},
199
+
{
200
+
"cell_type": "code",
201
+
"source": [
202
+
"for ticker in tickers:\n",
203
+
" data = yf.download(ticker, start=start_date, end=end_date)\n",
0 commit comments