diff --git a/tests/conftest.py b/tests/conftest.py index 591fd47bb..2181388fa 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,4 +1,5 @@ # pragma pylint: disable=missing-docstring +from tests.conftest_trades import create_mock_trades from freqtrade.persistence.models import Order import json import logging @@ -168,136 +169,9 @@ def patch_get_signal(freqtrade: FreqtradeBot, value=(True, False)) -> None: freqtrade.exchange.refresh_latest_ohlcv = lambda p: None -def create_mock_trades(fee): - """ - Create some fake trades ... - """ - # Simulate dry_run entries - trade = Trade( - pair='ETH/BTC', - stake_amount=0.001, - amount=123.0, - amount_requested=123.0, - fee_open=fee.return_value, - fee_close=fee.return_value, - open_rate=0.123, - exchange='bittrex', - open_order_id='dry_run_buy_12345', - strategy='DefaultStrategy', - ) - o = Order.parse_from_ccxt_object({ - 'id': '1234', - 'symbol': 'ETH/BTC', - 'status': 'closed', - 'side': 'buy', - 'price': 0.123, - 'amount': 123.0, - 'filled': 123.0, - 'remaining': 0.0, - }, 'ETH/BTC', 'buy') - trade.orders.append(o) - Trade.session.add(trade) - - trade = Trade( - pair='ETC/BTC', - stake_amount=0.001, - amount=123.0, - amount_requested=123.0, - fee_open=fee.return_value, - fee_close=fee.return_value, - open_rate=0.123, - close_rate=0.128, - close_profit=0.005, - exchange='bittrex', - is_open=False, - open_order_id='dry_run_sell_12345', - strategy='DefaultStrategy', - ) - o = Order.parse_from_ccxt_object({ - 'id': '1235', - 'symbol': 'ETC/BTC', - 'status': 'closed', - 'side': 'buy', - 'price': 0.123, - 'amount': 123.0, - 'filled': 123.0, - 'remaining': 0.0, - }, 'ETH/BTC', 'buy') - trade.orders.append(o) - o = Order.parse_from_ccxt_object({ - 'id': '12366', - 'symbol': 'ETC/BTC', - 'status': 'closed', - 'side': 'sell', - 'price': 0.128, - 'amount': 123.0, - 'filled': 123.0, - 'remaining': 0.0, - }, 'ETH/BTC', 'sell') - trade.orders.append(o) - Trade.session.add(trade) - - trade = Trade( - pair='XRP/BTC', - stake_amount=0.001, - amount=123.0, - fee_open=fee.return_value, - fee_close=fee.return_value, - open_rate=0.05, - close_rate=0.06, - close_profit=0.01, - exchange='bittrex', - is_open=False, - ) - o = Order.parse_from_ccxt_object({ - 'id': '41231a12a', - 'symbol': 'XRP/BTC', - 'status': 'closed', - 'side': 'buy', - 'price': 0.05, - 'amount': 123.0, - 'filled': 123.0, - 'remaining': 0.0, - }, 'ETH/BTC', 'buy') - trade.orders.append(o) - o = Order.parse_from_ccxt_object({ - 'id': '41231a666a', - 'symbol': 'XRP/BTC', - 'status': 'closed', - 'side': 'stop_loss', - 'price': 0.06, - 'amount': 123.0, - 'filled': 123.0, - 'remaining': 0.0, - }, 'ETH/BTC', 'sell') - trade.orders.append(o) - Trade.session.add(trade) - - # Simulate prod entry - trade = Trade( - pair='ETC/BTC', - stake_amount=0.001, - amount=123.0, - amount_requested=124.0, - fee_open=fee.return_value, - fee_close=fee.return_value, - open_rate=0.123, - exchange='bittrex', - open_order_id='prod_buy_12345', - strategy='DefaultStrategy', - ) - o = Order.parse_from_ccxt_object({ - 'id': 'prod_buy_12345', - 'symbol': 'ETC/BTC', - 'status': 'open', - 'side': 'buy', - 'price': 0.123, - 'amount': 123.0, - 'filled': 123.0, - 'remaining': 0.0, - }, 'ETH/BTC', 'buy') - trade.orders.append(o) - Trade.session.add(trade) +@pytest.fixture(scope='function') +def mock_trades(fee): + return create_mock_trades(fee) @pytest.fixture(autouse=True) diff --git a/tests/conftest_trades.py b/tests/conftest_trades.py new file mode 100644 index 000000000..6966eb68f --- /dev/null +++ b/tests/conftest_trades.py @@ -0,0 +1,162 @@ +import pytest + +from freqtrade.persistence.models import Order, Trade + + +def mock_trade_1(fee): + trade = Trade( + pair='ETH/BTC', + stake_amount=0.001, + amount=123.0, + amount_requested=123.0, + fee_open=fee.return_value, + fee_close=fee.return_value, + open_rate=0.123, + exchange='bittrex', + open_order_id='dry_run_buy_12345', + strategy='DefaultStrategy', + ) + o = Order.parse_from_ccxt_object({ + 'id': '1234', + 'symbol': 'ETH/BTC', + 'status': 'closed', + 'side': 'buy', + 'price': 0.123, + 'amount': 123.0, + 'filled': 123.0, + 'remaining': 0.0, + }, 'ETH/BTC', 'buy') + trade.orders.append(o) + return trade + + +def mock_trade_2(fee): + """ + Closed trade... + """ + trade = Trade( + pair='ETC/BTC', + stake_amount=0.001, + amount=123.0, + amount_requested=123.0, + fee_open=fee.return_value, + fee_close=fee.return_value, + open_rate=0.123, + close_rate=0.128, + close_profit=0.005, + exchange='bittrex', + is_open=False, + open_order_id='dry_run_sell_12345', + strategy='DefaultStrategy', + ) + o = Order.parse_from_ccxt_object({ + 'id': '1235', + 'symbol': 'ETC/BTC', + 'status': 'closed', + 'side': 'buy', + 'price': 0.123, + 'amount': 123.0, + 'filled': 123.0, + 'remaining': 0.0, + }, 'ETH/BTC', 'buy') + trade.orders.append(o) + o = Order.parse_from_ccxt_object({ + 'id': '12366', + 'symbol': 'ETC/BTC', + 'status': 'closed', + 'side': 'sell', + 'price': 0.128, + 'amount': 123.0, + 'filled': 123.0, + 'remaining': 0.0, + }, 'ETH/BTC', 'sell') + trade.orders.append(o) + return trade + + +def mock_trade_3(fee): + """ + Closed trade + """ + trade = Trade( + pair='XRP/BTC', + stake_amount=0.001, + amount=123.0, + fee_open=fee.return_value, + fee_close=fee.return_value, + open_rate=0.05, + close_rate=0.06, + close_profit=0.01, + exchange='bittrex', + is_open=False, + ) + o = Order.parse_from_ccxt_object({ + 'id': '41231a12a', + 'symbol': 'XRP/BTC', + 'status': 'closed', + 'side': 'buy', + 'price': 0.05, + 'amount': 123.0, + 'filled': 123.0, + 'remaining': 0.0, + }, 'ETH/BTC', 'buy') + trade.orders.append(o) + o = Order.parse_from_ccxt_object({ + 'id': '41231a666a', + 'symbol': 'XRP/BTC', + 'status': 'closed', + 'side': 'stop_loss', + 'price': 0.06, + 'amount': 123.0, + 'filled': 123.0, + 'remaining': 0.0, + }, 'ETH/BTC', 'sell') + trade.orders.append(o) + return trade + + +def mock_trade_4(fee): + """ + Simulate prod entry + """ + trade = Trade( + pair='ETC/BTC', + stake_amount=0.001, + amount=123.0, + amount_requested=124.0, + fee_open=fee.return_value, + fee_close=fee.return_value, + open_rate=0.123, + exchange='bittrex', + open_order_id='prod_buy_12345', + strategy='DefaultStrategy', + ) + o = Order.parse_from_ccxt_object({ + 'id': 'prod_buy_12345', + 'symbol': 'ETC/BTC', + 'status': 'open', + 'side': 'buy', + 'price': 0.123, + 'amount': 123.0, + 'filled': 123.0, + 'remaining': 0.0, + }, 'ETH/BTC', 'buy') + trade.orders.append(o) + + +def create_mock_trades(fee): + """ + Create some fake trades ... + """ + # Simulate dry_run entries + trade = mock_trade_1(fee) + Trade.session.add(trade) + + trade = mock_trade_2(fee) + Trade.session.add(trade) + + trade = mock_trade_3(fee) + Trade.session.add(trade) + + trade = mock_trade_4(fee) + Trade.session.add(trade)