Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Python for Finance. - Second Edition

You're reading from  Python for Finance. - Second Edition

Product type Book
Published in Jun 2017
Publisher
ISBN-13 9781787125698
Pages 586 pages
Edition 2nd Edition
Languages

Table of Contents (23) Chapters

Python for Finance Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Customer Feedback
Preface
Python Basics Introduction to Python Modules Time Value of Money Sources of Data Bond and Stock Valuation Capital Asset Pricing Model Multifactor Models and Performance Measures Time-Series Analysis Portfolio Theory Options and Futures Value at Risk Monte Carlo Simulation Credit Risk Analysis Exotic Options Volatility, Implied Volatility, ARCH, and GARCH Index

Forming an n-stock portfolio


The following program generates a return matrix with three stocks plus S&P500:

import statsimport numpy as np
import pandas as pd
tickers=['IBM','dell','wmt']
path1='http://chart.yahoo.com/table.csv?s=^GSPC'
final=pd.read_csv(path1,usecols=[0,6],index_col=0)
final.columns=['^GSPC']
path2='http://chart.yahoo.com/table.csv?s=ttt'
for ticker in tickers:
    print ticker
    x = pd.read_csv(path2.replace('ttt',ticker),usecols=[0,6],index_col=0)
    x.columns=[ticker]
    final=pd.merge(final,x,left_index=True,right_index=True)

To show the first and last few lines, we use the .head() and .tail() functions as follows:

>>>final.head()
              ^GSPC     IBM   dell    wmt
Date                                     
2013-10-18  1744.50  172.85  13.83  75.71
2013-10-17  1733.15  173.90  13.85  75.78
2013-10-16  1721.54  185.73  13.85  75.60
2013-10-15  1698.06  183.67  13.83  74.37
2013-10-14  1710.14  185.97  13.85  74.68
>>>final.tail()
       ...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}