Book Image

NumPy: Beginner's Guide

By : Ivan Idris
Book Image

NumPy: Beginner's Guide

By: Ivan Idris

Overview of this book

Table of Contents (21 chapters)
NumPy Beginner's Guide Third Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
NumPy Functions' References
Index

Time for action – comparing stock log returns


We will download the stock quotes for the last year of two trackers using matplotlib. As mentioned in the previous Chapter 9, Plotting with matplotlib, we can retrieve quotes from Yahoo Finance. We will compare the log returns of the close price of DIA and SPY (DIA tracks the Dow Jones index; SPY tracks the S & P 500 index). We will also perform the Jarque–Bera test on the difference of the log returns.

  1. Write a function that can return the close price for a specified stock:

    def get_close(symbol):
       today = date.today()
       start = (today.year - 1, today.month, today.day)
    
       quotes = quotes_historical_yahoo(symbol, start, today)
       quotes = np.array(quotes)
    
       return quotes.T[4]
  2. Calculate the log returns for DIA and SPY. Compute the log returns by taking the natural logarithm of the close price and then taking the difference of consecutive values:

    spy =  np.diff(np.log(get_close("SPY")))
    dia =  np.diff(np.log(get_close("DIA")))
  3. The means comparison...