ml-finance-python

python scripts for finance machine learning

git clone https://9o.is/git/ml-finance-python.git

README.md

(12052B)


      1 # Chapter 02: Market & Fundamental Data
      2 
      3 This chapter introduces market and fundamental data sources and the environment in which they are created. Familiarity with various types of orders and the trading infrastructure matters because they affect backtest simulations of a trading strategy. We also illustrate how to use Python to access and work with trading and financial statement data.
      4  
      5 In particular, this chapter will cover the following topics:
      6 - How market microstructure shapes market data
      7 - How to reconstruct the order book from tick data using Nasdaq ITCH 
      8 - How to summarize tick data using various types of bars
      9 - How to work with eXtensible Business Reporting Language (XBRL)-encoded electronic filings
     10 - How to parse and combine market and fundamental data to create a P/E series
     11 - How to access various market and fundamental data sources using Python
     12 
     13 ## How to work with Market Data
     14 
     15 Market data results from the placement and processing of buy and sell orders in the course of the trading of financial instruments on the many marketplaces. The data reflects the institutional environment of trading venues, including the rules and regulations that govern orders, trade execution, and price formation.
     16 
     17 Algorithmic traders use ML algorithms to analyze the flow of buy and sell orders and the resulting volume and price statistics to extract trade signals or features that capture insights into, for example, demand-supply dynamics or the behavior of certain market participants.
     18 
     19 This section reviews institutional features that impact the simulation of a trading strategy during a backtest. Then, we will take a look at how tick data can be reconstructed from the order book source. Next, we will highlight several methods that regularize tick data and aim to maximize the information content. Finally, we will illustrate how to access various market data provider interfaces and highlight several providers.
     20 
     21 ###  Market microstructure
     22 
     23 Market microstructure is the branch of financial economics that investigates the trading process and the organization of related markets. The following references provide insights into institutional details that can be quite complex and diverse across asset classes and their derivatives, trading venues, and geographies, as well as data about the trading activities on various exchanges around the world
     24 
     25 - [Trading and Exchanges - Market Microstructure for Practitioners](https://global.oup.com/ushe/product/trading-and-exchanges-9780195144703?cc=us&lang=en&), Larry Harris, Oxford University Press, 2002
     26 - [World Federation of Exchanges](https://www.world-exchanges.org/our-work/statistics)
     27 
     28 ### Working with Order Book data
     29 The primary source of market data is the order book, which is continuously updated in real-time throughout the day to reflect all trading activity. Exchanges typically offer this data as a real-time service and may provide some historical data for free. 
     30 
     31 #### The FIX protocol
     32 
     33 The trading activity is reflected in numerous messages about trade orders sent by market participants. These messages typically conform to the electronic Financial Information eXchange (FIX) communications protocol for real-time exchange of securities transactions and market data or a native exchange protocol. 
     34 
     35 - [FIX Trading Standards](https://www.fixtrading.org/standards/)
     36 - Python: [Simplefix](https://github.com/da4089/simplefix)
     37 - C++ version: [quickfixengine](http://www.quickfixengine.org/)
     38 - Interactive Brokers [interface](https://www.interactivebrokers.com/en/index.php?f=4988)
     39 
     40 #### Nasdaq TotalView-ITCH data
     41 
     42 While FIX has a dominant large market share, exchanges also offer native protocols. The Nasdaq offers a TotalView ITCH direct data-feed protocol that allows subscribers to track individual orders for equity instruments from placement to execution or cancellation.
     43 
     44 - The ITCH [Specifications](http://www.nasdaqtrader.com/content/technicalsupport/specifications/dataproducts/NQTVITCHspecification.pdf)
     45 - [Sample Files](ftp://emi.nasdaq.com/ITCH/)
     46 
     47 #### Code Examples
     48 
     49 - The folder [NASDAQ TotalView ITCH Order Book](01_NASDAQ_TotalView-ITCH_Order_Book) contains the notebooks to
     50     - download NASDAQ Total View sample tick data,
     51     - parse the messages from the binary source data
     52     - reconstruct the order book for a given stock
     53     - visualize order flow data
     54     - normalize tick data
     55 - Binary Data services: the `struct` [module](https://docs.python.org/3/library/struct.html)
     56 
     57 #### Other protocols
     58 
     59  - Native exchange protocols [around the world](https://en.wikipedia.org/wiki/List_of_electronic_trading_protocols_
     60 
     61 ### Access to Market Data
     62 
     63 There are several options to access market data via API using Python. In this chapter, we first present a few sources built into the [`pandas`](https://pandas.pydata.org/) library. Then we briefly introduce the trading platform [Quantopian](https://www.quantopian.com/posts), the data provider [Quandl](https://www.quandl.com/) (acquired by NASDAQ in 12/2018) and the backtesting library [`zipline`](https://github.com/quantopian/zipline) that we will use later in the book, and list several additional options to access various types of market data. The directory [data_providers](02_data_providers) contains several notebooks that illustrate the usage of these options.
     64 
     65 #### Remote data access using pandas
     66 
     67 - read_html [docs](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_html.html?highlight=pandas%20io%20read_html)
     68 - S&P 500 constituents from [Wikipedia](https://en.wikipedia.org/wiki/List_of_S%26P_500_companies)
     69 - `pandas-datareader`[docs](https://pandas-datareader.readthedocs.io/en/latest/index.html)
     70 
     71 #### Code Examples
     72 
     73 The folder [data providers](02_data_providers) contains examples to use various data providers.
     74 
     75 Relevant sources include:
     76 
     77 - Quandl [docs](https://docs.quandl.com/docs) and Python [API](https://www.quandl.com/tools/python)
     78 - [Quantopian](https://www.quantopian.com/posts)
     79 - [Zipline](http://www.zipline.io/)
     80 - [LOBSTER](https://lobsterdata.com/)
     81 - [The Investor Exchange](https://iextrading.com/)
     82 - [Money.net](https://www.money.net/)
     83 - [Trading Economic](https://tradingeconomics.com/)
     84 - [Barchart](https://www.barchart.com/)
     85 - [Alpha Vantage](https://www.alphavantage.co/)
     86 - [Alpha Trading Labs](https://www.alphatradinglabs.com/)
     87 
     88 News
     89 - [Bloomberg and Reuters lose data share to smaller rivals](https://www.ft.com/content/622855dc-2d31-11e8-9b4b-bc4b9f08f381), FT, 2018
     90 
     91 
     92 ## How to work with Fundamental data
     93 
     94 Fundamental data pertains to the economic drivers that determine the value of securities. The nature of the data depends on the asset class:
     95 - For equities and corporate credit, it includes corporate financials as well as industry and economy-wide data.
     96 - For government bonds, it includes international macro-data and foreign exchange.
     97 - For commodities, it includes asset-specific supply-and-demand determinants, such as weather data for crops. 
     98 
     99 We will focus on equity fundamentals for the US, where data is easier to access. There are some 13,000+ public companies worldwide that generate 2 million pages of annual reports and 30,000+ hours of earnings calls. In algorithmic trading, fundamental data and features engineered from this data may be used to derive trading signals directly, for example as value indicators, and are an essential input for predictive models, including machine learning models.
    100 
    101 
    102 ### Financial statement data
    103 
    104 The Securities and Exchange Commission (SEC) requires US issuers, that is, listed companies and securities, including mutual funds to file three quarterly financial statements (Form 10-Q) and one annual report (Form 10-K), in addition to various other regulatory filing requirements.
    105 
    106 Since the early 1990s, the SEC made these filings available through its Electronic Data Gathering, Analysis, and Retrieval (EDGAR) system. They constitute the primary data source for the fundamental analysis of equity and other securities, such as corporate credit, where the value depends on the business prospects and financial health of the issuer. 
    107 
    108 #### Automated processing using XBRL markup
    109 
    110 Automated analysis of regulatory filings has become much easier since the SEC introduced XBRL, a free, open, and global standard for the electronic representation and exchange of business reports. XBRL is based on XML; it relies on [taxonomies](https://www.sec.gov/dera/data/edgar-log-file-data-set.html) that define the meaning of the elements of a report and map to tags that highlight the corresponding information in the electronic version of the report. One such taxonomy represents the US Generally Accepted Accounting Principles (GAAP).
    111 
    112 The SEC introduced voluntary XBRL filings in 2005 in response to accounting scandals before requiring this format for all filers since 2009 and continues to expand the mandatory coverage to other regulatory filings. The SEC maintains a website that lists the current taxonomies that shape the content of different filings and can be used to extract specific items.
    113 
    114 There are several avenues to track and access fundamental data reported to the SEC:
    115 - As part of the [EDGAR Public Dissemination Service]((https://www.sec.gov/oit/announcement/public-dissemination-service-system-contact.html)) (PDS), electronic feeds of accepted filings are available for a fee. 
    116 - The SEC updates [RSS feeds](https://www.sec.gov/structureddata/rss-feeds-submitted-filings) every 10 minutes, which list structured disclosure submissions.
    117 - There are public [index files](https://www.sec.gov/edgar/searchedgar/accessing-edgar-data.htm) for the retrieval of all filings through FTP for automated processing.
    118 - The financial statement (and notes) datasets contain parsed XBRL data from all financial statements and the accompanying notes.
    119 
    120 The SEC also publishes log files containing the [internet search traffic](https://www.sec.gov/dera/data/edgar-log-file-data-set.html) for EDGAR filings through SEC.gov, albeit with a six-month delay.
    121 
    122 
    123 #### Building a fundamental data time series
    124 
    125 The scope of the data in the [Financial Statement and Notes](https://www.sec.gov/dera/data/financial-statement-and-notes-data-set.html) datasets consists of numeric data extracted from the primary financial statements (Balance sheet, income statement, cash flows, changes in equity, and comprehensive income) and footnotes on those statements. The data is available as early as 2009.
    126 
    127 
    128 The folder [03_sec_edgar](03_sec_edgar) contains the notebook [edgar_xbrl](03_sec_edgar/edgar_xbrl.ipynb) to download and parse EDGAR data in XBRL format, and create fundamental metrics like the P/E ratio by combining financial statement and price data.
    129 
    130 ### Other fundamental data sources
    131 
    132 - [Compilation of macro resources by the Yale Law School](https://library.law.yale.edu/news/75-sources-economic-data-statistics-reports-and-commentary)
    133 - [Capital IQ](www.capitaliq.com)
    134 - [Compustat](www.compustat.com)
    135 - [MSCI Barra](www.mscibarra.com)
    136 - [Northfield Information Services](www.northinfo.com)
    137 - [Quantitative Services Group](www.qsg.com)
    138 
    139 
    140 ### Efficient data storage with pandas
    141 
    142 We'll be using many different data sets in this book, and it's worth comparing the main formats for efficiency and performance. In particular, we compare the following:
    143 
    144 - CSV: Comma-separated, standard flat text file format.
    145 - HDF5: Hierarchical data format, developed initially at the National Center for Supercomputing, is a fast and scalable storage format for numerical data, available in pandas using the PyTables library.
    146 - Parquet: A binary, columnar storage format, part of the Apache Hadoop ecosystem, that provides efficient data compression and encoding and has been developed by Cloudera and Twitter. It is available for pandas through the pyarrow library, led by Wes McKinney, the original author of pandas.
    147 
    148 The notebook [storage_benchmark](04_storage_benchmark/storage_benchmark.ipynb) in the directory [04_storage_benchmark](04_storage_benchmark) compares the performance of the preceding libraries.