chore: simplify backtest loop interface
This commit is contained in:
@@ -1455,7 +1455,6 @@ class Backtesting:
|
|||||||
self,
|
self,
|
||||||
start_date: datetime,
|
start_date: datetime,
|
||||||
end_date: datetime,
|
end_date: datetime,
|
||||||
increment: timedelta,
|
|
||||||
pairs: list[str],
|
pairs: list[str],
|
||||||
data: dict[str, list[tuple]],
|
data: dict[str, list[tuple]],
|
||||||
):
|
):
|
||||||
@@ -1464,7 +1463,7 @@ class Backtesting:
|
|||||||
:returns: generator of (current_time, pair, is_first)
|
:returns: generator of (current_time, pair, is_first)
|
||||||
where is_first is True for the first pair of each new candle
|
where is_first is True for the first pair of each new candle
|
||||||
"""
|
"""
|
||||||
current_time = start_date + increment
|
current_time = start_date + self.timeframe_td
|
||||||
self.progress.init_step(
|
self.progress.init_step(
|
||||||
BacktestState.BACKTEST, int((end_date - start_date) / self.timeframe_td)
|
BacktestState.BACKTEST, int((end_date - start_date) / self.timeframe_td)
|
||||||
)
|
)
|
||||||
@@ -1484,8 +1483,9 @@ class Backtesting:
|
|||||||
pair_detail_cache: dict[str, list[tuple]] = {}
|
pair_detail_cache: dict[str, list[tuple]] = {}
|
||||||
pair_tradedir_cache: dict[str, LongShort | None] = {}
|
pair_tradedir_cache: dict[str, LongShort | None] = {}
|
||||||
pairs_with_open_trades = [t.pair for t in LocalTrade.bt_trades_open]
|
pairs_with_open_trades = [t.pair for t in LocalTrade.bt_trades_open]
|
||||||
|
|
||||||
for current_time_det, is_first, has_detail, idx in self.time_generator_det(
|
for current_time_det, is_first, has_detail, idx in self.time_generator_det(
|
||||||
current_time, current_time + increment
|
current_time, current_time + self.timeframe_td
|
||||||
):
|
):
|
||||||
# Loop for each detail candle.
|
# Loop for each detail candle.
|
||||||
# Yields only the start date if no detail timeframe is set.
|
# Yields only the start date if no detail timeframe is set.
|
||||||
@@ -1584,9 +1584,7 @@ class Backtesting:
|
|||||||
row,
|
row,
|
||||||
is_last_row,
|
is_last_row,
|
||||||
trade_dir,
|
trade_dir,
|
||||||
) in self.time_pair_generator(
|
) in self.time_pair_generator(start_date, end_date, list(data.keys()), data):
|
||||||
start_date, end_date, self.timeframe_td, list(data.keys()), data
|
|
||||||
):
|
|
||||||
self.backtest_loop(row, pair, current_time, trade_dir, not is_last_row)
|
self.backtest_loop(row, pair, current_time, trade_dir, not is_last_row)
|
||||||
|
|
||||||
self.handle_left_open(LocalTrade.bt_trades_open_pp, data=data)
|
self.handle_left_open(LocalTrade.bt_trades_open_pp, data=data)
|
||||||
|
|||||||
Reference in New Issue
Block a user