Add 6th mock trade

This commit is contained in:
Matthias 2020-09-10 07:40:19 +02:00
parent 083c358044
commit 3c521f55b2
6 changed files with 81 additions and 22 deletions

View File

@ -18,6 +18,7 @@ from freqtrade.state import RunMode
from tests.conftest import (create_mock_trades, get_args, log_has, log_has_re, from tests.conftest import (create_mock_trades, get_args, log_has, log_has_re,
patch_exchange, patch_exchange,
patched_configuration_load_config_file) patched_configuration_load_config_file)
from tests.conftest_trades import MOCK_TRADE_COUNT
def test_setup_utils_configuration(): def test_setup_utils_configuration():
@ -1116,7 +1117,7 @@ def test_show_trades(mocker, fee, capsys, caplog):
pargs = get_args(args) pargs = get_args(args)
pargs['config'] = None pargs['config'] = None
start_show_trades(pargs) start_show_trades(pargs)
assert log_has("Printing 5 Trades: ", caplog) assert log_has(f"Printing {MOCK_TRADE_COUNT} Trades: ", caplog)
captured = capsys.readouterr() captured = capsys.readouterr()
assert "Trade(id=1" in captured.out assert "Trade(id=1" in captured.out
assert "Trade(id=2" in captured.out assert "Trade(id=2" in captured.out

View File

@ -23,7 +23,7 @@ from freqtrade.persistence import Trade
from freqtrade.resolvers import ExchangeResolver from freqtrade.resolvers import ExchangeResolver
from freqtrade.worker import Worker from freqtrade.worker import Worker
from tests.conftest_trades import (mock_trade_1, mock_trade_2, mock_trade_3, 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) logging.getLogger('').setLevel(logging.INFO)
@ -189,6 +189,9 @@ def create_mock_trades(fee):
trade = mock_trade_5(fee) trade = mock_trade_5(fee)
Trade.session.add(trade) Trade.session.add(trade)
trade = mock_trade_6(fee)
Trade.session.add(trade)
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
def patch_coingekko(mocker) -> None: def patch_coingekko(mocker) -> None:

View File

@ -1,6 +1,9 @@
from freqtrade.persistence.models import Order, Trade from freqtrade.persistence.models import Order, Trade
MOCK_TRADE_COUNT = 6
def mock_order_1(): def mock_order_1():
return { return {
'id': '1234', 'id': '1234',
@ -80,9 +83,9 @@ def mock_trade_2(fee):
open_order_id='dry_run_sell_12345', open_order_id='dry_run_sell_12345',
strategy='DefaultStrategy', 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) 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) trade.orders.append(o)
return trade return trade
@ -132,9 +135,9 @@ def mock_trade_3(fee):
exchange='bittrex', exchange='bittrex',
is_open=False, 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) 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) trade.orders.append(o)
return trade return trade
@ -169,7 +172,7 @@ def mock_trade_4(fee):
open_order_id='prod_buy_12345', open_order_id='prod_buy_12345',
strategy='DefaultStrategy', 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) trade.orders.append(o)
return trade return trade
@ -218,8 +221,59 @@ def mock_trade_5(fee):
strategy='SampleStrategy', strategy='SampleStrategy',
stoploss_order_id='prod_stoploss_3455' 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) 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) trade.orders.append(o)
return trade return trade

View File

