mirror of
https://github.com/freqtrade/freqtrade.git
synced 2024-11-10 10:21:59 +00:00
make Strategy store roi and stoploss values as numbers to avoid later casting
This commit is contained in:
parent
5190cd507e
commit
2ce03ab1b5
|
@ -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:
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue
Block a user