Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Python for Algorithmic Trading Cookbook
Python for Algorithmic Trading Cookbook

Python for Algorithmic Trading Cookbook: Recipes for designing, building, and deploying algorithmic trading strategies with Python

Arrow left icon
Profile Icon Jason Strimpel
Arrow right icon
€17.99 €35.99
Book Aug 2024 412 pages 1st Edition
eBook
€17.99 €35.99
Print
€30.99 €44.99
Subscription
Free Trial
Renews at €18.99p/m
Arrow left icon
Profile Icon Jason Strimpel
Arrow right icon
€17.99 €35.99
Book Aug 2024 412 pages 1st Edition
eBook
€17.99 €35.99
Print
€30.99 €44.99
Subscription
Free Trial
Renews at €18.99p/m
eBook
€17.99 €35.99
Print
€30.99 €44.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon AI Assistant (beta) to help accelerate your learning
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Table of content icon View table of contents Preview book icon Preview Book

Python for Algorithmic Trading Cookbook

Acquire Free Financial Market Data with Cutting-Edge Python Libraries

A May 2017 Economist cover declared data to be the world’s most valuable resource. It’s none truer than in algorithmic trading. As algorithmic traders, it’s our job to acquire and make sense of billions of rows of market data for use in trading algorithms. In this context, it’s crucial to gather high-quality, reliable data that can adequately support trading algorithms and market research. Luckily for us, it’s possible to acquire high-quality data for free (or nearly free).

This chapter offers recipes for a series of different Python libraries—including the cutting-edge OpenBB Platform—to acquire free financial market data using Python. One of the primary challenges most non-professional traders face is getting all the data required for analysis together in one place. The OpenBB Platform addresses this issue. We’ll dive into acquiring data for a variety of assets, including stocks, options, futures (both continuous and individual contracts), and Fama-French factors.

One crucial point to remember is that data can vary across different sources. For instance, prices from two sources might differ due to distinct data sourcing methods or different adjustment methods for corporate actions. Some of the libraries we’ll cover might download data for the same asset from the same source. However, libraries vary in how they return that data based on options that help you preprocess the data in preparation for research.

Lastly, while we’ll focus heavily on mainstream financial data in this chapter, financial data is not limited to prices. The concept of “alternative data,” which includes non-traditional data sources such as satellite images, web traffic data, or customer reviews, can be an important source of information for developing trading strategies. The Python tools to acquire and process this type of data are outside the scope of this book. We’ve intentionally left out the methods of acquiring and processing this type of data since it’s covered in other resources dedicated to the topic.

In this chapter, we’ll cover the following recipes:

  • Diving into continuous futures data with Nasdaq Data Link
  • Exploring S&P 500 ratios data with Nasdaq Data Link
  • Working with stock market data with the OpenBB Platform
  • Fetching historic futures data with the OpenBB Platform
  • Navigating options market data with the OpenBB Platform
  • Harnessing factor data using pandas_datareader

Technical requirements

This book relies on the Anaconda distribution of Python. We’ll use Jupyter Notebook and Python script files to write the code. Unless specified otherwise, all the code can be written in Jupyter Notebooks.

Download and install the Anaconda distribution of Python. You can do this by going to https://www.anaconda.com/download. Depending on your operating system, the instructions for downloading and installing will vary. Please refer to the Anaconda documentation for detailed instructions.

Anaconda ships with a package manager called conda. Package managers make it easy to install, remove, and update Python packages. There’s a great cheat sheet for the conda package manager that you can download from https://docs.conda.io/projects/conda/en/4.6.0/_downloads/52a95608c49671267e40c689e0bc00ca/conda-cheatsheet.pdf.

Once you’ve installed the Anaconda distribution, open your Terminal on Mac or Linux or the Anaconda Prompt on Windows. If you’re a Windows user, make sure to use the Command Prompt instead of the Powershell prompt. Then follow these steps:

  1. Update the conda package manager:
    conda update -n base conda -y
  2. Create a virtual environment:
    conda create -n my-quant-stack python=3.10 -y
  3. After the installation process is complete, activate the environment:
    conda activate my-quant-stack
  4. Install Jupyter Notebook using the package manager that ships with Python, pip:
    pip install notebook matplotlib

