In [2]:
# Imports
%matplotlib inline
import pandas as pd
import pandas.io.data
import numpy as np
import pytz
from datetime import datetime
import zipline as zp
import matplotlib.pyplot as plt
import matplotlib as mpl
# Emisoras para comparar
emisora = 'AZTECACPO.MX'
emisora_2 = 'TLEVISACPO.MX'
start = datetime(2010, 5, 1, 0, 0, 0, 0, pytz.utc)
data_emisora = pd.io.data.get_data_yahoo(emisora, start=start)
data_emisora_2 = pd.io.data.get_data_yahoo(emisora_2, start=start)
# Indicadores
print str(emisora), data_emisora.describe()
print str(emisora_2), data_emisora_2.describe()
# Calcula cambios entre días
data = pd.DataFrame()
data[str(emisora)] = data_emisora['Adj Close'].pct_change()
data[str(data_emisora_2)] = data_emisora_2['Adj Close'].pct_change()
data.columns = [emisora, emisora_2]
print data.head()
In [9]:
data.plot()
plt.ylabel('% Rendimiento');
plt.figure()
Out[9]:
In [10]:
dias_promedio = 30
data = pd.DataFrame()
mavg = pd.ewma(data_emisora['Adj Close'], dias_promedio)
mavg_2 = pd.ewma(data_emisora_2['Adj Close'], dias_promedio)
data[str(emisora)] = mavg
data[str(emisora_2)] = mavg_2
data.plot()
plt.figure()
Out[10]:
In [11]:
delta = data_emisora['Close'].diff()
dUp, dDown = delta.copy( ), delta.copy( )
dUp[ dUp < 0 ] = 0
dDown[ dDown > 0 ] = 0
n=14
RolUp = pd.ewma(dUp, n)
RolDown = pd.ewma(dDown, n).abs()
RS = RolUp / RolDown
RSI = 100. - 100./(1.+RS)
RSI.plot();
plt.axhline(20, color='k', alpha=0.2)
plt.annotate('sobreventa',xy=(0.5, 0.3), xycoords='figure fraction', fontsize=20, alpha=0.4, ha='center')
plt.axhline(80, color='k', alpha=0.2)
plt.annotate('sobrecompra',xy=(0.5, 0.8), xycoords='figure fraction', fontsize=20, alpha=0.4,ha='center')
plt.title('RSI %s (%i dias)' % (emisora, n));
plt.ylim([0,100]);
plt.ylabel('%');
plt.figure()
Out[11]:
In [14]:
df = pd.io.data.get_data_yahoo(['AZTECACPO.MX', 'TLEVISACPO.MX', '^MXX'],
start=datetime(2010, 1, 1))['Adj Close']
rets = df.pct_change()
fig=plt.figure(figsize=(6,6))
plt.scatter(rets.mean(), rets.std(), s=50)
plt.xlabel('Expected returns')
plt.ylabel('Risk')
for label, x, y in zip(rets.columns, rets.mean(), rets.std()):
plt.annotate(
label,
xy = (x, y), xytext = (20, -20),
textcoords = 'offset points', ha = 'right', va = 'bottom',
bbox = dict(boxstyle = 'round,pad=0.5', fc = 'w', alpha = 0.5),
arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0'))
In [ ]: