Event-Driven Backtester Concepts
Relevant source files
The following files were used as context for generating this wiki page:
Stolgo is a Price Action Trading Analysis Library designed to automate trading rules based on price movement rather than just technical indicators. While traditional backtesting often relies on vectorized calculations, the project identifies a roadmap item to implement an Event-Driven Backtester to better simulate real-world price action trading environments.
The library currently provides the foundational "Event" detection components, such as candlestick patterns, trend identification, and breakout logic, which serve as the triggers within a backtesting system.
Sources: README.md:1-12, README.md:61-64
Core Analysis Components (Event Triggers)
The backtesting logic in Stolgo relies on discrete price action events. These events are detected using specialized classes that analyze historical OHLC (Open, High, Low, Close) data.
Candlestick Pattern Recognition
Patterns serve as the primary signals for entering or exiting trades. The CandleStick class evaluates individual or sequences of candles to identify specific market sentiments.
| Pattern | Method | Description |
|---|---|---|
| Bullish Engulfing | is_bullish_engulfing | A green candle that completely overlaps the body of the previous red candle. |
| Hammer | is_hammer_candle | A candle with a small body and a long lower wick, indicating potential reversal. |
| Inverse Hammer | is_inverse_hammer_candle | A candle with a small body and a long upper wick. |
| Doji | is_doji_candle | A candle where open and close are nearly equal, suggesting indecision. |
Sources: lib/stolgo/candlestick.py:3-85
Trend and Breakout Logic
Trend and breakout events determine the broader context in which candlestick patterns occur.
- Trend Detection: The
Trendclass identifies sustained movements (uptrends or downtrends) over a specified number of periods. - Consolidation & Breakout: The
Breakoutclass identifies periods where price remains within a percentage range (is_consolidating) and triggers an event when the price closes above (is_breaking_out) or below (is_breaking_down) that range.
Sources: lib/stolgo/trend.py:7-80, lib/stolgo/breakout.py:6-79
Data Flow for Backtesting
The data flow starts with fetching data from external providers (Nasdaq or Samco), formatting it into a standard DataFrame, and then passing it through the analysis engines.
The diagram shows the transition from raw data acquisition via Nasdaq or Samco classes to the analysis modules like Breakout and Trend.
Sources: lib/stolgo/nasdaq.py:59-122, lib/stolgo/samco.py:126-166, lib/stolgo/breakout.py:37-79
Data Feed and Resampling
For accurate backtesting, data must often be adjusted to different timeframes. The Samco module provides both historical and intraday data fetching, while the helper module provides resampling utilities to convert granular data into higher-level periods (e.g., 1-minute to 5-minute candles).
OHLC Resampling Logic
The get_data_resample function ensures that when data is aggregated, the OHLC integrity is maintained:
- Open: First value of the period.
- High: Maximum value of the period.
- Low: Minimum value of the period.
- Close: Last value of the period.
- Volume: Sum of all volume in the period.
Sources: lib/stolgo/helper.py:84-88, lib/stolgo/samco.py:149-166
Strategy Implementation Example
A typical event-driven strategy combines multiple modules. For instance, a "Breakout" strategy might require price consolidation followed by a specific candlestick confirmation.
from stolgo.breakout import Breakout
from stolgo.candlestick import CandleStick
# 1. Check if price is breaking out of a 13-period consolidation
breakout_engine = Breakout(periods=13, percentage=2)
if breakout_engine.is_breaking_out(dfs):
# 2. Confirm with a Bullish Engulfing pattern at the breakout point
candle_engine = CandleStick()
if candle_engine.is_bullish_engulfing(dfs):
# 3. Trigger Buy Event
pass
Sources: lib/stolgo/breakout.py:37-56, lib/stolgo/candlestick.py:12-23
Technical Requirements for Backtesting
| Component | Responsibility | Relevant Files |
|---|---|---|
| Data Handlers | Fetching and cleaning market data via REST/Curl. | nasdaq.py, samco.py, request.py |
| Exception Handling | Managing data gaps or invalid formats using BadDataError. | exception.py, candlestick.py |
| Date Utilities | Calculating period offsets and business day ranges. | helper.py |
| Logic Engines | Identifying technical "Events" in the price stream. | trend.py, breakout.py, candlestick.py |
Sources: lib/stolgo/exception.py:1-3, lib/stolgo/helper.py:46-82, lib/stolgo/request.py:64-106
Summary
Event-Driven Backtester Concepts in Stolgo revolve around the modular detection of price action patterns. By decoupling data acquisition (via Nasdaq or Samco providers) from price analysis logic (via CandleStick, Trend, and Breakout classes), the library allows developers to construct complex trading rules that respond to discrete market events. The integration of resampling and date-range helpers ensures that these events can be backtested across various timeframes and historical periods.