In [1]:
from datetime import datetime
import pandas as pd
import pandas.io.data
import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
pd.set_option('max_columns', 50)
%matplotlib inline

In [2]:
run_console = False
if run_console:
    %qtconsole

In [3]:
update_from_yahoo = False
if update_from_yahoo:
    today = datetime.now()
    ipsa = pd.io.data.get_data_yahoo('^IPSA', 
                                     start=datetime(2003, 1, 10), 
                                     end=today)
    spy = pd.io.data.get_data_yahoo('SPY', 
                                     start=datetime(1993, 1, 29), 
                                     end=today)
    eem = pd.io.data.get_data_yahoo('EEM', 
                                     start=datetime(2003, 4, 15), 
                                     end=today)
    vea = pd.io.data.get_data_yahoo('VEA', 
                                     start=datetime(2007, 7, 26), 
                                     end=today)
    ipsa.to_csv('IPSA.csv')
    spy.to_csv('SPY.csv')
    eem.to_csv('EEM.csv')
    vea.to_csv('VEA.csv')

In [4]:
ipsa = pd.read_csv('IPSA.csv', index_col='Date', parse_dates=True, usecols=['Date','Adj Close'])
spy = pd.read_csv('SPY.csv', index_col='Date', parse_dates=True, usecols=['Date','Adj Close'])
eem = pd.read_csv('EEM.csv', index_col='Date', parse_dates=True, usecols=['Date','Adj Close'])
vea = pd.read_csv('VEA.csv', index_col='Date', parse_dates=True, usecols=['Date','Adj Close'])

In [5]:
ipsa.columns = ['IPSA']
spy.columns = ['SPY']
eem.columns = ['EEM']
vea.columns = ['VEA']
afp = pd.DataFrame()
afp = afp.combineAdd(ipsa)
afp = afp.combineAdd(spy)
afp = afp.combineAdd(eem)
afp = afp.combineAdd(vea)

In [6]:
afp.info()
afp.tail()


<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 5383 entries, 1993-01-29 00:00:00 to 2014-03-04 00:00:00
Data columns (total 4 columns):
EEM     2740 non-null float64
IPSA    2611 non-null float64
SPY     5312 non-null float64
VEA     1663 non-null float64
dtypes: float64(4)
Out[6]:
EEM IPSA SPY VEA
Date
2014-02-26 39.01 3675.28 184.85 41.64
2014-02-27 39.75 3689.01 185.82 41.81
2014-02-28 39.48 3718.88 186.29 41.86
2014-03-03 38.78 3661.64 184.98 41.01
2014-03-04 39.44 3703.81 187.58 41.82

5 rows × 4 columns


In [7]:
afp.plot(subplots=True, figsize=(10, 10), sharex=True)
#plt.legend(loc='best')


Out[7]:
array([<matplotlib.axes.AxesSubplot object at 0x0934BEB0>,
       <matplotlib.axes.AxesSubplot object at 0x094E5150>,
       <matplotlib.axes.AxesSubplot object at 0x094FEE10>,
       <matplotlib.axes.AxesSubplot object at 0x0968B590>], dtype=object)

In [8]:
afp['2007':].plot(subplots=True, figsize=(10, 10), sharex=True)


Out[8]:
array([<matplotlib.axes.AxesSubplot object at 0x094EFDF0>,
       <matplotlib.axes.AxesSubplot object at 0x09B6A2D0>,
       <matplotlib.axes.AxesSubplot object at 0x09BA04F0>,
       <matplotlib.axes.AxesSubplot object at 0x09BCD470>], dtype=object)
# 0,20 SPY + 0,15 IPSA + 0,17 EEM + 0,06 VEA + 0,50 dolar (interbancario del dia)

In [9]:
afp['2013'].plot(subplots=True, figsize=(10, 10), sharex=True)


Out[9]:
array([<matplotlib.axes.AxesSubplot object at 0x09DEC7F0>,
       <matplotlib.axes.AxesSubplot object at 0x0AE635F0>,
       <matplotlib.axes.AxesSubplot object at 0x0AF19810>,
       <matplotlib.axes.AxesSubplot object at 0x0AF47790>], dtype=object)

In [10]:
afp['2007'].corr()


Out[10]:
EEM IPSA SPY VEA
EEM 1.000000 0.675602 0.730738 0.946608
IPSA 0.675602 1.000000 0.859658 0.549217
SPY 0.730738 0.859658 1.000000 0.777531
VEA 0.946608 0.549217 0.777531 1.000000

4 rows × 4 columns


In [11]:
afpvar = afp.pct_change()

In [12]:
afpvar['2007'].corr()


Out[12]:
EEM IPSA SPY VEA
EEM 1.000000 0.586316 0.895215 0.920688
IPSA 0.586316 1.000000 0.552495 0.537326
SPY 0.895215 0.552495 1.000000 0.899823
VEA 0.920688 0.537326 0.899823 1.000000

