<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by QuantRocket on Medium]]></title>
        <description><![CDATA[Stories by QuantRocket on Medium]]></description>
        <link>https://medium.com/@quantrocket?source=rss-6bc03eef1004------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*DTkHAFoYr2Sks21UwbgVEw.png</url>
            <title>Stories by QuantRocket on Medium</title>
            <link>https://medium.com/@quantrocket?source=rss-6bc03eef1004------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 22:32:04 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@quantrocket/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Extracting Structured Datasets for Systematic Strategies from Unstructured Textual Sources: Brain…]]></title>
            <link>https://quantrocket.medium.com/extracting-structured-datasets-for-systematic-strategies-from-unstructured-textual-sources-brain-1c600c2e0ae0?source=rss-6bc03eef1004------2</link>
            <guid isPermaLink="false">https://medium.com/p/1c600c2e0ae0</guid>
            <category><![CDATA[quantitative-finance]]></category>
            <category><![CDATA[fundamental-analysis]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[algorithmic-trading]]></category>
            <category><![CDATA[alternative-data]]></category>
            <dc:creator><![CDATA[QuantRocket]]></dc:creator>
            <pubDate>Thu, 11 Jul 2024 18:50:01 GMT</pubDate>
            <atom:updated>2024-07-11T18:50:01.460Z</atom:updated>
            <content:encoded><![CDATA[<h3>Extracting Structured Datasets for Systematic Strategies from Unstructured Textual Sources: Brain Alternative Datasets</h3><p>Natural Language Processing (NLP) is a broad field that enables computers to process and analyze unstructured textual data. In this article, we present several proprietary Brain datasets derived from news articles, SEC regulatory filings, and earnings calls, along with case studies implemented in QuantRocket.</p><blockquote>For more details about the content discussed and the Brain methodology please refer to <em>Handbook of Alternative Data in Finance</em>, Chapter 8, edited by Gautam Mitra et al.</blockquote><h3>1. Introduction</h3><p>In recent years, active investors have continuously sought additional sources of information on financial markets. Consequently, the number of alternative data providers and platforms that aggregate and integrate alternative data for asset managers has steadily increased, creating a large and diverse ecosystem.</p><p>The term “alternative data” has broadened to include a variety of sources, such as satellite imagery, credit card transactions, weather data, social media, news, and corporate documents. Essentially, alternative data encompasses all data that falls outside the realm of traditional financial and economic data, yet holds potential value for investment strategies.</p><p>A significant portion of the available alternative data is derived from textual documents using Natural Language Processing (NLP) tools, a rapidly advancing field. This document offers a general overview of Brain’s approaches to creating alternative datasets based on various language metrics applied to textual documents, including news articles, SEC filings, and earnings call transcripts.</p><h3>2. Sources of Textual Information</h3><p><strong>Newsflow Collection</strong>: Public news is a significant source of information. According to the efficient market hypothesis (EMH), public news should not be a useful trading source since its content is quickly reflected in market prices. However, EMH has been challenged since the 1980s, with studies highlighting inconsistencies between EMH and observed data. Recent research suggests these inconsistencies can be interpreted through order flow dynamics. Thus, changes in news flow may impact market trades and prices. Analyzing the correlation between sentiment changes in news and market performance is widely studied. Assessing the sentiment of news flow on individual assets by measuring aggregated sentiment, news volume, and related metrics can be valuable.</p><p><strong>SEC Filings: Form 10-Ks and 10-Qs</strong>: Form 10-K (annual) and Form 10-Q (quarterly) filings required by the SEC are valuable information sources. These documents provide detailed information about a company’s history, performance, and potential risks. Despite their public accessibility, studies suggest market inefficiencies in capturing the information due to increased complexity and length. Analysis of the past decade’s 10-K filings shows trends of increasing document length, higher readability scores (indicating increased difficulty), and longer sentences.</p><p><strong>Earnings Calls Transcripts</strong>: Earnings calls, where top executives present quarterly or annual results and future outlooks to analysts, media, and investors, are another vital information source. The last part of the call includes a Q&amp;A session addressing specific relevant topics. Earnings calls are structured similarly for global stocks, unlike 10-K and 10-Q filings, which are US-specific. Studies highlight the value of earnings call content for investors. Various providers offer access to earnings call transcripts, using manual, software-assisted, or hybrid transcription processes, with availability ranging from minutes to hours after the event.</p><h3>3. Structured Datasets from Unstructured Sources</h3><p>Unlike “structured data,” which is organized, typically quantitative, and easily processed by a computer, a vast amount of information is available in an unstructured form. This unstructured data is generally in its native form, not organized or easily processed by a computer. The textual documentation we discuss, such as news, corporate documents, and transcripts, falls into this second category. Natural Language Processing (NLP) is a broad field that enables computers to process and analyze textual data.</p><h3>3.1 Measuring Sentiment and Attention in Corporate Newsflow</h3><p>Measuring sentiment in texts is a common activity nowadays with applications ranging from assessing marketing and political campaigns to monitoring news and corporate documents. Many providers calculate sentiment for listed companies based on various sources like news, social media, and documents.</p><p>There is no single “sentiment” algorithm; each measure depends on assumptions and specific methodological choices, from the sources used to the parsing approach and aggregation phase.</p><p>Here, we present an overview of the process used to calculate the “Brain Sentiment Indicator” (BSI) and discuss some specific methodological choices.</p><p>The BSI algorithm is based on a simple approach that prioritizes simplicity and explicability. It uses a combination of semantic rules for financial news classification and a dictionary-based approach (Bag of Words — BOW). Sentiment is calculated from news headlines, which typically summarize the overall meaning of the text while limiting the noise from longer texts.</p><p>When calculating metrics related to news volume, one must consider that average news volume depends on the specific company. A normalized measure of volume, often called “buzz,” can be useful. “Buzz” is defined as the difference between current news volume and the past volume average in units of standard deviations. A value close to 0 indicates no significant media attention compared to the average, while a value significantly larger than 0 highlights increased media attention. An high buzz in combination with negative sentiment typically highlights very negative events in a company.</p><h3>3.2 Use case study based on Brain Sentiment Indicator</h3><p>In this use case, we explore the impact of news buzz over a short time horizon — specifically, the previous day — on stock forward returns by conducting a quintile backtest using Pipeline and Alphalens.</p><pre>from zipline.pipeline import Pipeline, brain<br>from zipline.pipeline.factors import AverageDollarVolume<br><br>avg_dollar_volume = AverageDollarVolume(window_length=90)<br><br>buzz_volume_sentiment =  brain.BSI.slice(1).BUZZ_VOLUME_SENTIMENT.latest<br><br>pipeline = Pipeline(<br>    columns={<br>        &quot;buzz_volume_sentiment&quot;: buzz_volume_sentiment,<br>    },<br>    screen=(<br>        avg_dollar_volume.top(1000)<br>        &amp; buzz_volume_sentiment.notnull()<br>    )<br>)</pre><p>By analyzing the buzz field over a 1-day horizon, we observed that stocks with the lowest buzz (quintile 1) on the previous day tend to outperform those with the highest buzz (quintile 5). This behavior could be explained by the idea that an anomalously high number of news articles (high buzz) published in the short term (the previous day) typically corresponds to bad news. Conversely, if the number of news articles is similar to the past average (low buzz), it indicates that the business is performing as usual.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/860/0*DSLiGq8XCKQp4EPE.png" /></figure><h3>3.3 Measures on Corporate Documents</h3><p>Recent literature highlights interesting inefficiencies in the market’s response to company filings due to the increased complexity and length of these reports (see, for example, “Lazy Prices” by Cohen et al., 2018, and “The Positive Similarity of Company Filings and the Cross-Section of Stock Returns” by M. Padysak, 2020).</p><p>One main idea presented in “Lazy Prices” is that authors of company filings tend to retain much of the structure and content in their newest filings when the underlying business is strong and growing. Conversely, when the business exhibits weakness, the author needs to provide more explanations, often resulting in textual amendments and additions.</p><p>To systematically exploit the information available in 10-K and 10-Q reports, one can sort reports using similarity metrics compared to the previous year’s report for the same period. Additionally, measuring various language characteristics, such as sentiment, the number of positive/negative words, or the prevalence of specific language styles (e.g., “litigious,” “constraining”), can provide insights. Other language characteristics, like readability scores or lexical richness, can also be informative.</p><p>These measures can be used either in absolute terms or by examining changes compared to previous reports. For example, one could analyze differences in language styles and systematically go long or short on stocks based on these metrics, such as avoiding stocks with an increase in litigious language.</p><p>To reduce noise and focus on text dense with relevant information, metrics can also be calculated on specific sections, such as the “Management’s Discussion and Analysis of Financial Condition and Results of Operations” (MD&amp;A) or “Risk Factors” sections.</p><h3>3.4 Use case study based on measures on corporate documents</h3><p>In this use case, we explore the impact of metrics related to the amount of constraining language present in the latest 10-K report.</p><pre>avg_dollar_volume = AverageDollarVolume(window_length=90)<br><br>constraining_language =  brain.BLMCF.slice(&quot;10-K&quot;).SCORE_CONSTRAINING.latest<br><br>pipeline = Pipeline(<br>    columns={<br>        &quot;constraining_language&quot;: constraining_language,<br>    },<br>    screen=(<br>        avg_dollar_volume.top(1000)<br>        &amp; constraining_language.notnull()<br>    )<br>)</pre><p>A quintile backtest performed using Alphalens shows that the bottom quintile (quintile 1), representing reports with the least constraining language, outperforms those with the most constraining language (quintile 5).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/860/0*wrce2Kd8lxOf-QBj.png" /></figure><p>Furthermore, the five quintiles exhibit a clear, ordered performance that aligns with their ranking.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/860/0*0np6OF59-KmGUmJs.png" /></figure><h3>3.5 Measures on Earnings Calls Transcripts</h3><p>The same analyses and metrics used for company filings (Section 3.2) can also be applied to Earnings Calls Transcripts (ECT). ECTs are rich in relevant information regarding a company’s performance, outlook, and potential risks. Unlike SEC forms, which are primarily regulatory and often emphasize risks and threats, ECTs provide a more balanced discussion of results, business opportunities, and risks due to their more discursive nature.</p><p>For ECTs, distinguishing between changers and non-changers may be less relevant than other systematic patterns or investment ideas, which could involve the relative “sign” of the differences or the absolute values of certain features.</p><h3>3.6 Use case study based on measures on earnings calls transcripts</h3><p>By sorting stocks based on the sentiment expressed in the management discussion section of their last earnings call, we discovered that stocks with higher sentiment tend to outperform those with lower sentiment, though the effect is not very large and not persistent in all subintervals. This finding is illustrated in the following use case, which utilizes the Alphalens package.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/860/0*hsxEc-b3PWii5d44.png" /></figure><h3>4. Final Remarks</h3><p>We have presented various approaches to extracting information from textual sources such as news articles, SEC regulatory filings, and earnings calls, transforming this information into usable structured datasets. In particular, we discussed several proprietary Brain datasets based on these sources, along with case studies implemented within the QuantRocket platform.</p><h3>About QuantRocket</h3><p>QuantRocket is a Python-based platform for researching, backtesting, and trading quantitative strategies. It provides a JupyterLab environment, offers a suite of data integrations, and supports multiple backtesters: Zipline, the open-source backtester that originally powered Quantopian; Alphalens, an alpha factor analysis library; Moonshot, a vectorized backtester based on pandas; and MoonshotML, a walk-forward machine learning backtester. Built on Docker, QuantRocket can be deployed locally or to the cloud and has an open architecture that is flexible and extensible.</p><p><a href="https://www.quantrocket.com/">Learn more</a> or <a href="https://www.quantrocket.com/installation/">install QuantRocket now</a> to get started.</p><h3>About Brain</h3><p><a href="https://braincompany.co/">Brain</a> is a research-focused company that develops proprietary signals based on alternative data and algorithms for investment strategies on financial markets.</p><p>The Brain team combines extensive experience on financial markets with strong competences in Statistics, Machine Learning and Natural Language Processing.</p><p><em>Originally published at </em><a href="https://www.quantrocket.com/blog/brain-alternative-data/"><em>https://www.quantrocket.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1c600c2e0ae0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Is There Alpha in Borrow Fees?]]></title>
            <link>https://quantrocket.medium.com/is-there-alpha-in-borrow-fees-8c15f4e7d848?source=rss-6bc03eef1004------2</link>
            <guid isPermaLink="false">https://medium.com/p/8c15f4e7d848</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[short-selling]]></category>
            <category><![CDATA[algorithmic-trading]]></category>
            <category><![CDATA[quantitative-finance]]></category>
            <dc:creator><![CDATA[QuantRocket]]></dc:creator>
            <pubDate>Thu, 16 May 2024 16:03:09 GMT</pubDate>
            <atom:updated>2024-05-16T16:03:09.189Z</atom:updated>
            <content:encoded><![CDATA[<p>Borrow fees reflect how likely short sellers think a stock is to decline. Can this information be incorporated into trading strategies as an alpha factor? This article uses Alphalens to explore the relationship between borrow fees and forward returns and uses Moonshot and Zipline to demonstrate ways to incorporate borrow fees into long or short strategies.</p><h3>Summary</h3><ul><li>High borrow fees are an indication that many short sellers think a stock will decline.</li><li>Short sellers are right: stocks with high borrow fees tend to decline.</li><li>High borrow fees largely offset the profits from shorting high borrow fee stocks, limiting the alpha potential of short strategies.</li><li>Long investors can improve returns by avoiding stocks with high borrow fees.</li></ul><h3>What are Borrow Fees?</h3><p>Borrow fees, also known as stock loan fees, are the fees short sellers pay to stock owners to borrow shares they wish to sell short. The payment is typically split between the stock owner whose shares are loaned out and the broker who facilitates the loan.</p><p>The amount that short sellers pay is a variable percentage of the dollar value of the loaned shares. Borrow fees are quoted as annualized interest rates but are assessed daily. For example, a short seller who borrows $100,000 worth of shares at an annualized interest rate of 1% will be assessed a daily fee of 1/360th of 1 percent of $100,000, or $2.78 per day.</p><p>Fees can range widely, from as low as 0.25% (25 basis points) per year for easy-to-borrow stocks up to 100% or more for hard-to-borrow stocks. The fees are determined by supply and demand: the more traders want to short a stock, the higher the fee. Consequently, borrow fees are a useful indicator of the collective sentiment of short sellers about any particular stock.</p><p>QuantRocket maintains an archive of historical borrow fees for US and global stocks dating back to 2018. The data are sourced from Interactive Brokers and are updated daily.</p><h3>Alphalens Analysis</h3><p>Short sellers are typically considered to be informed market participants, aka smart money. If high borrow fees mean that short sellers think that a stock is overvalued or a company is in trouble, can we use that information as a source of alpha?</p><p>To find out, I first turn to Pipeline and Alphalens to analyze the relationship between borrow fees and forward returns for US stocks. I begin by creating a Pipeline with two columns: daily borrow fees for all common stocks, and dollar volume quantiles (as a proxy for size). I exclude microcaps, defined here as the bottom 25% by dollar volume, due to their atypical characteristics that can obscure underlying trends.</p><pre>from zipline.pipeline import Pipeline, ibkr, master<br>from zipline.pipeline.factors import AverageDollarVolume<br><br># limit analysis to common stocks<br>universe = master.SecuritiesMaster.usstock_SecurityType2.latest.eq(&quot;Common Stock&quot;)<br><br>borrow_fee = ibkr.BorrowFees.FeeRate.latest<br><br>avg_dollar_volume = AverageDollarVolume(window_length=90)<br><br>pipeline = Pipeline(<br>    columns={<br>        &quot;borrow_fee&quot;: borrow_fee,<br>        &quot;size&quot;: avg_dollar_volume.quantiles(4)<br>    },<br>    initial_universe=universe,<br>    screen=(<br>        # ignore microcaps<br>        avg_dollar_volume.percentile_between(26, 100)<br>    )<br>)</pre><p>I pass the Pipeline to Alphalens, partitioning stocks at the 50th, 75th, and 90th percentiles by borrow fee:</p><pre><br>al.from_pipeline(<br>    pipeline,<br>    start_date=&quot;2018-04-16&quot;, # this is the start date of the borrow fees data<br>    end_date=&quot;2024-04-30&quot;,<br>    factor=&quot;borrow_fee&quot;,<br>    groupby=&quot;size&quot;,<br>    quantiles=[0, 0.5, 0.75, 0.9, 1], # split at 50th, 75th, and 90th percentile<br>    relative_returns=False, # Alphalens shows relative and absolute returns by default, let&#39;s ignore relative<br>    segment=&quot;Y&quot;<br>)</pre><h3>Results</h3><p>Stocks with high borrow fees perform worse than stocks with low borrow fees. In the bar plot, we see that while most US stocks had positive returns since 2018, the decile of stocks with the highest borrow fees (quantile 4) had negative returns. The line plot below the bar plot shows the cumulative performance of a factor-weighted portfolio that weights positions proportionally to their borrow fees. A portfolio concentrated in high borrow fee stocks would have steadily declined since 2018. It appears that short sellers are skillful at identifying stocks that are likely to underperform.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/853/0*bcWsX1A_soc_eOqc.png" /></figure><p>The following subplots analyze the relationship between borrow fees and forward returns by company size (using dollar volume as a proxy for company size). The leftmost plot analyzes smallcaps (group 1), the middle plot analyzes midcaps (group 2), and the rightmost plot analyzes largecaps (group 3). In each plot, stocks with the highest borrow fees (borrow fee quantile 4, blue line) perform worst.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*GV-fTWxM87eJb4xK.png" /></figure><h3>Should We Short Stocks with High Borrow Fees?</h3><p>Given that stocks with high borrow fees tend to decline, can we make money by shorting a portfolio of high borrow fee stocks? We will benefit from the stocks’ declines but will have to pay high fees to borrow them. Which effect will dominate?</p><p>To find out, I use Moonshot to backtest a strategy that shorts stocks in the highest borrow fee decile, and I use Moonshot’s built-in IBKRBorrowFees class to debit borrowing costs. Highlights of the Moonshot strategy are shown below:</p><pre>from quantrocket.fundamental import get_ibkr_borrow_fees_reindexed_like<br>from quantrocket.master import get_securities_reindexed_like<br><br>class HighBorrowShort(Moonshot):<br><br>    ...<br><br>    SLIPPAGE_CLASSES = IBKRBorrowFees<br><br>    def prices_to_signals(self, prices: pd.DataFrame):<br><br>        ...<br><br>        # rank by borrow fee and short the 10% with the highest fees<br>        borrow_fees = get_ibkr_borrow_fees_reindexed_like(closes)<br>        borrow_fee_ranks = borrow_fees.rank(axis=1, ascending=False, pct=True)<br>        short_signals = borrow_fee_ranks &lt;= 0.10<br><br>        return -short_signals.astype(int)</pre><p>The cumulative return plot reveals that the strategy is profitable on a gross basis (green line), but the high borrow fees (red line) eat up the profits and result in a roughly flat net performance (blue line). It seems that while short sellers can accurately predict which stocks will decline, they have difficulty profiting from their knowledge due to competition from other short sellers who drive up borrow costs. This is a good example of the limits to arbitrage.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*sJ_oNDp7IDJ1Z-pc.png" /></figure><h3>Using Borrow Fees as a Filter in Long Strategies</h3><p>Although borrowing costs make it difficult for short sellers to turn their insights into profits, long investors have it easier. Since they don’t incur borrowing costs, they can benefit from the insights of short sellers by avoiding owning stocks with high borrow fees. In this way, short sellers function as research analysts for long portfolio managers, advising them of stocks to exclude from their portfolios.</p><p>Using Zipline, I create a simple long-only strategy that buys an equal-weighted portfolio of midcap stocks (defined here as stocks in the 50th-75th percentile by dollar volume) excluding stocks with borrow fees above 0.3%:</p><pre>MAX_BORROW_FEE = 0.3<br><br>def make_pipeline():<br><br>    ...<br>    screen = AverageDollarVolume(window_length=90).percentile_between(50, 75)<br><br>    if MAX_BORROW_FEE:<br>        screen &amp;= (ibkr.BorrowFees.FeeRate.latest &lt;= MAX_BORROW_FEE)<br><br>    ...</pre><p>I run a parameter scan to test the strategy with and without the borrow fee filter. The portfolio that excludes high borrow fee stocks (green line) outperforms the portfolio that does not exclude them (blue line).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*H-DHlxilwmwuL1tl.png" /></figure><h3>Conclusion</h3><p>Short sellers are skilled at identifying stocks that are likely to decline, as evidenced by the negative relationship between borrow fees and forward returns. However, when short sellers attempt to profit from their predictions by shorting these stocks, they drive up borrow fees, which eats up their profit. Long portfolio managers are best positioned to benefit from the insights of short sellers by avoiding stocks with high borrow fees.</p><h3>Explore this research on your own</h3><p>This research was created with QuantRocket. Clone the <a href="https://www.quantrocket.com/code/?repo=borrow-fees-alpha">borrow-fees-alpha</a> repository to get the code and perform your own analysis.</p><p>quantrocket codeload clone &#39;borrow-fees-alpha&#39;</p><h3>About QuantRocket</h3><p>QuantRocket is a Python-based platform for researching, backtesting, and trading quantitative strategies. It provides a JupyterLab environment, offers a suite of data integrations, and supports multiple backtesters: Zipline, the open-source backtester that originally powered Quantopian; Alphalens, an alpha factor analysis library; Moonshot, a vectorized backtester based on pandas; and MoonshotML, a walk-forward machine learning backtester. Built on Docker, QuantRocket can be deployed locally or to the cloud and has an open architecture that is flexible and extensible.</p><p><a href="https://www.quantrocket.com/">Learn more</a> or <a href="https://www.quantrocket.com/installation/">install QuantRocket now</a> to get started.</p><p><em>Originally published at </em><a href="https://www.quantrocket.com/blog/borrow-fees-alpha/"><em>https://www.quantrocket.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8c15f4e7d848" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Cloud or Local: Where to Run Your Quant Trading?]]></title>
            <link>https://quantrocket.medium.com/cloud-or-local-where-to-run-your-quant-trading-a0ad267d3644?source=rss-6bc03eef1004------2</link>
            <guid isPermaLink="false">https://medium.com/p/a0ad267d3644</guid>
            <category><![CDATA[cloud-computing]]></category>
            <category><![CDATA[algorithmic-trading]]></category>
            <category><![CDATA[quantitative-finance]]></category>
            <dc:creator><![CDATA[QuantRocket]]></dc:creator>
            <pubDate>Thu, 30 Nov 2023 14:43:15 GMT</pubDate>
            <atom:updated>2023-11-30T14:43:15.463Z</atom:updated>
            <content:encoded><![CDATA[<p>Is it better to run your quant trading in the cloud or locally? In this article, I outline the pros and cons of each approach and explain why running locally is often better for research while running in the cloud is better for live trading.</p><h4>Don’t assume the cloud is better</h4><p>It’s common to imagine that every serious workload should run in the cloud. We associate the cloud with modern, forward-looking companies and products. However, quants should analyze their use case and not make assumptions. Many commonly cited benefits of the cloud apply more to medium and large companies than to quant researchers working alone or in small teams. Articles touting the benefits of the cloud usually position the cloud as an alternative to an in-house data center. But for most quant researchers, the more applicable contrast is between the cloud and a local workstation.</p><h4>The cloud is more expensive and less powerful than your local workstation</h4><p>Companies often move to the cloud to save money, as they can rent compute resources for cheaper than they can pay in-house IT staff to manage an on-premise data center. But for a quant, the cloud is usually the more expensive option. Most quants already own a powerful workstation or laptop which is well-suited for quantitative research. The cloud introduces an additional expense.</p><p>Even if your workstation needs upgrading, buying a new workstation is usually a better deal than renting the equivalent machine in the cloud. A powerful workstation costs a few thousand dollars, while renting a comparably powerful cloud instance costs a few hundred dollars a month. This means that renting will become more expensive than buying within less than a year. And you can use the purchased workstation for multiple purposes, not just quant research.</p><p>If you run in the cloud, the high cost may lead you to skimp on resources and choose a less powerful machine than your local workstation, which will lessen the cost differential but slow down your research. Choosing a machine with ample CPU and memory is one of the most important things you can do to ensure a productive and enjoyable research experience.</p><p>One scenario where the cloud is cheaper than running locally is when you need specialized compute resources for a temporary workload, like training a machine learning model that requires more GPU than your local workstation offers. In this case, the temporary nature of the workload makes renting cheaper than buying.</p><h4>The cloud provides an isolated environment, but so can Docker</h4><p>Historically, an advantage of the cloud over a local workstation was that it provides an isolated environment that you can customize with the exact packages you need for your research. If you mess up the environment, you can destroy the cloud instance and start over with a fresh instance. In contrast, since you use your local workstation for multiple purposes, you can’t as easily or safely wipe out your environment and start over.</p><p>This advantage of the cloud is less relevant today because of Docker. With Docker, you can create a custom environment for your research that runs on your local workstation yet is isolated from the rest of your operating system. You can destroy the environment and start over just as you would with a cloud instance. In fact, Docker is more flexible than the cloud alone because Docker allows you to deploy the same application to the cloud or to your local workstation without any changes to the application. This makes it easy to migrate back and forth between your workstation and the cloud or to deploy to both environments. Docker is the technology <a href="https://www.quantrocket.com/">QuantRocket</a> uses to support both local and cloud deployments.</p><h4>The cloud is more reliable than your home office</h4><p>Cloud providers have redundant power supplies and internet connections, as well as teams of engineers on call to fix problems. This is an area where the cloud handily beats a local workstation running in your home office. Unless you have a long-running generator and a backup internet connection, running locally makes you more vulnerable to outages than running in the cloud.</p><p>However, the superior uptime of the cloud is only relevant to live trading and other unattended processes. It doesn’t help with research or anything interactive: a local outage that takes down your workstation will also take down your ability to access the cloud, disrupting your activity either way.</p><h4>Can you use a local machine for live trading?</h4><p>Using a local workstation for live trading is possible if your trading strategy trades infrequently enough that you can recover from temporary outages. For example, a strategy that trades once a day after the close can recover from a temporary outage because you can simply re-run the trades when power is restored. In contrast, outages are more disruptive for a strategy that trades every minute as they will cause you to miss time-sensitive opportunities.</p><h4>Use a different machine for research and live trading</h4><p>Whether you run locally or in the cloud, use a dedicated machine for live trading, not the same machine you use for research or other purposes. Your own research activities, if performed on the same computer as your live trading, are much more likely to crash your live trading than an occasional power outage or lost internet connection. Every time you load a large dataset into memory or run a CPU-intensive analysis, you will jeopardize the ability of your live trading to run smoothly.</p><h4>Conclusion</h4><p>Quants deciding where to run their infrastructure should weigh the cost and performance benefits of running locally against the reliability benefits of running in the cloud. A basic rule of thumb is to run your research locally and run your live trading in the cloud.</p><h4>About QuantRocket</h4><p>QuantRocket is a Python-based platform for researching, backtesting, and trading quantitative strategies. It provides a JupyterLab environment, offers a suite of data integrations, and supports multiple backtesters: Zipline, the open-source backtester that originally powered Quantopian; Alphalens, an alpha factor analysis library; Moonshot, a vectorized backtester based on pandas; and MoonshotML, a walk-forward machine learning backtester. Built on Docker, QuantRocket can be deployed locally or to the cloud and has an open architecture that is flexible and extensible.</p><p><a href="https://www.quantrocket.com/">Learn more</a> or <a href="https://www.quantrocket.com/installation/">install QuantRocket now</a> to get started.</p><p><em>Originally published at </em><a href="https://www.quantrocket.com/blog/cloud-vs-local/"><em>https://www.quantrocket.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a0ad267d3644" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Market Cap vs Dollar Volume: Which to Use for Universe Selection?]]></title>
            <link>https://quantrocket.medium.com/market-cap-vs-dollar-volume-which-to-use-for-universe-selection-f9260731daaa?source=rss-6bc03eef1004------2</link>
            <guid isPermaLink="false">https://medium.com/p/f9260731daaa</guid>
            <category><![CDATA[quantitative-finance]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[algorithmic-trading]]></category>
            <dc:creator><![CDATA[QuantRocket]]></dc:creator>
            <pubDate>Fri, 20 Oct 2023 14:16:22 GMT</pubDate>
            <atom:updated>2023-10-20T14:16:22.234Z</atom:updated>
            <content:encoded><![CDATA[<p>Market cap and dollar volume are two commonly used metrics for filtering a trading universe by size of security. Does it matter which one you use? In this post, I quantify the difference between market cap and dollar volume and explain the kinds of stocks that may unexpectedly appear in your universe with each metric.</p><h3>Overview of market cap and dollar volume</h3><p>Market cap and dollar volume are both proxies for size but are conceptually different. Market cap is a <em>valuation measure</em> that reflects how much the market thinks a company is worth. It is calculated by multiplying the share price by the number of shares outstanding. Dollar volume is a <em>liquidity measure</em> that reflects how easy it is to trade in and out of a stock. It is calculated by multiplying the share price by the volume of shares traded over a given period. Market cap tells us something about the company — how valuable it is — while dollar volume tells us something about the stock — how active it is.</p><p>The choice to use market cap or dollar volume is often dictated by the available data. Market cap is typically provided with fundamental data, because the number of outstanding shares is reported in a company’s financial statements. If you don’t have fundamental data, it’s an easy choice to use dollar volume, which can be calculated from price and volume alone.</p><p>If you have both price and fundamental data, the choice might depend on what type of strategy you’re researching. If you’re a long-term investor using fundamental data to identify companies with certain qualities, market cap is a natural choice since it tells you something about the company. If you’re a trader targeting short-term moves in a security, liquidity may be paramount, making dollar volume a natural choice.</p><p>For the most part, we expect market cap and dollar volume to be correlated: a company like Apple has a large market cap and is highly liquid, while we expect a tiny startup to have a low market cap and be illiquid. To visualize this correlation, I use the Pipeline API to rank all US stocks (excluding ETFs) by market cap and dollar volume from smallest to largest and make a scatter plot of the results. The red regression line represents perfect correlation, where a stock’s dollar volume rank exactly equals its market cap rank.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/626/0*ifanQdIpxz9IKlMh.png" /></figure><p>While there is clearly a strong correlation between market cap and dollar volume — most points cluster along the red line — there are also many points that are far from the red regression line, indicating securities that rank much lower on one metric than the other.</p><h3>Large cap stocks vs high dollar volume stocks</h3><p>In the next two plots, I focus on the largest and most liquid stocks. In the left-hand plot, I show how the top market cap decile is distributed across dollar volume deciles, and in the right-hand plot, I show how the top dollar volume decile is distributed across market cap deciles. In other words, in the left-hand plot, we are asking how liquid the largest companies are, and in the right-hand plot, we are asking how large the most liquid companies are.</p><p>The top market cap decile and the top dollar volume decile share about 75% common membership. These are stocks like Apple — liquid and large. Another 15% of the top market cap decile comes from the second largest dollar volume decile (decile 9) — stocks that are still quite liquid. But the remaining 10% of large cap stocks come from lower dollar volume deciles, as low as dollar volume decile 3. Similarly for the top dollar volume decile, about 20% of the stocks come from the second largest market cap decile (decile 9), but the remaining 5% of stocks come from market cap deciles as low as decile 4.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*dhNdxLMfHULplN_T.png" /></figure><h3>What’s in your universe?</h3><p>Next, I sort and filter my pipeline results to inspect some of these low decile stocks that are showing up in my high decile screens.</p><p>What kinds of stocks have high market cap but low dollar volume? Most of the stocks in this category are ADRs (American Depository Receipts) of large foreign companies. These companies’ stocks primarily trade in their home countries and are not as widely traded in the US. Investors who exclude ADRs in addition to filtering by market cap will remove most of the low dollar volume stocks from their large cap universe.</p><p>Conversely, what kinds of stocks have high dollar volume but low market cap? A sampling of names that currently surface from my pipeline screen are Tupperware (TUP), Nikola (NKLA), and Affirm (AFRM). These are all meme stocks popular with retail traders. The companies are struggling in various ways and thus have lower market caps, but they are heavily traded by retail traders and thus have high dollar volumes. Traders who filter their universe by dollar volume expecting to get S&amp;P-500-caliber stocks should be mindful of the presence of such securities.</p><h3>About QuantRocket</h3><p>QuantRocket is a Python-based platform for researching, backtesting, and trading quantitative strategies. It provides a JupyterLab environment, offers a suite of data integrations, and supports multiple backtesters: Zipline, the open-source backtester that originally powered Quantopian; Alphalens, an alpha factor analysis library; Moonshot, a vectorized backtester based on pandas; and MoonshotML, a walk-forward machine learning backtester. Built on Docker, QuantRocket can be deployed locally or to the cloud and has an open architecture that is flexible and extensible.</p><p><a href="https://www.quantrocket.com/">Learn more</a> or <a href="https://www.quantrocket.com/installation/">install QuantRocket now</a> to get started.</p><p><em>Originally published at </em><a href="https://www.quantrocket.com/blog/market-cap-vs-dollar-volume/"><em>https://www.quantrocket.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f9260731daaa" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Researching the Quality Factor with Alphalens and Zipline]]></title>
            <link>https://quantrocket.medium.com/researching-the-quality-factor-with-alphalens-and-zipline-4b39d98f3681?source=rss-6bc03eef1004------2</link>
            <guid isPermaLink="false">https://medium.com/p/4b39d98f3681</guid>
            <category><![CDATA[fundamental-analysis]]></category>
            <category><![CDATA[algorithmic-trading]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[quantitative-finance]]></category>
            <dc:creator><![CDATA[QuantRocket]]></dc:creator>
            <pubDate>Thu, 28 Sep 2023 13:55:45 GMT</pubDate>
            <atom:updated>2023-09-28T13:55:45.478Z</atom:updated>
            <content:encoded><![CDATA[<p>Buying high-quality stocks and avoiding low-quality ones can improve investment returns. In this post, I use Alphalens and Zipline to analyze the Piotroski F-Score, a composite measure of a firm’s financial health and quality.</p><blockquote>This post is part of the <a href="https://www.quantrocket.com/blog/topics/fundamental-factors/">fundamental factors</a> series, which explores techniques for researching fundamental factors using Pipeline, Alphalens, and Sharadar US fundamental data in QuantRocket.</blockquote><h3>What is the Piotroski F-Score?</h3><p>Joseph Piotroski, an accounting professor, developed the Piotroski F-Score in 2000 as a way to identify high-quality stocks. The Piotroski F-Score is often used by value investors to avoid the so-called value trap, that is, the trap of buying cheap stocks that are only cheap because they are in trouble. (This is how Piotroski used the F-Score in the original paper.) However, the F-Score can also be used as a standalone factor and is commonly referred to as the quality factor (although some practitioners define quality using alternative formulas).</p><p>The Piotroski F-Score is a composite factor comprising multiple sub-factors. It is calculated as the sum of 9 true/false criteria, resulting in a possible score of 0–9. Points are awarded as follows:</p><p><strong>Profitability Criteria</strong>:</p><ul><li>1 point if return on assets (ROA) is positive in the current year</li><li>1 point if operating cash flow is positive in the current year</li><li>1 point if ROA is higher in the current period than in the previous year</li><li>1 point if cash flow from operations is higher than ROA (quality of earnings)</li></ul><p><strong>Funding Criteria</strong>:</p><ul><li>1 point if long term debt is lower in the current period than in the previous year (decreased leverage)</li><li>1 point if current ratio is higher in the current period than in the previous year (more liquidity)</li><li>1 point if no new shares were issued in the last year (lack of dilution)</li></ul><p><strong>Operating Efficiency Criteria</strong>:</p><ul><li>1 point if gross margin is higher in the current period than in the previous year</li><li>1 point if asset turnover ratio is higher in the current period than in the previous year</li></ul><p>The three broad categories of the Piotroski F-Score criteria (profitability criteria, funding criteria, and operating efficiency criteria) correspond roughly to the themes previously explored in this series. We saw that profitability is good (see: <a href="https://www.quantrocket.com/blog/profitability-factor-alphalens/">Analyzing the Profitability Factor with Alphalens</a>), too much debt is bad (see: <a href="https://www.quantrocket.com/blog/financial-distress-factors/">Financial Distress Factors: the Altman Z-Score and Interest Coverage Ratio</a>), and improving operating efficiency is good (see: <a href="https://www.quantrocket.com/blog/profitability-growth/">What’s Better, High Profit Margins or Improving Profit Margins?</a>). The Piotroski F-Score combines multiple independently useful factors into a single score.</p><h3>The Piostroski F-Score in Pipeline</h3><p>Using the Piotroski F-Score in Pipeline is simple, as a built-in factor is provided:</p><pre>from zipline.pipeline import sharadar<br><br>f_score = sharadar.PiotroskiFScore()</pre><p>However, to demonstrate how multi-factor scores can be constructed using any criteria the researcher wants, let’s peek at a snippet of the source code to see how the score is computed. First, we compute the individual boolean criteria (e.g. return_on_assets &gt; 0), then we call as_factor() to convert the boolean output to 1s or 0s. The resulting numbers are then summed to produce the final F-Score:</p><pre>...<br># ART = as reported, trailing 12 months fundamentals<br>fundamentals = sharadar.Fundamentals.slice(&#39;ART&#39;)<br><br># get necessary fields<br>return_on_assets = fundamentals.ROA.latest<br>operating_cash_flow = fundamentals.NCFO.latest<br>...<br><br># compute score<br>f_scores = (<br>    (return_on_assets &gt; 0).as_factor()<br>    + (operating_cash_flow &gt; 0).as_factor()<br>    ...<br>)</pre><h3>Alphalens Analysis of the Piotroski F-Score</h3><p>I use Alphalens to analyze how the Piotroski F-Score impacts stock performance. I group stocks into 3 buckets based on their F-Scores: a bucket for stocks with low scores (0–3), another bucket for medium scores (4–6), and a third bucket for high scores (7–9). I include market cap as a grouping factor to see if the F-Score’s predictive efficacy differs for small caps vs large caps.</p><pre>from zipline.pipeline import Pipeline, sharadar<br>import alphalens as al<br><br>fundamentals = sharadar.Fundamentals.slice(&quot;ART&quot;)<br>marketcap = fundamentals.MARKETCAP.latest<br><br>f_scores = sharadar.PiotroskiFScore()<br><br>pipeline = Pipeline(<br>    columns={<br>        &quot;quality&quot;: f_scores,<br>        &quot;size&quot;: marketcap.quantiles(4)<br>    }<br>)<br><br>al.from_pipeline(<br>    pipeline,<br>    start_date=&quot;1999-02-01&quot;,<br>    end_date=&quot;2023-06-30&quot;,<br>    periods=[1, 5, 21],<br>    factor=&quot;quality&quot;,<br>    # 3 bins: 0-3, 4-6, 7-9<br>    bins=[-1,3,6,9],<br>    groupby=[<br>        &quot;size&quot;,<br>    ],<br>    segment=&quot;Y&quot;,<br>)</pre><h3>Results</h3><p>30% of stocks have low F-Scores (0–3), 55% have medium F-Scores (4–6), and 15% have high F-Scores (7–9). The following plot shows the cumulative return of each basket of stocks. The high-quality stocks (blue line) handily outperform the low-quality stocks (red line), largely as a result of experiencing smaller drawdowns during bear markets (including the most recent bear market in 2022):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/860/0*DUmeq6OxpRPWZa1Q.png" /></figure><h3>Market Cap Subgroup Analysis</h3><p>Next I look at the composition of the low- and high-quality buckets by market cap. The low-quality bucket (left-hand pie plot) is dominated by stocks in the smallest two quartiles (quartiles 0 and 1, shaded blue and light orange), while the high-quality bucket (right-hand pie plot) is dominated by stocks in the largest two quartiles (quartiles 2 and 3, shaded green and dark orange). The finding that low-quality stocks tend to be small caps while high-quality stocks tend to be large caps is intuitive: all companies start out small, and the higher-quality ones grow to become large companies while the lower-quality ones remain small.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/860/0*OhZxmuxTxk4ku5-f.png" /></figure><p>The next plot shows the cumulative return by F-Score bucket for each of the 4 market cap quartiles, where quartile 0 contains the smallest companies and quartile 3 contains the largest. Appealingly, the performance spread between high- and low-quality stocks is not limited to small cap stocks (quartile 0) but is robust among all quartiles, including large companies (quartile 3). This means that investors can adopt the quality factor in their investing strategies without having to deal with the challenges of trading small caps:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/860/0*pjngyRQbK_6M7GWa.png" /></figure><h3>Should we apply a value filter to a high-quality screen?</h3><p>Unlike valuation metrics such as the price-to-earnings ratio or enterprise multiple, the Piotroski F-Score does not take into account a stock’s current market price. A portfolio of high-quality stocks may include stocks that are expensive or cheap by valuation measures.</p><p>As mentioned above, Joseph Piotroski originally proposed the F-Score as a quality filter to be applied on top of a value screen. That is, an investor would select a basket of cheap stocks, then apply the F-Score to weed out the low-quality stocks within that subset.</p><p>In the following analysis, I proceed in the opposite order, applying value as a filter on top of a high-quality screen. To do so, I create a new pipeline that limits the analysis to high-quality stocks (F-Score &gt;= 7). Since the Piotroski F-Score works not only on small caps but on large caps, I also choose to limit the analysis to the top 50% of stocks by market cap. Within this universe of high-quality, large-cap stocks, I divide stocks into 4 buckets by their valuation (using enterprise multiple as my valuation ratio) and use Alphalens to see if cheap high-quality stocks outperform expensive high-quality stocks. The modified code looks like this:</p><pre>enterprise_multiple = fundamentals.EVEBITDA.latest<br><br>are_high_quality = f_scores &gt;= 7<br>are_large_caps = marketcap.percentile_between(50, 100)<br><br>screen = are_high_quality &amp; are_large_caps<br><br>pipeline = Pipeline(<br>    columns={<br>        &quot;value&quot;: enterprise_multiple,<br>    },<br>    screen=screen<br>)<br><br>al.from_pipeline(<br>    pipeline,<br>    start_date=&quot;1999-02-01&quot;,<br>    end_date=&quot;2023-06-30&quot;,<br>    periods=[1, 5, 21],<br>    factor=&quot;value&quot;,<br>    quantiles=4,<br>    segment=&quot;Y&quot;,<br>)</pre><p>To highlight the spread between quartiles, the following plot shows the relative cumulative return (not actual cumulative return) of each value quartile. The cheapest high-quality stocks are represented by quartile 1 (red line), while the most expensive (but still high quality) stocks are represented by quartile 4 (blue line). Cheap, high-quality stocks significantly outperformed other high-quality stocks from around 2000 to 2015, but then the relationship reversed from 2015 to early 2020. Since 2020, cheap, high-quality stocks have resumed outperforming:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/853/0*1oF1ZzPbXQ04ileu.png" /></figure><h3>Zipline Trading Strategy: High-Quality S&amp;P 500 Stocks</h3><p>A typical research workflow with Pipeline is to use Alphalens to learn how a factor behaves in different areas of the market, then use Zipline to apply the insights to a particular trading strategy. With Alphalens, we ignore the practical constraints of trading (e.g. liquidity, transaction costs, and preferred number of holdings) and focus on the factor’s performance. Once we know where alpha can be found, we use Zipline to craft a trading strategy that is practical to implement and deploy.</p><p>Along these lines, I use Zipline to backtest a simple trading strategy based on my Alphalens research. The strategy focuses on large-cap, high-quality stocks, purchasing an equal-weighted portfolio of S&amp;P 500 stocks with F-Scores &gt;= 7 and rebalancing monthly. The strategy code is simple enough to paste in its entirety:</p><pre>import zipline.api as algo<br>from zipline.pipeline import Pipeline, sharadar<br>from zipline.finance import commission<br><br>def initialize(context: algo.Context):<br><br>    algo.set_benchmark(algo.symbol(&#39;SPY&#39;))<br>    algo.set_commission(commission.PerShare(0.005))<br><br>    # select S&amp;P500 stocks with F Scores &gt;= 7<br>    screen = sharadar.SP500.in_sp500.latest<br>    f_scores = sharadar.PiotroskiFScore()<br>    screen &amp;= f_scores &gt;= 7<br><br>    pipe = Pipeline(screen=screen)<br>    algo.attach_pipeline(pipe, &#39;pipe&#39;)<br><br>    # rebalance monthly<br>    algo.schedule_function(<br>        rebalance,<br>        date_rule=algo.date_rules.month_start())<br><br>def rebalance(context: algo.Context, data: algo.BarData):<br><br>    desired_portfolio = algo.pipeline_output(&#39;pipe&#39;).index<br><br>    current_positions = context.portfolio.positions<br><br>    # open new positions and rebalance existing positions<br>    for asset in desired_portfolio:<br>        algo.order_target_percent(asset, 1 / len(desired_portfolio))<br><br>    # liquidate positions that no longer pass the screen<br>    for asset in current_positions:<br>        if asset not in desired_portfolio:<br>            algo.order_target_percent(asset, 0)</pre><p>The cumulative performance of the strategy vs the benchmark, SPY, is shown below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/919/0*5huzmMBrboo2TVZd.png" /></figure><p>The following plot shows that the total number of holdings generally varies between 100 and 200. Depending on the trader’s preferences, a possible next step might be to refine the strategy to reduce the number of holdings. One way to do this, in line with the above research, would be to buy only the cheapest high-quality stocks, rather than buying all high-quality stocks as was done here.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/898/0*-QuOEaxsRKvT0PJp.png" /></figure><p><em>Originally published at </em><a href="https://www.quantrocket.com/blog/piotroski-f-score/"><em>https://www.quantrocket.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4b39d98f3681" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Financial Distress Factors: the Altman Z-Score and Interest Coverage Ratio]]></title>
            <link>https://quantrocket.medium.com/financial-distress-factors-the-altman-z-score-and-interest-coverage-ratio-f768640f3c5?source=rss-6bc03eef1004------2</link>
            <guid isPermaLink="false">https://medium.com/p/f768640f3c5</guid>
            <category><![CDATA[quantitative-finance]]></category>
            <category><![CDATA[fundamental-analysis]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[algorithmic-trading]]></category>
            <dc:creator><![CDATA[QuantRocket]]></dc:creator>
            <pubDate>Thu, 07 Sep 2023 14:16:42 GMT</pubDate>
            <atom:updated>2023-09-07T14:16:42.180Z</atom:updated>
            <content:encoded><![CDATA[<p>Are rising interest rates straining balance sheets and increasing the risk of bankruptcies? This article investigates two financial distress factors, the Altman Z-Score and interest coverage ratio, to see if distress is on the rise and how it impacts stock returns.</p><blockquote>This post is part of the <a href="https://www.quantrocket.com/blog/topics/fundamental-factors/">fundamental factors</a> series, which explores techniques for researching fundamental factors using Pipeline, Alphalens, and Sharadar US fundamental data in QuantRocket.</blockquote><h3>Zombie Firms in the News</h3><p>Last fall, as the Federal Reserve aggressively raised interest rates, the Economist <a href="https://www.economist.com/business/2022/09/27/a-reckoning-has-begun-for-corporate-debt-monsters">published an article</a> about so-called “zombie firms,” defined as companies that don’t earn enough income to cover the interest expense on their debts. Zombie firms are not financially viable but continue to operate, often through additional borrowing. With interest rates rising, the article warned, the number of zombie firms could be expected to rise.</p><p>The article piqued my interest but lacked quantitative detail. I wondered precisely to what extent companies are struggling with greater interest costs and whether a trader or investor can capitalize on that knowledge, either by avoiding zombie firms in their portfolio or perhaps even shorting them.</p><p>Among the benefits of quantitative research is that it can confirm, qualify, or refute the ideas you read in the news. In contrast, discretionary investors have only their own cognitive biases with which to evaluate news articles. Such investors often embrace or reject ideas based on what happened last year (recency bias), or what happened in a memorable year like 1987 or 2008 (salience bias), or whether the idea supports their existing investment thesis (confirmation bias). Investors without quantitative tools risk committing capital to weak or sensationalized ideas that are not supported by the data.</p><h3>The Interest Coverage Ratio and the Altman Z-Score</h3><p>For perspective on the Economist article, I use Pipeline and Alphalens to analyze the interest coverage ratio, which is calculated by dividing a company’s earnings before interest and taxes (EBIT) by its interest expenses for the same period. A zombie firm is defined as a company with an interest coverage ratio (ICR) below 1, indicating that the company has less earnings than interest expenses.</p><p>To widen the analysis, I also include a second financial distress factor, the Altman Z-Score. This score measures a company’s likelihood of going bankrupt within two years. It is calculated as a linear combination of 5 weighted business ratios, with the weights determined by fitting a logistic regression model to a dataset of bankrupt and non-bankrupt companies. The Altman Z-score formula is:</p><p>Altman Z Score = 1.2A + 1.4B + 3.3C + 0.6D + 1.0E</p><p>Where:</p><ul><li>A = working capital / total assets</li><li>B = retained earnings / total assets</li><li>C = earnings before interest and tax / total assets</li><li>D = market value of equity / total liabilities</li><li>E = sales / total assets</li></ul><p>Altman Z-Scores are often interpreted as follows:</p><ul><li>Z-Score &gt;= 3: “Safe” Zone</li><li>0 &lt; Z-Score &lt; 3: “Grey” Zone</li><li>Z-Score &lt; 0: “Distress” Zone</li></ul><h3>Is Financial Distress on the Rise?</h3><p>First, I look at the prevalence of financial distress over time.</p><p>Pipeline contains built-in factors for the Altman Z-Score and the interest coverage ratio. In the following code, I define companies with an Altman Z-Score below 0 as “distressed” and companies with an ICR below 1 as “zombies.” However, I limit the zombie classification to companies with debt-to-equity ratios above the market-wide median, to ensure the company has enough debt to make the low ICR a burden.</p><pre>from zipline.pipeline import sharadar<br><br>altman = sharadar.AltmanZScore()<br>are_distressed = altman &lt; 0<br><br>icr = sharadar.InterestCoverageRatio()<br>de = sharadar.Fundamentals.slice(&quot;ART&quot;).DE.latest<br><br>are_zombies = (icr &lt; 1) &amp; (de &gt; de.median())</pre><p>I plot the median Altman Z-Score and median ICR over time. The median interest coverage ratio (blue line) declined throughout the 2010s from a high of 7 (earnings sufficient to cover interest expenses 7 times over) to a low of 4 shortly before the pandemic, then spiked during the pandemic (when interest rates dropped to near zero). Interest coverage ratios have declined since then but remain above pre-pandemic levels. Meanwhile, the median Altman Z-Score (green line) has generally hovered between 2 and 3 and is currently near the bottom of that range:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/923/0*UON7Mz5VptLMlUMv.png" /></figure><p>Arguably, the median values are less important than the number of companies that fall below the critical thresholds. Next I plot the percentage of firms that are zombies or Altman-distressed. The percentage of zombie firms (blue line) has been relatively stable over time and does not seem particularly elevated at present. This leads me to discount the Economist article somewhat. However, the percentage of distressed firms as measured by the Altman Z-Score (green line) has risen sharply since the end of 2021 and is currently at its highest level ever:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/941/0*fuqic4yFthhDkLNX.png" /></figure><h3>Impact on Stock Returns</h3><p>To see if financial distress leads to poor stock performance, I split stocks into two buckets: one bucket with companies that are zombies or Altman-distressed (or both), and a second bucket with everything else. I include market cap and volatility quartiles in the Pipeline to see how the size and volatility factors interact with the financial distress factor.</p><pre>from zipline.pipeline import Pipeline<br>from zipline.pipeline.factors import AnnualizedVolatility<br>import alphalens as al<br><br>pipeline = Pipeline(<br>    columns={<br>        &#39;distressed&#39;: are_distressed | are_zombies,<br>        &#39;size&#39;: marketcap.quantiles(4),<br>        &#39;volatility&#39;: AnnualizedVolatility().quantiles(4)<br>    }<br>)<br><br>al.from_pipeline(<br>    pipeline,<br>    start_date=&quot;1999-02-01&quot;,<br>    end_date=&quot;2023-06-30&quot;,<br>    periods=[1, 5, 21],<br>    factor=&quot;distressed&quot;,<br>    bins=[-1, 0, 1],<br>    groupby=[<br>        &quot;size&quot;,<br>        &quot;volatility&quot;,<br>    ],<br>    segment=&quot;Y&quot;<br>)</pre><h3>Results</h3><p>In the following plot, Altman-distressed and zombie companies are represented by quantile 2 (the blue line) while non-distressed companies are in quantile 1 (the red line). The financially distressed bucket is more volatile and underperforms over time, albeit with occasional spikes higher during speculative periods like the dotcom bubble:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/853/0*8j1Uwei7uRYcJyHT.png" /></figure><h3>Market Cap Subgroup Analysis</h3><p>The next set of plots breaks down performance by market cap quartile (where quartile 0 contains the smallest companies and quartile 3 contains the largest). The lagging performance of financially distressed stocks (blue lines) is consistent across market cap quartiles.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/863/0*nS-XPnFSL26zxFLy.png" /></figure><h3>Volatility Subgroup Analysis</h3><p>Next I look at results by volatility quartile. The left-hand pie plot shows the composition of the non-distressed bucket by volatility quartile, while the right-hand pie plot shows the composition of the distressed bucket by volatility quartile. As the right-hand plot shows, distress and volatility go hand-in-hand, with the most volatile stocks (volatility quartile 3, shaded in dark orange) making up 63% of the distressed bucket.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/860/0*tlBecp2JVVKL28Jl.png" /></figure><p>The next set of plots show the cumulative return of distressed firms (blue lines) and non-distressed firms (red lines) by volatility quartile. In volatility quartile 3 (bottom right), distressed firms underperform non-distressed firms, but the difference is small (and volatile). Combined with the right-hand pie plot above, a reasonable conclusion would be that most distressed firms are highly volatile and behave similarly to other highly volatile stocks.</p><p>In contrast, the plots for the 2 lowest volatility quartiles (quartiles 0 and 1, top left and top right) show a wider and more stable spread between distressed and non-distressed firms, suggesting that low volatility stocks are a more promising subgroup for exploiting financial distress factors. A distressed stock with low volatility seems counterintuitive (as we associate distress with volatility), but counterintuitive areas of the market are often fruitful areas for research as fewer traders are looking there.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/860/0*s0tQtDuK4Q3LNb6W.png" /></figure><p><em>Originally published at </em><a href="https://www.quantrocket.com/blog/financial-distress-factors/"><em>https://www.quantrocket.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f768640f3c5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Sector Neutralization: Why It Matters and How to Use It]]></title>
            <link>https://quantrocket.medium.com/sector-neutralization-why-it-matters-and-how-to-use-it-d022fac3c35d?source=rss-6bc03eef1004------2</link>
            <guid isPermaLink="false">https://medium.com/p/d022fac3c35d</guid>
            <category><![CDATA[algorithmic-trading]]></category>
            <category><![CDATA[quantitative-finance]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[fundamental-analysis]]></category>
            <dc:creator><![CDATA[QuantRocket]]></dc:creator>
            <pubDate>Tue, 22 Aug 2023 13:17:34 GMT</pubDate>
            <atom:updated>2023-08-22T13:17:34.374Z</atom:updated>
            <content:encoded><![CDATA[<p>Sector neutralization is a technique to hedge out sector bets and reduce the impact of sector-specific risks on the portfolio by ranking factors within sectors rather than across sectors. This post uses the debt-to-equity ratio to show why sector neutralization is important and how to perform it in Pipeline.</p><blockquote>This post is part of the <a href="https://www.quantrocket.com/blog/topics/fundamental-factors/">fundamental factors</a> series, which explores techniques for researching fundamental factors using Pipeline, Alphalens, and Sharadar US fundamental data in QuantRocket.</blockquote><h3>Debt-to-Equity Ratio: Sector Differences</h3><p>Many fundamental metrics naturally vary across sectors and industries. These variations don’t reflect better or worse companies but simply reflect the different ways different industries operate. Sector neutralization can be useful with these kinds of metrics.</p><p>Below, I plot the median debt-to-equity ratio by sector. The median D/E ratio for the financial sector is much higher than for other sectors. Consider that banks use debt (money borrowed from depositors) to earn income by making loans, so debt-to-equity ratios are high. Meanwhile, the median D/E ratio for the healthcare sector is considerably lower than for other sectors. This is largely driven by biotech companies, which due to their speculative nature tend to be financed by equity rather than debt.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*-GTJ29OdqMmxAdKW.png" /></figure><h3>Concentration Risk</h3><p>If we form a portfolio by ranking stocks on D/E ratio, the financial sector will be over-represented at one end of the portfolio and the healthcare sector will be over-represented at the other end. We can visualize this concentration risk with Pipeline.</p><p>Below, I rank stocks by D/E ratio and form quintiles from the ranks, including sector in the pipeline output:</p><pre>from zipline.pipeline import master, sharadar, Pipeline<br>from zipline.research import run_pipeline<br><br>fundamentals = sharadar.Fundamentals.slice(&#39;ART&#39;)<br>de = fundamentals.DE.latest<br>sector = master.SecuritiesMaster.sharadar_Sector.latest<br><br>pipeline = Pipeline(<br>    columns={<br>        &#39;quintiles&#39;: de.rank().quintiles(),<br>        &#39;sector&#39;: sector,<br>    }<br>)<br><br>results = run_pipeline(pipeline, &#39;2022-12-30&#39;, &#39;2022-12-30&#39;)</pre><p>To establish a baseline, I first plot the relative size of different sectors by counting the number of securities in each sector. We can see that Financial Services, Healthcare, and Technology are the largest sectors by number of securities:</p><pre>securities_by_sector = results.groupby(&#39;sector&#39;).quintiles.count()<br>securities_by_sector.plot(kind=&quot;pie&quot;, title=&quot;Number of securities by sector&quot;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/496/0*KBWb7ZUSDjU-Kluj.png" /></figure><p>Next, I plot the sector breakdown of the lowest and highest quintiles by D/E ratio:</p><pre># the quintile labels are zero-indexed, meaning quintile 0 contains stocks with<br># the lowest D/E ratios, and quintile 4 contains stocks with the highest D/E ratios<br>lowest_quintile_by_sector = results[results.quintiles == 0].groupby(&#39;sector&#39;).quintiles.count()<br>highest_quintile_by_sector = results[results.quintiles == 4].groupby(&#39;sector&#39;).quintiles.count()<br><br>lowest_quintile_by_sector.plot(kind=&quot;pie&quot;, title=&quot;Number of securities in lowest DE quintile by sector&quot;)<br>highest_quintile_by_sector.plot(kind=&quot;pie&quot;, title=&quot;Number of securities in highest DE quintile by sector&quot;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/862/0*RNitPXNQCwY5rMWS.png" /></figure><p>As expected, healthcare stocks dominate the low D/E quintile, and financial stocks dominate the high D/E quintile. Consequently, a long-short portfolio formed using the D/E ratio would largely be just a bet on healthcare vs financials.</p><h3>Sector-Neutral Ranking</h3><p>To avoid sector concentration, we can incorporate the sector into the ranking process by using the groupby parameter of rank(). Normally, the rank() method ranks all securities from <em>1 ⭢ N</em>, where <em>N</em> is the number of securities in the universe. In contrast, rank(groupby=sector) ranks securities from <em>1 ⭢ N</em> within each sector, where <em>N</em> is the number of securities in the sector. In the resulting output, if there are <em>Y</em> sectors, there will be <em>Y</em> stocks ranked 1, <em>Y</em> stocks ranked 2, etc. Ranking by sector will allow us to form a portfolio in which each sector is equally represented.</p><p>The following pipeline expression ranks by D/E ratio within sectors, then forms quintiles from the resulting ranks:</p><pre>neutral_quintiles = de.rank(groupby=sector).quintiles()</pre><p>I plot the sector breakdown of the low D/E quintile in the portfolio formed without sector neutralization (on the left) and in the portfolio formed with sector neutralization (on the right). Sector neutralization has eliminated the over-weighting of healthcare and balanced the sectors:</p><pre>lowest_quintile_by_sector_neutralized = results[results.neutral_quintiles == 0].groupby(&#39;sector&#39;).neutral_quintiles.count()<br><br>lowest_quintile_by_sector.plot(kind=&quot;pie&quot;, title=&quot;Number of securities in lowest DE quintile by sector (not neutralized)&quot;)<br>lowest_quintile_by_sector_neutralized.plot(kind=&quot;pie&quot;, title=&quot;Number of securities in lowest DE quintile by sector (neutralized)&quot;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/908/0*_kWDunqdIbyuouOc.png" /></figure><h3>Demeaning by Sector</h3><p>Ranking factors within sectors is not the only way to neutralize sectors. Another approach is to demean factors within each sector. With demeaning, we calculate the mean D/E ratio for each sector and subtract it from the observed values. Accordingly, the resulting values are centered around zero for each sector, which allows us to better compare values across sectors. If we rank stocks on their demeaned D/E ratios, we are ranking them not on their absolute D/E ratio but on their D/E ratio relative to the sector average.</p><p>The following pipeline expression forms quintiles from sector-demeaned D/E ratios:</p><pre>demeaned_quintiles = de.demean(groupby=sector).quintiles()</pre><h3>Z-Scoring by Sector</h3><p>Alternatively, we can z-score the D/E ratios, which is like demeaning but includes the additional step of dividing the demeaned values by the standard deviation of D/E ratios for the sector. Whereas demeaning neutralizes the effect of one sector having generally higher or lower D/E ratios than another sector, z-scoring additionally removes the effect of one sector having a wider variation of D/E ratios than another sector.</p><p>To z-score in Pipeline, just use zscore() instead of demean():</p><pre>zscored_quintiles = de.zscore(groupby=sector).quintiles()</pre><p>Note that, unlike the ranking approach shown above, demeaning and z-scoring don’t guarantee that your quantiles will be equally weighted among all sectors. It is still possible that the best or worst D/E ratios will cluster more in one sector than another, even after adjusting for sector differences. Moreover, since some sectors are larger than others, we should naturally expect that those larger sectors will make up a larger portion of any given quantile, on average.</p><h3>Should you always neutralize sector exposure?</h3><p>For illustration, I intentionally chose a factor for this article that varies significantly across sectors. Many factors vary less, making sector neutralization less important. Even for factors with significant sector differences, sometimes gaining factor exposure may be more important than being sector neutral. For example, if targeting a growth factor leads you to be overweight technology, you may consider that acceptable. At the very least, however, it’s a good idea to understand your sector exposures.</p><p><em>Originally published at </em><a href="https://www.quantrocket.com/blog/sector-neutralization/"><em>https://www.quantrocket.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d022fac3c35d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Why Backtests Run Fast or Slow: A Comparison of Zipline, Moonshot, and Lean]]></title>
            <link>https://quantrocket.medium.com/why-backtests-run-fast-or-slow-a-comparison-of-zipline-moonshot-and-lean-abab2ff8bd44?source=rss-6bc03eef1004------2</link>
            <guid isPermaLink="false">https://medium.com/p/abab2ff8bd44</guid>
            <category><![CDATA[algorithmic-trading]]></category>
            <category><![CDATA[backtesting]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[apple-silicon]]></category>
            <category><![CDATA[quantitative-finance]]></category>
            <dc:creator><![CDATA[QuantRocket]]></dc:creator>
            <pubDate>Tue, 08 Aug 2023 20:44:40 GMT</pubDate>
            <atom:updated>2023-08-08T20:44:40.602Z</atom:updated>
            <content:encoded><![CDATA[<p>Backtest speed can significantly affect research friction. The ability to form a hypothesis and quickly get an answer from a backtest allows you to investigate more hypotheses. In this article, I explore several factors that affect backtest speed and compare the performance of 3 open-source backtesters.</p><p>The backtesters I compare are:</p><ul><li>Moonshot, a vectorized backtester written in Python and used in QuantRocket</li><li>Zipline, an event-driven backtester written in Python, originally developed by Quantopian and used in QuantRocket</li><li>Lean, an event-driven backtester written in C# and used in QuantConnect</li></ul><p>The factors I investigate for their impact on speed are:</p><ul><li>Universe size</li><li>Hardware</li><li>Backtester design (event-driven vs vectorized)</li><li>Programming language</li></ul><blockquote>Readers who want to reproduce the results in this article can find the trading strategies <a href="https://github.com/quantrocket-codeload/backtest-speed-comparison">on GitHub</a>. Hardware specifications are provided throughout the article.</blockquote><h3>Large vs Small Universes</h3><p>The larger your universe of securities, the more data the backtester must handle, and the slower it will run. To quantify this, I run a 10-year backtest of a fundamental factor strategy that selects high quality stocks as measured by the Piotroski F-Score, an indicator of a firm’s financial health. For one set of tests, I use a small, static universe of 10 securities. For the other set of tests, I use the entire US equities market of approximately 8,000 securities, out of which I select the top 1,000 by market capitalization, then buy all stocks with F-Scores of 7 or higher. The runtimes are as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Ng5mLBj7772zmLm7.png" /></figure><p>All 3 backtesters process the small universe in under 30 seconds but take considerably longer to process the large universe. Moreover, the larger universe results in a large variation in speed among the different backtesters, with Lean running 12x slower than Zipline and 75x slower than Moonshot. The takeaway is that backtest speed is a negligible factor when your universe is small, but it can become a major factor when your universe is large.</p><h3>Intel vs Apple Silicon</h3><p>For all subsequent tests, I run only the large universe strategy.</p><p>To demonstrate how hardware can affect backtest speed, I repeat the Zipline and Moonshot backtests on two different Apple computers: an older Mac Pro with an Intel chip and a newer MacBook Pro with an Apple Silicon (M1) chip. The runtimes are as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*jlZ2-Zi52l8SomFv.png" /></figure><p>The Mac with the Apple Silicon chip runs the Zipline backtest three times faster than the Intel Mac, and it runs the Moonshot backtest four times faster. These results align with the generally glowing reviews that Apple Silicon chips have received since they first began appearing on Macs in late 2020. If you’re in the market for new hardware, switching to a new Mac with Apple Silicon can provide an easy performance boost across multiple workflows.</p><h3>Event-Driven vs Vectorized Backtesters</h3><p>The above graphic shows that Moonshot runs faster than Zipline regardless of hardware. This can be attributed to the fact that Moonshot is a vectorized backtester while Zipline is an event-driven backtester. Event-driven backtesters run in a loop, feeding market data to your algorithm one bar at a time. Vectorized backtesters feed the entire data history to your algorithm all at once instead of one bar at a time. By not having to iterate separately through each bar, vectorized backtesters can achieve greater parallelization and will usually be faster than event-driven backtesters, all else being equal.</p><h3>Python vs C#</h3><p>The programming language in which a backtesting framework is written can also affect backtest speed. To demonstrate this, I compare the two event-driven backtesters: Zipline (written in Python) and Lean (written in C#). To compensate for any slowness in the Lean backtest that is attributable to running on QuantConnect’s shared cloud hardware, I run the Zipline test on low-end cloud hardware of similar or lesser quality. (For reference, I also include the Zipline performance on Apple Silicon.) The runtimes are as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*JUzeBGD3NZ4Bf8_F.png" /></figure><p>It might seem counterintuitive that Zipline runs three times faster than Lean on basic cloud hardware, given that C# is a compiled language which in theory should run faster than Python, an interpreted language. The explanation is that underlying libraries make a critical difference. Zipline is built on top of Python’s scientific computing libraries, most importantly numpy and pandas. These libraries are highly optimized and are themselves written in compiled languages. Although C# is natively fast, it lacks a comparable ecosystem of scientific packages. In a real-world application, most of the computational work is not done by the application’s own source code but by the underlying libraries it relies on; thus the quality of those libraries is paramount.</p><p>A compounding challenge for Lean as a C# backtester stems from the fact that most quants prefer to write their trading strategies in Python rather than C#. To support this preference, QuantConnect uses a library called Python.NET to create a Python wrapper around Lean’s C# code. Python.NET doesn’t take advantage of Python’s scientific computing libraries but rather adds a translation step that introduces additional latency into Lean backtests.</p><h3>Conclusion</h3><p>Speed is not the only, or even the most important, consideration when choosing a backtester. Nevertheless, quants who want to maximize their research productivity should keep the above factors in mind when choosing a backtester or purchasing new hardware.</p><p><em>Originally published at </em><a href="https://www.quantrocket.com/blog/backtest-speed-comparison/"><em>https://www.quantrocket.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=abab2ff8bd44" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[What’s Better, High Profit Margins or Improving Profit Margins?]]></title>
            <link>https://quantrocket.medium.com/whats-better-high-profit-margins-or-improving-profit-margins-a611dd2faa77?source=rss-6bc03eef1004------2</link>
            <guid isPermaLink="false">https://medium.com/p/a611dd2faa77</guid>
            <category><![CDATA[fundamental-analysis]]></category>
            <category><![CDATA[quantitative-finance]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[algorithmic-trading]]></category>
            <dc:creator><![CDATA[QuantRocket]]></dc:creator>
            <pubDate>Thu, 13 Jul 2023 13:23:09 GMT</pubDate>
            <atom:updated>2023-07-13T13:23:09.898Z</atom:updated>
            <content:encoded><![CDATA[<p>Should investors prefer companies with high profit margins or companies with improving profit margins? Is it better to own an unprofitable company that’s getting better, or a profitable company that’s getting worse? This post explores these questions by analyzing the profitability growth factor and how it interacts with the profitability and size factors to impact stock performance.</p><blockquote>This post is part of the <a href="https://www.quantrocket.com/blog/topics/fundamental-factors/">fundamental factors</a> series, which explores techniques for researching fundamental factors using Pipeline, Alphalens, and Sharadar US fundamental data in QuantRocket.</blockquote><h3>Profitability vs Profitability Growth</h3><p>In a <a href="https://www.quantrocket.com/blog/profitability-factor-alphalens/">previous post</a>, we saw that the stocks of very unprofitable companies (defined as companies with negative operating margins) perform worse than other stocks. We looked at a snapshot of profitability, but that doesn’t tell us anything about the trend of profitability over time. The top quantile in our analysis may have included companies that were profitable but in decline, and the bottom quantile may have included companies that were unprofitable but getting better. In this article, I modify the analysis to look at profitability growth, that is, the change in profitability over time.</p><h3>How to Calculate Profitability Growth in Pipeline</h3><p>To facilitate measuring changes in fundamentals over time, the Pipeline API provides a variety of built-in “periodic” functions for performing arbitrary computations spanning multiple fiscal periods. You can calculate average revenue over the last 8 quarters, compare EPS to its 4-year range, or screen for companies that haven’t cut their dividend in the last 10 years.</p><p>We can calculate the change in profitability using the built-in PeriodicChange() function. <a href="https://www.quantrocket.com/blog/profitability-factor-eda/">Previously</a>, we created an operating margin factor that included various refinements such as excluding companies with zero revenue. To use this factor with PeriodicChange(), we put the factor logic inside a function that accepts a period_offset argument; this argument controls the fiscal period to query. (See the notebook linked at the end of the article for a more detailed explanation.)</p><pre>from zipline.pipeline import sharadar<br><br>def OPMARGIN(period_offset=0, mask=None):<br>    &quot;&quot;&quot;<br>    Return an operating margin factor for the specified period_offset.<br>    &quot;&quot;&quot;<br>    fundamentals = sharadar.Fundamentals.slice(&quot;ART&quot;, period_offset)<br>    revenue = fundamentals.REVENUE.latest<br>    operating_margin = fundamentals.OPINC.latest / revenue.where(revenue &gt; 0)<br><br>    # exclude companies with negative operating expenses or negative cost of revenue<br>    opex = fundamentals.OPEX.latest<br>    cor = fundamentals.COR.latest<br>    operating_margin = operating_margin.where((opex &gt; 0) &amp; (cor &gt; 0))<br><br>    # clip range to (-1, 1) so as not to overweight outliers<br>    operating_margin = operating_margin.clip(min_bound=-1, max_bound=1)<br><br>    if mask is not None:<br>        operating_margin = operating_margin.where(mask)<br><br>    return operating_margin</pre><p>For this analysis, I calculate the 1-year (4-quarter) change in profitability by passing the above function to PeriodicChange() with a window_length of 4:</p><pre>from zipline.pipeline.periodic import PeriodicChange<br><br>profitability_growth = PeriodicChange(OPMARGIN, window_length=4)</pre><h3>Analyzing How Different Factors Interact</h3><p>I want Alphalens to answer several questions about profitability growth.</p><p>First, is profitability growth predictive of future returns? Do companies with improving operating margins outperform companies with deteriorating operating margins? Intuitively, we hypothesize that this will be the case.</p><p>Second, assuming profitability growth is predictive, how does it relate to profitability? To put the question in practical terms: if I buy a basket of companies with improving margins, am I mostly buying already profitable companies, or am I also buying many unprofitable companies that are becoming less unprofitable (and thus showing positive growth)? We learned in a <a href="https://www.quantrocket.com/blog/profitability-factor-alphalens/">previous post</a> that very unprofitable companies are bad investments, so mixing unprofitability (something bad) with profitability growth (something we expect to be good) in a single quantile seems likely to cloud the picture. It would be nice to see a separate breakdown of factor performance for profitable vs unprofitable companies.</p><p>Alphalens can provide such a breakdown if we pass it one or more secondary factors as grouping factors. So, I include a simple boolean column in my pipeline that captures whether a company is currently profitable:</p><pre>is_profitable = OPMARGIN() &gt; 0</pre><p>My third question is how the profitability growth factor interacts with the size factor. Breaking down factor performance by market cap is a generally useful practice in fundamental factor analysis. Due to their higher volatility, very small companies can potentially mask the effect of the factor you’re trying to study if the small caps cluster in particular quantiles. Including market cap as a grouping factor allows you to spot when this is happening. I split the market into 4 size quantiles:</p><pre>fundamentals = sharadar.Fundamentals.slice(&quot;ART&quot;)<br>size = fundamentals.MARKETCAP.latest.quantiles(4)</pre><p>I put my primary and secondary factors into a Pipeline:</p><pre>from zipline.pipeline import Pipeline<br><br>pipeline = Pipeline(<br>    columns={<br>        &#39;profitability_growth&#39;: profitability_growth,<br>        &#39;is_profitable&#39;: is_profitable,<br>        &#39;size&#39;: size<br>    }<br>)</pre><p>And I pass the pipeline to Alphalens, specifying the secondary factors in the groupby parameter:</p><pre>import alphalens as al<br><br>al.from_pipeline(<br>    pipeline,<br>    start_date=&quot;1999-02-01&quot;,<br>    end_date=&quot;2022-12-30&quot;,<br>    # name of the pipeline column containing the primary factor to analyze<br>    factor=&quot;profitability_growth&quot;,<br>    # split the data into 4 quantiles, by profitability_growth<br>    quantiles=4,<br>    # don&#39;t mix positive and negative values in a single quantile<br>    zero_aware=True,<br>    # analyze forward returns over 1, 5, and 21 trading days<br>    periods=[1, 5, 21],<br>    # one or more secondary factors to group the primary factor by<br>    groupby=[<br>        &quot;is_profitable&quot;,<br>        &quot;size&quot;<br>    ],<br>    # run the analysis in 1-year chunks to reduce memory usage<br>    segment=&quot;Y&quot;<br>)</pre><h3>Results</h3><h3>Factor Distribution</h3><p>Let’s look at the mean column. On average, the operating margins of companies in the bottom quantile (quantile 1) decline by 14 percentage points year-over-year, while the operating margins of companies in the top quantile (quantile 4) increase by 13 percentage points. The average change in operating margin for the two middle quantiles is -1 percentage point and +1 percentage point, respectively - not very different. In other words, there are a large number of companies in the middle of the distribution with stable operating margins.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*M4ZNyLJafqRr3Qp-4C7yrA.png" /></figure><h3>Relative and Actual Cumulative Return by Profitability Growth Quantile</h3><p>In the following cumulative return plots, we see that companies with improving operating margins (quantile 4, blue line) clearly outperform companies with deteriorating operating margins (quantile 1, red line). The middle quantiles (2 and 3) perform fairly similarly to each other, which perhaps shouldn’t be surprising since the companies in these quantiles have stable operating margins. Somewhat more surprising is that the performance of the middle quantiles is only a little better than the performance of the bottom quantile. In other words, although companies with deteriorating operating margins fare somewhat worse than companies with stable operating margins, the real story is that companies with improving operating margins strongly outperform everything else.</p><p>This answers the first question I posed above: profitability growth leads to superior returns.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/853/0*PKqBnTu28iTMN5Dk.png" /></figure><h3>Breakdown by Current Profitability</h3><p>The second question I posed was whether a basket of stocks with improving operating margins consists mostly of profitable companies or whether it also includes many unprofitable companies that are becoming less unprofitable. Because we included current profitability as a secondary grouping factor, Alphalens shows us the composition of the top and bottom profitability growth quantile by the grouping factor. Looking at the right-hand pie chart, we see that three-fourths of companies in the top quantile (improving operating margins) are profitable, while one-fourth are unprofitable. As shown in the left-hand pie chart, the bottom quantile (deteriorating operating margins) is split roughly equally between profitable and unprofitable companies.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/907/0*_A8WHf-rFAGToWAT.png" /></figure><p>Considering the presence of unprofitable companies in the top profitability growth quantile (right-hand pie chart), a new question emerges. Can we do better by excluding the 26% of unprofitable companies from our profitability growth portfolio? In other words, does having two good characteristics (improving operating margins and currently profitable) yield better results than having only one good characteristic (improving operating margins but still unprofitable)?</p><p>In the pair of plots below, Alphalens has split the market into profitable and unprofitable companies and has separately plotted the mean return by profitability growth quantile within each subgroup.</p><p>The red arrows compare the performance of profitability growth quantile 4 (companies with improving operating margins) in the two subgroups. We see that the quantile 4 return is a little higher in the profitable subgroup than in the unprofitable subgroup, but it is a fairly small difference, suggesting that there is a benefit, but only a modest one, to combining profitability and profitability growth.</p><p>The paired plots also allow us to answer another question I posed at the beginning of the article: is it better to own an unprofitable company that’s getting better, or a profitable company that’s getting worse? The blue arrows compare profitability growth quantile 4 in the unprofitable subgroup with profitability growth quantile 1 in the profitable subgroup. We see that it is better to own an unprofitable company that’s getting better than a profitable company that’s getting worse.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/882/0*wJ_q3B_38VECTrNi.png" /></figure><p>A final point to note in the above plots is that, in the profitable subgroup on the right, performance increases monotonically with profitability growth, while in the unprofitable subgroup on the left, the effect of profitability growth on performance is noisier and more erratic (for example, quantile 3 performs worst). This suggests that profitability growth is a more reliable indicator of future performance among profitable companies than among unprofitable companies. This is confirmed by the following plot, which shows a higher information coefficient (a measure of the correlation between factor values and future returns) for the profitable subgroup than for the unprofitable subgroup.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/848/0*iFF-eV8N6ZwwfumJ.png" /></figure><h3>Breakdown by Market Cap</h3><p>Looking at market cap, we first check whether small or large companies are clustering in the top or bottom quantile. The answer is mostly no: smaller companies (size quantile 0) are slightly overrepresented in the bottom quantile, but overall the bottom and top profitability growth quantiles contain companies of all sizes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/859/0*4tBDNALJ8b-CXbuL.png" /></figure><p>How does profitability growth affect performance within different market cap subgroups? The plots below show that profitability growth is primarily predictive among small cap companies. In size quantile 0 (the smallest companies), companies with improving operating margins (factor quantile 4) outperform other companies (see green arrow). In contrast, in size quantile 3 (the largest companies), performance varies little across the 4 profitability growth quantiles (see blue arrow). The takeaway is that screening for profitability growth primarily makes sense as a way to find small companies on the up-and-up. Profitability growth among large companies is less important.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/845/0*XGj1LwPQUXcGHWK_.png" /></figure><h3>Explore this research on your own</h3><blockquote>This research was created with QuantRocket. Clone the <a href="https://www.quantrocket.com/code/?repo=fundamental-factors">fundamental-factors</a> repository to get the code and perform your own analysis.</blockquote><p>quantrocket codeload clone &#39;fundamental-factors&#39;</p><p><em>Originally published at </em><a href="https://www.quantrocket.com/blog/profitability-growth/"><em>https://www.quantrocket.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a611dd2faa77" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Analyzing the Profitability Factor with Alphalens]]></title>
            <link>https://quantrocket.medium.com/analyzing-the-profitability-factor-with-alphalens-4e3f0cc11c35?source=rss-6bc03eef1004------2</link>
            <guid isPermaLink="false">https://medium.com/p/4e3f0cc11c35</guid>
            <category><![CDATA[fundamental-analysis]]></category>
            <category><![CDATA[quantitative-finance]]></category>
            <category><![CDATA[algorithmic-trading]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[QuantRocket]]></dc:creator>
            <pubDate>Wed, 28 Jun 2023 15:03:50 GMT</pubDate>
            <atom:updated>2023-07-13T13:24:01.218Z</atom:updated>
            <content:encoded><![CDATA[<p>How does a company’s profitability affect its stock returns? In this post, I use Alphalens, a Python library for analyzing alpha factors, to investigate the relationship between operating margin, a profitability ratio, and future returns.</p><blockquote>This is the second post in the <a href="https://www.quantrocket.com/blog/topics/fundamental-factors/">fundamental factors</a> series, which explores techniques for researching fundamental factors using Pipeline, Alphalens, and Sharadar US fundamental data in QuantRocket.</blockquote><h3>The Operating Margin Factor</h3><p>In the <a href="https://www.quantrocket.com/blog/profitability-factor-eda/">preceding post</a>, we looked at operating margin, one of three main profitability ratios along with gross margin and net margin. I said that operating margin is an appealing metric for measuring a company’s core operational performance, due to the kinds of expenses that are included and excluded from its calculation.</p><p>Starting with the basic formula for operating margin — operating income divided by revenue — we refined the formula based on several issues that exploratory data analysis brought to light. Specifically, we excluded companies with no revenue and companies with negative operating expenses or negative cost of revenue, and we clipped extreme negative outliers. The complete factor definition is shown below:</p><pre>from zipline.pipeline import sharadar<br><br># ART = as-reported, trailing-twelve-months fundamentals<br>fundamentals = sharadar.Fundamentals.slice(&#39;ART&#39;)<br><br>revenue = fundamentals.REVENUE.latest<br>operating_margin = fundamentals.OPINC.latest / revenue.where(revenue &gt; 0)<br><br># exclude companies with negative operating expenses or negative cost of revenue<br>opex = fundamentals.OPEX.latest<br>cor = fundamentals.COR.latest<br>operating_margin = operating_margin.where((opex &gt; 0) &amp; (cor &gt; 0))<br><br># clip range to (-1, 1) so as not to overweight outliers<br>operating_margin = operating_margin.clip(min_bound=-1, max_bound=1)</pre><blockquote>Note that operating margin is a different profitability measure than the <a href="https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2287202">Fama-French profitability factor</a>. The differences are that the Fama-French version subtracts interest from revenue while operating margin does not, and the Fama-French version scales by book equity while operating margin scales by revenue. The formula for the Fama-French profitability factor using Sharadar fields is: (REVENUE - COR - INTEXP - SGNA) / EQUITY.</blockquote><h3>Where Alphalens Fits in the Research Process</h3><p>Before we analyze operating margin with Alphalens, let’s take a step back and consider where Alphalens fits in the research process.</p><p>Alphalens is an open-source Python library for analyzing and quantifying the predictive potential of alpha factors. Alphalens was originally created by <a href="https://www.quantrocket.com/blog/quantopian-shutting-down/">Quantopian</a> and is among several Quantopian libraries that QuantRocket maintains. Alphalens takes factor data (such as the output of running a pipeline) and creates a tear sheet containing plots and tables that show the relative performance of baskets of stocks formed with the alpha factor.</p><p><a href="https://www.quantrocket.com/blog/profitability-factor-eda/">Previously</a>, we saw that exploratory data analysis (EDA) is a useful first step for getting to know a fundamental factor and discovering and addressing any data complications that might otherwise cause problems later on. After you’ve completed EDA, using Alphalens to analyze your factor’s predictive value is a logical next step in the research process. A benefit of Alphalens is that you can get feedback on whether a factor looks promising without having to write much code. Promising factors can be investigated further or used in backtests, while unpromising factors can be abandoned with little loss of the researcher’s time.</p><h3>Is Alphalens Only for Factor Investors?</h3><p>Alphalens compares the relative forward performance of different baskets of securities formed by sorting on the factor you’re analyzing. This approach is similar to how portfolio construction works in long/short and market-neutral strategies. Indeed, Alphalens was originally developed by Quantopian to assess alpha factors for its long/short hedge fund.</p><p>Nevertheless, Alphalens’ usefulness isn’t limited to analyzing long/short or long-only strategies that target baskets of stocks. Think of Alphalens as a tool that can help you understand how different factors “tilt the scales” of a stock’s performance. In some cases, a factor you analyze with Alphalens might be strong enough to use in a trading strategy on a standalone basis. In other cases, a factor that is less strong may still be useful for identifying favorable conditions (and avoiding unfavorable conditions) for an otherwise unrelated strategy.</p><p>For example, a short seller wanting to short stocks based on medium-term price movements might use factor analysis for universe selection. The trader would identify stocks having fundamental characteristics that make the stocks prone to decline and would apply the price-based rules only to that subset of stocks.</p><p>Factor analysis helps you understand how the market works, which is useful for any trader, not just factor investors.</p><h3>From Pipeline to Alphalens</h3><p>Pipeline and Alphalens pair nicely, as you can pass a pipeline object directly to Alphalens and get a tear sheet, using the from_pipeline() function.</p><p>Below, I tell Alphalens to split stocks into 4 quantiles based on operating margin. Because zero marks the boundary between unprofitable and profitable companies, I specify zero_aware=True, which will cause Alphalens to reserve 2 of the 4 quantiles for stocks with negative operating margins and the other 2 quantiles for stocks with positive operating margins. I run a 25-year analysis, instructing Alphalens to analyze forward returns over the subsequent 1, 5, and 21 trading days (approximately 1 month). For more detailed explanations of the parameters shown below, see the accompanying notebook linked at the end of the article.</p><pre>from zipline.pipeline import Pipeline<br>import alphalens as al<br><br>pipeline = Pipeline(<br>    columns={<br>        &#39;profitability&#39;: operating_margin,<br>    }<br>)<br><br>al.from_pipeline(<br>    pipeline,<br>    start_date=&quot;1998-02-01&quot;,<br>    end_date=&quot;2022-12-30&quot;,<br>    # name of the pipeline column containing the factor to analyze<br>    factor=&quot;profitability&quot;,<br>    # split the data into 4 quantiles, by profitability<br>    quantiles=4,<br>    # don&#39;t mix positive and negative values in a single quantile<br>    zero_aware=True,<br>    # analyze forward returns over 1, 5, and 21 trading days<br>    periods=[1, 5, 21],<br>    # run the analysis in 1-year chunks to reduce memory usage<br>    segment=&quot;Y&quot;<br>)</pre><h3>Results</h3><p>Alphalens packs a lot of information into a tear sheet. Let’s look at a few highlights.</p><h3>Factor Distribution</h3><p>According to the count % column of the tear sheet&#39;s factor distribution table, approximately one-fourth of companies have negative operating margins (factor quantiles 1 and 2, containing 13% of companies each), and the remaining three-fourths of companies have positive operating margins (factor quantiles 3 and 4). The mean column shows that the average operating margin of companies in the top quantile is 23%, while the average operating margin of companies in the bottom quantile is -66% (equivalent to spending $166 dollars for every $100 of revenue).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sJqlL0viob3cE_1UeQ50Zg.png" /></figure><h3>Mean Relative Return by Factor Quantile</h3><p>Companies with the worst operating margins (quantile 1) perform poorly compared to other quantiles. Interestingly, there is not much difference in performance among the other 3 quantiles (which include companies with somewhat negative, somewhat positive, and very positive operating margins, respectively). Investors are apparently not rewarded for seeking out the most profitable companies but are punished for owning the most unprofitable companies.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/853/0*3iBMXbSLRLPOtjDy.png" /></figure><h3>Relative Cumulative Return by Quantile</h3><p>A mean return plot only shows us the average and not how we got there. The next plot shows the cumulative return (on a relative basis) of each of the 4 quantiles. The overall underperformance of quantile 1 (very unprofitable companies) is again evident, but we see that quantile 1 sharply outperformed during the dotcom bubble of the late 1990s, when investors piled into unprofitable companies.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/853/0*z1luig7CP0c4Tg2h.png" /></figure><h3>Factor-Weighted Cumulative Return</h3><p>The next plot shows the cumulative return of a factor-weighted long/short portfolio constructed by buying profitable companies and shorting unprofitable companies. Such a portfolio would have generally trended higher but would have suffered drawdowns during the dotcom bubble as well as during several other periods when investors favored unprofitable companies, including during early-stage bull markets in 2003 and 2009 and during the pandemic rally of 2020–2021.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/853/0*UKgt531X9K1OCRGl.png" /></figure><h3>Actual Cumulative Return by Quantile</h3><p>The plot of relative cumulative return by quantile that we looked at above showed the long-term relative decline of quantile 1 over time. The next plot shows the actual (not relative) cumulative return of each quantile. Here we see that the poor relative performance of quantile 1 occurred not because unprofitable companies trended lower as a group but rather because they failed to trend upward with other companies. In other words, the problem with investing in unprofitable companies is not that you lose all your money but that, for the excitement of volatility, you miss out on the long-term uptrend of the broader market.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/853/0*cG7jvDLTjLI8nr74.png" /></figure><h3>Explore this research on your own</h3><blockquote>This research was created with QuantRocket. Clone the <a href="https://www.quantrocket.com/code/?repo=fundamental-factors">fundamental-factors</a> repository to get the code and perform your own analysis.</blockquote><p>quantrocket codeload clone &#39;fundamental-factors&#39;</p><p><em>Originally published at </em><a href="https://www.quantrocket.com/blog/profitability-factor-alphalens/"><em>https://www.quantrocket.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4e3f0cc11c35" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>