Workspace
Andy Ly/

Tech Stock Prices

0
Beta
Spinner

Tech Stock Prices

This dataset consists of the daily stock prices and volume of ten different tech companies: Apple (AAPL), Amazon (AMZN), Alibaba (BABA), Salesforce (CRM), Facebook (FB), Alphabet (GOOG), Intel (INTC), Microsoft (MSFT), Nvidia (NVDA), and Tesla (TSLA).

There are ten CSV files in the data/ folder named with the stock symbol for each of the ten companies listed above. Other companies can be downloaded from Yahoo Finance and uploaded into the workspace.

Using techniques learnt from the 'Data Manipulation with Pandas' course, the first thing I did was to transfer the csv file to the pandas dataframe using AAPL=pd.read_csv("data/AAPL.csv") as an example for Apple, followed by inspecting the dataframe using:

.info() shows information on each of the columns, such as the data type and number of missing values.

.describe() calculates a few summary statistics for each column (Apple is below)

Open High Low Close Adj Close \

count 2999.000000 2999.000000 2999.000000 2999.000000 2999.000000
mean 41.741022 42.170738 41.314441 41.760973 40.017151
std 36.499461 36.919202 36.090713 36.528357 37.023456
min 6.870357 7.000000 6.794643 6.858929 5.880607
25% 18.432500 18.613214 18.238571 18.472321 16.105654
50% 28.132500 28.437500 27.885000 28.162500 25.993275
75% 47.771250 47.997500 47.377500 47.737499 46.150312
max 161.679993 165.699997 161.000000 165.300003 165.300003

Volume

count 2.999000e+03
mean 2.714256e+08
std 2.263473e+08
min 4.100000e+07
25% 1.098044e+08
50% 1.858592e+08
75% 3.724658e+08
max 1.880998e+09

.head() returns the first few rows (the “head” of the DataFrame).

.tail() returns the last few rows (the “tail” of the DataFrame).

I then subsetted the Adj Close column for further analysis later on, and also extracted the mean, max and min Closing Prices using Apple as an example below:

print(AAPL["Adj Close"].mean()) print(AAPL["Adj Close"].max()) print(AAPL["Adj Close"].min())

40.01715128142714 165.300003 5.880607

I then created a simple line chart using:

import matplotlib.pyplot as plt

AAPL.plot(x="Date", y="Adj Close", kind ="line", title="Apple Stock Performance")

Plotting returns from closing prices:

Create daily_return

AAPL['daily_return'] = AAPL.Close.pct_change().mul(100)

Create monthly_return

AAPL['monthly_return'] = AAPL.Close.pct_change(30).mul(100)

Create annual_return

AAPL['annual_return'] = AAPL.Close.pct_change(360).mul(100)

Plot the result

AAPL.plot(subplots=True) plt.show()

#Which of the ten companies has the highest closing price based on the most recent data?

import pandas as pd print(AAPL["Adj Close"].tail(1)) print(AMZN["Adj Close"].tail(1)) AMZN @ print(BABA["Adj Close"].tail(1)) print(CRM["Adj Close"].tail(1)) print(FB["Adj Close"].tail(1)) print(GOOG["Adj Close"].tail(1)) print(INTC["Adj Close"].tail(1)) print(MSFT["Adj Close"].tail(1)) print(NVDA["Adj Close"].tail(1)) print(TSLA["Adj Close"].tail(1))

2998 165.300003 Name: Adj Close, dtype: float64 2998 3507.070068 Name: Adj Close, dtype: float64 1811 127.529999 Name: Adj Close, dtype: float64 2998 284.959991 Name: Adj Close, dtype: float64 2398 324.459991 Name: Adj Close, dtype: float64 2998 2849.040039 Name: Adj Close, dtype: float64 2997 50.0 Name: Adj Close, dtype: float64 2998 330.589996 Name: Adj Close, dtype: float64 2998 326.76001 Name: Adj Close, dtype: float64 2875 1144.76001 Name: Adj Close, dtype: float64

#Which of the ten companies have experienced the greatest percent increase in closing price over the course of their existence? TSLA = 23919.30%

TSLA_total_return = (TSLA_Close[2875] - TSLA_Close[0]) / TSLA_Close[0]*100 print(TSLA_total_return)