4 rows × 4 columns


In [13]:
m_afp = pd.read_csv('multifondos_afp.txt', index_col=['<TICKER>', '<DTYYMMDD>'], parse_dates=True, usecols=['<TICKER>', '<DTYYMMDD>','<CLOSE>'])

In [14]:
m_afp.tail()


Out[14]:
<CLOSE>
<TICKER> <DTYYMMDD>
PROVIDA-E 2014-02-26 28973.63
2014-02-27 28968.79
2014-02-28 28950.46
2014-03-01 28950.46
2014-03-02 28950.46

5 rows × 1 columns


In [15]:
m_afp.index.names


Out[15]:
FrozenList([u'<TICKER>', u'<DTYYMMDD>'])

In [16]:
m_afp.columns


Out[16]:
Index([u'<CLOSE>'], dtype='object')

In [17]:
mu_afp = m_afp.unstack(0)

In [18]:
mu_afp.index.names


Out[18]:
FrozenList([u'<DTYYMMDD>'])

In [19]:
mu_afp.columns


Out[19]:
MultiIndex(levels=[[u'<CLOSE>'], [u'CAPITAL-A', u'CAPITAL-B', u'CAPITAL-C', u'CAPITAL-D', u'CAPITAL-E', u'CUPRUM-A', u'CUPRUM-B', u'CUPRUM-C', u'CUPRUM-D', u'CUPRUM-E', u'HABITAT-A', u'HABITAT-B', u'HABITAT-C', u'HABITAT-D', u'HABITAT-E', u'MODELO-A', u'MODELO-B', u'MODELO-C', u'MODELO-D', u'MODELO-E', u'PLANVITAL-A', u'PLANVITAL-B', u'PLANVITAL-C', u'PLANVITAL-D', u'PLANVITAL-E', u'PROVIDA-A', u'PROVIDA-B', u'PROVIDA-C', u'PROVIDA-D', u'PROVIDA-E']],
           labels=[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]],
           names=[None, u'<TICKER>'])

In [20]:
mu_afp.tail()


Out[20]:
<CLOSE>
<TICKER> CAPITAL-A CAPITAL-B CAPITAL-C CAPITAL-D CAPITAL-E CUPRUM-A CUPRUM-B CUPRUM-C CUPRUM-D CUPRUM-E HABITAT-A HABITAT-B HABITAT-C HABITAT-D HABITAT-E MODELO-A MODELO-B MODELO-C MODELO-D MODELO-E PLANVITAL-A PLANVITAL-B PLANVITAL-C PLANVITAL-D PLANVITAL-E PROVIDA-A PROVIDA-B PROVIDA-C PROVIDA-D PROVIDA-E
<DTYYMMDD>
2014-02-26 28932.97 26227.29 27263.82 23851.37 29641.39 30298.95 27287.78 31672.24 25000.42 31507.81 30575.16 27164.69 30624.99 25024.78 28173.61 30053.34 26265.37 30591.44 24206.24 31656.07 29055.85 26956.99 56563.98 23617.58 49751.97 30286.43 26130.70 27243.55 23713.70 28973.63
2014-02-27 29024.55 26300.69 27327.46 23867.61 29635.00 30394.31 27360.34 31732.79 25017.34 31489.34 30668.15 27237.26 30687.14 25040.07 28164.08 30143.30 26341.24 30669.25 24235.41 31669.25 29141.64 27027.92 56687.53 23636.13 49736.48 30385.01 26209.69 27302.39 23728.64 28968.79
2014-02-28 29247.46 26446.97 27421.33 23895.40 29620.24 30634.00 27515.24 31837.55 25046.67 31468.60 30889.40 27371.46 30781.40 25063.41 28137.78 30373.02 26482.24 30768.46 24260.30 31646.24 29375.95 27178.92 56875.61 23669.57 49698.92 30621.71 26358.57 27398.99 23761.25 28950.46
2014-03-01 29247.46 26446.97 27421.33 23895.40 29620.24 30634.00 27515.24 31837.55 25046.67 31468.60 30889.40 27371.46 30781.40 25063.41 28137.78 30373.02 26482.24 30768.46 24260.30 31646.24 29375.95 27178.92 56875.61 23669.57 49698.92 30621.71 26358.57 27398.99 23761.25 28950.46
2014-03-02 29247.46 26446.97 27421.33 23895.40 29620.24 30634.00 27515.24 31837.55 25046.67 31468.60 30889.40 27371.46 30781.40 25063.41 28137.78 30373.02 26482.24 30768.46 24260.30 31646.24 29375.95 27178.92 56875.61 23669.57 49698.92 30621.71 26358.57 27398.99 23761.25 28950.46

5 rows × 30 columns