An algorithmic trading platform written in OCaml that supports live trading, paper trading, and backtesting. The platform uses a functional, modular architecture with strategies implemented as functors for maximum code reuse and type safety.
- Multiple execution modes: Backtesting, paper trading, and live trading
- Brokerage support: Alpaca (with extensible backend system)
- Market data sources: Alpaca and Tiingo
- Technical indicators: Integration with TA-lib via tacaml
- Web dashboard: Real-time visualization using React
- Strategy templates: Functional approach with reusable components
You'll need to install the following dependencies:
- tacaml: OCaml bindings for TA-lib technical analysis
- TA-lib: Technical analysis library (system dependency)
- Environment variables: Create a
.envrc
file in the project root with your API keys:
export APCA_API_KEY_ID=your_alpaca_key_id
export APCA_API_SECRET_KEY=your_alpaca_secret_key
export TIINGO_KEY=your_tiingo_key
export DASHBOARD_PASSWORD_HASH=your_hashed_password # Optional: for dashboard auth
- Build the project:
dune clean && dune build && dune install
Download historical data to populate indicators (preload data):
longleaf_downloader tiingo --begin=2023-12-01 --end=2023-12-31 --interval=10 --timeframe=minute data/dec23.json
Download target data for backtesting:
longleaf_downloader tiingo --begin=2024-01-01 --end=2024-12-31 --interval=10 --timeframe=minute data/24.json
Run a backtest with different strategies:
# Basic backtest starting from index 100
longleaf Backtest AStarExample data/24.json -i 100
# With preloaded indicator data
longleaf Backtest ThrowingCrossover --preload data/dec23.json --target data/24.json
# Other strategy examples
longleaf Backtest RsiMeanReversion data/24.json -i 100
longleaf Backtest MacdBollingerMomentum --preload data/dec23.json --target data/24.json
longleaf Backtest VolumeBreakout data/24.json -i 50
Run paper trading with live market data:
longleaf Paper --preload download -o papertrading.log
longleaf Live --preload download -o live_trading.log
The platform includes a real-time web dashboard for visualization:
- Start your strategy (backtest, paper, or live):
longleaf Backtest AStarExample data/24.json -i 100
- Launch the dashboard:
cd react
npm start
- Access the dashboard: Open
http://localhost:3000
in your browser
The dashboard provides:
- Real-time portfolio visualization
- Technical indicator charts with 30+ traces
- Strategy statistics and performance metrics
- Interactive symbol selection and data exploration
The platform includes numerous built-in strategies:
- AStarExample: A* search-based strategy example
- ThrowingCrossover: Moving average crossover with momentum
- RsiMeanReversion: RSI-based mean reversion strategy
- MacdBollingerMomentum: MACD and Bollinger Band momentum strategy
- VolumeBreakout: Volume-based breakout detection
- AdaptiveMomentumRegime: Regime-aware momentum strategy
- BuyAndHold: Simple buy and hold benchmark
- And many more in the
strategies/
directory
Strategies are built using OCaml functors with the template system. See existing strategies in strategies/
for examples. The basic pattern:
- Define buy trigger logic (entry conditions)
- Define sell trigger logic (exit conditions)
- Register strategy in
strategies/longleaf_strategies.ml
All strategies work seamlessly across backtesting, paper trading, and live trading modes.
Stop the program safely:
longleaf_shutdown
This ensures proper cleanup and data saving.
Strategies are located in the strategies/
directory. The platform uses eio
for concurrent execution with separate domains for strategy execution and the HTTP server. The architecture follows:
Parse options → Create backend → Instantiate strategy → Execute strategy
The backend abstraction allows the same strategy code to work across different execution environments (backtesting vs. live trading) and different brokerages, simply by changing the backend implementation.
All executables provide detailed help information:
longleaf --help
longleaf_downloader --help
longleaf_shutdown --help
This is an active project under development. If you encounter issues or want to contribute, please create an issue on the repository.