execute sell if get_signal OR ROI reached

This commit is contained in:
kryofly 2017-12-29 00:00:30 +01:00
parent 0abf0b0e39
commit 847dde0d65
2 changed files with 44 additions and 8 deletions

View File

@ -179,18 +179,21 @@ def handle_trade(trade: Trade) -> bool:
current_rate = exchange.get_ticker(trade.pair)['bid'] current_rate = exchange.get_ticker(trade.pair)['bid']
# Check if minimal roi has been reached # Check if minimal roi has been reached
if not min_roi_reached(trade, current_rate, datetime.utcnow()): if min_roi_reached(trade, current_rate, datetime.utcnow()):
return False logger.debug('Executing sell due to ROI ...')
execute_sell(trade, current_rate)
return True
# Check if sell signal has been enabled and triggered # Check if sell signal has been enabled and triggered
if _CONF.get('experimental', {}).get('use_sell_signal'): if _CONF.get('experimental', {}).get('use_sell_signal'):
logger.debug('Checking sell_signal ...') logger.debug('Checking sell_signal ...')
if not get_signal(trade.pair, SignalType.SELL): if get_signal(trade.pair, SignalType.SELL):
return False logger.debug('Executing sell due to sell signal ...')
execute_sell(trade, current_rate) execute_sell(trade, current_rate)
return True return True
return False
def get_target_bid(ticker: Dict[str, float]) -> float: def get_target_bid(ticker: Dict[str, float]) -> float:
""" Calculates bid target between current ask price and last price """ """ Calculates bid target between current ask price and last price """

View File

@ -216,8 +216,7 @@ def test_handle_trade(default_conf, limit_buy_order, limit_sell_order, mocker):
assert trade.calc_profit() == 0.00006217 assert trade.calc_profit() == 0.00006217
assert trade.close_date is not None assert trade.close_date is not None
def test_handle_trade_roi(default_conf, ticker, limit_buy_order, mocker, caplog):
def test_handle_trade_experimental(default_conf, ticker, limit_buy_order, mocker, caplog):
default_conf.update({'experimental': {'use_sell_signal': True}}) default_conf.update({'experimental': {'use_sell_signal': True}})
mocker.patch.dict('freqtrade.main._CONF', default_conf) mocker.patch.dict('freqtrade.main._CONF', default_conf)
@ -235,10 +234,44 @@ def test_handle_trade_experimental(default_conf, ticker, limit_buy_order, mocker
trade = Trade.query.first() trade = Trade.query.first()
trade.is_open = True trade.is_open = True
# FIX: sniffing logs, suggest handle_trade should not execute_sell
# instead that responsibility should be moved out of handle_trade(),
# we might just want to check if we are in a sell condition without
# executing
# if ROI is reached we must sell
mocker.patch('freqtrade.main.get_signal', side_effect=lambda s, t: False)
assert handle_trade(trade)
assert ('freqtrade', logging.DEBUG, 'Executing sell due to ROI ...') in caplog.record_tuples
# if ROI is reached we must sell even if sell-signal is not signalled
mocker.patch('freqtrade.main.get_signal', side_effect=lambda s, t: True)
assert handle_trade(trade)
assert ('freqtrade', logging.DEBUG, 'Executing sell due to ROI ...') in caplog.record_tuples
def test_handle_trade_experimental(default_conf, ticker, limit_buy_order, mocker, caplog):
default_conf.update({'experimental': {'use_sell_signal': True}})
mocker.patch.dict('freqtrade.main._CONF', default_conf)
mocker.patch('freqtrade.main.get_signal', side_effect=lambda s, t: True)
mocker.patch.multiple('freqtrade.rpc', init=MagicMock(), send_msg=MagicMock())
mocker.patch.multiple('freqtrade.main.exchange',
validate_pairs=MagicMock(),
get_ticker=ticker,
buy=MagicMock(return_value='mocked_limit_buy'))
mocker.patch('freqtrade.main.min_roi_reached', return_value=False)
init(default_conf, create_engine('sqlite://'))
create_trade(0.001)
trade = Trade.query.first()
trade.is_open = True
mocker.patch('freqtrade.main.get_signal', side_effect=lambda s, t: False) mocker.patch('freqtrade.main.get_signal', side_effect=lambda s, t: False)
value_returned = handle_trade(trade) value_returned = handle_trade(trade)
assert ('freqtrade', logging.DEBUG, 'Checking sell_signal ...') in caplog.record_tuples assert ('freqtrade', logging.DEBUG, 'Checking sell_signal ...') in caplog.record_tuples
assert value_returned is False assert value_returned is False
mocker.patch('freqtrade.main.get_signal', side_effect=lambda s, t: True)
assert handle_trade(trade)
assert ('freqtrade', logging.DEBUG, 'Executing sell due to sell signal ...') in caplog.record_tuples
def test_close_trade(default_conf, ticker, limit_buy_order, limit_sell_order, mocker): def test_close_trade(default_conf, ticker, limit_buy_order, limit_sell_order, mocker):