#Risk and return characteristics of the ten stocks import pandas as pd AAPL_daily_returns = AAPL["Adj Close"].pct_change() AAPL_annual_returns=AAPL_daily_returns.mean() * 250100 AAPL_annual_risk=AAPL_daily_returns.std() * 250100**0.5 print(AAPL_annual_returns, AAPL_annual_risk)

AMZN_daily_returns = AMZN["Adj Close"].pct_change() AMZN_annual_returns=AMZN_daily_returns.mean() * 250100 AMZN_annual_risk=AMZN_daily_returns.std() * 250100**0.5 print(AMZN_annual_returns, AMZN_annual_risk)

BABA_daily_returns = BABA["Adj Close"].pct_change() BABA_annual_returns=AMZN_daily_returns.mean() * 250100 BABA_annual_risk=BABA_daily_returns.std() * 250100**0.5 print(BABA_annual_returns, BABA_annual_risk)

CRM_daily_returns = CRM["Adj Close"].pct_change() CRM_annual_returns=CRM_daily_returns.mean() * 250100 CRM_annual_risk=CRM_daily_returns.std() * 250100**0.5 print(CRM_annual_returns, CRM_annual_risk)

FB_daily_returns = FB["Adj Close"].pct_change() FB_annual_returns=FB_daily_returns.mean() * 250100 FB_annual_risk=FB_daily_returns.std() * 250100**0.5 print(FB_annual_returns, FB_annual_risk)

GOOG_daily_returns = GOOG["Adj Close"].pct_change() GOOG_annual_returns=GOOG_daily_returns.mean() * 250100 GOOG_annual_risk=GOOG_daily_returns.std() * 250100**0.5 print(GOOG_annual_returns, GOOG_annual_risk)

INTC_daily_returns = INTC["Adj Close"].pct_change() INTC_annual_returns=INTC_daily_returns.mean() * 250100 INTC_annual_risk=INTC_daily_returns.std() * 250100**0.5 print(INTC_annual_returns, INTC_annual_risk)

MSFT_daily_returns = MSFT["Adj Close"].pct_change() MSFT_annual_returns=MSFT_daily_returns.mean() * 250100 MSFT_annual_risk=MSFT_daily_returns.std() * 250100**0.5 print(MSFT_annual_returns, MSFT_annual_risk)

NVDA_daily_returns = NVDA["Adj Close"].pct_change() NVDA_annual_returns=NVDA_daily_returns.mean() * 250100 NVDA_annual_risk=NVDA_daily_returns.std() * 250100**0.5 print(NVDA_annual_returns, NVDA_annual_risk)

TSLA_daily_returns = TSLA["Adj Close"].pct_change() TSLA_annual_returns=TSLA_daily_returns.mean() * 250100 TSLA_annual_risk=TSLA_daily_returns.std() * 250100**0.5 print(TSLA_annual_returns, TSLA_annual_risk)

30.835361896213993 44.16232781387055 32.06076340940009 49.17947496905724 32.06076340940009 54.42599120267086 28.933535453485803 56.021401720241364 30.071689883347407 57.57354737422246 21.74755680881299 40.75652209197331 14.386514045957583 45.18455001959572 25.02477717219213 39.441249381859485 45.11968692535343 66.88644574169882 63.24395141182999 88.4540428580504

#Nov 30th return 2021 returns_sort=AAPL.sort_index() print(returns_sort.loc[2021-11-30]) use iloc to index a column number

import pandas as pd
import pandas as np
import pandas_datareader as web

AAPL=pd.read_csv("data/AAPL.csv")
print(AAPL.info())
print(AAPL.describe())
print(AAPL.head(5))
print(AAPL.tail(5))
AAPL_Close=AAPL["Adj Close"]
print(AAPL_Close.head())
print(AAPL_Close.tail())
print(AAPL["Adj Close"].mean())
print(AAPL["Adj Close"].max())
print(AAPL["Adj Close"].min())

AAPL_start=(AAPL["Adj Close"].head(1))
AAPL_end=(AAPL["Adj Close"].tail(1))
AAPL_Return_Since_Inception=(AAPL_end - AAPL_start)/(AAPL_start)*100
print(AAPL_Return_Since_Inception)

