You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Architecture Design: Backtesting Engine with Historical Data Integration
I wanted to start conversation and create initial idea how we can implement backtesting engine on top of historical data from IB.
This is initial draft. Comment, extend, edit, change, update, add or remove. Literally anything. Think of it as a design document. As it is always the best to scope out before implementing. :)
Cheers!
Overview
This document provides a detailed design for a Backtesting Engine capable of simulating market movements based on historical data fetched via IBKit. The engine will allow users to specify the start and end dates for the simulation, adjust playback speed, and configure market data granularity, such as bar intervals or tick data.
The below setup ensures that the DataManager, MarketSimulator, handle data asynchronously, reacting to the data availability in real-time or as fast as the simulation parameters allow. This integration pattern is crucial for ensuring the backtesting engine can handle the data dynamics similar to live trading scenarios, making for a more robust and realistic testing environment.
Key Features
Historical Data Fetching: Utilize IBKit to retrieve historical market data.
Simulation Control: Users can set start and end dates, playback speed, and data resolution.
Market Simulation: Simulate market conditions from historical data to evaluate trading strategies.
User Configurability: Users can configure the backtest with flexible parameters.
Components
DataManager
Responsible for fetching and storing historical market data from IBKit.
Responsibilities: Fetches historical data based on the BacktestConfig.
Methods: fetchHistoricalData(config): Retrieves data from IBKit based on specified dates and resolution.
BacktestConfig
Stores user-defined settings such as start/end dates, playback speed, and data resolution.
Attributes: startDate: When the backtest should start. endDate: Optional end date; defaults to the current date if not specified. playbackSpeed: Multiplier for the speed at which the backtest runs (1x, 2x, etc.). resolution: Granularity of the data (e.g., tick, 1-minute bars, daily bars).
MarketSimulator
Simulates the market by replaying historical data at a user-defined speed and interval.
Methods: simulateMarket(data): Simulates market conditions by replaying data at the speed and resolution defined in BacktestConfig.
import Combine
import Foundation
classDataManager{vardataPublisher:AnyPublisher<[MarketData],Never>{fetchHistoricalData(config).eraseToAnyPublisher()}privatefunc fetchHistoricalData(_ config:BacktestConfig)->AnyPublisher<[MarketData],Never>{
// Simulated data fetching from IBKit
Just([MarketData]()) // Example, replace with actual IBKit call
.delay(for:.seconds(1), scheduler:RunLoop.main).eraseToAnyPublisher()}}
MarketSimulator.swift
import Combine
import Foundation
classMarketSimulator{privatevarcancellables:Set<AnyCancellable>=[]func simulateMarket(dataPublisher:AnyPublisher<[MarketData],Never>)->AnyPublisher<SimulatedData,Never>{
dataPublisher
.flatMap{ marketData ->AnyPublisher<SimulatedData,Never>in
// Process each market data to simulate market conditions
Just(SimulatedData(data: marketData)) // Example transformation
.delay(for:.milliseconds(200), scheduler:RunLoop.main).eraseToAnyPublisher()}.eraseToAnyPublisher()}}
Moreover, we can provide component that will help facilitate user their own strategies. But not necessary as every consumer of SDK should be able to do it on their own. Maybe just an example in playground
StrategyExecutor.swift
import Combine
import Foundation
classStrategyExecutor{privatevarcancellables:Set<AnyCancellable>=[]func executeStrategy(dataPublisher:AnyPublisher<SimulatedData,Never>)->AnyPublisher<[Trade],Never>{
dataPublisher
.map{ simulatedData ->[Trade]in
// Execute strategy logic based on simulated data
[Trade()] // Example transformation
}.eraseToAnyPublisher()}}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Architecture Design: Backtesting Engine with Historical Data Integration
I wanted to start conversation and create initial idea how we can implement backtesting engine on top of historical data from IB.
This is initial draft. Comment, extend, edit, change, update, add or remove. Literally anything. Think of it as a design document. As it is always the best to scope out before implementing. :)
Cheers!
Overview
This document provides a detailed design for a Backtesting Engine capable of simulating market movements based on historical data fetched via IBKit. The engine will allow users to specify the start and end dates for the simulation, adjust playback speed, and configure market data granularity, such as bar intervals or tick data.
The below setup ensures that the DataManager, MarketSimulator, handle data asynchronously, reacting to the data availability in real-time or as fast as the simulation parameters allow. This integration pattern is crucial for ensuring the backtesting engine can handle the data dynamics similar to live trading scenarios, making for a more robust and realistic testing environment.
Key Features
Components
DataManager
Responsible for fetching and storing historical market data from IBKit.
Responsibilities: Fetches historical data based on the BacktestConfig.
Methods:
fetchHistoricalData(config):
Retrieves data from IBKit based on specified dates and resolution.BacktestConfig
Stores user-defined settings such as start/end dates, playback speed, and data resolution.
Attributes:
startDate
: When the backtest should start.endDate
: Optional end date; defaults to the current date if not specified.playbackSpeed
: Multiplier for the speed at which the backtest runs (1x, 2x, etc.).resolution
: Granularity of the data (e.g., tick, 1-minute bars, daily bars).MarketSimulator
Simulates the market by replaying historical data at a user-defined speed and interval.
Methods:
simulateMarket(data)
: Simulates market conditions by replaying data at the speed and resolution defined in BacktestConfig.BacktestConfig.swift
DataManager.swift
MarketSimulator.swift
Moreover, we can provide component that will help facilitate user their own strategies. But not necessary as every consumer of SDK should be able to do it on their own. Maybe just an example in playground
StrategyExecutor.swift
Beta Was this translation helpful? Give feedback.
All reactions