Skip to content

Instantly share code, notes, and snippets.

Created June 3, 2014 21:23
Show Gist options
  • Save anonymous/f825d3625d8bd37925cd to your computer and use it in GitHub Desktop.
Save anonymous/f825d3625d8bd37925cd to your computer and use it in GitHub Desktop.
Ultima versión arreglada del tema de la P/U
# -*- coding: utf-8 -*-
# IMPORTAR MODULOS
import matplotlib as mpl
import matplotlib.dates as mdates
import pandas as pd
from pandas import DataFrame, Series
import datetime
import numpy as np
import matplotlib.pyplot as plt
#PARAMETROS DE FORMATO DE LOS GRAFICOS
mpl.rcParams['font.family']='Arial'
mpl.rcParams['font.size']=10
# FUNCIONES
def PercentageYear(date):
'''Calcula el % del año que ha pasado
Toma como argumento una fecha y devuelve un %'''
DaysToGo = float(date.dayofyear)
YearBegin = datetime.datetime(date.year -1,12,31)
YearEnd = datetime.datetime(date.year,12,31)
DaysInYear = float((YearEnd -YearBegin).days)
Perc = DaysToGo / DaysInYear
return Perc
def ChartLines(DF):
AuxDF = DataFrame(index = DF.index, columns = ['avg'])
AuxDF['avg']=DF.mean(axis=1)
AuxDF['SD1']=AuxDF['avg'] + DF.std (axis=1)
AuxDF['SDm1']=AuxDF['avg'] - DF.std (axis=1)
AuxDF['SD2']=AuxDF['avg'] + 2* DF.std (axis=1)
AuxDF['SDm2']=AuxDF['avg'] - 2* DF.std (axis=1)
AuxDF['PU']=DF
return AuxDF
def GraficaPU(DF):
fig = plt.figure()
ax = plt.subplot(111)
ColoresGrafico = {
'avg':'black',
'SD1':'gray',
'SDm1':'gray',
'SD2':'gray',
'SDm2':'gray',
'PU':'red'}
SizeLinea= {
'avg':1,
'SD1':1,
'SDm1':1,
'SD2':1,
'SDm2':1,
'PU':1}
TipoLinea ={
'avg':'-',
'SD1':'-.',
'SDm1':'-.',
'SD2':'-.',
'SDm2':'-.',
'PU':'-'}
for x in DF.columns:
ax.plot(DF.index, DF[x], TipoLinea[x], color = ColoresGrafico[x], linewidth=SizeLinea[x])
ax2 = ax.twinx()
ax2.plot(DF.index,DF.PU, color='red', linewidth=2)
myFmt = mdates.DateFormatter('%Y')
ax.xaxis.set_major_formatter(myFmt)
plt.savefig('pugraf2', dpi=100)
plt.show()
Utilidad = pd.read_excel('utilidad.xls','utilidad')
Utilidad = Utilidad.T
MarketCap = pd.read_excel('marketcap.xls','marketcap')
MarketCap.index = pd.to_datetime(MarketCap.index)
Porcentaje = Series(MarketCap.index.map(lambda x: PercentageYear(x)),index = MarketCap.index)
CurrentYearUtilidad = Utilidad.ix[MarketCap.index.map(lambda x:x.year)]
CurrentYearUtilidad.index = Porcentaje.index
ForwardYearUtilidad = Utilidad.ix[MarketCap.index.map(lambda x:x.year +1)]
ForwardYearUtilidad.index = Porcentaje.index
ForwardYearUtilidad.to_html('test.html')
TMFUtilidad = ForwardYearUtilidad.mul(Porcentaje, axis=0).add(CurrentYearUtilidad.mul((1-Porcentaje), axis=0), axis=0)
PUForward = MarketCap / TMFUtilidad
Mercado = (MarketCap.sum(axis=1) / TMFUtilidad.sum(axis=1))
Promedio = ChartLines(Mercado)
descuento = Promedio.ix[-1,'PU'] / Promedio.ix[-1,'avg'] -1
formdesc = '{percent:.2%}'.format(percent = descuento)
print "El mercado está transando a",Promedio.ix[-1,'PU'], "a un ", formdesc, " descuento"
GraficaPU(Promedio)
plt.savefig('pugraf2', dpi=100)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment