this is the nav!
Workspace
Andy Ly/

# Tech Stock Prices

0
Beta

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

.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:

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?

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

print(AAPL.info())
print(AAPL.describe())
print(AAPL.tail(5))
print(AAPL_Close.tail())

AAPL_Return_Since_Inception=(AAPL_end - AAPL_start)/(AAPL_start)*100
print(AAPL_Return_Since_Inception)

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
#
#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

print(AMZN.info)
print(AMZN.tail(1))
print(AMZN_Close.tail())
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

print(CRM.info)
print(CRM.tail(5))
print(CRM_Close.tail())
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

print(FB.info)
print(FB.tail(5))
print(FB_Close.tail())
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

print(CRM.info)
print(CRM.tail(5))
print(CRM_Close.tail())
import pandas as pd

print(GOOG.info)
print(GOOG.tail(5))
print(GOOG_Close.tail())
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

print(MSFT.info)
print(MSFT.tail(5))
print(MSFT_Close.tail())
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

print(NVDA.info)