From 2ce03ab1b53bf64e48a2e46e152548590adee8e6 Mon Sep 17 00:00:00 2001 From: Janne Sinivirta Date: Sun, 11 Feb 2018 15:02:42 +0200 Subject: [PATCH] make Strategy store roi and stoploss values as numbers to avoid later casting --- freqtrade/main.py | 5 ++--- freqtrade/optimize/hyperopt.py | 10 +++++----- freqtrade/strategy/strategy.py | 6 +++--- freqtrade/tests/optimize/test_hyperopt.py | 2 +- freqtrade/tests/strategy/test_strategy.py | 8 ++++---- 5 files changed, 15 insertions(+), 16 deletions(-) diff --git a/freqtrade/main.py b/freqtrade/main.py index a9ca230d4..bb218855d 100755 --- a/freqtrade/main.py +++ b/freqtrade/main.py @@ -296,14 +296,13 @@ def min_roi_reached(trade: Trade, current_rate: float, current_time: datetime) - strategy = Strategy() current_profit = trade.calc_profit_percent(current_rate) - if strategy.stoploss is not None and current_profit < float(strategy.stoploss): + if strategy.stoploss is not None and current_profit < strategy.stoploss: logger.debug('Stop loss hit.') return True # Check if time matches and current rate is above threshold time_diff = (current_time.timestamp() - trade.open_date.timestamp()) / 60 - for duration_string, threshold in strategy.minimal_roi.items(): - duration = float(duration_string) + for duration, threshold in strategy.minimal_roi.items(): if time_diff < duration: return False if time_diff > duration and current_profit > threshold: diff --git a/freqtrade/optimize/hyperopt.py b/freqtrade/optimize/hyperopt.py index 12c061b4f..cdb1c3a6f 100644 --- a/freqtrade/optimize/hyperopt.py +++ b/freqtrade/optimize/hyperopt.py @@ -225,12 +225,12 @@ def calculate_loss(total_profit: float, trade_count: int, trade_duration: float) return trade_loss + profit_loss + duration_loss -def generate_roi_table(params) -> Dict[str, float]: +def generate_roi_table(params) -> Dict[int, float]: roi_table = {} - roi_table["0"] = params['roi_p1'] + params['roi_p2'] + params['roi_p3'] - roi_table[str(params['roi_t3'])] = params['roi_p1'] + params['roi_p2'] - roi_table[str(params['roi_t3'] + params['roi_t2'])] = params['roi_p1'] - roi_table[str(params['roi_t3'] + params['roi_t2'] + params['roi_t1'])] = 0 + roi_table[0] = params['roi_p1'] + params['roi_p2'] + params['roi_p3'] + roi_table[params['roi_t3']] = params['roi_p1'] + params['roi_p2'] + roi_table[params['roi_t3'] + params['roi_t2']] = params['roi_p1'] + roi_table[params['roi_t3'] + params['roi_t2'] + params['roi_t1']] = 0 return roi_table diff --git a/freqtrade/strategy/strategy.py b/freqtrade/strategy/strategy.py index 9ef5a2071..27f334d5c 100644 --- a/freqtrade/strategy/strategy.py +++ b/freqtrade/strategy/strategy.py @@ -71,11 +71,11 @@ class Strategy(object): # Minimal ROI designed for the strategy self.minimal_roi = OrderedDict(sorted( - self.custom_strategy.minimal_roi.items(), - key=lambda tuple: float(tuple[0]))) # sort after converting to number + {int(key): value for (key, value) in self.custom_strategy.minimal_roi.items()}.items(), + key=lambda tuple: tuple[0])) # sort after converting to number # Optimal stoploss designed for the strategy - self.stoploss = self.custom_strategy.stoploss + self.stoploss = float(self.custom_strategy.stoploss) self.ticker_interval = self.custom_strategy.ticker_interval diff --git a/freqtrade/tests/optimize/test_hyperopt.py b/freqtrade/tests/optimize/test_hyperopt.py index 93cb6ba8b..365869275 100644 --- a/freqtrade/tests/optimize/test_hyperopt.py +++ b/freqtrade/tests/optimize/test_hyperopt.py @@ -255,7 +255,7 @@ def test_roi_table_generation(): 'roi_p2': 2, 'roi_p3': 3, } - assert generate_roi_table(params) == {'0': 6, '15': 3, '25': 1, '30': 0} + assert generate_roi_table(params) == {0: 6, 15: 3, 25: 1, 30: 0} # test log_trials_result diff --git a/freqtrade/tests/strategy/test_strategy.py b/freqtrade/tests/strategy/test_strategy.py index 890718d60..b0ce88e98 100644 --- a/freqtrade/tests/strategy/test_strategy.py +++ b/freqtrade/tests/strategy/test_strategy.py @@ -57,7 +57,7 @@ def test_strategy(result): strategy.init({'strategy': 'default_strategy'}) assert hasattr(strategy.custom_strategy, 'minimal_roi') - assert strategy.minimal_roi['0'] == 0.04 + assert strategy.minimal_roi[0] == 0.04 assert hasattr(strategy.custom_strategy, 'stoploss') assert strategy.stoploss == -0.10 @@ -86,7 +86,7 @@ def test_strategy_override_minimal_roi(caplog): strategy.init(config) assert hasattr(strategy.custom_strategy, 'minimal_roi') - assert strategy.minimal_roi['0'] == 0.5 + assert strategy.minimal_roi[0] == 0.5 assert ('freqtrade.strategy.strategy', logging.INFO, 'Override strategy \'minimal_roi\' with value in config file.' @@ -142,8 +142,8 @@ def test_strategy_singleton(): strategy1.init({'strategy': 'default_strategy'}) assert hasattr(strategy1.custom_strategy, 'minimal_roi') - assert strategy1.minimal_roi['0'] == 0.04 + assert strategy1.minimal_roi[0] == 0.04 strategy2 = Strategy() assert hasattr(strategy2.custom_strategy, 'minimal_roi') - assert strategy2.minimal_roi['0'] == 0.04 + assert strategy2.minimal_roi[0] == 0.04