refactor: extract detail/pair loop to separate generator
This commit is contained in:
@@ -1451,6 +1451,18 @@ class Backtesting:
|
|||||||
i += 1
|
i += 1
|
||||||
current_time += self.timeframe_detail_td
|
current_time += self.timeframe_detail_td
|
||||||
|
|
||||||
|
def time_pair_generator_det(self, current_time: datetime, pairs: list[str]):
|
||||||
|
for current_time_det, is_first, has_detail, idx in self.time_generator_det(
|
||||||
|
current_time, current_time + self.timeframe_td
|
||||||
|
):
|
||||||
|
# Loop for each detail candle.
|
||||||
|
# Yields only the start date if no detail timeframe is set.
|
||||||
|
|
||||||
|
# Pairs that have open trades should be processed first
|
||||||
|
new_pairlist = list(dict.fromkeys([t.pair for t in LocalTrade.bt_trades_open] + pairs))
|
||||||
|
for pair in new_pairlist:
|
||||||
|
yield current_time_det, is_first, has_detail, idx, pair
|
||||||
|
|
||||||
def time_pair_generator(
|
def time_pair_generator(
|
||||||
self,
|
self,
|
||||||
start_date: datetime,
|
start_date: datetime,
|
||||||
@@ -1484,17 +1496,13 @@ class Backtesting:
|
|||||||
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, pair in self.time_pair_generator_det(
|
||||||
current_time, current_time + self.timeframe_td
|
current_time, pairs
|
||||||
):
|
):
|
||||||
# Loop for each detail candle.
|
# Loop for each detail candle (if necessary) and pair
|
||||||
# Yields only the start date if no detail timeframe is set.
|
# Yields only the start date if no detail timeframe is set.
|
||||||
|
|
||||||
# Pairs that have open trades should be processed first
|
# Pairs that have open trades should be processed first
|
||||||
new_pairlist = list(
|
|
||||||
dict.fromkeys([t.pair for t in LocalTrade.bt_trades_open] + pairs)
|
|
||||||
)
|
|
||||||
for pair in new_pairlist:
|
|
||||||
trade_dir: LongShort | None = None
|
trade_dir: LongShort | None = None
|
||||||
if is_first:
|
if is_first:
|
||||||
# Main candle
|
# Main candle
|
||||||
@@ -1506,9 +1514,7 @@ class Backtesting:
|
|||||||
row_index += 1
|
row_index += 1
|
||||||
indexes[pair] = row_index
|
indexes[pair] = row_index
|
||||||
is_last_row = current_time == end_date
|
is_last_row = current_time == end_date
|
||||||
self.dataprovider._set_dataframe_max_index(
|
self.dataprovider._set_dataframe_max_index(self.required_startup + row_index)
|
||||||
self.required_startup + row_index
|
|
||||||
)
|
|
||||||
trade_dir = self.check_for_trade_entry(row)
|
trade_dir = self.check_for_trade_entry(row)
|
||||||
pair_tradedir_cache[pair] = trade_dir
|
pair_tradedir_cache[pair] = trade_dir
|
||||||
|
|
||||||
@@ -1547,13 +1553,7 @@ class Backtesting:
|
|||||||
|
|
||||||
is_last_row = current_time_det == end_date
|
is_last_row = current_time_det == end_date
|
||||||
|
|
||||||
yield (
|
yield current_time_det, pair, row, is_last_row, trade_dir
|
||||||
current_time_det,
|
|
||||||
pair,
|
|
||||||
row,
|
|
||||||
is_last_row,
|
|
||||||
trade_dir,
|
|
||||||
)
|
|
||||||
self.progress.increment()
|
self.progress.increment()
|
||||||
|
|
||||||
def backtest(self, processed: dict, start_date: datetime, end_date: datetime) -> dict[str, Any]:
|
def backtest(self, processed: dict, start_date: datetime, end_date: datetime) -> dict[str, Any]:
|
||||||
|
|||||||
Reference in New Issue
Block a user