Open rogarciago opened 3 years ago
import numpy as np import pandas as pd import plotly.graph_objs as go from pandas_datareader import data, wb from datetime import date import cufflinks as cf import yfinance as yf import seaborn as sns import matplotlib.pyplot as plt
start=pd.to_datetime('2014-01-01') end=pd.to_datetime('2021-01-03')
TSLA = data.DataReader('TSLA', 'yahoo', start, end) AAL = data.DataReader('AAL', 'yahoo', start, end) ELEKTRA = data.DataReader('ELEKTRA.MX', 'yahoo', start, end) GOLD = data.DataReader('GC=F', 'yahoo', start, end) BITCOIN = data.DataReader('BTC-USD', 'yahoo', start, end)
TSLA.head()
TSLA.to_csv("TSLA.csv") AAL.to_csv("AAL.csv") ELEKTRA.to_csv("ELEKTRA.csv") GOLD.to_csv("GOLD.csv") BITCOIN.to_csv("BITCOIN.csv")
TSLA= TSLA['Adj Close'] AAL = AAL['Adj Close'] ELEKTRA = ELEKTRA['Adj Close'] GOLD = GOLD['Adj Close'] BITCOIN=BITCOIN["Adj Close"]
fig = go.Figure()
fig.add_trace(go.Scatter(x=TSLA.index, y=TSLA, name='TESLA')) fig.add_trace(go.Scatter(x=AAL.index, y=AAL, name='AMERICAN AIRLINES')) fig.add_trace(go.Scatter(x=ELEKTRA.index, y=ELEKTRA, name='ELEKTRA')) fig.add_trace(go.Scatter(x=GOLD.index, y=GOLD, name='GOLD')) fig.add_trace(go.Scatter(x=BITCOIN.index, y=BITCOIN, name='BITCOIN'))
fig.update_xaxes( title = 'Date',rangeslider_visible=True )
fig.update_yaxes( title = 'Stock Price (USD)'
) fig.show()
TSLA_norm=TSLA.div(TSLA.iloc[0]).mul(100) AAL_norm=AAL.div(AAL.iloc[0]).mul(100) ELEKTRA_norm=ELEKTRA.div(ELEKTRA.iloc[0]).mul(100) GOLD_norm=GOLD.div(GOLD.iloc[0]).mul(100) BITCOIN_norm=BITCOIN.div(BITCOIN.iloc[0]).mul(100)
fig.add_trace(go.Scatter(x=TSLA.index, y=TSLA_norm, name='TESLA')) fig.add_trace(go.Scatter(x=AAL.index, y=AAL_norm, name='American Airlines')) fig.add_trace(go.Scatter(x=ELEKTRA.index, y=ELEKTRA_norm, name='ELEKTRA')) fig.add_trace(go.Scatter(x=GOLD.index, y=GOLD_norm, name='GOLD')) fig.add_trace(go.Scatter(x=BITCOIN.index, y=BITCOIN_norm, name='BITCOIN'))
fig.update_yaxes( title = 'Share Price (USD)' )
fig.show()
start=pd.to_datetime('2016-01-01') end=pd.to_datetime('2020-01-01')
df4=[TSLA, AAL, ELEKTRA, GOLD, BITCOIN] headers=["TSLA","AAL", "ELEKTRA", "GOLD", "BITCOIN"]
df3 = pd.concat(df4, axis=1, keys=headers) df3.dropna(inplace=True)
correlation_mat = df3.corr()
sns.heatmap(correlation_mat, annot = True)
plt.title("Portafolio Correlation")
plt.show()
!/usr/bin/env python
coding: utf-8
In[1]:
import numpy as np import pandas as pd import plotly.graph_objs as go from pandas_datareader import data, wb from datetime import date import cufflinks as cf import yfinance as yf import seaborn as sns import matplotlib.pyplot as plt
In[2]:
start=pd.to_datetime('2014-01-01') end=pd.to_datetime('2021-01-03')
In[3]:
TSLA = data.DataReader('TSLA', 'yahoo', start, end) AAL = data.DataReader('AAL', 'yahoo', start, end) ELEKTRA = data.DataReader('ELEKTRA.MX', 'yahoo', start, end) GOLD = data.DataReader('GC=F', 'yahoo', start, end) BITCOIN = data.DataReader('BTC-USD', 'yahoo', start, end)
In[4]:
TSLA.head()
In[4]:
TSLA.to_csv("TSLA.csv") AAL.to_csv("AAL.csv") ELEKTRA.to_csv("ELEKTRA.csv") GOLD.to_csv("GOLD.csv") BITCOIN.to_csv("BITCOIN.csv")
In[5]:
TSLA= TSLA['Adj Close'] AAL = AAL['Adj Close'] ELEKTRA = ELEKTRA['Adj Close'] GOLD = GOLD['Adj Close'] BITCOIN=BITCOIN["Adj Close"]
GRAFICA DE LAS ACCIONES DE LAS EMPRESAS
In[6]:
fig = go.Figure()
fig.add_trace(go.Scatter(x=TSLA.index, y=TSLA, name='TESLA')) fig.add_trace(go.Scatter(x=AAL.index, y=AAL, name='AMERICAN AIRLINES')) fig.add_trace(go.Scatter(x=ELEKTRA.index, y=ELEKTRA, name='ELEKTRA')) fig.add_trace(go.Scatter(x=GOLD.index, y=GOLD, name='GOLD')) fig.add_trace(go.Scatter(x=BITCOIN.index, y=BITCOIN, name='BITCOIN'))
fig.update_xaxes( title = 'Date',rangeslider_visible=True )
fig.update_yaxes( title = 'Stock Price (USD)'
) fig.show()
In[12]:
TSLA_norm=TSLA.div(TSLA.iloc[0]).mul(100) AAL_norm=AAL.div(AAL.iloc[0]).mul(100) ELEKTRA_norm=ELEKTRA.div(ELEKTRA.iloc[0]).mul(100) GOLD_norm=GOLD.div(GOLD.iloc[0]).mul(100) BITCOIN_norm=BITCOIN.div(BITCOIN.iloc[0]).mul(100)
GRAFICA DE LOS RETORNOS ACUMULADOS DE LAS ACCIONES DE LAS EMPRESAS
In[13]:
fig = go.Figure()
fig.add_trace(go.Scatter(x=TSLA.index, y=TSLA_norm, name='TESLA')) fig.add_trace(go.Scatter(x=AAL.index, y=AAL_norm, name='American Airlines')) fig.add_trace(go.Scatter(x=ELEKTRA.index, y=ELEKTRA_norm, name='ELEKTRA')) fig.add_trace(go.Scatter(x=GOLD.index, y=GOLD_norm, name='GOLD')) fig.add_trace(go.Scatter(x=BITCOIN.index, y=BITCOIN_norm, name='BITCOIN'))
fig.update_xaxes( title = 'Date',rangeslider_visible=True )
fig.update_yaxes( title = 'Share Price (USD)' )
Show
fig.show()
In[14]:
start=pd.to_datetime('2016-01-01') end=pd.to_datetime('2020-01-01')
In[15]:
TSLA = data.DataReader('TSLA', 'yahoo', start, end) AAL = data.DataReader('AAL', 'yahoo', start, end) ELEKTRA = data.DataReader('ELEKTRA.MX', 'yahoo', start, end) GOLD = data.DataReader('GC=F', 'yahoo', start, end) BITCOIN = data.DataReader('BTC-USD', 'yahoo', start, end)
In[16]:
TSLA= TSLA['Adj Close'] AAL = AAL['Adj Close'] ELEKTRA = ELEKTRA['Adj Close'] GOLD = GOLD['Adj Close'] BITCOIN=BITCOIN["Adj Close"]
In[17]:
df4=[TSLA, AAL, ELEKTRA, GOLD, BITCOIN] headers=["TSLA","AAL", "ELEKTRA", "GOLD", "BITCOIN"]
In[18]:
df3 = pd.concat(df4, axis=1, keys=headers) df3.dropna(inplace=True)
In[19]:
correlation_mat = df3.corr()
sns.heatmap(correlation_mat, annot = True)
plt.title("Portafolio Correlation")
plt.show()