Change stoploss_on_exchange in freqtradebot
This commit is contained in:
		| @@ -20,7 +20,7 @@ from freqtrade.exchange import timeframe_to_minutes | |||||||
| from freqtrade.persistence import Trade | from freqtrade.persistence import Trade | ||||||
| from freqtrade.rpc import RPCManager, RPCMessageType | from freqtrade.rpc import RPCManager, RPCMessageType | ||||||
| from freqtrade.resolvers import ExchangeResolver, StrategyResolver, PairListResolver | from freqtrade.resolvers import ExchangeResolver, StrategyResolver, PairListResolver | ||||||
| from freqtrade.state import State | from freqtrade.state import State, RunMode | ||||||
| from freqtrade.strategy.interface import SellType, IStrategy | from freqtrade.strategy.interface import SellType, IStrategy | ||||||
| from freqtrade.wallets import Wallets | from freqtrade.wallets import Wallets | ||||||
|  |  | ||||||
| @@ -75,6 +75,12 @@ class FreqtradeBot(object): | |||||||
|         persistence.init(self.config.get('db_url', None), |         persistence.init(self.config.get('db_url', None), | ||||||
|                          clean_open_orders=self.config.get('dry_run', False)) |                          clean_open_orders=self.config.get('dry_run', False)) | ||||||
|  |  | ||||||
|  |         # Stoploss on exchange does not make sense, therefore we need to disable that. | ||||||
|  |         if (self.dataprovider.runmode == RunMode.DRY_RUN and | ||||||
|  |            self.strategy.order_types.get('stoploss_on_exchange', False)): | ||||||
|  |             logger.info("Disabling stoploss_on_exchange during dry-run.") | ||||||
|  |             self.strategy.order_types['stoploss_on_exchange'] = False | ||||||
|  |             config['order_types']['stoploss_on_exchange'] = False | ||||||
|         # Set initial bot state from config |         # Set initial bot state from config | ||||||
|         initial_state = self.config.get('initial_state') |         initial_state = self.config.get('initial_state') | ||||||
|         self.state = State[initial_state.upper()] if initial_state else State.STOPPED |         self.state = State[initial_state.upper()] if initial_state else State.STOPPED | ||||||
|   | |||||||
| @@ -81,7 +81,7 @@ class StrategyResolver(IResolver): | |||||||
|             key=lambda t: t[0])) |             key=lambda t: t[0])) | ||||||
|         self.strategy.stoploss = float(self.strategy.stoploss) |         self.strategy.stoploss = float(self.strategy.stoploss) | ||||||
|  |  | ||||||
|         self._strategy_sanity_validations(config) |         self._strategy_sanity_validations() | ||||||
|  |  | ||||||
|     def _override_attribute_helper(self, config, attribute: str, default): |     def _override_attribute_helper(self, config, attribute: str, default): | ||||||
|         """ |         """ | ||||||
| @@ -102,7 +102,7 @@ class StrategyResolver(IResolver): | |||||||
|             setattr(self.strategy, attribute, default) |             setattr(self.strategy, attribute, default) | ||||||
|             config[attribute] = default |             config[attribute] = default | ||||||
|  |  | ||||||
|     def _strategy_sanity_validations(self, config): |     def _strategy_sanity_validations(self): | ||||||
|         if not all(k in self.strategy.order_types for k in constants.REQUIRED_ORDERTYPES): |         if not all(k in self.strategy.order_types for k in constants.REQUIRED_ORDERTYPES): | ||||||
|             raise ImportError(f"Impossible to load Strategy '{self.strategy.__class__.__name__}'. " |             raise ImportError(f"Impossible to load Strategy '{self.strategy.__class__.__name__}'. " | ||||||
|                               f"Order-types mapping is incomplete.") |                               f"Order-types mapping is incomplete.") | ||||||
| @@ -111,12 +111,6 @@ class StrategyResolver(IResolver): | |||||||
|             raise ImportError(f"Impossible to load Strategy '{self.strategy.__class__.__name__}'. " |             raise ImportError(f"Impossible to load Strategy '{self.strategy.__class__.__name__}'. " | ||||||
|                               f"Order-time-in-force mapping is incomplete.") |                               f"Order-time-in-force mapping is incomplete.") | ||||||
|  |  | ||||||
|         # Stoploss on exchange does not make sense, therefore we need to disable that. |  | ||||||
|         if config.get('dry_run'): |  | ||||||
|             logger.info("Disabling stoploss_on_exchange during dry-run.") |  | ||||||
|             self.strategy.order_types['stoploss_on_exchange'] = False |  | ||||||
|             config['order_types']['stoploss_on_exchange'] = False |  | ||||||
|  |  | ||||||
|     def _load_strategy( |     def _load_strategy( | ||||||
|             self, strategy_name: str, config: dict, extra_dir: Optional[str] = None) -> IStrategy: |             self, strategy_name: str, config: dict, extra_dir: Optional[str] = None) -> IStrategy: | ||||||
|         """ |         """ | ||||||
|   | |||||||
| @@ -272,34 +272,6 @@ def test_strategy_override_order_types(caplog): | |||||||
|         StrategyResolver(config) |         StrategyResolver(config) | ||||||
|  |  | ||||||
|  |  | ||||||
| def test_strategy_override_order_types_dryrun(caplog): |  | ||||||
|     caplog.set_level(logging.INFO) |  | ||||||
|  |  | ||||||
|     order_types = { |  | ||||||
|         'buy': 'market', |  | ||||||
|         'sell': 'limit', |  | ||||||
|         'stoploss': 'limit', |  | ||||||
|         'stoploss_on_exchange': True, |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     config = { |  | ||||||
|         'strategy': 'DefaultStrategy', |  | ||||||
|         'order_types': order_types, |  | ||||||
|         'dry_run': True, |  | ||||||
|     } |  | ||||||
|     resolver = StrategyResolver(config) |  | ||||||
|  |  | ||||||
|     assert resolver.strategy.order_types |  | ||||||
|     for method in ['buy', 'sell', 'stoploss', 'stoploss_on_exchange']: |  | ||||||
|         assert resolver.strategy.order_types[method] == order_types[method] |  | ||||||
|  |  | ||||||
|     assert log_has("Disabling stoploss_on_exchange during dry-run.", caplog.record_tuples) |  | ||||||
|  |  | ||||||
|     assert log_has("Override strategy 'order_types' with value in config file:" |  | ||||||
|                    " {'buy': 'market', 'sell': 'limit', 'stoploss': 'limit'," |  | ||||||
|                    " 'stoploss_on_exchange': False}.", caplog.record_tuples) |  | ||||||
|  |  | ||||||
|  |  | ||||||
| def test_strategy_override_order_tif(caplog): | def test_strategy_override_order_tif(caplog): | ||||||
|     caplog.set_level(logging.INFO) |     caplog.set_level(logging.INFO) | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user