Merge pull request #1921 from freqtrade/minor/backtest_optimize

[minor] Small cleanup to reduce dict lookups during backtesting/hyperopt
This commit is contained in:
Misagh 2019-06-12 10:31:44 +02:00 committed by GitHub
commit 0d8b572a17
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -232,10 +232,9 @@ class Backtesting(object):
def _get_sell_trade_entry( def _get_sell_trade_entry(
self, pair: str, buy_row: DataFrame, self, pair: str, buy_row: DataFrame,
partial_ticker: List, trade_count_lock: Dict, args: Dict) -> Optional[BacktestResult]: partial_ticker: List, trade_count_lock: Dict,
stake_amount: float, max_open_trades: int) -> Optional[BacktestResult]:
stake_amount = args['stake_amount']
max_open_trades = args.get('max_open_trades', 0)
trade = Trade( trade = Trade(
open_rate=buy_row.open, open_rate=buy_row.open,
open_date=buy_row.date, open_date=buy_row.date,
@ -251,8 +250,7 @@ class Backtesting(object):
# Increase trade_count_lock for every iteration # Increase trade_count_lock for every iteration
trade_count_lock[sell_row.date] = trade_count_lock.get(sell_row.date, 0) + 1 trade_count_lock[sell_row.date] = trade_count_lock.get(sell_row.date, 0) + 1
buy_signal = sell_row.buy sell = self.strategy.should_sell(trade, sell_row.open, sell_row.date, sell_row.buy,
sell = self.strategy.should_sell(trade, sell_row.open, sell_row.date, buy_signal,
sell_row.sell, low=sell_row.low, high=sell_row.high) sell_row.sell, low=sell_row.low, high=sell_row.high)
if sell.sell_flag: if sell.sell_flag:
@ -325,6 +323,7 @@ class Backtesting(object):
:return: DataFrame :return: DataFrame
""" """
processed = args['processed'] processed = args['processed']
stake_amount = args['stake_amount']
max_open_trades = args.get('max_open_trades', 0) max_open_trades = args.get('max_open_trades', 0)
position_stacking = args.get('position_stacking', False) position_stacking = args.get('position_stacking', False)
start_date = args['start_date'] start_date = args['start_date']
@ -375,7 +374,8 @@ class Backtesting(object):
trade_count_lock[row.date] = trade_count_lock.get(row.date, 0) + 1 trade_count_lock[row.date] = trade_count_lock.get(row.date, 0) + 1
trade_entry = self._get_sell_trade_entry(pair, row, ticker[pair][indexes[pair]:], trade_entry = self._get_sell_trade_entry(pair, row, ticker[pair][indexes[pair]:],
trade_count_lock, args) trade_count_lock, stake_amount,
max_open_trades)
if trade_entry: if trade_entry:
lock_pair_until[pair] = trade_entry.close_time lock_pair_until[pair] = trade_entry.close_time