This will set up a virtual environment using Python 3.10 and install Jupyter Notebook.

This chapter will use three Python libraries to acquire financial market data: the Nasdaq Data Link client, the OpenBB Platform, and pandas_datareader. The good news is that installing the OpenBB Platform installs many of the libraries you will need to acquire financial market data, including the Nasdaq Data Link client and pandas_datareader. As such, there is no need to install the libraries separately.

Install the OpenBB Platform with all extensions and providers (both officially supported and community-maintained ones) using pip:

pip install openbb[all]

This is the easiest way to set up the OpenBB Platform for this book.

Important note

In a macOS zsh Terminal shell, add quotation marks around the library name: "openbb[all]"

To install a single extension:

pip install openbb[charting]
pip install openbb[ta]

Or install a single provider:

pip install openbb[yfinance]

To install the Nightly distribution (this installs all extras by default):

pip install openbb-nightly

Important note

At the time of writing, installing the OpenBB Platform using pip isn’t compatible with environments such as Google Colab and Kaggle since they come with preinstalled packages that can conflict with the ones used with the OpenBB Platform. If you run into trouble installing the OpenBB Platform, please check the online documentation for the most up-to-date instructions.

Diving into continuous futures data with Nasdaq Data Link

Futures are one of the most common derivatives for trading and hedging purposes. Commercial traders such as food companies and energy traders use futures to hedge against the risk of price changes in the physical commodity. Hedge funds and high-frequency traders take advantage of minute price discrepancies, adding liquidity to the market, and lowering the cost of trading. Speculators like us use futures to express our views on market direction in a risk-defined way. Futures have a defined expiration date, after which point the contract stops trading. Market dynamics and trading strategies shift traders’ attention from one contract to the next through time. While the flexibility of futures is great for trading, it makes it hard to acquire data for research and backtesting.

The most common way to get started analyzing futures contracts is through continuous futures data. As the name implies, continuous futures provide a continuously adjusted data feed that takes into consideration the roll from one expiration to the next. Continuous futures data lets traders and analysts backtest long-term trading strategies. Since futures expire, traders buy or sell positions in the expiring contract to enter a similar position in the next expiring contract. It’s this process of contract expiration that makes continuous futures data important for long-term analysis. The downside of continuous futures data is that the impact of the roll over long time frames can be significant, so it needs to be taken into consideration in strategies that rely on different roll strategies.

With Nasdaq Data Link, previously known as Quandl, you can access continuous contract data for 600 futures contracts built on top of raw data from CME, ICE, LIFFE, and other exchanges. This recipe will guide you through the process of obtaining continuous futures data using the Nasdaq Data Link client.

Getting ready…

You can use the Nasdaq Data Link client to acquire data without an account or an API key, but you’re limited in the number of API calls you can make. Without a key, you can make 20 API calls in 10 minutes or up to 50 API calls per day.

