From 3c521f55b234bd1f7109342c963d72cdf5fec9a3 Mon Sep 17 00:00:00 2001 From: Matthias Date: Thu, 10 Sep 2020 07:40:19 +0200 Subject: [PATCH] Add 6th mock trade --- tests/commands/test_commands.py | 3 +- tests/conftest.py | 5 ++- tests/conftest_trades.py | 68 +++++++++++++++++++++++++++++---- tests/data/test_btanalysis.py | 3 +- tests/test_freqtradebot.py | 18 ++++----- tests/test_persistence.py | 6 +-- 6 files changed, 81 insertions(+), 22 deletions(-) diff --git a/tests/commands/test_commands.py b/tests/commands/test_commands.py index 290a3fa77..a35fc9fb8 100644 --- a/tests/commands/test_commands.py +++ b/tests/commands/test_commands.py @@ -18,6 +18,7 @@ from freqtrade.state import RunMode from tests.conftest import (create_mock_trades, get_args, log_has, log_has_re, patch_exchange, patched_configuration_load_config_file) +from tests.conftest_trades import MOCK_TRADE_COUNT def test_setup_utils_configuration(): @@ -1116,7 +1117,7 @@ def test_show_trades(mocker, fee, capsys, caplog): pargs = get_args(args) pargs['config'] = None start_show_trades(pargs) - assert log_has("Printing 5 Trades: ", caplog) + assert log_has(f"Printing {MOCK_TRADE_COUNT} Trades: ", caplog) captured = capsys.readouterr() assert "Trade(id=1" in captured.out assert "Trade(id=2" in captured.out diff --git a/tests/conftest.py b/tests/conftest.py index 1ef495301..fe55c8784 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -23,7 +23,7 @@ from freqtrade.persistence import Trade from freqtrade.resolvers import ExchangeResolver from freqtrade.worker import Worker from tests.conftest_trades import (mock_trade_1, mock_trade_2, mock_trade_3, - mock_trade_4, mock_trade_5) + mock_trade_4, mock_trade_5, mock_trade_6) logging.getLogger('').setLevel(logging.INFO) @@ -189,6 +189,9 @@ def create_mock_trades(fee): trade = mock_trade_5(fee) Trade.session.add(trade) + trade = mock_trade_6(fee) + Trade.session.add(trade) + @pytest.fixture(autouse=True) def patch_coingekko(mocker) -> None: diff --git a/tests/conftest_trades.py b/tests/conftest_trades.py index 03af7fa1d..43bf15e51 100644 --- a/tests/conftest_trades.py +++ b/tests/conftest_trades.py @@ -1,6 +1,9 @@ from freqtrade.persistence.models import Order, Trade +MOCK_TRADE_COUNT = 6 + + def mock_order_1(): return { 'id': '1234', @@ -80,9 +83,9 @@ def mock_trade_2(fee): open_order_id='dry_run_sell_12345', strategy='DefaultStrategy', ) - o = Order.parse_from_ccxt_object(mock_order_2(), 'ETH/BTC', 'buy') + o = Order.parse_from_ccxt_object(mock_order_2(), 'ETC/BTC', 'buy') trade.orders.append(o) - o = Order.parse_from_ccxt_object(mock_order_2_sell(), 'ETH/BTC', 'sell') + o = Order.parse_from_ccxt_object(mock_order_2_sell(), 'ETC/BTC', 'sell') trade.orders.append(o) return trade @@ -132,9 +135,9 @@ def mock_trade_3(fee): exchange='bittrex', is_open=False, ) - o = Order.parse_from_ccxt_object(mock_order_3(), 'ETH/BTC', 'buy') + o = Order.parse_from_ccxt_object(mock_order_3(), 'XRP/BTC', 'buy') trade.orders.append(o) - o = Order.parse_from_ccxt_object(mock_order_3_sell(), 'ETH/BTC', 'sell') + o = Order.parse_from_ccxt_object(mock_order_3_sell(), 'XRP/BTC', 'sell') trade.orders.append(o) return trade @@ -169,7 +172,7 @@ def mock_trade_4(fee): open_order_id='prod_buy_12345', strategy='DefaultStrategy', ) - o = Order.parse_from_ccxt_object(mock_order_4(), 'ETH/BTC', 'buy') + o = Order.parse_from_ccxt_object(mock_order_4(), 'ETC/BTC', 'buy') trade.orders.append(o) return trade @@ -218,8 +221,59 @@ def mock_trade_5(fee): strategy='SampleStrategy', stoploss_order_id='prod_stoploss_3455' ) - o = Order.parse_from_ccxt_object(mock_order_5(), 'ETH/BTC', 'buy') + o = Order.parse_from_ccxt_object(mock_order_5(), 'XRP/BTC', 'buy') trade.orders.append(o) - o = Order.parse_from_ccxt_object(mock_order_5_stoploss(), 'ETH/BTC', 'stoploss') + o = Order.parse_from_ccxt_object(mock_order_5_stoploss(), 'XRP/BTC', 'stoploss') + trade.orders.append(o) + return trade + + +def mock_order_6(): + return { + 'id': 'prod_buy_6', + 'symbol': 'LTC/BTC', + 'status': 'closed', + 'side': 'buy', + 'type': 'limit', + 'price': 0.15, + 'amount': 2.0, + 'filled': 2.0, + 'remaining': 0.0, + } + + +def mock_order_6_sell(): + return { + 'id': 'prod_sell_6', + 'symbol': 'LTC/BTC', + 'status': 'open', + 'side': 'sell', + 'type': 'limit', + 'price': 0.20, + 'amount': 2.0, + 'filled': 0.0, + 'remaining': 2.0, + } + + +def mock_trade_6(fee): + """ + Simulate prod entry with open sell order + """ + trade = Trade( + pair='LTC/BTC', + stake_amount=0.001, + amount=2.0, + amount_requested=2.0, + fee_open=fee.return_value, + fee_close=fee.return_value, + open_rate=0.15, + exchange='bittrex', + strategy='SampleStrategy', + open_order_id="prod_sell_6", + ) + o = Order.parse_from_ccxt_object(mock_order_6(), 'LTC/BTC', 'buy') + trade.orders.append(o) + o = Order.parse_from_ccxt_object(mock_order_6_sell(), 'LTC/BTC', 'stoploss') trade.orders.append(o) return trade diff --git a/tests/data/test_btanalysis.py b/tests/data/test_btanalysis.py index 3b5672e65..564dae0b1 100644 --- a/tests/data/test_btanalysis.py +++ b/tests/data/test_btanalysis.py @@ -20,6 +20,7 @@ from freqtrade.data.btanalysis import (BT_DATA_COLUMNS, from freqtrade.data.history import load_data, load_pair_history from freqtrade.optimize.backtesting import BacktestResult from tests.conftest import create_mock_trades +from tests.conftest_trades import MOCK_TRADE_COUNT def test_get_latest_backtest_filename(testdatadir, mocker): @@ -110,7 +111,7 @@ def test_load_trades_from_db(default_conf, fee, mocker): trades = load_trades_from_db(db_url=default_conf['db_url']) assert init_mock.call_count == 1 - assert len(trades) == 5 + assert len(trades) == MOCK_TRADE_COUNT assert isinstance(trades, DataFrame) assert "pair" in trades.columns assert "open_date" in trades.columns diff --git a/tests/test_freqtradebot.py b/tests/test_freqtradebot.py index 542206e62..ace44bfae 100644 --- a/tests/test_freqtradebot.py +++ b/tests/test_freqtradebot.py @@ -27,7 +27,7 @@ from tests.conftest import (create_mock_trades, get_patched_freqtradebot, get_patched_worker, log_has, log_has_re, patch_edge, patch_exchange, patch_get_signal, patch_wallet, patch_whitelist) -from tests.conftest_trades import (mock_order_1, mock_order_2, +from tests.conftest_trades import (MOCK_TRADE_COUNT, mock_order_1, mock_order_2, mock_order_2_sell, mock_order_3, mock_order_3_sell, mock_order_4, mock_order_5_stoploss) @@ -4206,17 +4206,17 @@ def test_sync_wallet_dry_run(mocker, default_conf, ticker, fee, limit_buy_order_ def test_cancel_all_open_orders(mocker, default_conf, fee, limit_buy_order, limit_sell_order): default_conf['cancel_open_orders_on_exit'] = True mocker.patch('freqtrade.exchange.Exchange.fetch_order', - side_effect=[ExchangeError(), limit_sell_order, limit_buy_order]) + side_effect=[ExchangeError(), limit_sell_order, limit_buy_order, limit_sell_order, ]) buy_mock = mocker.patch('freqtrade.freqtradebot.FreqtradeBot.handle_cancel_buy') sell_mock = mocker.patch('freqtrade.freqtradebot.FreqtradeBot.handle_cancel_sell') freqtrade = get_patched_freqtradebot(mocker, default_conf) create_mock_trades(fee) trades = Trade.query.all() - assert len(trades) == 5 + assert len(trades) == MOCK_TRADE_COUNT freqtrade.cancel_all_open_orders() assert buy_mock.call_count == 1 - assert sell_mock.call_count == 1 + assert sell_mock.call_count == 2 @pytest.mark.usefixtures("init_persistence") @@ -4244,15 +4244,15 @@ def test_update_open_orders(mocker, default_conf, fee, caplog): assert log_has_re(r"Error updating Order .*", caplog) caplog.clear() - assert len(Order.get_open_orders()) == 2 + assert len(Order.get_open_orders()) == 3 matching_buy_order = mock_order_4() matching_buy_order.update({ 'status': 'closed', }) mocker.patch('freqtrade.exchange.Exchange.fetch_order', return_value=matching_buy_order) freqtrade.update_open_orders() - # Only stoploss order is kept open - assert len(Order.get_open_orders()) == 1 + # Only stoploss and sell orders are kept open + assert len(Order.get_open_orders()) == 2 @pytest.mark.usefixtures("init_persistence") @@ -4277,7 +4277,7 @@ def test_update_closed_trades_without_assigned_fees(mocker, default_conf, fee): create_mock_trades(fee) trades = Trade.get_trades().all() - assert len(trades) == 5 + assert len(trades) == MOCK_TRADE_COUNT for trade in trades: assert trade.fee_open_cost is None assert trade.fee_open_currency is None @@ -4287,7 +4287,7 @@ def test_update_closed_trades_without_assigned_fees(mocker, default_conf, fee): freqtrade.update_closed_trades_without_assigned_fees() trades = Trade.get_trades().all() - assert len(trades) == 5 + assert len(trades) == MOCK_TRADE_COUNT for trade in trades: if trade.is_open: diff --git a/tests/test_persistence.py b/tests/test_persistence.py index 48b918128..d2da1c6a2 100644 --- a/tests/test_persistence.py +++ b/tests/test_persistence.py @@ -731,10 +731,10 @@ def test_adjust_min_max_rates(fee): @pytest.mark.usefixtures("init_persistence") -def test_get_open(default_conf, fee): +def test_get_open(fee): create_mock_trades(fee) - assert len(Trade.get_open_trades()) == 3 + assert len(Trade.get_open_trades()) == 4 @pytest.mark.usefixtures("init_persistence") @@ -1004,7 +1004,7 @@ def test_total_open_trades_stakes(fee): assert res == 0 create_mock_trades(fee) res = Trade.total_open_trades_stakes() - assert res == 0.003 + assert res == 0.004 @pytest.mark.usefixtures("init_persistence")