Overview

11 Algorithmic trading

Illustrates how three types of data flow can lead to insights, and non-financial data, being the newest of them, can be of special interest.
Shows how companies might evolve. The shaded area marks a window of opportunity. A merger or acquisition can rescue a company in decline.
Shows the workflow of a company in distress and its possible turnaround depending on its assets.
On an exchange, sellers and buyers can interact directly. A broker is an entity between a buyer and a seller. A broker usually facilitates trading on one or more exchanges.

Summary

  • The efficient market theory assumes that we cannot beat the market because the price results from all the information available to investors.
  • Non-financial data can be an untapped resource for learning details about assets that other investors do not yet know. It can contain hidden information to beat the market.
  • Data such as media, geolocation data, demographics, or trends might tell you much about additional insights that can be added as factors to a model.
  • Non-financial data is not as openly available as financial data (which must be reported for public companies). However, you can purchase access to non-financial data on some marketplaces.
  • A catalyst in equity markets is something that propels the price of a stock dramatically up or down.
  • Catalysts include mergers and acquisitions(M&A), bankruptcy filings, earnings calls, disasters, and interest rate changes.
  • M&A means that two companies join forces. Such a big event often creates bigger movements in share prices. Being able to predict successful M&As can be rewarding.
  • Bankruptcy is also a profit opportunity if existing shareholders panic and sell their shares too fast. However, it is a considerable risk to bet on a bankrupt company's turnaround. If a stalking horse agreement (a buyer’s commitment to buy the resources at a minimum price if an auction does not yield a higher price) is in place, risks are lower.
  • Every publicly traded company has quarterly earnings calls, during which the executives report on the company’s performance. Missing or surpassing the earnings estimates can cause substantial shifts in stock prices.
  • Disasters, such as wars and natural catastrophes, affect share prices. Some companies make more profits when disasters happen. Buying shares of them at the beginning of a calamity or selling them before a disaster ends can be a strategy to make profits.
  • The federal reserve interest (or its equivalent in other countries) defines the interest rate at which it lends money to borrowers. Capital-intensive companies—those that need more external investments to be successful—are more affected by interest rate changes.
  • Investing in low-priced capital-intensive companies can be beneficial before the FED reduces the rate. However, some capital-intensive companies face bankruptcy risks if the interest rates are high.
  • You can define algorithmic trading strategies, which you define when you buy and when you sell.
  • Backtesting is a way to test buy and sell the strategies you created by validating them using historical data.
  • Even if your backtesting strategy is successful, past results do not guarantee future results.
  • An SMA crossover strategy extracts buy and sell signals based on two simple moving averages (SMAs) with different time windows.
  • SMA crossover strategies do not always return better results than “buy low and sell high” strategies.
  • We can add more complex rules to evaluate buy-and-sell decisions in algorithms. Factors are the criteria we might consider for decision-making.
  • While we can use Python to program trading algorithms and backtest them, some platforms offer this service. Customers of these services do not need coding experience.
  • A trader buys assets through a broker, who will purchase them from an exchange. Brokers hold assets in the name of a holder.
  • A trader can buy crypto assets directly from an exchange.
  • Exchanges and brokers charge commissions per order.
  • A market order is to be transacted immediately at the current price.
  • A limit order is an order executed at a specific price. The order is canceled if the price is not reached during a trading day.
  • A stop order triggers an order when the share price exceeds a defined stop price.
  • A stop limit triggers an order executed when the share price is between a minimum (limit price) and a maximum (stop price).
  • A trailing stop order adjusts the stop price. It moves with the market price and is triggered by sharp changes.
  • Fill or kill(FOK) is a parameter that determines whether an entire order is executed. The broker will not execute the order in parts.
  • Good ‘til canceled(GTC) is a parameter for an order that does not expire. The person who created it can either execute or cancel it. Some brokers will still mark GTC orders as expired if they are not executed over an extended period.
  • If you set a stop order with a GTC, you can prevent yourself from significant losses when the market collapses.

FAQ

