Simplify enter_positions and add global pairlock check

This commit is contained in:
Matthias 2020-10-24 14:46:13 +02:00
parent 05be33ccd4
commit ff7ba23477
2 changed files with 34 additions and 4 deletions

View File

@ -180,8 +180,10 @@ class FreqtradeBot:
# First process current opened trades (positions) # First process current opened trades (positions)
self.exit_positions(trades) self.exit_positions(trades)
# Evaluate if protections should apply
self.protections.global_stop()
# Then looking for buy opportunities # Then looking for buy opportunities
if self.get_free_open_trades() and not self.protections.global_stop(): if self.get_free_open_trades():
self.enter_positions() self.enter_positions()
Trade.session.flush() Trade.session.flush()
@ -361,6 +363,9 @@ class FreqtradeBot:
logger.info("No currency pair in active pair whitelist, " logger.info("No currency pair in active pair whitelist, "
"but checking to sell open trades.") "but checking to sell open trades.")
return trades_created return trades_created
if PairLocks.is_global_lock():
logger.info("Global pairlock active. Not creating new trades.")
return trades_created
# Create entity and execute trade for each pair from whitelist # Create entity and execute trade for each pair from whitelist
for pair in whitelist: for pair in whitelist:
try: try:
@ -369,8 +374,7 @@ class FreqtradeBot:
logger.warning('Unable to create trade for %s: %s', pair, exception) logger.warning('Unable to create trade for %s: %s', pair, exception)
if not trades_created: if not trades_created:
logger.debug("Found no buy signals for whitelisted currencies. " logger.debug("Found no buy signals for whitelisted currencies. Trying again...")
"Trying again...")
return trades_created return trades_created

View File

@ -15,7 +15,7 @@ from freqtrade.exceptions import (DependencyException, ExchangeError, Insufficie
InvalidOrderException, OperationalException, PricingError, InvalidOrderException, OperationalException, PricingError,
TemporaryError) TemporaryError)
from freqtrade.freqtradebot import FreqtradeBot from freqtrade.freqtradebot import FreqtradeBot
from freqtrade.persistence import Order, Trade from freqtrade.persistence import Order, PairLocks, Trade
from freqtrade.persistence.models import PairLock from freqtrade.persistence.models import PairLock
from freqtrade.rpc import RPCMessageType from freqtrade.rpc import RPCMessageType
from freqtrade.state import RunMode, State from freqtrade.state import RunMode, State
@ -678,6 +678,32 @@ def test_enter_positions_no_pairs_in_whitelist(default_conf, ticker, limit_buy_o
assert log_has("Active pair whitelist is empty.", caplog) assert log_has("Active pair whitelist is empty.", caplog)
@pytest.mark.usefixtures("init_persistence")
def test_enter_positions_global_pairlock(default_conf, ticker, limit_buy_order, fee,
mocker, caplog) -> None:
patch_RPCManager(mocker)
patch_exchange(mocker)
mocker.patch.multiple(
'freqtrade.exchange.Exchange',
fetch_ticker=ticker,
buy=MagicMock(return_value={'id': limit_buy_order['id']}),
get_fee=fee,
)
freqtrade = FreqtradeBot(default_conf)
patch_get_signal(freqtrade)
n = freqtrade.enter_positions()
message = "Global pairlock active. Not creating new trades."
n = freqtrade.enter_positions()
# 0 trades, but it's not because of pairlock.
assert n == 0
assert not log_has(message, caplog)
PairLocks.lock_pair('*', arrow.utcnow().shift(minutes=20).datetime, 'Just because')
n = freqtrade.enter_positions()
assert n == 0
assert log_has(message, caplog)
def test_create_trade_no_signal(default_conf, fee, mocker) -> None: def test_create_trade_no_signal(default_conf, fee, mocker) -> None:
default_conf['dry_run'] = True default_conf['dry_run'] = True