In [63]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import yfinance as yf
from pandas.plotting import scatter_matrix
In [2]:
TSLA = (yf.download("TSLA", period ="5y"), "TSLA")
tsla = TSLA[0]
[*********************100%***********************]  1 of 1 completed
In [3]:
GM = (yf.download("GM", period = "5y"), "GM")
gm = GM[0]
[*********************100%***********************]  1 of 1 completed
In [4]:
FORD = (yf.download("F", period = "5y"), "Ford")
ford = FORD[0]
[*********************100%***********************]  1 of 1 completed
In [5]:
FORD[0].head()
Out[5]:
Open High Low Close Adj Close Volume
Date
2015-09-04 13.68 13.70 13.40 13.56 10.176533 31138400
2015-09-08 13.77 13.78 13.54 13.67 10.259084 30030100
2015-09-09 13.81 13.84 13.47 13.53 10.154016 30846300
2015-09-10 13.52 13.83 13.35 13.73 10.304112 38696300
2015-09-11 13.75 13.81 13.53 13.71 10.289104 22804500

DATA visualization

In [53]:
def plottern(df, col, type="line"):
    df[0][col].plot(figsize = (12,8), label=df[1], kind =type)
In [54]:
FIRMS = [TSLA, GM, FORD]
for a in FIRMS:
    plottern(a, "Close")
plt.legend()
plt.show()
In [8]:
for a in FIRMS:
    plottern(a, "Volume")
    
plt.legend()
plt.show()
In [9]:
ford["Volume"].argmax()
Out[9]:
1149
In [10]:
ford.iloc[1149,:]
Out[10]:
Open         5.030000e+00
High         5.190000e+00
Low          4.750000e+00
Close        4.830000e+00
Adj Close    4.830000e+00
Volume       2.318008e+08
Name: 2020-03-31 00:00:00, dtype: float64
In [11]:
# Total trade
def totaltrade(df):
    df[0]["Totaltrade"] = df[0]["Close"]*df[0]["Volume"]
for a in FIRMS:
    totaltrade(a)
In [12]:
FORD[0].head()
Out[12]:
Open High Low Close Adj Close Volume Totaltrade
Date
2015-09-04 13.68 13.70 13.40 13.56 10.176533 31138400 4.222367e+08
2015-09-08 13.77 13.78 13.54 13.67 10.259084 30030100 4.105115e+08
2015-09-09 13.81 13.84 13.47 13.53 10.154016 30846300 4.173504e+08
2015-09-10 13.52 13.83 13.35 13.73 10.304112 38696300 5.313002e+08
2015-09-11 13.75 13.81 13.53 13.71 10.289104 22804500 3.126497e+08
In [13]:
for a in FIRMS:
    plottern(a, "Totaltrade")
plt.legend()
plt.show()
In [14]:
TSLA[0]["Totaltrade"].argmax()
Out[14]:
1255
In [15]:
TSLA[0].iloc[TSLA[0]["Totaltrade"].argmax(),:]
Out[15]:
Open          4.446100e+02
High          5.001400e+02
Low           4.401100e+02
Close         4.983200e+02
Adj Close     4.983200e+02
Volume        1.183744e+08
Totaltrade    5.898833e+10
Name: 2020-08-31 00:00:00, dtype: float64
In [16]:
## Moving averages
GM[0]["Close"].rolling(50).mean().plot(figsize = (12,8), label="MA50")
GM[0]["Close"].rolling(200).mean().plot(figsize = (12,8), label = "MA200")
GM[0]["Close"].plot()
plt.legend()
plt.show()

Basic Financial analysis

In [26]:
GM[0]["returns"] = GM[0]["Close"]/GM[0]["Close"].shift(1) - 1
In [24]:
FORD[0]["returns"] = FORD[0]["Close"].pct_change()
In [28]:
TSLA[0]["returns"] = TSLA[0]["Close"].pct_change()
In [27]:
GM[0].head()
Out[27]:
Open High Low Close Adj Close Volume Totaltrade returns
Date
2015-09-04 28.780001 29.040001 28.570000 28.879999 23.300987 13435300 3.880115e+08 NaN
2015-09-08 29.500000 29.940001 29.500000 29.879999 24.107805 19298900 5.766511e+08 0.034626
2015-09-09 30.200001 30.400000 29.520000 29.620001 23.898033 16053600 4.755076e+08 -0.008701
2015-09-10 29.190001 30.469999 29.190001 30.000000 24.502430 18672000 5.601600e+08 0.012829
2015-09-11 30.010000 30.200001 29.580000 30.150000 24.624937 15344700 4.626427e+08 0.005000
In [35]:
GM[0]["returns"].hist(bins = 50)
Out[35]:
<AxesSubplot:>
In [36]:
FORD[0]["returns"].hist(bins = 50)
Out[36]:
<AxesSubplot:>
In [37]:
TSLA[0]["returns"].hist(bins = 50)
Out[37]:
<AxesSubplot:>
In [52]:
def histo(df, col):
    df[0][col].hist(bins=50, figsize =(12,8), label=df[1], alpha=0.4)
for a in FIRMS:
    histo(a, "returns")
plt.legend()
plt.show()
In [55]:
#KDE plot of returns
for a in FIRMS:
    plottern(a, "returns", "kde")
plt.legend()
plt.show()
In [61]:
#boxplot of returns
box = pd.concat([TSLA[0]["returns"],GM[0]["returns"],FORD[0]["returns"]], axis=1)
box.columns = ["TSLA returns", "GM returns", "FORD returns"]
box.plot(kind="box", figsize = (12,8), colormap="jet")
Out[61]:
<AxesSubplot:>
In [65]:
#
scatter_matrix(box, figsize = (9,9), alpha = 0.2, hist_kwds={"bins":50});
In [66]:
# Relationship between ford and GM.
In [69]:
# Cumulative returns
def cumulativeR(df):
    df[0]["CumRet"] = (1 + df[0]["returns"]).cumprod()
In [70]:
for a in FIRMS:
    cumulativeR(a)
In [71]:
FORD[0].head()
Out[71]:
Open High Low Close Adj Close Volume Totaltrade returns CumRet
Date
2015-09-04 13.68 13.70 13.40 13.56 10.176533 31138400 4.222367e+08 NaN NaN
2015-09-08 13.77 13.78 13.54 13.67 10.259084 30030100 4.105115e+08 0.008112 1.008112
2015-09-09 13.81 13.84 13.47 13.53 10.154016 30846300 4.173504e+08 -0.010241 0.997788
2015-09-10 13.52 13.83 13.35 13.73 10.304112 38696300 5.313002e+08 0.014782 1.012537
2015-09-11 13.75 13.81 13.53 13.71 10.289104 22804500 3.126497e+08 -0.001457 1.011062
In [73]:
# LINE PLOT OF CUMULATIVE RETURNS
for a in FIRMS:
    plottern(a, "CumRet")
plt.legend()
plt.show()
In [ ]: