Skip to main content

qs_backtest/
lib.rs

1//! `qs-backtest` — Backtesting engine for the quant-system workspace.
2//!
3//! This crate provides tools for replaying historical market data through the
4//! [`qs_core::TradeEngine`] to evaluate trading strategies and predefined
5//! signal sets.
6//!
7//! # Two modes of operation
8//!
9//! 1. **Strategy-driven** — implement the [`Strategy`] trait; the runner feeds
10//!    market data tick-by-tick and your strategy decides when to act.
11//! 2. **Signal replay** - provide strict timestamped [`RawSignal`] values; the runner injects them at the correct moments while replaying price data.
12//!
13//! # Key types
14//!
15//! | Type | Purpose |
16//! |------|---------|
17//! | [`BacktestRunner`] | Orchestrates the backtest loop (both modes) |
18//! | [`BacktestExecutor`] | Tracks simulated fills, positions, and P&L |
19//! | [`BacktestResult`] | Final report with P&L, drawdown, and execution artifacts |
20//! | [`DataFeed`] | Trait for sequential market event sources |
21//! | [`Strategy`] | Trait for strategy-driven backtests |
22
23pub mod artifacts;
24pub mod currency;
25pub mod data_feed;
26pub mod economic_support;
27pub mod evaluation;
28pub mod executor;
29pub mod future_executor;
30pub mod ledger;
31pub mod mtm;
32pub mod portfolio;
33pub mod profile;
34pub mod report;
35pub mod runner;
36pub mod sizing;
37pub mod strategy;
38
39// ── Convenience re-exports ──────────────────────────────────────────────────
40
41pub use artifacts::{
42    CloseEvent, CompletedPosition, ExecutionMetadata, FutureBacktestArtifacts,
43    InstrumentSizingArtifact, MarketEntrySizingAudit, MarketEntrySizingBasis, NetPnlOutcome,
44    OpenPositionSnapshot, PendingOrderLifecycleEvent, PendingOrderLifecycleState,
45    PendingOrderSnapshot, RecordedFill, ReplayInstrumentArtifact, ReplayInstrumentManifest,
46    RiskBasisStatus, RiskTranche,
47};
48pub use currency::{
49    ConversionError, ConversionLeg, ConversionLegAudit, ConversionPriceSide, ConversionQuoteBook,
50    ConversionResult, ConversionRoute, FxPair, FxPairDirection, QuoteValidationError,
51    RunCurrencyPlan, RunCurrencyPlanError, resolve_conversion_route, resolve_fx_pair,
52};
53pub use data_feed::{DataFeed, MarketEvent, VecFeed};
54pub use economic_support::{
55    EconomicSupportError, LEGACY_ECONOMIC_GUARD_ID, LegacyEconomicModel, SupportedLegacyEconomics,
56    guarded_instrument_spec, resolve_legacy_economics,
57};
58pub use evaluation::{
59    BootstrapConfig, BreakdownDimension, EvaluationContext, EvaluationOptions, EvaluationReport,
60    EvaluationSection, GroupFilter, PositionFilter, PositionSide,
61};
62pub use executor::BacktestExecutor;
63pub use future_executor::FutureExecutor;
64pub use mtm::{
65    DEFAULT_MTM_MAX_POINTS, MAX_MTM_MAX_POINTS, MIN_MTM_MAX_POINTS, MtmCurveCollector,
66    MtmOutputPolicy, MtmOutputPolicyError, MtmOutputSummary,
67};
68pub use profile::{
69    EntryGeometryPolicy, EntryLevelResolution, EntryProfileRoutingError, EntryResolutionContext,
70    GeneratedTargetResolution, ManagementProfile, PositionRef, PositionResolver,
71    PreparedEntryProfiles, PriceGridSource, ProfileApplicationError, ProfileError, ProfileRegistry,
72    ProfileRegistryError, ProfileValidationError, RawSignal, ResolvedEntry, RuleConfigDef,
73    StoplossMode, TargetResolution, TargetResolutionSource, TargetSelection, TargetSource,
74    allocate_target_units, resolve_signal, resolve_unprofiled_entry,
75};
76pub use report::{
77    BacktestResult, CloseReasonStats, DurationStats, MonthlyReturn, PositionSummary, RiskMetrics,
78    StreakStats, SubsetStats, TradeResult,
79};
80pub use runner::{
81    BacktestRunner, FutureQuoteConfig, ReplayCancelled, ReplayProgress, StreamingReplayError,
82};
83pub use strategy::{
84    AnalysisBoundary, AnalysisBoundaryOutput, AnalysisContext, AnalysisError, AnalysisPipeline,
85    AnnotationError, AnnotationId, AnnotationLimits, AnnotationTimeline, AnnotationUse,
86    BacktestConfiguredStrategyAdapter, BarSeriesSpec, BarWindow, ClosedBar,
87    ConfiguredHistoricalBindings, ConfiguredNamedInputBinding, ConfiguredSourceBinding,
88    ConfiguredStrategyAdapterBuildError, ConfiguredStrategyAdapterError,
89    ConfiguredStrategyAdapterPreflightError, ConfirmedPivotAnalyzer, HistoricalAnalyzer,
90    HistoricalNamedInputProjector, HistoricalObservationView, HistoricalSeriesView,
91    HistoricalStrategy, HistoricalVolumeProjection, JournalKind, MAX_ANALYZERS,
92    MAX_ANNOTATION_ID_BYTES, MAX_ANNOTATION_NOTE_BYTES, MAX_ANNOTATIONS, MAX_CHART_REF_BYTES,
93    MAX_DECISION_LATENCY_MS, MAX_DECISION_RECORDS, MAX_EXPERIMENT_LABEL_BYTES,
94    MAX_INSTRUMENT_BYTES, MAX_JOURNAL_PER_CALLBACK, MAX_JOURNAL_REASON_BYTES, MAX_JOURNAL_RECORDS,
95    MAX_JOURNAL_VALUE_KEY_BYTES, MAX_JOURNAL_VALUES, MAX_OBSERVATION_SOURCE_SERIES,
96    MAX_OBSERVATIONS_PER_BOUNDARY, MAX_PIVOT_SIDE_BARS, MAX_REASON_BYTES, MAX_RETAINED_BARS,
97    MAX_RETAINED_OBSERVATIONS, MAX_SERIES_ID_BYTES, MAX_SIGNALS_PER_CALLBACK,
98    MAX_STRATEGY_ID_BYTES, MAX_STRATEGY_REVISION_BYTES, MAX_STRATEGY_TITLE_BYTES,
99    MAX_TRADE_ID_BYTES, MAX_WARMUP_BARS, MAX_ZONE_ID_BYTES, MissingIntervalPolicy, MomentumState,
100    MultiTimeframeSeries, NamedInputProjectionContext, NamedInputProjectionError,
101    ObservationOrigin, ObservationSelection, ObservationStore, ObservationStoreLimits,
102    ObservationWindow, PivotConfig, PriceBasis, PriceZone, ProjectedNamedInput, RejectionPattern,
103    SeriesError, SeriesId, SeriesRequirement, SeriesViewError, SeriesWarmupState, Strategy,
104    StrategyAnnotation, StrategyBacktestResult, StrategyComparisonMetrics,
105    StrategyComparisonSnapshot, StrategyConfigError, StrategyContext, StrategyDecisionDraft,
106    StrategyDecisionKind, StrategyDecisionOutput, StrategyDecisionRecord, StrategyDecisionRecorder,
107    StrategyDecisionRetention, StrategyDescriptor, StrategyDomainError, StrategyEvent,
108    StrategyExperimentComparison, StrategyExperimentError, StrategyFeedback, StrategyFeedbackEvent,
109    StrategyId, StrategyJournalDraft, StrategyJournalError, StrategyJournalOutput,
110    StrategyJournalRecord, StrategyJournalRecorder, StrategyJournalRetention, StrategyObservation,
111    StrategyObservationDraft, StrategyObservationValue, StrategyOutput, StrategyReplayError,
112    StrategyReplayInputError, StrategyRequirements, StrategyResearchLimits, StrategyResearchOutput,
113    StrategyRetentionLimits, StrategyRuntimeError, SwingKind, SwingPoint, Timeframe,
114    WarmupRequirement, ZoneId, ZoneSide, ZoneSource, ZoneState,
115};