diff --git a/freqtrade/analyze.py b/freqtrade/analyze.py index e13d2b39d..3fdfa4ff2 100644 --- a/freqtrade/analyze.py +++ b/freqtrade/analyze.py @@ -1,3 +1,4 @@ +from enum import Enum import logging from datetime import timedelta @@ -10,6 +11,10 @@ from freqtrade.vendor.qtpylib.indicators import awesome_oscillator logger = logging.getLogger(__name__) +class SignalType(Enum): + BUY = "buy" + SELL = "sell" + def parse_ticker_dataframe(ticker: list) -> DataFrame: """ @@ -90,9 +95,9 @@ def analyze_ticker(pair: str) -> DataFrame: return dataframe -def get_buy_signal(pair: str) -> bool: +def get_signal(pair: str, signal: SignalType) -> bool: """ - Calculates a buy signal based several technical analysis indicators + Calculates current signal based several technical analysis indicators :param pair: pair in format BTC_ANT or BTC-ANT :return: True if pair is good for buying, False otherwise """ @@ -107,6 +112,6 @@ def get_buy_signal(pair: str) -> bool: if signal_date < arrow.now() - timedelta(minutes=10): return False - signal = latest['buy'] == 1 - logger.debug('buy_trigger: %s (pair=%s, signal=%s)', latest['date'], pair, signal) - return signal + result = latest[signal.value] == 1 + logger.debug('%s_trigger: %s (pair=%s, signal=%s)', signal.value, latest['date'], pair, result) + return result diff --git a/freqtrade/main.py b/freqtrade/main.py index 79f39a32f..3fb0dd64d 100755 --- a/freqtrade/main.py +++ b/freqtrade/main.py @@ -13,9 +13,10 @@ from cachetools import cached, TTLCache from jsonschema import validate from freqtrade import __version__, exchange, persistence -from freqtrade.analyze import get_buy_signal -from freqtrade.misc import CONF_SCHEMA, State, get_state, update_state, build_arg_parser, throttle, \ - FreqtradeException +from freqtrade.analyze import get_signal, SignalType +from freqtrade.misc import ( + CONF_SCHEMA, State, get_state, update_state, build_arg_parser, throttle, FreqtradeException +) from freqtrade.persistence import Trade from freqtrade.rpc import telegram @@ -223,7 +224,7 @@ def create_trade(stake_amount: float) -> Optional[Trade]: # Pick pair based on StochRSI buy signals for _pair in whitelist: - if get_buy_signal(_pair): + if get_signal(_pair, SignalType.BUY): pair = _pair break else: diff --git a/freqtrade/tests/test_analyze.py b/freqtrade/tests/test_analyze.py index 3c0ce15b2..741e72191 100644 --- a/freqtrade/tests/test_analyze.py +++ b/freqtrade/tests/test_analyze.py @@ -5,7 +5,7 @@ import pytest from pandas import DataFrame from freqtrade.analyze import parse_ticker_dataframe, populate_buy_trend, populate_indicators, \ - get_buy_signal + get_signal, SignalType @pytest.fixture @@ -32,8 +32,8 @@ def test_populates_buy_trend(result): def test_returns_latest_buy_signal(mocker): buydf = DataFrame([{'buy': 1, 'date': datetime.today()}]) mocker.patch('freqtrade.analyze.analyze_ticker', return_value=buydf) - assert get_buy_signal('BTC-ETH') + assert get_signal('BTC-ETH', SignalType.BUY) buydf = DataFrame([{'buy': 0, 'date': datetime.today()}]) mocker.patch('freqtrade.analyze.analyze_ticker', return_value=buydf) - assert not get_buy_signal('BTC-ETH') + assert not get_signal('BTC-ETH', SignalType.BUY) diff --git a/freqtrade/tests/test_main.py b/freqtrade/tests/test_main.py index 74a776f22..5be1dce3e 100644 --- a/freqtrade/tests/test_main.py +++ b/freqtrade/tests/test_main.py @@ -16,7 +16,7 @@ from freqtrade.persistence import Trade def test_process_trade_creation(default_conf, ticker, health, mocker): mocker.patch.dict('freqtrade.main._CONF', default_conf) mocker.patch.multiple('freqtrade.main.telegram', init=MagicMock(), send_msg=MagicMock()) - mocker.patch('freqtrade.main.get_buy_signal', side_effect=lambda _: True) + mocker.patch('freqtrade.main.get_signal', side_effect=lambda s, t: True) mocker.patch.multiple('freqtrade.main.exchange', validate_pairs=MagicMock(), get_ticker=ticker, @@ -45,7 +45,7 @@ def test_process_trade_creation(default_conf, ticker, health, mocker): def test_process_exchange_failures(default_conf, ticker, health, mocker): mocker.patch.dict('freqtrade.main._CONF', default_conf) mocker.patch.multiple('freqtrade.main.telegram', init=MagicMock(), send_msg=MagicMock()) - mocker.patch('freqtrade.main.get_buy_signal', side_effect=lambda _: True) + mocker.patch('freqtrade.main.get_signal', side_effect=lambda s, t: True) sleep_mock = mocker.patch('time.sleep', side_effect=lambda _: None) mocker.patch.multiple('freqtrade.main.exchange', validate_pairs=MagicMock(), @@ -62,7 +62,7 @@ def test_process_runtime_error(default_conf, ticker, health, mocker): msg_mock = MagicMock() mocker.patch.dict('freqtrade.main._CONF', default_conf) mocker.patch.multiple('freqtrade.main.telegram', init=MagicMock(), send_msg=msg_mock) - mocker.patch('freqtrade.main.get_buy_signal', side_effect=lambda _: True) + mocker.patch('freqtrade.main.get_signal', side_effect=lambda s, t: True) mocker.patch.multiple('freqtrade.main.exchange', validate_pairs=MagicMock(), get_ticker=ticker, @@ -80,7 +80,7 @@ def test_process_runtime_error(default_conf, ticker, health, mocker): def test_process_trade_handling(default_conf, ticker, limit_buy_order, health, mocker): mocker.patch.dict('freqtrade.main._CONF', default_conf) mocker.patch.multiple('freqtrade.main.telegram', init=MagicMock(), send_msg=MagicMock()) - mocker.patch('freqtrade.main.get_buy_signal', side_effect=lambda _: True) + mocker.patch('freqtrade.main.get_signal', side_effect=lambda s, t: True) mocker.patch.multiple('freqtrade.main.exchange', validate_pairs=MagicMock(), get_ticker=ticker, @@ -102,7 +102,7 @@ def test_process_trade_handling(default_conf, ticker, limit_buy_order, health, m def test_create_trade(default_conf, ticker, limit_buy_order, mocker): mocker.patch.dict('freqtrade.main._CONF', default_conf) - mocker.patch('freqtrade.main.get_buy_signal', side_effect=lambda _: True) + mocker.patch('freqtrade.main.get_signal', side_effect=lambda s, t: True) mocker.patch.multiple('freqtrade.main.telegram', init=MagicMock(), send_msg=MagicMock()) mocker.patch.multiple('freqtrade.main.exchange', validate_pairs=MagicMock(), @@ -132,7 +132,7 @@ def test_create_trade(default_conf, ticker, limit_buy_order, mocker): def test_create_trade_no_stake_amount(default_conf, ticker, mocker): mocker.patch.dict('freqtrade.main._CONF', default_conf) - mocker.patch('freqtrade.main.get_buy_signal', side_effect=lambda _: True) + mocker.patch('freqtrade.main.get_signal', side_effect=lambda s, t: True) mocker.patch.multiple('freqtrade.main.telegram', init=MagicMock(), send_msg=MagicMock()) mocker.patch.multiple('freqtrade.main.exchange', validate_pairs=MagicMock(), @@ -145,7 +145,7 @@ def test_create_trade_no_stake_amount(default_conf, ticker, mocker): def test_create_trade_no_pairs(default_conf, ticker, mocker): mocker.patch.dict('freqtrade.main._CONF', default_conf) - mocker.patch('freqtrade.main.get_buy_signal', side_effect=lambda _: True) + mocker.patch('freqtrade.main.get_signal', side_effect=lambda s, t: True) mocker.patch.multiple('freqtrade.main.telegram', init=MagicMock(), send_msg=MagicMock()) mocker.patch.multiple('freqtrade.main.exchange', validate_pairs=MagicMock(), @@ -161,7 +161,7 @@ def test_create_trade_no_pairs(default_conf, ticker, mocker): def test_handle_trade(default_conf, limit_buy_order, limit_sell_order, mocker): mocker.patch.dict('freqtrade.main._CONF', default_conf) - mocker.patch('freqtrade.main.get_buy_signal', side_effect=lambda _: True) + mocker.patch('freqtrade.main.get_signal', side_effect=lambda s, t: True) mocker.patch.multiple('freqtrade.main.telegram', init=MagicMock(), send_msg=MagicMock()) mocker.patch.multiple('freqtrade.main.exchange', validate_pairs=MagicMock(), @@ -194,7 +194,7 @@ def test_handle_trade(default_conf, limit_buy_order, limit_sell_order, mocker): def test_close_trade(default_conf, ticker, limit_buy_order, limit_sell_order, mocker): mocker.patch.dict('freqtrade.main._CONF', default_conf) - mocker.patch('freqtrade.main.get_buy_signal', side_effect=lambda _: True) + mocker.patch('freqtrade.main.get_signal', side_effect=lambda s, t: True) mocker.patch.multiple('freqtrade.main.telegram', init=MagicMock(), send_msg=MagicMock()) mocker.patch.multiple('freqtrade.main.exchange', validate_pairs=MagicMock(), diff --git a/freqtrade/tests/test_telegram.py b/freqtrade/tests/test_telegram.py index 795901ced..c5ca9da3f 100644 --- a/freqtrade/tests/test_telegram.py +++ b/freqtrade/tests/test_telegram.py @@ -79,7 +79,7 @@ def test_authorized_only_exception(default_conf, mocker): def test_status_handle(default_conf, update, ticker, mocker): mocker.patch.dict('freqtrade.main._CONF', default_conf) - mocker.patch('freqtrade.main.get_buy_signal', side_effect=lambda _: True) + mocker.patch('freqtrade.main.get_signal', side_effect=lambda s, t: True) msg_mock = MagicMock() mocker.patch.multiple('freqtrade.main.telegram', _CONF=default_conf, @@ -117,7 +117,7 @@ def test_status_handle(default_conf, update, ticker, mocker): def test_status_table_handle(default_conf, update, ticker, mocker): mocker.patch.dict('freqtrade.main._CONF', default_conf) - mocker.patch('freqtrade.main.get_buy_signal', side_effect=lambda _: True) + mocker.patch('freqtrade.main.get_signal', side_effect=lambda s, t: True) msg_mock = MagicMock() mocker.patch.multiple( 'freqtrade.main.telegram', @@ -160,7 +160,7 @@ def test_status_table_handle(default_conf, update, ticker, mocker): def test_profit_handle(default_conf, update, ticker, limit_buy_order, limit_sell_order, mocker): mocker.patch.dict('freqtrade.main._CONF', default_conf) - mocker.patch('freqtrade.main.get_buy_signal', side_effect=lambda _: True) + mocker.patch('freqtrade.main.get_signal', side_effect=lambda s, t: True) msg_mock = MagicMock() mocker.patch.multiple('freqtrade.main.telegram', _CONF=default_conf, @@ -204,7 +204,7 @@ def test_profit_handle(default_conf, update, ticker, limit_buy_order, limit_sell def test_forcesell_handle(default_conf, update, ticker, mocker): mocker.patch.dict('freqtrade.main._CONF', default_conf) - mocker.patch('freqtrade.main.get_buy_signal', side_effect=lambda _: True) + mocker.patch('freqtrade.main.get_signal', side_effect=lambda s, t: True) msg_mock = MagicMock() mocker.patch.multiple('freqtrade.main.telegram', _CONF=default_conf, @@ -232,7 +232,7 @@ def test_forcesell_handle(default_conf, update, ticker, mocker): def test_forcesell_all_handle(default_conf, update, ticker, mocker): mocker.patch.dict('freqtrade.main._CONF', default_conf) - mocker.patch('freqtrade.main.get_buy_signal', side_effect=lambda _: True) + mocker.patch('freqtrade.main.get_signal', side_effect=lambda s, t: True) msg_mock = MagicMock() mocker.patch.multiple('freqtrade.main.telegram', _CONF=default_conf, @@ -260,7 +260,7 @@ def test_forcesell_all_handle(default_conf, update, ticker, mocker): def test_forcesell_handle_invalid(default_conf, update, mocker): mocker.patch.dict('freqtrade.main._CONF', default_conf) - mocker.patch('freqtrade.main.get_buy_signal', side_effect=lambda _: True) + mocker.patch('freqtrade.main.get_signal', side_effect=lambda s, t: True) msg_mock = MagicMock() mocker.patch.multiple('freqtrade.main.telegram', _CONF=default_conf, @@ -297,7 +297,7 @@ def test_forcesell_handle_invalid(default_conf, update, mocker): def test_performance_handle( default_conf, update, ticker, limit_buy_order, limit_sell_order, mocker): mocker.patch.dict('freqtrade.main._CONF', default_conf) - mocker.patch('freqtrade.main.get_buy_signal', side_effect=lambda _: True) + mocker.patch('freqtrade.main.get_signal', side_effect=lambda s, t: True) msg_mock = MagicMock() mocker.patch.multiple('freqtrade.main.telegram', _CONF=default_conf, @@ -331,7 +331,7 @@ def test_performance_handle( def test_count_handle(default_conf, update, ticker, mocker): mocker.patch.dict('freqtrade.main._CONF', default_conf) - mocker.patch('freqtrade.main.get_buy_signal', side_effect=lambda _: True) + mocker.patch('freqtrade.main.get_signal', side_effect=lambda s, t: True) msg_mock = MagicMock() mocker.patch.multiple( 'freqtrade.main.telegram', @@ -365,7 +365,7 @@ def test_count_handle(default_conf, update, ticker, mocker): def test_performance_handle_invalid(default_conf, update, mocker): mocker.patch.dict('freqtrade.main._CONF', default_conf) - mocker.patch('freqtrade.main.get_buy_signal', side_effect=lambda _: True) + mocker.patch('freqtrade.main.get_signal', side_effect=lambda s, t: True) msg_mock = MagicMock() mocker.patch.multiple('freqtrade.main.telegram', _CONF=default_conf,