Book Image

Python for Finance

By : Yuxing Yan
Book Image

Python for Finance

By: Yuxing Yan

Overview of this book

Table of Contents (20 chapters)
Python for Finance
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Comparing return versus volatility for several stocks


The following program shows the locations of five stocks on the return versus volatility graph:

import numpy as np
import matplotlib.pyplot as plt; plt.rcdefaults()
from matplotlib.finance import quotes_historical_yahoo
stocks = ('IBM', 'GE', 'WMT', 'C', 'AAPL')
begdate=(2013,1,1)
enddate=(2013,11,30)
def ret_vol(ticker):
    x = quotes_historical_yahoo(ticker,begdate,enddate,asobject=True,adjusted=True)
    logret = log(x.aclose[1:]/x.aclose[:-1])
    return(exp(sum(logret))-1,std(logret))    
ret=[];vol=[]
for ticker in stocks:
    r,v=ret_vol(ticker)
    ret.append(r)
    vol.append(v*sqrt(252))
labels = ['{0}'.format(i) for i in stocks]
xlabel('Volatility (annualized)')
ylabel('Annual return')
title('Return vs. volatility')
plt.subplots_adjust(bottom = 0.1)
color=np.array([ 0.18,  0.96,  0.75,  0.3,  0.9])
plt.scatter(vol, ret, marker = 'o',  c=color,s = 1000,cmap=plt.get_cmap('Spectral'))
for label, x, y in zip(labels, vol, ret)...