10.12.2019 Views

Python for Finance

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

ticker='IBM'

begdate=(2013,1,1)

enddate=(2013,11,9)

#

x = getData(ticker, begdate, enddate,asobject=True, adjusted=True)

logret = np.log(x.aclose[1:]/x.aclose[:-1])

yyyymm=[]

d0=x.date

#

for i in range(0,np.size(logret)):

yyyymm.append(''.join([d0[i].strftime("%Y"),d0[i].

strftime("%m")]))

y=pd.DataFrame(logret,yyyymm,columns=['retMonthly'])

retMonthly=y.groupby(y.index).sum()

print(retMonthly.head())

The output is shown here:

Chapter 8

Merging datasets by date

The following program merges the daily adjusted closing price of IBM with the

daily Fama-French 3-factor time-series. The ffMonthly.pkl is available at:

http://canisius.edu/~yany/python/ffDaily.pkl:

from matplotlib.finance import quotes_historical_yahoo_ochl as getData

import numpy as np

import pandas as pd

ticker='IBM'

begdate=(2016,1,2)

enddate=(2017,1,9)

x =getData(ticker, begdate, enddate,asobject=True, adjusted=True)

myName=ticker+'_adjClose'

x2=pd.DataFrame(x['aclose'],x.date,columns=[myName])

ff=pd.read_pickle('c:/temp/ffDaily.pkl')

final=pd.merge(x2,ff,left_index=True,right_index=True)

print(final.head())

[ 253 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!