Chapter 12. Applications to Finance

Throughout the first 11 chapters of this book, we looked at pandas and how you perform various tasks with the library. We focused mostly on how to work with pandas, often using made-up data created to demonstrate the feature but with an occasional diversion now and then into some more real-world examples.

In this final chapter, we will use pandas to perform a number of different financial analyses of stock data obtained from Yahoo! Finance. We will briefly cover a number of topics in financial analysis. The focus will be on using pandas to derive results from the domain of finance, specifically, time-series stock data, and not on details of the financial theory.

Specifically, in this chapter, we will progress through the following tasks:

  • Fetching and organizing stock data from Yahoo!
  • Plotting time-series prices
  • Plotting volume-series data
  • Calculating simple daily percentage change
  • Calculating simple daily cumulative returns
  • Resampling data from daily to monthly returns
  • Analyzing distribution of returns
  • Performing a moving-average calculation
  • Comparing average daily returns across stocks
  • Correlating stocks based on the daily percentage change of closing price
  • Volatility calculation
  • Determining risk relative to expected returns

Setting up the IPython notebook

The first step is to make sure that we have included all of the necessary Python libraries for all of the tasks that will be performed. This includes matplotlib for graphs, datetime to manage various dates and time in the data, a few methods from NumPy, and random number capabilities from the random library:

In [1]:
   # necessary imports for the workbook
   import pandas as pd
   import pandas.io.data
   import numpy as np
   import datetime
   import matplotlib.pyplot as plt

   # Set some pandas options
   pd.set_option('display.notebook_repr_html', False)
   pd.set_option('display.max_columns', 6)
   pd.set_option('display.max_rows', 10) 
   pd.set_option('display.width', 78) 
   pd.set_option('precision', 4)

   # do all our graphics inline
   %matplotlib inline
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.55.18