def test_PairLocks_getlongestlock(use_db): PairLocks.timeframe = '5m' # No lock should be present PairLocks.use_db = use_db if use_db: assert len(PairLock.query.all()) == 0 assert PairLocks.use_db == use_db pair = 'ETH/BTC' assert not PairLocks.is_pair_locked(pair) PairLocks.lock_pair(pair, arrow.utcnow().shift(minutes=4).datetime) # ETH/BTC locked for 4 minutes assert PairLocks.is_pair_locked(pair) lock = PairLocks.get_pair_longest_lock(pair) assert lock.lock_end_time.replace( tzinfo=timezone.utc) > arrow.utcnow().shift(minutes=3) assert lock.lock_end_time.replace( tzinfo=timezone.utc) < arrow.utcnow().shift(minutes=14) PairLocks.lock_pair(pair, arrow.utcnow().shift(minutes=15).datetime) assert PairLocks.is_pair_locked(pair) lock = PairLocks.get_pair_longest_lock(pair) # Must be longer than above assert lock.lock_end_time.replace( tzinfo=timezone.utc) > arrow.utcnow().shift(minutes=14) PairLocks.reset_locks() PairLocks.use_db = True
def prepare_backtest(self, enable_protections): """ Backtesting setup method - called once for every call to "backtest()". """ PairLocks.use_db = False PairLocks.timeframe = self.config['timeframe'] Trade.use_db = False PairLocks.reset_locks() Trade.reset_trades()
def prepare_backtest(self, enable_protections): """ Backtesting setup method - called once for every call to "backtest()". """ PairLocks.use_db = False Trade.use_db = False if enable_protections: # Reset persisted data - used for protections only PairLocks.reset_locks() Trade.reset_trades()
def prepare_backtest(self, enable_protections): """ Backtesting setup method - called once for every call to "backtest()". """ PairLocks.use_db = False PairLocks.timeframe = self.config['timeframe'] Trade.use_db = False PairLocks.reset_locks() Trade.reset_trades() self.rejected_trades = 0 self.dataprovider.clear_cache() self._load_protections(self.strategy)
def test_PairLocks_reason(use_db): PairLocks.timeframe = '5m' PairLocks.use_db = use_db # No lock should be present if use_db: assert len(PairLock.query.all()) == 0 assert PairLocks.use_db == use_db PairLocks.lock_pair('XRP/USDT', arrow.utcnow().shift(minutes=4).datetime, 'TestLock1') PairLocks.lock_pair('ETH/USDT', arrow.utcnow().shift(minutes=4).datetime, 'TestLock2') assert PairLocks.is_pair_locked('XRP/USDT') assert PairLocks.is_pair_locked('ETH/USDT') PairLocks.unlock_reason('TestLock1') assert not PairLocks.is_pair_locked('XRP/USDT') assert PairLocks.is_pair_locked('ETH/USDT') PairLocks.reset_locks() PairLocks.use_db = True
def __init__(self, config: Dict[str, Any]) -> None: LoggingMixin.show_output = False self.config = config # Reset keys for backtesting remove_credentials(self.config) self.strategylist: List[IStrategy] = [] self.all_results: Dict[str, Dict] = {} self.exchange = ExchangeResolver.load_exchange( self.config['exchange']['name'], self.config) self.dataprovider = DataProvider(self.config, None) if self.config.get('strategy_list', None): for strat in list(self.config['strategy_list']): stratconf = deepcopy(self.config) stratconf['strategy'] = strat self.strategylist.append( StrategyResolver.load_strategy(stratconf)) validate_config_consistency(stratconf) else: # No strategy list specified, only one strategy self.strategylist.append( StrategyResolver.load_strategy(self.config)) validate_config_consistency(self.config) if "timeframe" not in self.config: raise OperationalException( "Timeframe (ticker interval) needs to be set in either " "configuration or as cli argument `--timeframe 5m`") self.timeframe = str(self.config.get('timeframe')) self.timeframe_min = timeframe_to_minutes(self.timeframe) self.pairlists = PairListManager(self.exchange, self.config) if 'VolumePairList' in self.pairlists.name_list: raise OperationalException( "VolumePairList not allowed for backtesting.") if 'PerformanceFilter' in self.pairlists.name_list: raise OperationalException( "PerformanceFilter not allowed for backtesting.") if len(self.strategylist ) > 1 and 'PrecisionFilter' in self.pairlists.name_list: raise OperationalException( "PrecisionFilter not allowed for backtesting multiple strategies." ) self.dataprovider.add_pairlisthandler(self.pairlists) self.pairlists.refresh_pairlist() if len(self.pairlists.whitelist) == 0: raise OperationalException("No pair in whitelist.") if config.get('fee', None) is not None: self.fee = config['fee'] else: self.fee = self.exchange.get_fee( symbol=self.pairlists.whitelist[0]) Trade.use_db = False Trade.reset_trades() PairLocks.timeframe = self.config['timeframe'] PairLocks.use_db = False PairLocks.reset_locks() self.wallets = Wallets(self.config, self.exchange, log=False) # Get maximum required startup period self.required_startup = max( [strat.startup_candle_count for strat in self.strategylist])
def test_PairLocks(use_db): PairLocks.timeframe = '5m' PairLocks.use_db = use_db # No lock should be present if use_db: assert len(PairLock.query.all()) == 0 assert PairLocks.use_db == use_db pair = 'ETH/BTC' assert not PairLocks.is_pair_locked(pair) PairLocks.lock_pair(pair, arrow.utcnow().shift(minutes=4).datetime) # ETH/BTC locked for 4 minutes assert PairLocks.is_pair_locked(pair) # XRP/BTC should not be locked now pair = 'XRP/BTC' assert not PairLocks.is_pair_locked(pair) # Unlocking a pair that's not locked should not raise an error PairLocks.unlock_pair(pair) PairLocks.lock_pair(pair, arrow.utcnow().shift(minutes=4).datetime) assert PairLocks.is_pair_locked(pair) # Get both locks from above locks = PairLocks.get_pair_locks(None) assert len(locks) == 2 # Unlock original pair pair = 'ETH/BTC' PairLocks.unlock_pair(pair) assert not PairLocks.is_pair_locked(pair) assert not PairLocks.is_global_lock() pair = 'BTC/USDT' # Lock until 14:30 lock_time = datetime(2020, 5, 1, 14, 30, 0, tzinfo=timezone.utc) PairLocks.lock_pair(pair, lock_time) assert not PairLocks.is_pair_locked(pair) assert PairLocks.is_pair_locked(pair, lock_time + timedelta(minutes=-10)) assert not PairLocks.is_global_lock(lock_time + timedelta(minutes=-10)) assert PairLocks.is_pair_locked(pair, lock_time + timedelta(minutes=-50)) assert not PairLocks.is_global_lock(lock_time + timedelta(minutes=-50)) # Should not be locked after time expired assert not PairLocks.is_pair_locked(pair, lock_time + timedelta(minutes=10)) locks = PairLocks.get_pair_locks(pair, lock_time + timedelta(minutes=-2)) assert len(locks) == 1 assert 'PairLock' in str(locks[0]) # Unlock all PairLocks.unlock_pair(pair, lock_time + timedelta(minutes=-2)) assert not PairLocks.is_global_lock(lock_time + timedelta(minutes=-50)) # Global lock PairLocks.lock_pair('*', lock_time) assert PairLocks.is_global_lock(lock_time + timedelta(minutes=-50)) # Global lock also locks every pair seperately assert PairLocks.is_pair_locked(pair, lock_time + timedelta(minutes=-50)) assert PairLocks.is_pair_locked('XRP/USDT', lock_time + timedelta(minutes=-50)) if use_db: locks = PairLocks.get_all_locks() locks_db = PairLock.query.all() assert len(locks) == len(locks_db) assert len(locks_db) > 0 else: # Nothing was pushed to the database assert len(PairLocks.get_all_locks()) > 0 assert len(PairLock.query.all()) == 0 # Reset use-db variable PairLocks.reset_locks() PairLocks.use_db = True