AAPL_daily_returns = AAPL["Adj Close"].pct_change()
print(AAPL_daily_returns)
AAPL_annual_returns=AAPL_daily_returns.mean() * 250*100
print(AAPL_annual_returns)
AAPL_annual_risk=AAPL_daily_returns.std() * 250*100**0.5
print(AAPL_annual_risk)
# monthly returns #returns since inception
#
#AAPL_monthly_return = AAPL["Adj Close"].pct_change(30).mul(100)
#print(AAPL_monthly_return)
AAPL_total_return = (AAPL_Close[2998] - AAPL_Close[0]) / AAPL_Close[0]*100
print(AAPL_total_return)

#Nov 30th return 2021
returns_sort=AAPL.sort_index()
print(returns_sort.loc[2017-11-13])

import pandas as pd

import matplotlib.pyplot as plt

AAPL.plot(x="Date", y="Adj Close", kind ="line", title="Apple Stock Performance")
import pandas as pd

AMZN=pd.read_csv("data/AMZN.csv")
print(AMZN.info)
print(AMZN.head(1))
print(AMZN.tail(1))
AMZN_Close=AMZN["Adj Close"]
print(AMZN_Close.head())
print(AMZN_Close.tail())
print(AMZN["Adj Close"].mean())
print(AMZN["Adj Close"].max())
print(AMZN["Adj Close"].min())
import pandas as pd

import matplotlib.pyplot as plt

AMZN.plot(x="Date", y="Adj Close", kind ="line", title="AMZN Stock Performance")
import pandas as pd

CRM=pd.read_csv("data/CRM.csv")
print(CRM.info)
print(CRM.head(5))
print(CRM.tail(5))
CRM_Close=CRM["Adj Close"]
print(CRM_Close.head())
print(CRM_Close.tail())
print(CRM["Adj Close"].mean())
print(CRM["Adj Close"].max())
print(CRM["Adj Close"].min())
import pandas as pd

import matplotlib.pyplot as plt

CRM.plot(x="Date", y="Adj Close", kind ="line", title="CRM Stock Performance")

import pandas as pd

FB=pd.read_csv("data/FB.csv")
print(FB.info)
print(FB.head(5))
print(FB.tail(5))
FB_Close=FB["Adj Close"]
print(FB_Close.head())
print(FB_Close.tail())
print(FB["Adj Close"].mean())
print(FB["Adj Close"].max())
print(FB["Adj Close"].min())
import pandas as pd

import matplotlib.pyplot as plt

FB.plot(x="Date", y="Adj Close", kind ="line", title="FB Stock Performance")
import pandas as pd

CRM=pd.read_csv("data/CRM.csv")
print(CRM.info)
print(CRM.head(5))
print(CRM.tail(5))
CRM_Close=CRM["Adj Close"]
print(CRM_Close.head())
print(CRM_Close.tail())
print(CRM["Adj Close"].mean())
print(CRM["Adj Close"].max())
print(CRM["Adj Close"].min())
import pandas as pd

GOOG=pd.read_csv("data/GOOG.csv")
print(GOOG.info)
print(GOOG.head(5))
print(GOOG.tail(5))
GOOG_Close=GOOG["Adj Close"]
print(GOOG_Close.head())
print(GOOG_Close.tail())
print(GOOG["Adj Close"].mean())
print(GOOG["Adj Close"].max())
print(GOOG["Adj Close"].min())
import pandas as pd

import matplotlib.pyplot as plt

GOOG.plot(x="Date", y="Adj Close", kind ="line", title="GOOG Stock Performance")
import pandas as pd

MSFT=pd.read_csv("data/MSFT.csv")
print(MSFT.info)
print(MSFT.head(5))
print(MSFT.tail(5))
MSFT_Close=MSFT["Adj Close"]
print(MSFT_Close.head())
print(MSFT_Close.tail())
print(MSFT["Adj Close"].mean())
print(MSFT["Adj Close"].max())
print(MSFT["Adj Close"].min())
import pandas as pd

import matplotlib.pyplot as plt

MSFT.plot(x="Date", y="Adj Close", kind ="line", title="MSFT Stock Performance")
import pandas as pd

NVDA=pd.read_csv("data/NVDA.csv")
print(NVDA.info)
print(NVDA.head(5))
print(NVDA.tail(5))
NVDA_Close=NVDA["Adj Close"]
print(NVDA_Close.head())
print(NVDA_Close.tail())
print(NVDA["Adj Close"].mean())
print(NVDA["Adj Close"].max())
print(NVDA["Adj Close"].min())



  • AI Chat
  • Code