What is “non-financial data,” and why does it matter for algorithmic trading?Non-financial data includes information outside traditional fundamentals and price/volume charts, such as social media sentiment, satellite imagery, geolocation/movement data, and demographic trends. Because it’s newer and unevenly explored, it can reveal underrepresented signals that the market hasn’t fully priced in. Relevance varies by sector (e.g., social sentiment is often more impactful for consumer brands than B2B firms). Used well, it can challenge the efficient market theory by providing unique edges.
How should I validate a trading strategy before using it live?Use a rigorous backtesting framework with clearly defined validation criteria: - Establish a baseline (e.g., buy-and-hold from first to last day). - Include realistic frictions: transaction costs, slippage, and bid–ask spreads; consider taxes if applicable. - Avoid overfitting by testing across multiple parameter sets, assets, and market regimes. - Prefer test-driven development thinking: define what success looks like before coding the logic.
How does the SMA crossover backtest in this chapter work, and what did it show?The strategy calculates two simple moving averages (short and long). Buy when the short SMA crosses above the long SMA; sell when it crosses below. In the chapter’s NVDA example, a 10/20-day SMA underperformed buy-and-hold, while other SMA window pairs outperformed. Lesson: results are parameter- and asset-dependent; no single SMA setup works universally.
What are “catalysts,” and how can I build strategies around them?Catalysts are events that can move prices sharply: earnings, analyst ratings, product launches, legislation, lawsuits, M&A, bankruptcies, disasters, and interest-rate changes. You can: - Specialize per catalyst (e.g., earnings sentiment from transcripts, option strategies for volatility). - Use alternative data (e.g., Glassdoor/LinkedIn employee sentiment for M&A integration risk). - Define quantifiable rules (if X, buy; if Y, sell), and backtest around catalyst windows. Always account for increased volatility and execution risk.
Do I need to be a big company to use “big data” sources?Not necessarily. Some alternative datasets are available via marketplaces like AWS Data Exchange and Snowflake Marketplace. Practical approach: - Start with an investment thesis; determine what extra data would validate it. - Prioritize accessible sources (news/APIs, social media, transcripts). - Consider targeted paid datasets if they directly test your thesis. The chapter’s Waymo example shows how to track rollouts, activity levels, and incident trends to validate a thesis.
What’s the difference between exchanges and brokers, and how do crypto exchanges fit in?- Stock exchanges are regulated venues; brokers are intermediaries that route and custody your trades. Broker coverage, data freshness, and fees vary by exchange and region. - Crypto exchanges are usually private companies with lighter regulation, offering trading, custody, and blockchain transaction handling via APIs. Risks are higher, governance varies by jurisdiction, and investor protections can be limited compared to public equities.
Which order types and modalities should I know, and when should I use them?Order types: - Market: execute immediately at current price. - Limit: execute at a specified price or better. - Stop (stop-loss): converts to market when stop price is hit. - Stop-limit: triggers a limit order at stop; can miss fills in fast markets. - Trailing stop: stop level moves with price by a set amount. Modalities: - FOK (Fill-or-Kill): fill the whole order immediately or cancel. - GTC (Good-’til-Canceled): keep the order active until executed or it expires per broker rules.
How do slippage, spreads, and costs affect algorithmic performance?They can turn a seemingly profitable backtest into a losing live system: - Slippage: difference between signaled and actual fill price, especially around volatile catalysts. - Bid–ask spread: you buy at ask and sell at bid; it’s a hidden cost each trade. - Transaction fees and taxes: frequent trades amplify costs and tax events. Mitigation: model these frictions in backtests, prefer limit orders when appropriate, and reduce unnecessary turnover.
How can I place orders programmatically with Interactive Brokers and Alpaca?- Interactive Brokers: connect your Python client to Trader Workstation (ib_insync), request market data, and submit Market or Limit orders. Note data may be delayed without specific subscriptions; IB supports many exchanges. - Alpaca: initialize the TradingClient (paper=True for sandbox), discover tradable assets, query quotes, then submit Market or Limit orders with time-in-force (e.g., FOK, GTC). Useful for testing execution logic and integrating with your algorithms.
What risks should programmers consider with advanced or high-frequency trading?- HFT demands ultra-low latency, specialized infra, and proximity to exchanges; Python alone may be too slow to compete. - Leverage and short selling magnify gains and losses; risk management is critical. - Overfitting complex signal stacks is easy; robust out-of-sample testing is essential. - As the chapter analogizes: entering advanced strategies without preparation is like a lightweight challenging a heavyweight—proceed only with training, risk controls, and humility.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Investing for Programmers ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Investing for Programmers ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Investing for Programmers ebook for free