From 1e5b0e87269834c9a24e1f5d687e9f58bc67c2d4 Mon Sep 17 00:00:00 2001 From: gcarq Date: Wed, 1 Nov 2017 00:26:32 +0100 Subject: [PATCH] adapt tests --- freqtrade/tests/test_backtesting.py | 4 +- freqtrade/tests/test_hyperopt.py | 2 +- freqtrade/tests/test_main.py | 46 +++++++++++--- freqtrade/tests/test_persistence.py | 21 ------- freqtrade/tests/test_telegram.py | 96 ++++++++++++++++++++++++++--- 5 files changed, 127 insertions(+), 42 deletions(-) delete mode 100644 freqtrade/tests/test_persistence.py diff --git a/freqtrade/tests/test_backtesting.py b/freqtrade/tests/test_backtesting.py index 4bd04e721..deabebfb9 100644 --- a/freqtrade/tests/test_backtesting.py +++ b/freqtrade/tests/test_backtesting.py @@ -11,7 +11,7 @@ from freqtrade.analyze import analyze_ticker from freqtrade.main import should_sell from freqtrade.persistence import Trade -logging.disable(logging.DEBUG) # disable debug logs that slow backtesting a lot +logging.disable(logging.DEBUG) # disable debug logs that slow backtesting a lot def format_results(results): @@ -63,7 +63,7 @@ def backtest(conf, pairs, mocker): # calculate win/lose forwards from buy point for row2 in ticker[row.Index:].itertuples(index=True): if should_sell(trade, row2.close, row2.date): - current_profit = (row2.close - trade.open_rate) / trade.open_rate + current_profit = trade.calc_profit(row2.close) trades.append((pair, current_profit, row2.Index - row.Index)) break diff --git a/freqtrade/tests/test_hyperopt.py b/freqtrade/tests/test_hyperopt.py index 5fedff519..5053cbfcd 100644 --- a/freqtrade/tests/test_hyperopt.py +++ b/freqtrade/tests/test_hyperopt.py @@ -12,7 +12,7 @@ from pandas import DataFrame from freqtrade.tests.test_backtesting import backtest, format_results from freqtrade.vendor.qtpylib.indicators import crossed_above -logging.disable(logging.DEBUG) # disable debug logs that slow backtesting a lot +logging.disable(logging.DEBUG) # disable debug logs that slow backtesting a lot # set TARGET_TRADES to suit your number concurrent trades so its realistic to 20days of data TARGET_TRADES = 1200 diff --git a/freqtrade/tests/test_main.py b/freqtrade/tests/test_main.py index 67e66bd23..c098c3dc0 100644 --- a/freqtrade/tests/test_main.py +++ b/freqtrade/tests/test_main.py @@ -1,5 +1,6 @@ # pragma pylint: disable=missing-docstring import copy +from datetime import datetime from unittest.mock import MagicMock, call import pytest @@ -60,23 +61,36 @@ def test_create_trade(conf, mocker): 'ask': 0.072661, 'last': 0.07256061 }), - buy=MagicMock(return_value='mocked_order_id')) + buy=MagicMock(return_value='mocked_limit_buy')) # Save state of current whitelist whitelist = copy.deepcopy(conf['exchange']['pair_whitelist']) init(conf, 'sqlite://') - for pair in ['BTC_ETH', 'BTC_TKN', 'BTC_TRST', 'BTC_SWT']: + for _ in ['BTC_ETH', 'BTC_TKN', 'BTC_TRST', 'BTC_SWT']: trade = create_trade(15.0) Trade.session.add(trade) Trade.session.flush() assert trade is not None - assert trade.open_rate == 0.072661 - assert trade.pair == pair - assert trade.exchange == Exchanges.BITTREX.name - assert trade.amount == 206.43811673387373 assert trade.stake_amount == 15.0 assert trade.is_open assert trade.open_date is not None + assert trade.exchange == Exchanges.BITTREX.name + + # Simulate fulfilled LIMIT_BUY order for trade + trade.update({ + 'id': 'mocked_limit_buy', + 'type': 'LIMIT_BUY', + 'pair': 'mocked', + 'opened': datetime.utcnow(), + 'rate': 0.072661, + 'amount': 206.43811673387373, + 'remaining': 0.0, + 'closed': datetime.utcnow(), + }) + + assert trade.open_rate == 0.072661 + assert trade.amount == 206.43811673387373 + assert whitelist == conf['exchange']['pair_whitelist'] buy_signal.assert_has_calls( @@ -94,14 +108,28 @@ def test_handle_trade(conf, mocker): 'ask': 0.172661, 'last': 0.17256061 }), - buy=MagicMock(return_value='mocked_order_id')) + sell=MagicMock(return_value='mocked_limit_sell')) trade = Trade.query.filter(Trade.is_open.is_(True)).first() assert trade + handle_trade(trade) + assert trade.open_order_id == 'mocked_limit_sell' + + # Simulate fulfilled LIMIT_SELL order for trade + trade.update({ + 'id': 'mocked_sell_limit', + 'type': 'LIMIT_SELL', + 'pair': 'mocked', + 'opened': datetime.utcnow(), + 'rate': 0.17256061, + 'amount': 206.43811673387373, + 'remaining': 0.0, + 'closed': datetime.utcnow(), + }) + assert trade.close_rate == 0.17256061 - assert trade.close_profit == 137.4872490056564 + assert trade.close_profit == 1.3748724900565639 assert trade.close_date is not None - assert trade.open_order_id == 'dry_run' def test_close_trade(conf, mocker): diff --git a/freqtrade/tests/test_persistence.py b/freqtrade/tests/test_persistence.py deleted file mode 100644 index 6a74ad715..000000000 --- a/freqtrade/tests/test_persistence.py +++ /dev/null @@ -1,21 +0,0 @@ -# pragma pylint: disable=missing-docstring -from freqtrade.exchange import Exchanges -from freqtrade.persistence import Trade - - -def test_exec_sell_order(mocker): - api_mock = mocker.patch('freqtrade.main.exchange.sell', side_effect='mocked_order_id') - trade = Trade( - pair='BTC_ETH', - stake_amount=1.00, - open_rate=0.50, - amount=10.00, - exchange=Exchanges.BITTREX, - open_order_id='mocked' - ) - profit = trade.exec_sell_order(1.00, 10.00) - api_mock.assert_called_once_with('BTC_ETH', 1.0, 10.0) - assert profit == 100.0 - assert trade.close_rate == 1.0 - assert trade.close_profit == profit - assert trade.close_date is not None diff --git a/freqtrade/tests/test_telegram.py b/freqtrade/tests/test_telegram.py index 354abb086..8a6018129 100644 --- a/freqtrade/tests/test_telegram.py +++ b/freqtrade/tests/test_telegram.py @@ -46,6 +46,7 @@ def conf(): validate(configuration, CONF_SCHEMA) return configuration + @pytest.fixture def update(): _update = Update(0) @@ -78,8 +79,27 @@ def test_status_handle(conf, update, mocker): Trade.session.add(trade) Trade.session.flush() + # Trigger status while we don't know the open_rate yet _status(bot=MagicBot(), update=update) - assert msg_mock.call_count == 2 + + # Simulate fulfilled LIMIT_BUY order for trade + trade.update({ + 'id': 'mocked_limit_buy', + 'type': 'LIMIT_BUY', + 'pair': 'mocked', + 'opened': datetime.utcnow(), + 'rate': 0.07256060, + 'amount': 206.43811673387373, + 'remaining': 0.0, + 'closed': datetime.utcnow(), + }) + Trade.session.flush() + + # Trigger status while we have a fulfilled order for the open trade + _status(bot=MagicBot(), update=update) + + assert msg_mock.call_count == 3 + assert 'Waiting until order is fulfilled' in msg_mock.call_args_list[-2][0][0] assert '[BTC_ETH]' in msg_mock.call_args_list[-1][0][0] @@ -95,14 +115,36 @@ def test_profit_handle(conf, update, mocker): 'ask': 0.072661, 'last': 0.07256061 }), - buy=MagicMock(return_value='mocked_order_id')) + buy=MagicMock(return_value='mocked_limit_buy')) init(conf, 'sqlite://') # Create some test data trade = create_trade(15.0) assert trade - trade.close_rate = 0.07256061 - trade.close_profit = 100.00 + + # Simulate fulfilled LIMIT_BUY order for trade + trade.update({ + 'id': 'mocked_limit_buy', + 'type': 'LIMIT_BUY', + 'pair': 'mocked', + 'opened': datetime.utcnow(), + 'rate': 0.07256061, + 'amount': 206.43811673387373, + 'remaining': 0.0, + 'closed': datetime.utcnow(), + }) + # Simulate fulfilled LIMIT_SELL order for trade + trade.update({ + 'id': 'mocked_limit_sell', + 'type': 'LIMIT_SELL', + 'pair': 'mocked', + 'opened': datetime.utcnow(), + 'rate': 0.0802134, + 'amount': 206.43811673387373, + 'remaining': 0.0, + 'closed': datetime.utcnow(), + }) + trade.close_date = datetime.utcnow() trade.open_order_id = None trade.is_open = False @@ -111,7 +153,8 @@ def test_profit_handle(conf, update, mocker): _profit(bot=MagicBot(), update=update) assert msg_mock.call_count == 2 - assert '(100.00%)' in msg_mock.call_args_list[-1][0][0] + assert '*ROI:* `1.582013 (10.55%)`' in msg_mock.call_args_list[-1][0][0] + assert 'Best Performing:* `BTC_ETH: 10.55%`' in msg_mock.call_args_list[-1][0][0] def test_forcesell_handle(conf, update, mocker): @@ -132,6 +175,19 @@ def test_forcesell_handle(conf, update, mocker): # Create some test data trade = create_trade(15.0) assert trade + + # Simulate fulfilled LIMIT_BUY order for trade + trade.update({ + 'id': 'mocked_limit_buy', + 'type': 'LIMIT_BUY', + 'pair': 'mocked', + 'opened': datetime.utcnow(), + 'rate': 0.07256060, + 'amount': 206.43811673387373, + 'remaining': 0.0, + 'closed': datetime.utcnow(), + }) + Trade.session.add(trade) Trade.session.flush() @@ -161,10 +217,32 @@ def test_performance_handle(conf, update, mocker): # Create some test data trade = create_trade(15.0) assert trade - trade.close_rate = 0.07256061 - trade.close_profit = 100.00 + + # Simulate fulfilled LIMIT_BUY order for trade + trade.update({ + 'id': 'mocked_limit_buy', + 'type': 'LIMIT_BUY', + 'pair': 'mocked', + 'opened': datetime.utcnow(), + 'rate': 0.07256061, + 'amount': 206.43811673387373, + 'remaining': 0.0, + 'closed': datetime.utcnow(), + }) + + # Simulate fulfilled LIMIT_SELL order for trade + trade.update({ + 'id': 'mocked_limit_sell', + 'type': 'LIMIT_SELL', + 'pair': 'mocked', + 'opened': datetime.utcnow(), + 'rate': 0.0802134, + 'amount': 206.43811673387373, + 'remaining': 0.0, + 'closed': datetime.utcnow(), + }) + trade.close_date = datetime.utcnow() - trade.open_order_id = None trade.is_open = False Trade.session.add(trade) Trade.session.flush() @@ -172,7 +250,7 @@ def test_performance_handle(conf, update, mocker): _performance(bot=MagicBot(), update=update) assert msg_mock.call_count == 2 assert 'Performance' in msg_mock.call_args_list[-1][0][0] - assert 'BTC_ETH 100.00%' in msg_mock.call_args_list[-1][0][0] + assert 'BTC_ETH\t10.55%' in msg_mock.call_args_list[-1][0][0] def test_start_handle(conf, update, mocker):