Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b89d0fa

Browse files
committedAug 10, 2023
Created using Colaboratory
1 parent 4d4613e commit b89d0fa

File tree

1 file changed

+237
-0
lines changed

1 file changed

+237
-0
lines changed
 

‎2_Stock_Market_Analysis.ipynb

Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
{
2+
"nbformat": 4,
3+
"nbformat_minor": 0,
4+
"metadata": {
5+
"colab": {
6+
"provenance": [],
7+
"authorship_tag": "ABX9TyOeVStG6ntWcwzmsPaoMdtB",
8+
"include_colab_link": true
9+
},
10+
"kernelspec": {
11+
"name": "python3",
12+
"display_name": "Python 3"
13+
},
14+
"language_info": {
15+
"name": "python"
16+
}
17+
},
18+
"cells": [
19+
{
20+
"cell_type": "markdown",
21+
"metadata": {
22+
"id": "view-in-github",
23+
"colab_type": "text"
24+
},
25+
"source": [
26+
"<a href=\"https://colab.research.google.com/github/derkaiser9423/PythonPracticeProject/blob/master/2_Stock_Market_Analysis.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
27+
]
28+
},
29+
{
30+
"cell_type": "markdown",
31+
"source": [
32+
"It focuses on using API to get data, handling data, and the most important visualizing for further analysis."
33+
],
34+
"metadata": {
35+
"id": "AIyxedTiUjQa"
36+
}
37+
},
38+
{
39+
"cell_type": "code",
40+
"source": [
41+
"from IPython import get_ipython\n",
42+
"get_ipython().magic('reset -sf')"
43+
],
44+
"metadata": {
45+
"id": "4RwilnSDXnZT"
46+
},
47+
"execution_count": 14,
48+
"outputs": []
49+
},
50+
{
51+
"cell_type": "markdown",
52+
"source": [
53+
"*This* code will remove all variables. It a good start to monitor variable's changes.\n",
54+
"First, it imports the IPython kernel object. Then, it calls the \"get_ipython()\" function, which returns the IPython kernel object.\n",
55+
"The magic() method allows us to execute the IPython magic commands. The magic's command \"reset -sf\" deletes all variables for a whole new session."
56+
],
57+
"metadata": {
58+
"id": "LEj2XcEBXnGD"
59+
}
60+
},
61+
{
62+
"cell_type": "code",
63+
"execution_count": 6,
64+
"metadata": {
65+
"colab": {
66+
"base_uri": "https://localhost:8080/"
67+
},
68+
"id": "4ychhVqwURS5",
69+
"outputId": "45f60f37-2f11-4295-b8aa-5aa1072010b0"
70+
},
71+
"outputs": [
72+
{
73+
"output_type": "stream",
74+
"name": "stdout",
75+
"text": [
76+
"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."
128+
],
129+
"metadata": {
130+
"id": "X_u-4IAEVuGP"
131+
}
132+
},
133+
{
134+
"cell_type": "code",
135+
"source": [
136+
"start_date = datetime.datetime.now() - pd.DateOffset(months=3)\n",
137+
"end_date = datetime.datetime.now()"
138+
],
139+
"metadata": {
140+
"id": "WVhfTCeqWNOy"
141+
},
142+
"execution_count": 16,
143+
"outputs": []
144+
},
145+
{
146+
"cell_type": "markdown",
147+
"source": [
148+
"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",
204+
" df_list.append(data)"
205+
],
206+
"metadata": {
207+
"colab": {
208+
"base_uri": "https://localhost:8080/"
209+
},
210+
"id": "Iz3YNn1MZmTv",
211+
"outputId": "4384841f-720f-4167-a3f3-a19d0c26e0b3"
212+
},
213+
"execution_count": 23,
214+
"outputs": [
215+
{
216+
"output_type": "stream",
217+
"name": "stdout",
218+
"text": [
219+
"[*********************100%***********************] 1 of 1 completed\n",
220+
"[*********************100%***********************] 1 of 1 completed\n",
221+
"[*********************100%***********************] 1 of 1 completed\n",
222+
"[*********************100%***********************] 1 of 1 completed\n"
223+
]
224+
}
225+
]
226+
},
227+
{
228+
"cell_type": "markdown",
229+
"source": [
230+
"Why list for dateframe?"
231+
],
232+
"metadata": {
233+
"id": "ex2LqbbEaBnd"
234+
}
235+
}
236+
]
237+
}

0 commit comments

Comments
 (0)
Please sign in to comment.