Remote data access

The pandas module can retrieve econometric data from various websites on the Internet. The types of data that can be downloaded varies from stock prices and option prices to macroeconomic data. The websites in question are listed as follows:

It's quite possible that you are not interested in all of this econometric data; therefore, in this section, we will only download option data from Yahoo! Finance with the purpose of calculating the price of a straddle.

Note

Options are financial contracts that derive their price from other financial instruments, for instance, stocks. The two fundamental types of options are calls and puts. Calls give you the right to buy the underlying instrument, for example, shares in IBM at a predetermined price called the strike price. Puts give you the opposite right to sell at a given strike price.

Option contracts are also tied to an expiration date, after which the contract is no longer valid. The rules related to expiration are too complicated to explain fully here. For all the finance details, have a look at Python for Finance, Yuxing Yan, Packt Publishing, which is listed in the Preface. A straddle is an option combination consisting of a put and a call option with the same expiration date. For a straddle, these options are typically chosen to be at-the-money, meaning that the strike price is close to the current stock price. This option strategy is market neutral; it doesn't matter whether the stock price goes up or down. However, to make profit, the stock price has to move within the expiration period; more than the price of the call and put options combined. In other words, the stock price has to move more than the price of the straddle. The price of a straddle is, therefore, equal to the price change the market currently expects to occur.

In the following example, we will ignore holidays. You can check manually for holidays falling on a Friday using the tips from https://stackoverflow.com/questions/9187215/datetime-python-next-business-day. The market is closed on a couple of Fridays each year, such as Good Friday. To calculate the price of the AAPL straddle, expiring next Friday, follow these steps:

  1. Import the pandas Options class:
    from pandas.io.data import Options
  2. Define the following function to determine the next Friday starting from today with the standard Python code:
    def next_friday():
        today = datetime.date.today()
        return today + datetime.timedelta( (4-today.weekday()) % 7 )
  3. For a straddle, we need to get the call and put options, which are closest to the current stock price. The AAPL option contracts are a bit problematic. For reasons that are too technical to explain here, it might not be possible to determine unique option contracts with the strike price closest to the current stock price. To be on the safer side, we will select the most popular options. By definition, these are the options with the highest open interest. Define the following function that retrieves the price of an at-the-money put or call as follows:
    def get_price(options, is_call, is_put):
       fri = next_friday()
       option_list = options.get_near_stock_price(above_below=1, call=is_call, put=is_put, expiry=fri)[0]
       option =  option_list[option_list["Open Int"] == option_list["Open Int"].max()]
       
       return option["Last"].values[0]

    Recall that an option can be either a put or call contract. Therefore, is_put and is_call are Boolean variables. We use the pandas get_near_stock_price() method of the Options class to get the options closest to the current stock price. In the pandas DataFrame that we obtain, there is a column named Open Int, which is indicative of how popular a given option contract is. We select the most popular contract with the max() method. The Last column in the DataFrame gives the last traded price. This is the price that we are interested in and, therefore, return.

  4. Create an Options object for AAPL that gets data from Yahoo! Finance:
    options = Options('AAPL', "yahoo")

The rest of the code is simple and self-explanatory. You can find the code in the price_straddle.py file in this book's code bundle:

from pandas.io.data import Options
import datetime


def next_friday():
    today = datetime.date.today()
    return today + datetime.timedelta( (4-today.weekday()) % 7 )

def get_price(options, is_call, is_put):
   fri = next_friday()
   option_list = options.get_near_stock_price(above_below=1, call=is_call, put=is_put, expiry=fri)[0]
   option =  option_list[option_list["Open Int"] == option_list["Open Int"].max()]
   
   return option["Last"].values[0]

def get_straddle():
   options = Options('AAPL', "yahoo")
   call =  get_price(options, True, False)
   put = get_price(options, False, True)

   return call + put

if __name__ == "__main__":
   print get_straddle()
..................Content has been hidden....................

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