Since an account is free, it makes sense to sign up. This is something you can do via the Nasdaq Data Link website (https://data.nasdaq.com/). Your API key is in your profile (https://data.nasdaq.com/account/profile). Also, make sure to install nasdaqdatalink with pip if it’s not installed already:

pip install nasdaq-data-link

How to do it…

The Nasdaq Data Link client is easy to use:

  1. Import the necessary library:
    import nasdaqdatalink
  2. Set your API key:
    nasdaqdatalink.ApiConfig.api_key = "your_api_key"
  3. Download the continuous futures data:
    data = nasdaqdatalink.get("CHRIS/CME_ES1")
  4. Inspect the downloaded data:
    print(data)

    Running the preceding code generates a pandas DataFrame and prints the data to the screen:

Figure 1.1: Historic settlement prices of the ES continuous futures contract

Figure 1.1: Historic settlement prices of the ES continuous futures contract

How it works…

The nasdaqdatalink library provides a simple way to download data from Nasdaq Data Link. The format of the symbol is CHRIS/{EXCHANGE}_{CODE}{NUMBER}, where {EXCHANGE} is the exchange the contract trades on (CME, for example), {CODE}, is the contract code (CL, for example), and {NUMBER} is the “depth” associated with the chained contract. For instance, the front-month contract has a depth of 1, the second-month contract has a depth of 2, and so on. In the preceding example, we are retrieving the continuous contract data for the E-Mini S&P 500 Futures contract from the Chicago Mercantile Exchange (CME). The get function downloads the data as a pandas DataFrame.

There’s more…

The nasdaqdatalink library provides different options for downloading most data feeds:

  1. Instead of a pandas DataFrame, return a NumPy array:
    data = nasdaqdatalink.get("CHRIS/CME_ES1", returns="numpy")
  2. Define a start and end date for the data series:
    data = nasdaqdatalink.get(
        "CHRIS/CME_ES1",
        start_date="2001-12-31",
        end_date="2005-12-31"
    )
  3. You can request specific columns by adding a dot and the column number to the symbol:
    data = nasdaqdatalink.get(
        "CHRIS/CME_ES1.8",
        start_date="2001-12-31",
        end_date="2005-12-31"
    )
  4. We’ll cover resampling in detail in Chapter 2, Analyze and Transform Financial Market Data with pandas, but the nasdaqdatalink library can resample data to a monthly interval:
    data = nasdaqdatalink.get(
        "CHRIS/CME_ES1",
        collapse="monthly"
    )
  5. Finally, you can request multiple symbols at once. In this example, we’re creating a futures curve using four sequential futures contracts and plotting the curve on the last date:
    contracts = [
        "CHRIS/CME_ES1.6",
        "CHRIS/CME_ES2.6",
        "CHRIS/CME_ES3.6",
        "CHRIS/CME_ES4.6",
    ]
    data = nasdaqdatalink.get(
        contracts,
        start_date="2015-01-01",
        end_date="2015-12-31"
    )
    data.iloc[0].plot(title=f"ES on {data.index[0]}");

    By running the preceding code, a plot is generated showing the settlement prices across expirations:

Figure 1.2: The first four ES continuous contracts on 2015-01-02

Figure 1.2: The first four ES continuous contracts on 2015-01-02

See also

A great way to get free resources for using Python for market data analysis is the free, twice-weekly newsletter from PyQuant News. You can sign up here: https://www.pyquantnews.com/subscribe-to-the-pyquant-newsletter.

To dive deeper into Nasdaq Data Link data, check out the following resources:

Exploring S&P 500 ratios data with Nasdaq Data Link

The performance of professional investment managers that actively trade is normally compared against a benchmark. A common benchmark for comparison is the S&P 500 index of US stocks. Non-professional traders can also use the S&P 500 as a benchmark for risk and performance analysis and to hedge systematic risk from portfolios. Getting compiled data for the underlying stocks that make up the S&P 500—such as the price-to-earnings ratio—has historically been difficult. These ratios are now available for download through the Nasdaq Data Link Python client. This recipe will guide you through how to use the library to download and analyze S&P 500 ratios.

How to do it…

If you’re extending the code from the previous recipe, then your API is already set. If you’re starting from scratch, follow the first two steps:

  1. Import the necessary library:
    import nasdaqdatalink
  2. Set your API key:
    nasdaqdatalink.ApiConfig.api_key = "your_api_key"
  3. Fetch the monthly dividend yield for the S&P 500:
    data = nasdaqdatalink.get("MULTPL/SP500_DIV_YIELD_MONTH")
  4. Plot the dividend yield through time:
    data.plot(title="S&P 500 dividend yield (
        12 month dividend per share)/price")

    Running the preceding code generates a plot of the S&P 500 dividend yield through time:

Figure 1.3: S&P 500 dividend yield from index inception

Figure 1.3: S&P 500 dividend yield from index inception

How it works…

Similar to the first recipe, we import the Nasdaq Data Link client, set the API key, and use the same get method. The format of the symbol is MULTPL/{RATIO}, where {RATIO} is the name of the ratio to download. The result is a pandas DataFrame unless you set additional options (see the previous recipe).

There’s more…

There are 36 S&P 500 ratios available with up-to-date data. Some of the most popular are as follows:

  • SP500_PE_RATIO_MONTH: S&P 500 price-to-earnings ratio
  • SHILLER_PE_RATIO_MONTH: S&P 500 Shiller PE ratio. This is also known as the Cyclically Adjusted PE ratio (CAPE ratio).
  • SP500_EARNINGS_YIELD_MONTH: Earnings yield of the S&P 500 based on the trailing 12-month earnings divided by index price. The current yield is estimated from the latest reported earnings and current index value.
  • SP500_REAL_PRICE_MONTH: S&P 500 index not adjusted for inflation.

See also

You can find a complete list of the ratios that are available for download at https://data.nasdaq.com/data/MULTPL-sp-500-ratios.

Working with stock market data with the OpenBB Platform

You may remember the meme stock hysteria that sent GameStop’s stock up 1,744% in January 2021. One of the good things that came from that episode was the GameStonk terminal, now rebranded as OpenBB. OpenBB is the most popular open-source finance projects on GitHub for good reason: it provides a single interface to access hundreds of data feeds from one place in a standard way. OpenBB has a command-line interface that is great for manual investment research. But when it’s time to get data into Python, you want the OpenBB Platform. This recipe will guide you through the process of using the OpenBB Platform to fetch stock market data.

Getting ready…

By now, you should have the OpenBB Platform installed in your virtual environment. If not, go back to the beginning of this chapter and get it set up. The OpenBB Platform is free to use and offers a web-based UI to manage your configuration files, store API keys, and get code walkthroughs and examples. Sign up for a free Hub account at https://my.openbb.co/login. The popular course, Getting Started with Python for Quant Finance, uses OpenBB exclusively for all the code. Check out https://www.pyquantnews.com/getting-started-with-python-for-quant-finance for information on how to join.

How to do it…

Using the OpenBB Platform involves one import:

  1. Import the OpenBB Platform:
    from openbb import obb
    obb.user.preferences.output_type = "dataframe"
  2. Use the historical method to download price data for the SPY ETF:
    data = obb.equity.price.historical("SPY", provider="yfinance")
  3. Inspect the resulting DataFrame:
    print(data)

    Running the preceding code generates a pandas DataFrame and prints the data to the screen:

Figure 1.4: Historic price data for SPY

Figure 1.4: Historic price data for SPY

How it works…

The OpenBB Platform follows an easy-to-understand namespace convention. All the methods for acquiring stock price data are methods on openbb.equity.

The historical method accepts a ticker symbol and returns the open, high, low, close, adjusted close, volume, dividend, and split adjustments in a pandas DataFrame. The additional parameters you can specify are as follows:

  • start_date: Start date to get data from with
  • interval: Interval (in minutes) to get data—that is, 1, 5, 15, 30, 60, or 1,440
  • end_date: End date to get data from with
  • provider: Source of data extracted

There’s more…

An important benefit of using the OpenBB Platform is choosing your data source. By default, the OpenBB Platform will attempt to download data from free sources such as Yahoo! Finance. In most OpenBB Platform calls, you can indicate a different source. To use a source that requires an API key (either free or paid), you can configure it in the OpenBB Hub.

Tip

Check out the OpenBB Platform documentation for the latest functionality: https://docs.openbb.co.

Let’s look at some more of the functions of the OpenBB Platform.

Comparison of fundamental data

Not only can the OpenBB Platform download fundamental data in an organized and usable way, but it can also concatenate it in a single Pandas DataFrame for further analysis.

We can use the following code to see the balance sheet metrics from AAPL and MSFT:

obb.equity.fundamental.metrics(
    "AAPL,MSFT",
    provider="yfinance"
)

The output of the preceding snippet is a pandas DataFrame with fundamental data for each ticker that was passed:

Figure 1.5: Balance sheet data for MSFT and AAPL

Figure 1.5: Balance sheet data for MSFT and AAPL

Building stock screeners

One of the most powerful features of the OpenBB Platform is the custom stock screener. It uses the Finviz stock screener under the hood and surfaces metrics across a range of stocks based on either pre-built or custom criteria. See the documentation for more on how to use the OpenBB screener functions (https://docs.openbb.co/platform/reference/equity/screener):

  1. Create an overview screener based on a list of stocks using the default view:
    obb.equity.compare.groups(
        group="industry",
        metric="valuation",
        provider="finviz"
    )

    The output of the preceding snippet is the following pandas DataFrame:

Figure 1.6: Results of a comparison screener between F, GE, and TSLA

Figure 1.6: Results of a comparison screener between F, GE, and TSLA

  1. Create a screener that returns the top gainers from the technology sector based on a preset:
    obb.equity.compare.groups(
        group="technology",
        metric="performance",
        provider="finviz"
    )

    The output of the preceding snippet is the following pandas DataFrame:

Figure 1.7: Results of a screener showing the day’s top-gaining stocks

Figure 1.7: Results of a screener showing the day’s top-gaining stocks

  1. Create a screener that presents an overview grouped by sector:
    obb.equity.compare.groups(
        group="sector",
        metric="overview",
        provider="finviz"
    )

    The output of the preceding snippet is the following pandas DataFrame:

Figure 1.8: Results of a screener grouped by sector

Figure 1.8: Results of a screener grouped by sector

See also

For more on OpenBB and the Finviz stock screener, check out the following resources:

Fetching historic futures data with the OpenBB Platform

Traders use continuous futures data for backtesting trading strategies. Futures traders use the roll from one contract to another as a potential opportunity for profit. Some traders simply pick a date before expiration to roll to the next contract, while others use sophisticated techniques involving open interest. This basis trade is persistently one of the most popular trading strategies for futures traders. These traders want control over the data that’s used to compute the basis trade, so acquiring individual contract data is important. This recipe will guide you through the process of using the OpenBB Platform to fetch individual futures contract data.

Getting ready…

By now, you should have the OpenBB Platform installed in your virtual environment. If not, go back to the beginning of this chapter and get it set up.

How to do it…

We’ll use the futures functionality in the OpenBB Platform to download individual futures data for free:

  1. Import pandas and the OpenBB Platform:
    import pandas as pd
    from openbb import obb
    obb.user.preferences.output_type = "dataframe"
  2. Download the current futures curve for the VIX futures contract from the Chicago Board Options Exchange (CBOE):
    data = obb.derivatives.futures.curve(symbol="VX")
  3. Inspect the resulting DataFrame:
    print(data)

    Running the preceding code generates the futures curve for the VIX futures contract:

Figure 1.9: Settlement prices for the forward Eurodollar futures contracts

Figure 1.9: Settlement prices for the forward Eurodollar futures contracts

  1. Update the DataFrame index to the expiration dates and plot the settlement prices:
    data.index = pd.to_datetime(data.expiration)
    data.plot()

    By running the proceeding code, we plot the VIX futures curve:

Figure 1.10: VIX futures curve

Figure 1.10: VIX futures curve

There’s more…

You can use the obb.derivatives.futures.historical method to get historical data for an individual expiration. Stitching together data across a range of years can provide insight into the market’s expectation of supply and demand of the underlying commodity:

  1. First, create a list containing the year and month expirations you’re interested in:
    expirations = [
        "2024-12",
        "2025-12",
        "2026-12",
        "2027-12",
        "2028-12",
        "2029-12",
        "2030-12",
    ]
  2. The preceding code creates a Python list of expiration years and dates in string format. Now, loop through each of the expirations to download the data:
    contracts = []
    for expiration in expirations:
        df = (
            obb
            .derivatives
            .futures
            .historical(
                symbol="CL",
                expiration=expiration,
                start_date="2020-01-01",
                end_date="2022-12-31"
            )
        ).rename(columns={
            "close": expiration
        })
        contracts.append(df[expiration])
  3. For each of the contracts, use the OpenBB Platform to download historical futures data for the CL contract between January 1, 2020, and 31 December 31, 2022. Using the pandas rename method, change the column name from "close" to the expiration date. Finally, append the newly created pandas DataFrame to a list of DataFrames:
    historical = (
        pd
        .DataFrame(contracts)
        .transpose()
        .dropna()
    )
  4. Concatenate the DataFrames together, swap the columns and rows using the transpose method, and drop any records with no data using the dropna method. Inspect the resulting DataFrame:
    print(historical)

    By printing the DataFrame, we will see the historical settlement prices:

Figure 1.11: Historic settlement prices for the December CL futures contract

Figure 1.11: Historic settlement prices for the December CL futures contract

The result is the historical data between January 2020 and December 2022 for each of the December expirations between 2023 and 2030:

  1. To visualize the market’s expectation of the future supply and demand of the December contract, you can plot the last price:
    historical.iloc[-1].plot()

    Here's the output:

Figure 1.12: Futures curve for the December CL contract

Figure 1.12: Futures curve for the December CL contract

See also

For more on the OpenBB Platform futures functionality, you can browse the following documentation:

Navigating options market data with the OpenBB Platform

Options are exchange-listed derivative contracts that convey the right (but not the obligation) to buy or sell the underlying stock at a certain price on or before a certain expiration date. Options are among the most versatile financial instruments in the market. They allow traders to define their risk profiles before entering trades and express market views not only on the direction of the underlying but the volatility. While options offer a high degree of flexibility for trading, this feature complicates data collection for research and backtesting.

A single underlying stock can have an array of options contracts with different combinations of strike prices and expiration dates. Collecting and manipulating this data is a challenge. The combination of options contracts for all strikes and expiration dates is commonly referred to as an options chain. There can be thousands of individual options contracts at a given time for a single underlying stock. Not only does the number of individual contracts pose a challenge, but getting price data has historically been expensive. With the introduction of the OpenBB Platform, it is now only a few lines of Python code to download options chains into a pandas DataFrame. This recipe will walk you through acquiring options data using the OpenBB Platform.

Getting ready…

By now, you should have the OpenBB Platform installed in your virtual environment. If not, go back to the beginning of this chapter and get it set up.

How to do it…

Similar to how we used the OpenBB Platform for futures data, we can use it for options data too:

  1. Import the OpenBB Platform and Matplotlib for visualization:
    from openbb import obb
    obb.user.preferences.output_type = "dataframe"
  2. Use the chains method to download the entire options chain:
    chains = obb.derivatives.options.chains(symbol="SPY")
  3. Inspect the resulting DataFrame:
    chains.info()

    By running the preceding code, we’ll see the details of the options chains data:

Figure 1.13: Preview of the data downloaded for the SPY options chains

Figure 1.13: Preview of the data downloaded for the SPY options chains

Note that there are 8,518 options contracts for the SPY Exchange Traded Fund (ETF) that can be downloaded from CBOE (for free).

How it works…

The obb.derivatives.options.chains method downloads the entire options chain and stores it in a pandas DataFrame. The obb.derivatives.options.chains has an additional optional parameter:

  • provider: The source from which the data should be downloaded. The default is CBOE. You can also select Tradier, Intrinio, or TMX. Note that for Tradier, Intrinio, and TMX, you need to provide your API key, which can be configured in the OpenBB Hub.

There’s more…

You can use the OpenBB Platform to download historical options data for a single contract. To do this, you need the option symbol.

We’ll use the obb.equity.price.historical method to get the historical options data for an SPY call option with a strike price of $550 expiring on December 20, 2024:

data = obb.equity.price.historical(
    symbol="SPY241220C00550000",
    provider="yfinance"
)[["close", "volume"]]

The result is a pandas DataFrame with the closing price and volume of the options contract.

Figure 1.14: Closing prices and volume of the SPY options contract

Figure 1.14: Closing prices and volume of the SPY options contract

Options Greeks

Options Greeks measure how options prices change given a change in one of the inputs to an options pricing model. For example, delta measures how an options price changes given a change in the underlying stock price.

Using obb.derivatives.options.chains, the OpenBB Platform returns the most used Greeks including Delta, Gamma, Theta, Vega, and Rho.

See also

Options are a fascinating and deep topic that is rich with opportunities for trading. You can learn more about options, volatility, and how to analyze both via the OpenBB Platform:

Harnessing factor data using pandas_datareader

Diversification is great until the entire market declines in value. That’s because the overall market influences all assets. Factors can offset some of these risks by targeting drivers of return not influenced by the market. Common factors are size (large-cap versus small-cap) and style (value versus growth). If you think small-cap stocks will outperform large-cap stocks, then you might want exposure to small-cap stocks. If you think value stocks will outperform growth stocks, then you might want exposure to value stocks. In either case, you want to measure the risk contribution of the factor. Eugene Fama and Kenneth French built the Fama-French three-factor model in 1992. The three Fama-French factors are constructed using six value-weight portfolios formed on capitalization and book-to-market.

The three factors are as follows:

  • Small Minus Big, which represents the differential between the average returns of three small-cap portfolios and three large-cap portfolios.
  • High Minus Low, which quantifies the difference in average returns between two value-oriented portfolios and two growth-oriented portfolios.
  • Rm-Rf, which denotes the market’s excess return over the risk-free rate.

We’ll explore how to measure and isolate alpha in Chapter 5, Build Alpha Factors for Stock Portfolios. This recipe will guide you through the process of using pandas_datareader to fetch historic factor data for use in your analysis.

Getting ready…

By now, you should have the OpenBB Platform installed in your virtual environment. If not, go back to the beginning of this chapter and get it set up. By installing the OpenBB Platform, pandas_datareader will be installed and ready to use.

How to do it…

Using the pandas_datareader library, we have access to dozens of investment research factors:

  1. Import pandas_datareader:
    import pandas_datareader as pdr
  2. Download the monthly factor data starting in January 2000:
    factors = pdr.get_data_famafrench("F-F_Research_Data_Factors")
  3. Get a description of the research data factors:
    print(factors[“DESCR”])

    The result is an explanation of the data included in the DataFrame:

Figure 1.15: Preview of the description that is downloaded with factor data

Figure 1.15: Preview of the description that is downloaded with factor data

  1. Inspect the monthly factor data:
    print(factors[0].head())

By running the preceding code, we get a DataFrame containing monthly factor data:

Figure 1.16: Preview of the monthly data downloaded from the Fama-French Data Library

Figure 1.16: Preview of the monthly data downloaded from the Fama-French Data Library

  1. Inspect the annual factor data:
    print(factors[1].head())

    By running the preceding code, we get a DataFrame containing annual factor data:

Figure 1.17: Preview of the annual data downloaded from the Fama-French Data Library

Figure 1.17: Preview of the annual data downloaded from the Fama-French Data Library

How it works…

Under the hood, pandas_datareader fetches data from the Fama-French Data Library by downloading a compressed CSV file, uncompressing it, and creating a pandas DataFrame.

There are 297 different datasets with different factor data available from the Fama-French Data Library. Here are some popular versions of the Fama-French 3-factor model for different regions:

  • Developed_3_Factors
  • Developed_ex_US_3_Factors
  • Europe_3_Factors
  • Japan_3_Factors
  • Asia_Pacific_ex_Japan_3_Factors

You can use these in the get_data_famafrench method, just like F-F_Research_Data_Factors.

Some datasets return a dictionary with more than one DataFrame representing data for different time frames, portfolio weighting methodologies, and aggregate statistics. Data for these portfolios can be accessed using numerical keys. For example, the 5_Industry_Portfolios dataset returns eight DataFrames in the dictionary. The first can be accessed using the 0 key, the second using the 1 key, and so on. Each dictionary includes a description of the dataset, which can be accessed using the DESCR key.

There’s more…

pandas_datareader can be used to access data from many remote online sources. These include Tiingo, IEX, Alpha Vantage, FRED, Eurostat, and many more. Review the full list of data sources on the documentation page: https://pandas-datareader.readthedocs.io/en/latest/remote_data.html.

See also

For more details on the factors available in the investment factor research library, take a look at the following resources. For another example of using the Fama-French 3-factor model, see the resources on the PyQuant News website:

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Follow practical Python recipes to acquire, visualize, and store market data for market research
  • Design, backtest, and evaluate the performance of trading strategies using professional techniques
  • Deploy trading strategies built in Python to a live trading environment with API connectivity
  • Purchase of the print or Kindle book includes a free PDF eBook

Description

Discover how Python has made algorithmic trading accessible to non-professionals with unparalleled expertise and practical insights from Jason Strimpel, founder of PyQuant News and a seasoned professional with global experience in trading and risk management. This book guides you through from the basics of quantitative finance and data acquisition to advanced stages of backtesting and live trading. Detailed recipes will help you leverage the cutting-edge OpenBB SDK to gather freely available data for stocks, options, and futures, and build your own research environment using lightning-fast storage techniques like SQLite, HDF5, and ArcticDB. This book shows you how to use SciPy and statsmodels to identify alpha factors and hedge risk, and construct momentum and mean-reversion factors. You’ll optimize strategy parameters with walk-forward optimization using VectorBT and construct a production-ready backtest using Zipline Reloaded. Implementing all that you’ve learned, you’ll set up and deploy your algorithmic trading strategies in a live trading environment using the Interactive Brokers API, allowing you to stream tick-level data, submit orders, and retrieve portfolio details. By the end of this algorithmic trading book, you'll not only have grasped the essential concepts but also the practical skills needed to implement and execute sophisticated trading strategies using Python.

What you will learn

  • Acquire and process freely available market data with the OpenBB Platform
  • Build a research environment and populate it with financial market data
  • Use machine learning to identify alpha factors and engineer them into signals
  • Use VectorBT to find strategy parameters using walk-forward optimization
  • Build production-ready backtests with Zipline Reloaded and evaluate factor performance
  • Set up the code framework to connect and send an order to Interactive Brokers

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Aug 16, 2024
Length 412 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781835084700
Category :
Languages :
Concepts :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon AI Assistant (beta) to help accelerate your learning
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want

Product Details

Publication date : Aug 16, 2024
Length 412 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781835084700
Category :
Languages :
Concepts :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Table of Contents

16 Chapters
Preface Chevron down icon Chevron up icon
1. Chapter 1: Acquire Free Financial Market Data with Cutting-Edge Python Libraries Chevron down icon Chevron up icon
2. Chapter 2: Analyze and Transform Financial Market Data with pandas Chevron down icon Chevron up icon
3. Chapter 3: Visualize Financial Market Data with Matplotlib, Seaborn, and Plotly Dash Chevron down icon Chevron up icon
4. Chapter 4: Store Financial Market Data on Your Computer Chevron down icon Chevron up icon
5. Chapter 5: Build Alpha Factors for Stock Portfolios Chevron down icon Chevron up icon
6. Chapter 6: Vector-Based Backtesting with VectorBT Chevron down icon Chevron up icon
7. Chapter 7: Event-Based Backtesting Factor Portfolios with Zipline Reloaded Chevron down icon Chevron up icon
8. Chapter 8: Evaluate Factor Risk and Performance with Alphalens Reloaded Chevron down icon Chevron up icon
9. Chapter 9: Assess Backtest Risk and Performance Metrics with Pyfolio Chevron down icon Chevron up icon
10. Chapter 10: Set Up the Interactive Brokers Python API Chevron down icon Chevron up icon
11. Chapter 11: Manage Orders, Positions, and Portfolios with the IB API Chevron down icon Chevron up icon
12. Chapter 12: Deploy Strategies to a Live Environment Chevron down icon Chevron up icon
13. Chapter 13: Advanced Recipes for Market Data and Strategy Management Chevron down icon Chevron up icon
14. Index Chevron down icon Chevron up icon
15. Other Books You May Enjoy Chevron down icon Chevron up icon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.