Fix some tests to call update_trade with order object

This commit is contained in:
Matthias 2022-02-10 20:40:50 +01:00
parent 1b1216fc87
commit 508e677d70
4 changed files with 62 additions and 30 deletions

View File

@ -16,7 +16,6 @@ from sqlalchemy.sql.schema import UniqueConstraint
from freqtrade.constants import DATETIME_PRINT_FORMAT, NON_OPEN_EXCHANGE_STATES
from freqtrade.enums import SellType
from freqtrade.exceptions import DependencyException, OperationalException
from freqtrade.misc import safe_value_fallback
from freqtrade.persistence.migrations import check_migrate

View File

@ -2208,7 +2208,7 @@ def limit_sell_order_usdt_open():
'id': 'mocked_limit_sell_usdt',
'type': 'limit',
'side': 'sell',
'pair': 'mocked',
'symbol': 'mocked',
'datetime': arrow.utcnow().isoformat(),
'timestamp': arrow.utcnow().int_timestamp,
'price': 2.20,

View File

@ -227,7 +227,8 @@ def test_edge_overrides_stoploss(limit_buy_order_usdt, fee, caplog, mocker,
freqtrade.strategy.min_roi_reached = MagicMock(return_value=False)
freqtrade.enter_positions()
trade = Trade.query.first()
trade.update(limit_buy_order_usdt)
oobj = Order.parse_from_ccxt_object(limit_buy_order_usdt, 'ADA/USDT', 'buy')
trade.update_trade(oobj)
#############################################
# stoploss shoud be hit
@ -292,7 +293,8 @@ def test_create_trade(default_conf_usdt, ticker_usdt, limit_buy_order_usdt, fee,
assert trade.exchange == 'binance'
# Simulate fulfilled LIMIT_BUY order for trade
trade.update(limit_buy_order_usdt)
oobj = Order.parse_from_ccxt_object(limit_buy_order_usdt, 'ADA/USDT', 'buy')
trade.update_trade(oobj)
assert trade.open_rate == 2.0
assert trade.amount == 30.0
@ -1803,7 +1805,8 @@ def test_handle_trade(default_conf_usdt, limit_buy_order_usdt, limit_sell_order_
assert trade
time.sleep(0.01) # Race condition fix
trade.update(limit_buy_order_usdt)
oobj = Order.parse_from_ccxt_object(limit_buy_order_usdt, limit_buy_order_usdt['symbol'], 'buy')
trade.update_trade(oobj)
assert trade.is_open is True
freqtrade.wallets.update()
@ -1812,7 +1815,9 @@ def test_handle_trade(default_conf_usdt, limit_buy_order_usdt, limit_sell_order_
assert trade.open_order_id == limit_sell_order_usdt['id']
# Simulate fulfilled LIMIT_SELL order for trade
trade.update(limit_sell_order_usdt)
oobj = Order.parse_from_ccxt_object(
limit_sell_order_usdt, limit_sell_order_usdt['symbol'], 'sell')
trade.update_trade(oobj)
assert trade.close_rate == 2.2
assert trade.close_profit == 0.09451372
@ -1962,8 +1967,11 @@ def test_close_trade(default_conf_usdt, ticker_usdt, limit_buy_order_usdt,
trade = Trade.query.first()
assert trade
trade.update(limit_buy_order_usdt)
trade.update(limit_sell_order_usdt)
oobj = Order.parse_from_ccxt_object(limit_buy_order_usdt, limit_buy_order_usdt['symbol'], 'buy')
trade.update_trade(oobj)
oobj = Order.parse_from_ccxt_object(
limit_sell_order_usdt, limit_sell_order_usdt['symbol'], 'sell')
trade.update_trade(oobj)
assert trade.is_open is False
with pytest.raises(DependencyException, match=r'.*closed trade.*'):
@ -3103,7 +3111,8 @@ def test_sell_profit_only(
freqtrade.enter_positions()
trade = Trade.query.first()
trade.update(limit_buy_order_usdt)
oobj = Order.parse_from_ccxt_object(limit_buy_order_usdt, limit_buy_order_usdt['symbol'], 'buy')
trade.update_trade(oobj)
freqtrade.wallets.update()
patch_get_signal(freqtrade, value=(False, True, None, None))
assert freqtrade.handle_trade(trade) is handle_first
@ -3139,7 +3148,9 @@ def test_sell_not_enough_balance(default_conf_usdt, limit_buy_order_usdt, limit_
trade = Trade.query.first()
amnt = trade.amount
trade.update(limit_buy_order_usdt)
oobj = Order.parse_from_ccxt_object(limit_buy_order_usdt, limit_buy_order_usdt['symbol'], 'buy')
trade.update_trade(oobj)
patch_get_signal(freqtrade, value=(False, True, None, None))
mocker.patch('freqtrade.wallets.Wallets.get_free', MagicMock(return_value=trade.amount * 0.985))
@ -3247,7 +3258,8 @@ def test_ignore_roi_if_buy_signal(default_conf_usdt, limit_buy_order_usdt,
freqtrade.enter_positions()
trade = Trade.query.first()
trade.update(limit_buy_order_usdt)
oobj = Order.parse_from_ccxt_object(limit_buy_order_usdt, limit_buy_order_usdt['symbol'], 'buy')
trade.update_trade(oobj)
freqtrade.wallets.update()
patch_get_signal(freqtrade, value=(True, True, None, None))
assert freqtrade.handle_trade(trade) is False
@ -3437,7 +3449,8 @@ def test_disable_ignore_roi_if_buy_signal(default_conf_usdt, limit_buy_order_usd
freqtrade.enter_positions()
trade = Trade.query.first()
trade.update(limit_buy_order_usdt)
oobj = Order.parse_from_ccxt_object(limit_buy_order_usdt, limit_buy_order_usdt['symbol'], 'buy')
trade.update_trade(oobj)
# Sell due to min_roi_reached
patch_get_signal(freqtrade, value=(True, False, None, None))
assert freqtrade.handle_trade(trade) is True
@ -3812,7 +3825,8 @@ def test_order_book_depth_of_market(
assert len(Trade.query.all()) == 1
# Simulate fulfilled LIMIT_BUY order for trade
trade.update(limit_buy_order_usdt)
oobj = Order.parse_from_ccxt_object(limit_buy_order_usdt, 'ADA/USDT', 'buy')
trade.update_trade(oobj)
assert trade.open_rate == 2.0
assert whitelist == default_conf_usdt['exchange']['pair_whitelist']
@ -3906,7 +3920,8 @@ def test_order_book_ask_strategy(
assert trade
time.sleep(0.01) # Race condition fix
trade.update(limit_buy_order_usdt)
oobj = Order.parse_from_ccxt_object(limit_buy_order_usdt, limit_buy_order_usdt['symbol'], 'buy')
trade.update_trade(oobj)
freqtrade.wallets.update()
assert trade.is_open is True

View File

@ -108,7 +108,8 @@ def test_update_limit_order(limit_buy_order_usdt, limit_sell_order_usdt, fee, ca
assert trade.close_date is None
trade.open_order_id = 'something'
trade.update(limit_buy_order_usdt)
oobj = Order.parse_from_ccxt_object(limit_buy_order_usdt, 'ADA/USDT', 'buy')
trade.update_trade(oobj)
assert trade.open_order_id is None
assert trade.open_rate == 2.00
assert trade.close_profit is None
@ -119,7 +120,8 @@ def test_update_limit_order(limit_buy_order_usdt, limit_sell_order_usdt, fee, ca
caplog.clear()
trade.open_order_id = 'something'
trade.update(limit_sell_order_usdt)
oobj = Order.parse_from_ccxt_object(limit_sell_order_usdt, 'ADA/USDT', 'sell')
trade.update_trade(oobj)
assert trade.open_order_id is None
assert trade.close_rate == 2.20
assert trade.close_profit == round(0.0945137157107232, 8)
@ -146,7 +148,8 @@ def test_update_market_order(market_buy_order_usdt, market_sell_order_usdt, fee,
)
trade.open_order_id = 'something'
trade.update(market_buy_order_usdt)
oobj = Order.parse_from_ccxt_object(market_buy_order_usdt, 'ADA/USDT', 'buy')
trade.update_trade(oobj)
assert trade.open_order_id is None
assert trade.open_rate == 2.0
assert trade.close_profit is None
@ -158,7 +161,8 @@ def test_update_market_order(market_buy_order_usdt, market_sell_order_usdt, fee,
caplog.clear()
trade.is_open = True
trade.open_order_id = 'something'
trade.update(market_sell_order_usdt)
oobj = Order.parse_from_ccxt_object(market_sell_order_usdt, 'ADA/USDT', 'sell')
trade.update_trade(oobj)
assert trade.open_order_id is None
assert trade.close_rate == 2.2
assert trade.close_profit == round(0.0945137157107232, 8)
@ -181,9 +185,11 @@ def test_calc_open_close_trade_price(limit_buy_order_usdt, limit_sell_order_usdt
)
trade.open_order_id = 'something'
trade.update(limit_buy_order_usdt)
oobj = Order.parse_from_ccxt_object(limit_buy_order_usdt, 'ADA/USDT', 'buy')
trade.update_trade(oobj)
assert trade._calc_open_trade_value() == 60.15
trade.update(limit_sell_order_usdt)
oobj = Order.parse_from_ccxt_object(limit_sell_order_usdt, 'ADA/USDT', 'sell')
trade.update_trade(oobj)
assert isclose(trade.calc_close_trade_value(), 65.835)
# Profit in USDT
@ -236,7 +242,8 @@ def test_calc_close_trade_price_exception(limit_buy_order_usdt, fee):
)
trade.open_order_id = 'something'
trade.update(limit_buy_order_usdt)
oobj = Order.parse_from_ccxt_object(limit_buy_order_usdt, 'ADA/USDT', 'buy')
trade.update_trade(oobj)
assert trade.calc_close_trade_value() == 0.0
@ -257,7 +264,8 @@ def test_update_open_order(limit_buy_order_usdt):
assert trade.close_date is None
limit_buy_order_usdt['status'] = 'open'
trade.update(limit_buy_order_usdt)
oobj = Order.parse_from_ccxt_object(limit_buy_order_usdt, 'ADA/USDT', 'buy')
trade.update_trade(oobj)
assert trade.open_order_id is None
assert trade.close_profit is None
@ -276,8 +284,9 @@ def test_update_invalid_order(limit_buy_order_usdt):
exchange='binance',
)
limit_buy_order_usdt['type'] = 'invalid'
oobj = Order.parse_from_ccxt_object(limit_buy_order_usdt, 'ADA/USDT', 'meep')
with pytest.raises(ValueError, match=r'Unknown order type'):
trade.update(limit_buy_order_usdt)
trade.update_trade(oobj)
@pytest.mark.usefixtures("init_persistence")
@ -304,7 +313,8 @@ def test_calc_open_trade_value(limit_buy_order_usdt, fee):
exchange='binance',
)
trade.open_order_id = 'open_trade'
trade.update(limit_buy_order_usdt) # Buy @ 2.0
oobj = Order.parse_from_ccxt_object(limit_buy_order_usdt, 'ADA/USDT', 'buy')
trade.update_trade(oobj) # Buy @ 2.0
# Get the open rate price with the standard fee rate
assert trade._calc_open_trade_value() == 60.15
@ -325,14 +335,16 @@ def test_calc_close_trade_price(limit_buy_order_usdt, limit_sell_order_usdt, fee
exchange='binance',
)
trade.open_order_id = 'close_trade'
trade.update(limit_buy_order_usdt) # Buy @ 2.0
oobj = Order.parse_from_ccxt_object(limit_buy_order_usdt, 'ADA/USDT', 'buy')
trade.update_trade(oobj) # Buy @ 2.0
# Get the close rate price with a custom close rate and a regular fee rate
assert trade.calc_close_trade_value(rate=2.5) == 74.8125
# Get the close rate price with a custom close rate and a custom fee rate
assert trade.calc_close_trade_value(rate=2.5, fee=0.003) == 74.775
# Test when we apply a Sell order, and ask price with a custom fee rate
trade.update(limit_sell_order_usdt)
oobj = Order.parse_from_ccxt_object(limit_sell_order_usdt, 'ADA/USDT', 'sell')
trade.update_trade(oobj)
assert trade.calc_close_trade_value(fee=0.005) == 65.67
@ -409,7 +421,9 @@ def test_calc_profit(limit_buy_order_usdt, limit_sell_order_usdt, fee):
exchange='binance',
)
trade.open_order_id = 'something'
trade.update(limit_buy_order_usdt) # Buy @ 2.0
oobj = Order.parse_from_ccxt_object(limit_buy_order_usdt, 'ADA/USDT', 'buy')
trade.update_trade(oobj) # Buy @ 2.0
# Custom closing rate and regular fee rate
# Higher than open rate - 2.1 quote
@ -424,7 +438,8 @@ def test_calc_profit(limit_buy_order_usdt, limit_sell_order_usdt, fee):
assert trade.calc_profit(rate=1.9, fee=0.003) == round(-3.320999999999998, 8)
# Test when we apply a Sell order. Sell higher than open rate @ 2.2
trade.update(limit_sell_order_usdt)
oobj = Order.parse_from_ccxt_object(limit_sell_order_usdt, 'ADA/USDT', 'sell')
trade.update_trade(oobj)
assert trade.calc_profit() == round(5.684999999999995, 8)
# Test with a custom fee rate on the close trade
@ -443,7 +458,9 @@ def test_calc_profit_ratio(limit_buy_order_usdt, limit_sell_order_usdt, fee):
exchange='binance'
)
trade.open_order_id = 'something'
trade.update(limit_buy_order_usdt) # Buy @ 2.0
oobj = Order.parse_from_ccxt_object(limit_buy_order_usdt, 'ADA/USDT', 'buy')
trade.update_trade(oobj) # Buy @ 2.0
# Higher than open rate - 2.1 quote
assert trade.calc_profit_ratio(rate=2.1) == round(0.04476309226932673, 8)
@ -457,7 +474,8 @@ def test_calc_profit_ratio(limit_buy_order_usdt, limit_sell_order_usdt, fee):
assert trade.calc_profit_ratio(rate=1.9, fee=0.003) == round(-0.05521197007481293, 8)
# Test when we apply a Sell order. Sell higher than open rate @ 2.2
trade.update(limit_sell_order_usdt)
oobj = Order.parse_from_ccxt_object(limit_sell_order_usdt, 'ADA/USDT', 'sell')
trade.update_trade(oobj)
assert trade.calc_profit_ratio() == round(0.0945137157107232, 8)
# Test with a custom fee rate on the close trade