TheoryCraft: A Quantitative Trading Backtesting and Execution Engine

TheoryCraft is an open-source backtesting engine built in Elixir for quantitative trading. It provides an event-driven, composable architecture for building, testing, and validating systematic trading strategies on historical and streaming market data.

TheoryCraft helps traders and researchers answer a simple question: does a trading strategy actually work?

By providing a unified backtesting and execution engine, TheoryCraft enables systematic strategies to be tested on historical data, validated statistically, and executed consistently across backtest, paper, and live environments.

This documentation describes the TheoryCraft Engine and its core components.

Why Backtesting Matters

Systematic trading relies on data-driven validation. Before deploying capital, traders and researchers need to verify that their strategies perform as expected across different market conditions.

A backtesting engine allows you to:

  • Test trading strategies against historical market data before risking real capital
  • Measure statistical properties such as win rate, expectancy, and drawdown
  • Identify weaknesses and edge cases in strategy logic
  • Compare multiple approaches under identical conditions
  • Build confidence through reproducible results

Without proper backtesting, traders operate on assumptions rather than evidence. TheoryCraft provides the infrastructure to validate trading ideas systematically.

What is the TheoryCraft Engine?

TheoryCraft is a quantitative trading backtesting and execution engine designed for building, testing, and running systematic trading strategies. It processes market data through configurable pipelines, manages broker interactions, and executes strategy logic in a unified, event-driven architecture.

The engine handles three core responsibilities:

  1. Market data ingestion and transformation - Load historical data or connect to streaming sources, resample to multiple timeframes, and compute technical indicators
  2. Broker connectivity and simulation - Interface with brokers for live and paper trading, or simulate order execution for backtesting
  3. Strategy and portfolio execution - Run trading logic, manage positions, calculate statistics, and optimize parameters

Who is This Engine For?

TheoryCraft serves multiple audiences with different goals:

Quantitative researchers exploring systematic strategies can use TheoryCraft to backtest ideas on historical data. A Python integration is planned for researchers who prefer that ecosystem.

Discretionary traders who want to validate their trading models can test setups against historical data to measure actual performance rather than relying on memory or intuition.

Elixir developers building trading systems benefit from native integration with OTP, GenStage, and the BEAM runtime. The engine leverages Elixir's strengths in concurrent, fault-tolerant systems.

No prior Elixir experience is required to understand the concepts in this documentation. Code examples are provided for those ready to implement.

Core Concepts

TheoryCraft organizes trading system development around three main components.

MarketSource

Handles market data ingestion, timeframe resampling, and indicator computation. Emits MarketEvent structures to downstream components.

Learn more about MarketSource and DataFeeds →

Broker

Manages order execution, position tracking, and account state. Emits BrokerEvent structures for order updates and position changes. Supports simulated execution for backtesting and real broker connections for live trading.

⚠️ Currently in development.

Learn more about Broker →

Engines

Consume MarketEvent and BrokerEvent streams to perform specific tasks: strategy execution, portfolio management, statistics calculation, or parameter optimization.

⚠️ Currently in development.

Learn more about Engines →

Event-Driven Workflow

The TheoryCraft Engine is built around an event-driven workflow that connects market data, execution, and strategy logic through structured events.

Market data and broker updates are emitted as events and consumed by engines responsible for strategy execution, portfolio management, statistics, or optimization.

This design allows the same strategy logic to run consistently across backtesting, paper trading, and live trading environments.

Read the detailed event-driven architecture →

Key Capabilities

The TheoryCraft Engine is under active development.
The following capabilities are currently available and stable, while others are being built incrementally.

Implemented

Streaming data pipelines - Process millions of market events without loading entire datasets into memory. Built on GenStage for backpressure-aware streaming.

Multi-timeframe support - Resample tick data to any interval including seconds, minutes, hours, days, weeks, and months.

Technical indicator computation - Calculate indicators through the TheoryCraftTA library with support for parallel processing of independent calculations.

Custom processors - Implement the Processor behavior to add domain-specific transformations to the data pipeline.

Multiple data feed integrations - Connect to historical data sources such as Dukascopy with support for 1600+ instruments across forex, equities, commodities, and indices.

Fault tolerance - Built on OTP supervisors for resilient, self-healing systems that handle failures gracefully.

Livebook integration - Interactive development and visualization through Livebook via the KinoTheoryCraft library.

In Development

Broker integrations - Connect to Interactive Brokers, Binance, and other brokers for live order execution. Simulated broker for backtesting.

Strategy execution - StrategyEngine for running trading logic and generating entry and exit signals.

Portfolio management - PortfolioEngine for dynamically managing and coordinating multiple strategies.

Performance analytics - StatisticsEngine for calculating metrics such as Sharpe ratio, maximum drawdown, and win rate.

Parameter optimization - OptimizerEngine for systematic parameter search and strategy refinement.

Python integration - Use TheoryCraft from Python for researchers who prefer that environment.

The Ecosystem

TheoryCraft consists of several libraries that work together:

LibraryPurposeStatus
theory_craftCore engine with MarketSource, pipelines, and processorsActive development
theory_craft_taTechnical analysis indicators via TA-Lib integrationActive development
kino_theory_craftInteractive visualizations in Livebook notebooksActive development
dukascopyHistorical market data from Dukascopy Bank for 1600+ instrumentsStable

All libraries are open-source under the Apache 2.0 license.

Next Steps

Continue with the documentation to learn how to use TheoryCraft:

Concepts - Understand the architecture and components

Guides - Step-by-step instructions for common tasks

Installation

Add TheoryCraft to your Elixir project by including the dependencies in your mix.exs file:

def deps do
  [
    {:theory_craft, github: "theorycraft-trading/theory_craft"},
    {:dukascopy, github: "theorycraft-trading/dukascopy"}
  ]
end

TheoryCraft is under active development. Pin to a specific commit hash for stability in production environments.

Community and Support

Report issues and contribute on GitHub. All repositories welcome contributions.

License

TheoryCraft and its ecosystem libraries are open-source software licensed under Apache 2.0.