@@ -1246,9 +1246,28 @@ def run_vector_backtest(
12461246 show_progress (bool): Whether to show progress bars during
12471247 data source initialization. This is useful for long-running
12481248 initialization processes.
1249+ dynamic_position_sizing (bool): Whether to use dynamic position
1250+ sizing based on volatility or other factors. Defaults to False.
12491251
12501252 Returns:
12511253 Backtest: Instance of Backtest
1254+
1255+ Examples:
1256+ # Basic usage
1257+ backtest = app.run_vector_backtest(
1258+ strategy=my_strategy,
1259+ backtest_date_range=my_date_range,
1260+ initial_amount=1000
1261+ )
1262+
1263+ # With custom storage and checkpoints
1264+ backtest = app.run_vector_backtest(
1265+ strategy=my_strategy,
1266+ backtest_date_range=my_date_range,
1267+ initial_amount=1000,
1268+ use_checkpoints=True,
1269+ backtest_storage_directory="./backtest_results"
1270+ )
12521271 """
12531272 # Use registered strategy if none provided
12541273 if strategy is None :
@@ -1479,6 +1498,107 @@ def run_backtests(
14791498
14801499 return backtests
14811500
1501+ def get_backtest_data (
1502+ self ,
1503+ strategy : TradingStrategy ,
1504+ backtest_date_range : BacktestDateRange ,
1505+ show_progress : bool = True ,
1506+ fill_missing_data : bool = True ,
1507+ ) -> Dict [str , Any ]:
1508+ """
1509+ Get all data sources with their corresponding data for a given
1510+ strategy and backtest window.
1511+
1512+ This method retrieves the market data for all data sources defined
1513+ in the strategy, considering the warmup window for each data source.
1514+ The data is returned as a dictionary where keys are data source
1515+ identifiers and values are the corresponding DataFrames.
1516+
1517+ Args:
1518+ strategy (TradingStrategy): The strategy containing the data
1519+ sources to retrieve data for.
1520+ backtest_date_range (BacktestDateRange): The date range for
1521+ the backtest window.
1522+ show_progress (bool): Whether to show progress bars during
1523+ data retrieval. Defaults to True.
1524+ fill_missing_data (bool): If True, missing time series data
1525+ entries will be filled automatically. Defaults to True.
1526+
1527+ Returns:
1528+ Dict[str, Any]: A dictionary where keys are data source
1529+ identifiers (e.g., "BTC/EUR_ohlcv") and values are the
1530+ corresponding data (typically pandas DataFrames).
1531+
1532+ Example:
1533+ ```python
1534+ from investing_algorithm_framework import (
1535+ create_app, TradingStrategy, BacktestDateRange, DataSource
1536+ )
1537+ from datetime import datetime, timezone
1538+
1539+ class MyStrategy(TradingStrategy):
1540+ data_sources = [
1541+ DataSource(
1542+ identifier="btc_data",
1543+ symbol="BTC/EUR",
1544+ time_frame="1h",
1545+ warmup_window=100,
1546+ market="BITVAVO"
1547+ )
1548+ ]
1549+ # ... strategy implementation
1550+
1551+ app = create_app()
1552+ app.add_strategy(MyStrategy)
1553+
1554+ backtest_range = BacktestDateRange(
1555+ start_date=datetime(2024, 1, 1, tzinfo=timezone.utc),
1556+ end_date=datetime(2024, 6, 1, tzinfo=timezone.utc)
1557+ )
1558+
1559+ # Get all data for the strategy
1560+ data = app.get_backtest_data(
1561+ strategy=MyStrategy(),
1562+ backtest_date_range=backtest_range
1563+ )
1564+
1565+ # Access data by identifier
1566+ btc_df = data["btc_data"]
1567+ ```
1568+
1569+ Raises:
1570+ OperationalException: If no data sources are defined in the
1571+ strategy or if data cannot be retrieved for a data source.
1572+ """
1573+ # Get data sources from the strategy
1574+ data_sources = strategy .data_sources
1575+
1576+ if data_sources is None or len (data_sources ) == 0 :
1577+ raise OperationalException (
1578+ "No data sources defined in the strategy. "
1579+ "Please define data sources to retrieve backtest data."
1580+ )
1581+
1582+ # Setup backtest data providers
1583+ self .initialize_data_sources_backtest (
1584+ data_sources = data_sources ,
1585+ backtest_date_range = backtest_date_range ,
1586+ show_progress = show_progress ,
1587+ fill_missing_data = fill_missing_data ,
1588+ )
1589+
1590+ # Get the data provider service
1591+ data_provider_service = self .container .data_provider_service ()
1592+
1593+ # Retrieve vectorized backtest data for all data sources
1594+ data = data_provider_service .get_vectorized_backtest_data (
1595+ data_sources = data_sources ,
1596+ start_date = backtest_date_range .start_date ,
1597+ end_date = backtest_date_range .end_date ,
1598+ )
1599+
1600+ return data
1601+
14821602 def run_backtest (
14831603 self ,
14841604 backtest_date_range : BacktestDateRange ,
@@ -1545,10 +1665,10 @@ def run_backtest(
15451665 the backtest. This is useful for long-running backtests.
15461666 market (str): The market to use for the backtest. This is used
15471667 to create a portfolio configuration if no portfolio
1548- configuration is provided.
1668+ configuration is provided in the strategy .
15491669 trading_symbol (str): The trading symbol to use for the backtest.
15501670 This is used to create a portfolio configuration if no
1551- portfolio configuration is provided.
1671+ portfolio configuration is provided in the strategy .
15521672 fill_missing_data (bool): If True (default), missing time series
15531673 data entries will be filled automatically before running the
15541674 backtest.
0 commit comments