@ -20,6 +20,7 @@ from freqtrade.data.btanalysis import (BT_DATA_COLUMNS,
from freqtrade.data.history import load_data, load_pair_history from freqtrade.data.history import load_data, load_pair_history
from freqtrade.optimize.backtesting import BacktestResult from freqtrade.optimize.backtesting import BacktestResult
from tests.conftest import create_mock_trades from tests.conftest import create_mock_trades
from tests.conftest_trades import MOCK_TRADE_COUNT
def test_get_latest_backtest_filename(testdatadir, mocker): 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']) trades = load_trades_from_db(db_url=default_conf['db_url'])
assert init_mock.call_count == 1 assert init_mock.call_count == 1
assert len(trades) == 5 assert len(trades) == MOCK_TRADE_COUNT
assert isinstance(trades, DataFrame) assert isinstance(trades, DataFrame)
assert "pair" in trades.columns assert "pair" in trades.columns
assert "open_date" in trades.columns assert "open_date" in trades.columns

View File

@ -27,7 +27,7 @@ from tests.conftest import (create_mock_trades, get_patched_freqtradebot,
get_patched_worker, log_has, log_has_re, get_patched_worker, log_has, log_has_re,
patch_edge, patch_exchange, patch_get_signal, patch_edge, patch_exchange, patch_get_signal,
patch_wallet, patch_whitelist) 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_2_sell, mock_order_3,
mock_order_3_sell, mock_order_4, mock_order_5_stoploss) 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): def test_cancel_all_open_orders(mocker, default_conf, fee, limit_buy_order, limit_sell_order):
default_conf['cancel_open_orders_on_exit'] = True default_conf['cancel_open_orders_on_exit'] = True
mocker.patch('freqtrade.exchange.Exchange.fetch_order', 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') buy_mock = mocker.patch('freqtrade.freqtradebot.FreqtradeBot.handle_cancel_buy')
sell_mock = mocker.patch('freqtrade.freqtradebot.FreqtradeBot.handle_cancel_sell') sell_mock = mocker.patch('freqtrade.freqtradebot.FreqtradeBot.handle_cancel_sell')
freqtrade = get_patched_freqtradebot(mocker, default_conf) freqtrade = get_patched_freqtradebot(mocker, default_conf)
create_mock_trades(fee) create_mock_trades(fee)
trades = Trade.query.all() trades = Trade.query.all()
assert len(trades) == 5 assert len(trades) == MOCK_TRADE_COUNT
freqtrade.cancel_all_open_orders() freqtrade.cancel_all_open_orders()
assert buy_mock.call_count == 1 assert buy_mock.call_count == 1
assert sell_mock.call_count == 1 assert sell_mock.call_count == 2
@pytest.mark.usefixtures("init_persistence") @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) assert log_has_re(r"Error updating Order .*", caplog)
caplog.clear() 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 = mock_order_4()
matching_buy_order.update({ matching_buy_order.update({
'status': 'closed', 'status': 'closed',
}) })
mocker.patch('freqtrade.exchange.Exchange.fetch_order', return_value=matching_buy_order) mocker.patch('freqtrade.exchange.Exchange.fetch_order', return_value=matching_buy_order)
freqtrade.update_open_orders() freqtrade.update_open_orders()
# Only stoploss order is kept open # Only stoploss and sell orders are kept open
assert len(Order.get_open_orders()) == 1 assert len(Order.get_open_orders()) == 2
@pytest.mark.usefixtures("init_persistence") @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) create_mock_trades(fee)
trades = Trade.get_trades().all() trades = Trade.get_trades().all()
assert len(trades) == 5 assert len(trades) == MOCK_TRADE_COUNT
for trade in trades: for trade in trades:
assert trade.fee_open_cost is None assert trade.fee_open_cost is None
assert trade.fee_open_currency 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() freqtrade.update_closed_trades_without_assigned_fees()
trades = Trade.get_trades().all() trades = Trade.get_trades().all()
assert len(trades) == 5 assert len(trades) == MOCK_TRADE_COUNT
for trade in trades: for trade in trades:
if trade.is_open: if trade.is_open:

View File

@ -731,10 +731,10 @@ def test_adjust_min_max_rates(fee):
@pytest.mark.usefixtures("init_persistence") @pytest.mark.usefixtures("init_persistence")
def test_get_open(default_conf, fee): def test_get_open(fee):
create_mock_trades(fee) create_mock_trades(fee)
assert len(Trade.get_open_trades()) == 3 assert len(Trade.get_open_trades()) == 4
@pytest.mark.usefixtures("init_persistence") @pytest.mark.usefixtures("init_persistence")
@ -1004,7 +1004,7 @@ def test_total_open_trades_stakes(fee):
assert res == 0 assert res == 0
create_mock_trades(fee) create_mock_trades(fee)
res = Trade.total_open_trades_stakes() res = Trade.total_open_trades_stakes()
assert res == 0.003 assert res == 0.004
@pytest.mark.usefixtures("init_persistence") @pytest.mark.usefixtures("init_persistence")