remove .query from most tests

This commit is contained in:
Matthias 2023-03-16 07:25:04 +01:00
parent 6ed337faa3
commit 9d6e973e5b
9 changed files with 144 additions and 134 deletions

View File

@ -4,7 +4,7 @@ from pathlib import Path
from unittest.mock import MagicMock from unittest.mock import MagicMock
import pytest import pytest
from sqlalchemy import create_engine, text from sqlalchemy import create_engine, select, text
from freqtrade.constants import DEFAULT_DB_PROD_URL from freqtrade.constants import DEFAULT_DB_PROD_URL
from freqtrade.enums import TradingMode from freqtrade.enums import TradingMode
@ -235,8 +235,9 @@ def test_migrate_new(mocker, default_conf, fee, caplog):
# Run init to test migration # Run init to test migration
init_db(default_conf['db_url']) init_db(default_conf['db_url'])
assert len(Trade.query.filter(Trade.id == 1).all()) == 1 trades = Trade.session.scalars(select(Trade).filter(Trade.id == 1)).all()
trade = Trade.query.filter(Trade.id == 1).first() assert len(trades) == 1
trade = trades[0]
assert trade.fee_open == fee.return_value assert trade.fee_open == fee.return_value
assert trade.fee_close == fee.return_value assert trade.fee_close == fee.return_value
assert trade.open_rate_requested is None assert trade.open_rate_requested is None
@ -404,7 +405,7 @@ def test_migrate_pairlocks(mocker, default_conf, fee, caplog):
init_db(default_conf['db_url']) init_db(default_conf['db_url'])
assert len(PairLock.query.all()) == 2 assert len(PairLock.get_all_locks().all()) == 2
assert len(PairLock.query.filter(PairLock.pair == '*').all()) == 1 assert len(PairLock.query.filter(PairLock.pair == '*').all()) == 1
pairlocks = PairLock.query.filter(PairLock.pair == 'ETH/BTC').all() pairlocks = PairLock.query.filter(PairLock.pair == 'ETH/BTC').all()
assert len(pairlocks) == 1 assert len(pairlocks) == 1

View File

@ -4,6 +4,7 @@ from types import FunctionType
import arrow import arrow
import pytest import pytest
from sqlalchemy import select
from freqtrade.constants import DATETIME_PRINT_FORMAT from freqtrade.constants import DATETIME_PRINT_FORMAT
from freqtrade.enums import TradingMode from freqtrade.enums import TradingMode
@ -2575,11 +2576,11 @@ def test_recalc_trade_from_orders_dca(data) -> None:
trade.recalc_trade_from_orders() trade.recalc_trade_from_orders()
Trade.commit() Trade.commit()
orders1 = Order.query.all() orders1 = Order.session.scalars(select(Order)).all()
assert orders1 assert orders1
assert len(orders1) == idx + 1 assert len(orders1) == idx + 1
trade = Trade.query.first() trade = Trade.session.scalars(select(Trade)).first()
assert trade assert trade
assert len(trade.orders) == idx + 1 assert len(trade.orders) == idx + 1
if idx < len(data) - 1: if idx < len(data) - 1:
@ -2596,6 +2597,6 @@ def test_recalc_trade_from_orders_dca(data) -> None:
assert pytest.approx(trade.close_profit_abs) == data['end_profit'] assert pytest.approx(trade.close_profit_abs) == data['end_profit']
assert pytest.approx(trade.close_profit) == data['end_profit_ratio'] assert pytest.approx(trade.close_profit) == data['end_profit_ratio']
assert not trade.is_open assert not trade.is_open
trade = Trade.query.first() trade = Trade.session.scalars(select(Trade)).first()
assert trade assert trade
assert trade.open_order_id is None assert trade.open_order_id is None

View File

@ -14,7 +14,7 @@ def test_PairLocks(use_db):
PairLocks.use_db = use_db PairLocks.use_db = use_db
# No lock should be present # No lock should be present
if use_db: if use_db:
assert len(PairLock.query.all()) == 0 assert len(PairLock.get_all_locks().all()) == 0
assert PairLocks.use_db == use_db assert PairLocks.use_db == use_db
@ -88,13 +88,13 @@ def test_PairLocks(use_db):
if use_db: if use_db:
locks = PairLocks.get_all_locks() locks = PairLocks.get_all_locks()
locks_db = PairLock.query.all() locks_db = PairLock.get_all_locks().all()
assert len(locks) == len(locks_db) assert len(locks) == len(locks_db)
assert len(locks_db) > 0 assert len(locks_db) > 0
else: else:
# Nothing was pushed to the database # Nothing was pushed to the database
assert len(PairLocks.get_all_locks()) > 0 assert len(PairLocks.get_all_locks()) > 0
assert len(PairLock.query.all()) == 0 assert len(PairLock.get_all_locks().all()) == 0
# Reset use-db variable # Reset use-db variable
PairLocks.reset_locks() PairLocks.reset_locks()
PairLocks.use_db = True PairLocks.use_db = True

View File

@ -74,7 +74,7 @@ def generate_mock_trade(pair: str, fee: float, is_open: bool,
trade.close(close_price) trade.close(close_price)
trade.exit_reason = exit_reason trade.exit_reason = exit_reason
Trade.query.session.add(trade) Trade.session.add(trade)
Trade.commit() Trade.commit()
return trade return trade

View File

@ -4,6 +4,7 @@ from unittest.mock import ANY, MagicMock, PropertyMock
import pytest import pytest
from numpy import isnan from numpy import isnan
from sqlalchemy import select
from freqtrade.edge import PairInfo from freqtrade.edge import PairInfo
from freqtrade.enums import SignalDirection, State, TradingMode from freqtrade.enums import SignalDirection, State, TradingMode
@ -354,7 +355,7 @@ def test_rpc_delete_trade(mocker, default_conf, fee, markets, caplog, is_short):
with pytest.raises(RPCException, match='invalid argument'): with pytest.raises(RPCException, match='invalid argument'):
rpc._rpc_delete('200') rpc._rpc_delete('200')
trades = Trade.query.all() trades = Trade.session.scalars(select(Trade)).all()
trades[1].stoploss_order_id = '1234' trades[1].stoploss_order_id = '1234'
trades[2].stoploss_order_id = '1234' trades[2].stoploss_order_id = '1234'
assert len(trades) > 2 assert len(trades) > 2
@ -717,7 +718,7 @@ def test_rpc_force_exit(default_conf, ticker, fee, mocker) -> None:
mocker.patch(f'{EXMS}._dry_is_price_crossed', MagicMock(return_value=False)) mocker.patch(f'{EXMS}._dry_is_price_crossed', MagicMock(return_value=False))
freqtradebot.enter_positions() freqtradebot.enter_positions()
# make an limit-buy open trade # make an limit-buy open trade
trade = Trade.query.filter(Trade.id == '3').first() trade = Trade.session.scalars(select(Trade).filter(Trade.id == '3')).first()
filled_amount = trade.amount / 2 filled_amount = trade.amount / 2
# Fetch order - it's open first, and closed after cancel_order is called. # Fetch order - it's open first, and closed after cancel_order is called.
mocker.patch( mocker.patch(
@ -753,7 +754,7 @@ def test_rpc_force_exit(default_conf, ticker, fee, mocker) -> None:
freqtradebot.config['max_open_trades'] = 3 freqtradebot.config['max_open_trades'] = 3
freqtradebot.enter_positions() freqtradebot.enter_positions()
trade = Trade.query.filter(Trade.id == '2').first() trade = Trade.session.scalars(select(Trade).filter(Trade.id == '2')).first()
amount = trade.amount amount = trade.amount
# make an limit-buy open trade, if there is no 'filled', don't sell it # make an limit-buy open trade, if there is no 'filled', don't sell it
mocker.patch( mocker.patch(
@ -771,7 +772,7 @@ def test_rpc_force_exit(default_conf, ticker, fee, mocker) -> None:
assert cancel_order_mock.call_count == 2 assert cancel_order_mock.call_count == 2
assert trade.amount == amount assert trade.amount == amount
trade = Trade.query.filter(Trade.id == '3').first() trade = Trade.session.scalars(select(Trade).filter(Trade.id == '3')).first()
# make an limit-sell open trade # make an limit-sell open trade
mocker.patch( mocker.patch(

View File

@ -14,6 +14,7 @@ from fastapi import FastAPI, WebSocketDisconnect
from fastapi.exceptions import HTTPException from fastapi.exceptions import HTTPException
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
from requests.auth import _basic_auth_str from requests.auth import _basic_auth_str
from sqlalchemy import select
from freqtrade.__init__ import __version__ from freqtrade.__init__ import __version__
from freqtrade.enums import CandleType, RunMode, State, TradingMode from freqtrade.enums import CandleType, RunMode, State, TradingMode
@ -624,7 +625,7 @@ def test_api_trades(botclient, mocker, fee, markets, is_short):
assert rc.json()['offset'] == 0 assert rc.json()['offset'] == 0
create_mock_trades(fee, is_short=is_short) create_mock_trades(fee, is_short=is_short)
Trade.query.session.flush() Trade.session.flush()
rc = client_get(client, f"{BASE_URI}/trades") rc = client_get(client, f"{BASE_URI}/trades")
assert_response(rc) assert_response(rc)
@ -677,7 +678,7 @@ def test_api_delete_trade(botclient, mocker, fee, markets, is_short):
create_mock_trades(fee, is_short=is_short) create_mock_trades(fee, is_short=is_short)
ftbot.strategy.order_types['stoploss_on_exchange'] = True ftbot.strategy.order_types['stoploss_on_exchange'] = True
trades = Trade.query.all() trades = Trade.session.scalars(select(Trade)).all()
trades[1].stoploss_order_id = '1234' trades[1].stoploss_order_id = '1234'
Trade.commit() Trade.commit()
assert len(trades) > 2 assert len(trades) > 2
@ -685,7 +686,7 @@ def test_api_delete_trade(botclient, mocker, fee, markets, is_short):
rc = client_delete(client, f"{BASE_URI}/trades/1") rc = client_delete(client, f"{BASE_URI}/trades/1")
assert_response(rc) assert_response(rc)
assert rc.json()['result_msg'] == 'Deleted trade 1. Closed 1 open orders.' assert rc.json()['result_msg'] == 'Deleted trade 1. Closed 1 open orders.'
assert len(trades) - 1 == len(Trade.query.all()) assert len(trades) - 1 == len(Trade.session.scalars(select(Trade)).all())
assert cancel_mock.call_count == 1 assert cancel_mock.call_count == 1
cancel_mock.reset_mock() cancel_mock.reset_mock()
@ -694,11 +695,11 @@ def test_api_delete_trade(botclient, mocker, fee, markets, is_short):
assert_response(rc, 502) assert_response(rc, 502)
assert cancel_mock.call_count == 0 assert cancel_mock.call_count == 0
assert len(trades) - 1 == len(Trade.query.all()) assert len(trades) - 1 == len(Trade.session.scalars(select(Trade)).all())
rc = client_delete(client, f"{BASE_URI}/trades/2") rc = client_delete(client, f"{BASE_URI}/trades/2")
assert_response(rc) assert_response(rc)
assert rc.json()['result_msg'] == 'Deleted trade 2. Closed 2 open orders.' assert rc.json()['result_msg'] == 'Deleted trade 2. Closed 2 open orders.'
assert len(trades) - 2 == len(Trade.query.all()) assert len(trades) - 2 == len(Trade.session.scalars(select(Trade)).all())
assert stoploss_mock.call_count == 1 assert stoploss_mock.call_count == 1
rc = client_delete(client, f"{BASE_URI}/trades/502") rc = client_delete(client, f"{BASE_URI}/trades/502")

View File

@ -14,6 +14,7 @@ import arrow
import pytest import pytest
import time_machine import time_machine
from pandas import DataFrame from pandas import DataFrame
from sqlalchemy import select
from telegram import Chat, Message, ReplyKeyboardMarkup, Update from telegram import Chat, Message, ReplyKeyboardMarkup, Update
from telegram.error import BadRequest, NetworkError, TelegramError from telegram.error import BadRequest, NetworkError, TelegramError
@ -692,7 +693,7 @@ def test_profit_handle(default_conf_usdt, update, ticker_usdt, ticker_sell_up, f
# Create some test data # Create some test data
freqtradebot.enter_positions() freqtradebot.enter_positions()
trade = Trade.query.first() trade = Trade.session.scalars(select(Trade)).first()
context = MagicMock() context = MagicMock()
# Test with invalid 2nd argument (should silently pass) # Test with invalid 2nd argument (should silently pass)
@ -945,7 +946,7 @@ def test_telegram_forceexit_handle(default_conf, update, ticker, fee,
# Create some test data # Create some test data
freqtradebot.enter_positions() freqtradebot.enter_positions()
trade = Trade.query.first() trade = Trade.session.scalars(select(Trade)).first()
assert trade assert trade
# Increase the price and sell it # Increase the price and sell it
@ -1020,7 +1021,7 @@ def test_telegram_force_exit_down_handle(default_conf, update, ticker, fee,
fetch_ticker=ticker_sell_down fetch_ticker=ticker_sell_down
) )
trade = Trade.query.first() trade = Trade.session.scalars(select(Trade)).first()
assert trade assert trade
# /forceexit 1 # /forceexit 1

View File

@ -10,6 +10,7 @@ from unittest.mock import ANY, MagicMock, PropertyMock, patch
import arrow import arrow
import pytest import pytest
from pandas import DataFrame from pandas import DataFrame
from sqlalchemy import select
from freqtrade.constants import CANCEL_REASON, UNLIMITED_STAKE_AMOUNT from freqtrade.constants import CANCEL_REASON, UNLIMITED_STAKE_AMOUNT
from freqtrade.enums import (CandleType, ExitCheckTuple, ExitType, RPCMessageType, RunMode, from freqtrade.enums import (CandleType, ExitCheckTuple, ExitType, RPCMessageType, RunMode,
@ -247,7 +248,7 @@ def test_edge_overrides_stoploss(limit_order, fee, caplog, mocker,
patch_get_signal(freqtrade) patch_get_signal(freqtrade)
freqtrade.strategy.min_roi_reached = MagicMock(return_value=False) freqtrade.strategy.min_roi_reached = MagicMock(return_value=False)
freqtrade.enter_positions() freqtrade.enter_positions()
trade = Trade.query.first() trade = Trade.session.scalars(select(Trade)).first()
caplog.clear() caplog.clear()
############################################# #############################################
ticker_val.update({ ticker_val.update({
@ -278,7 +279,7 @@ def test_total_open_trades_stakes(mocker, default_conf_usdt, ticker_usdt, fee) -
freqtrade = FreqtradeBot(default_conf_usdt) freqtrade = FreqtradeBot(default_conf_usdt)
patch_get_signal(freqtrade) patch_get_signal(freqtrade)
freqtrade.enter_positions() freqtrade.enter_positions()
trade = Trade.query.first() trade = Trade.session.scalars(select(Trade)).first()
assert trade is not None assert trade is not None
assert trade.stake_amount == 60.0 assert trade.stake_amount == 60.0
@ -286,7 +287,7 @@ def test_total_open_trades_stakes(mocker, default_conf_usdt, ticker_usdt, fee) -
assert trade.open_date is not None assert trade.open_date is not None
freqtrade.enter_positions() freqtrade.enter_positions()
trade = Trade.query.order_by(Trade.id.desc()).first() trade = Trade.session.scalars(select(Trade).order_by(Trade.id.desc())).first()
assert trade is not None assert trade is not None
assert trade.stake_amount == 60.0 assert trade.stake_amount == 60.0
@ -317,7 +318,7 @@ def test_create_trade(default_conf_usdt, ticker_usdt, limit_order,
patch_get_signal(freqtrade, enter_short=is_short, enter_long=not is_short) patch_get_signal(freqtrade, enter_short=is_short, enter_long=not is_short)
freqtrade.create_trade('ETH/USDT') freqtrade.create_trade('ETH/USDT')
trade = Trade.query.first() trade = Trade.session.scalars(select(Trade)).first()
trade.is_short = is_short trade.is_short = is_short
assert trade is not None assert trade is not None
assert pytest.approx(trade.stake_amount) == 60.0 assert pytest.approx(trade.stake_amount) == 60.0
@ -568,12 +569,12 @@ def test_process_trade_creation(default_conf_usdt, ticker_usdt, limit_order, lim
freqtrade = FreqtradeBot(default_conf_usdt) freqtrade = FreqtradeBot(default_conf_usdt)
patch_get_signal(freqtrade, enter_short=is_short, enter_long=not is_short) patch_get_signal(freqtrade, enter_short=is_short, enter_long=not is_short)
trades = Trade.query.filter(Trade.is_open.is_(True)).all() trades = Trade.get_open_trades()
assert not trades assert not trades
freqtrade.process() freqtrade.process()
trades = Trade.query.filter(Trade.is_open.is_(True)).all() trades = Trade.get_open_trades()
assert len(trades) == 1 assert len(trades) == 1
trade = trades[0] trade = trades[0]
assert trade is not None assert trade is not None
@ -640,11 +641,11 @@ def test_process_trade_handling(default_conf_usdt, ticker_usdt, limit_buy_order_
freqtrade = FreqtradeBot(default_conf_usdt) freqtrade = FreqtradeBot(default_conf_usdt)
patch_get_signal(freqtrade) patch_get_signal(freqtrade)
trades = Trade.query.filter(Trade.is_open.is_(True)).all() trades = Trade.get_open_trades()
assert not trades assert not trades
freqtrade.process() freqtrade.process()
trades = Trade.query.filter(Trade.is_open.is_(True)).all() trades = Trade.get_open_trades()
assert len(trades) == 1 assert len(trades) == 1
# Nothing happened ... # Nothing happened ...
@ -671,7 +672,7 @@ def test_process_trade_no_whitelist_pair(default_conf_usdt, ticker_usdt, limit_b
assert pair not in default_conf_usdt['exchange']['pair_whitelist'] assert pair not in default_conf_usdt['exchange']['pair_whitelist']
# create open trade not in whitelist # create open trade not in whitelist
Trade.query.session.add(Trade( Trade.session.add(Trade(
pair=pair, pair=pair,
stake_amount=0.001, stake_amount=0.001,
fee_open=fee.return_value, fee_open=fee.return_value,
@ -681,7 +682,7 @@ def test_process_trade_no_whitelist_pair(default_conf_usdt, ticker_usdt, limit_b
open_rate=0.01, open_rate=0.01,
exchange='binance', exchange='binance',
)) ))
Trade.query.session.add(Trade( Trade.session.add(Trade(
pair='ETH/USDT', pair='ETH/USDT',
stake_amount=0.001, stake_amount=0.001,
fee_open=fee.return_value, fee_open=fee.return_value,
@ -838,7 +839,7 @@ def test_execute_entry(mocker, default_conf_usdt, fee, limit_order,
# Should create an open trade with an open order id # Should create an open trade with an open order id
# As the order is not fulfilled yet # As the order is not fulfilled yet
trade = Trade.query.first() trade = Trade.session.scalars(select(Trade)).first()
trade.is_short = is_short trade.is_short = is_short
assert trade assert trade
assert trade.is_open is True assert trade.is_open is True
@ -865,7 +866,7 @@ def test_execute_entry(mocker, default_conf_usdt, fee, limit_order,
mocker.patch(f'{EXMS}.create_order', MagicMock(return_value=order)) mocker.patch(f'{EXMS}.create_order', MagicMock(return_value=order))
assert freqtrade.execute_entry(pair, stake_amount, is_short=is_short) assert freqtrade.execute_entry(pair, stake_amount, is_short=is_short)
trade = Trade.query.all()[2] trade = Trade.session.scalars(select(Trade)).all()[2]
trade.is_short = is_short trade.is_short = is_short
assert trade assert trade
assert trade.open_order_id is None assert trade.open_order_id is None
@ -883,7 +884,7 @@ def test_execute_entry(mocker, default_conf_usdt, fee, limit_order,
order['id'] = '555' order['id'] = '555'
mocker.patch(f'{EXMS}.create_order', MagicMock(return_value=order)) mocker.patch(f'{EXMS}.create_order', MagicMock(return_value=order))
assert freqtrade.execute_entry(pair, stake_amount) assert freqtrade.execute_entry(pair, stake_amount)
trade = Trade.query.all()[3] trade = Trade.session.scalars(select(Trade)).all()[3]
trade.is_short = is_short trade.is_short = is_short
assert trade assert trade
assert trade.open_order_id is None assert trade.open_order_id is None
@ -896,7 +897,7 @@ def test_execute_entry(mocker, default_conf_usdt, fee, limit_order,
freqtrade.strategy.custom_stake_amount = lambda **kwargs: 150.0 freqtrade.strategy.custom_stake_amount = lambda **kwargs: 150.0
assert freqtrade.execute_entry(pair, stake_amount, is_short=is_short) assert freqtrade.execute_entry(pair, stake_amount, is_short=is_short)
trade = Trade.query.all()[4] trade = Trade.session.scalars(select(Trade)).all()[4]
trade.is_short = is_short trade.is_short = is_short
assert trade assert trade
assert pytest.approx(trade.stake_amount) == 150 assert pytest.approx(trade.stake_amount) == 150
@ -905,7 +906,7 @@ def test_execute_entry(mocker, default_conf_usdt, fee, limit_order,
order['id'] = '557' order['id'] = '557'
freqtrade.strategy.custom_stake_amount = lambda **kwargs: 20 / 0 freqtrade.strategy.custom_stake_amount = lambda **kwargs: 20 / 0
assert freqtrade.execute_entry(pair, stake_amount, is_short=is_short) assert freqtrade.execute_entry(pair, stake_amount, is_short=is_short)
trade = Trade.query.all()[5] trade = Trade.session.scalars(select(Trade)).all()[5]
trade.is_short = is_short trade.is_short = is_short
assert trade assert trade
assert pytest.approx(trade.stake_amount) == 2.0 assert pytest.approx(trade.stake_amount) == 2.0
@ -934,7 +935,7 @@ def test_execute_entry(mocker, default_conf_usdt, fee, limit_order,
order['id'] = '5566' order['id'] = '5566'
freqtrade.strategy.custom_entry_price = lambda **kwargs: 0.508 freqtrade.strategy.custom_entry_price = lambda **kwargs: 0.508
assert freqtrade.execute_entry(pair, stake_amount, is_short=is_short) assert freqtrade.execute_entry(pair, stake_amount, is_short=is_short)
trade = Trade.query.all()[6] trade = Trade.session.scalars(select(Trade)).all()[6]
trade.is_short = is_short trade.is_short = is_short
assert trade assert trade
assert trade.open_rate_requested == 0.508 assert trade.open_rate_requested == 0.508
@ -951,7 +952,7 @@ def test_execute_entry(mocker, default_conf_usdt, fee, limit_order,
) )
assert freqtrade.execute_entry(pair, stake_amount, is_short=is_short) assert freqtrade.execute_entry(pair, stake_amount, is_short=is_short)
trade = Trade.query.all()[7] trade = Trade.session.scalars(select(Trade)).all()[7]
trade.is_short = is_short trade.is_short = is_short
assert trade assert trade
assert trade.open_rate_requested == 10 assert trade.open_rate_requested == 10
@ -961,7 +962,7 @@ def test_execute_entry(mocker, default_conf_usdt, fee, limit_order,
order['id'] = '5568' order['id'] = '5568'
freqtrade.strategy.custom_entry_price = lambda **kwargs: "string price" freqtrade.strategy.custom_entry_price = lambda **kwargs: "string price"
assert freqtrade.execute_entry(pair, stake_amount, is_short=is_short) assert freqtrade.execute_entry(pair, stake_amount, is_short=is_short)
trade = Trade.query.all()[8] trade = Trade.session.scalars(select(Trade)).all()[8]
# Trade(id=9, pair=ETH/USDT, amount=0.20000000, is_short=False, # Trade(id=9, pair=ETH/USDT, amount=0.20000000, is_short=False,
# leverage=1.0, open_rate=10.00000000, open_since=...) # leverage=1.0, open_rate=10.00000000, open_since=...)
# Trade(id=9, pair=ETH/USDT, amount=0.60000000, is_short=True, # Trade(id=9, pair=ETH/USDT, amount=0.60000000, is_short=True,
@ -982,7 +983,7 @@ def test_execute_entry(mocker, default_conf_usdt, fee, limit_order,
freqtrade.exchange.get_max_pair_stake_amount = MagicMock(return_value=500) freqtrade.exchange.get_max_pair_stake_amount = MagicMock(return_value=500)
assert freqtrade.execute_entry(pair, 2000, is_short=is_short) assert freqtrade.execute_entry(pair, 2000, is_short=is_short)
trade = Trade.query.all()[9] trade = Trade.session.scalars(select(Trade)).all()[9]
trade.is_short = is_short trade.is_short = is_short
assert pytest.approx(trade.stake_amount) == 500 assert pytest.approx(trade.stake_amount) == 500
@ -991,7 +992,7 @@ def test_execute_entry(mocker, default_conf_usdt, fee, limit_order,
freqtrade.strategy.leverage.reset_mock() freqtrade.strategy.leverage.reset_mock()
assert freqtrade.execute_entry(pair, 200, leverage_=3) assert freqtrade.execute_entry(pair, 200, leverage_=3)
assert freqtrade.strategy.leverage.call_count == 0 assert freqtrade.strategy.leverage.call_count == 0
trade = Trade.query.all()[10] trade = Trade.session.scalars(select(Trade)).all()[10]
assert trade.leverage == 1 if trading_mode == 'spot' else 3 assert trade.leverage == 1 if trading_mode == 'spot' else 3
@ -1053,7 +1054,7 @@ def test_execute_entry_min_leverage(mocker, default_conf_usdt, fee, limit_order,
freqtrade.strategy.leverage = MagicMock(return_value=5.0) freqtrade.strategy.leverage = MagicMock(return_value=5.0)
assert freqtrade.execute_entry(pair, stake_amount, is_short=is_short) assert freqtrade.execute_entry(pair, stake_amount, is_short=is_short)
trade = Trade.query.first() trade = Trade.session.scalars(select(Trade)).first()
assert trade.leverage == 5.0 assert trade.leverage == 5.0
# assert trade.stake_amount == 2 # assert trade.stake_amount == 2
@ -1158,7 +1159,7 @@ def test_handle_stoploss_on_exchange(mocker, default_conf_usdt, fee, caplog, is_
# as a trade actually happened # as a trade actually happened
caplog.clear() caplog.clear()
freqtrade.enter_positions() freqtrade.enter_positions()
trade = Trade.query.first() trade = Trade.session.scalars(select(Trade)).first()
trade.is_short = is_short trade.is_short = is_short
trade.is_open = True trade.is_open = True
trade.open_order_id = None trade.open_order_id = None
@ -1271,7 +1272,7 @@ def test_handle_sle_cancel_cant_recreate(mocker, default_conf_usdt, fee, caplog,
patch_get_signal(freqtrade, enter_short=is_short, enter_long=not is_short) patch_get_signal(freqtrade, enter_short=is_short, enter_long=not is_short)
freqtrade.enter_positions() freqtrade.enter_positions()
trade = Trade.query.first() trade = Trade.session.scalars(select(Trade)).first()
trade.is_short = is_short trade.is_short = is_short
trade.is_open = True trade.is_open = True
trade.open_order_id = None trade.open_order_id = None
@ -1316,7 +1317,7 @@ def test_create_stoploss_order_invalid_order(
freqtrade.strategy.order_types['stoploss_on_exchange'] = True freqtrade.strategy.order_types['stoploss_on_exchange'] = True
freqtrade.enter_positions() freqtrade.enter_positions()
trade = Trade.query.first() trade = Trade.session.scalars(select(Trade)).first()
trade.is_short = is_short trade.is_short = is_short
caplog.clear() caplog.clear()
freqtrade.create_stoploss_order(trade, 200) freqtrade.create_stoploss_order(trade, 200)
@ -1367,7 +1368,7 @@ def test_create_stoploss_order_insufficient_funds(
freqtrade.strategy.order_types['stoploss_on_exchange'] = True freqtrade.strategy.order_types['stoploss_on_exchange'] = True
freqtrade.enter_positions() freqtrade.enter_positions()
trade = Trade.query.first() trade = Trade.session.scalars(select(Trade)).first()
trade.is_short = is_short trade.is_short = is_short
caplog.clear() caplog.clear()
freqtrade.create_stoploss_order(trade, 200) freqtrade.create_stoploss_order(trade, 200)
@ -1435,7 +1436,7 @@ def test_handle_stoploss_on_exchange_trailing(
patch_get_signal(freqtrade, enter_short=is_short, enter_long=not is_short) patch_get_signal(freqtrade, enter_short=is_short, enter_long=not is_short)
freqtrade.enter_positions() freqtrade.enter_positions()
trade = Trade.query.first() trade = Trade.session.scalars(select(Trade)).first()
trade.is_short = is_short trade.is_short = is_short
trade.is_open = True trade.is_open = True
trade.open_order_id = None trade.open_order_id = None
@ -1554,7 +1555,7 @@ def test_handle_stoploss_on_exchange_trailing_error(
freqtrade.strategy.order_types['stoploss_on_exchange_interval'] = 60 freqtrade.strategy.order_types['stoploss_on_exchange_interval'] = 60
patch_get_signal(freqtrade, enter_short=is_short, enter_long=not is_short) patch_get_signal(freqtrade, enter_short=is_short, enter_long=not is_short)
freqtrade.enter_positions() freqtrade.enter_positions()
trade = Trade.query.first() trade = Trade.session.scalars(select(Trade)).first()
trade.is_short = is_short trade.is_short = is_short
trade.is_open = True trade.is_open = True
trade.open_order_id = None trade.open_order_id = None
@ -1668,7 +1669,7 @@ def test_handle_stoploss_on_exchange_custom_stop(
patch_get_signal(freqtrade, enter_short=is_short, enter_long=not is_short) patch_get_signal(freqtrade, enter_short=is_short, enter_long=not is_short)
freqtrade.enter_positions() freqtrade.enter_positions()
trade = Trade.query.first() trade = Trade.session.scalars(select(Trade)).first()
trade.is_short = is_short trade.is_short = is_short
trade.is_open = True trade.is_open = True
trade.open_order_id = None trade.open_order_id = None
@ -1796,7 +1797,7 @@ def test_tsl_on_exchange_compatible_with_edge(mocker, edge_conf, fee, limit_orde
freqtrade.active_pair_whitelist = freqtrade.edge.adjust(freqtrade.active_pair_whitelist) freqtrade.active_pair_whitelist = freqtrade.edge.adjust(freqtrade.active_pair_whitelist)
freqtrade.enter_positions() freqtrade.enter_positions()
trade = Trade.query.first() trade = Trade.session.scalars(select(Trade)).first()
trade.is_open = True trade.is_open = True
trade.open_order_id = None trade.open_order_id = None
trade.stoploss_order_id = 100 trade.stoploss_order_id = 100
@ -2162,7 +2163,7 @@ def test_handle_trade(
freqtrade.enter_positions() freqtrade.enter_positions()
trade = Trade.query.first() trade = Trade.session.scalars(select(Trade)).first()
trade.is_short = is_short trade.is_short = is_short
assert trade assert trade
@ -2217,7 +2218,7 @@ def test_handle_overlapping_signals(
freqtrade.enter_positions() freqtrade.enter_positions()
# Buy and Sell triggering, so doing nothing ... # Buy and Sell triggering, so doing nothing ...
trades = Trade.query.all() trades = Trade.session.scalars(select(Trade)).all()
nb_trades = len(trades) nb_trades = len(trades)
assert nb_trades == 0 assert nb_trades == 0
@ -2225,7 +2226,7 @@ def test_handle_overlapping_signals(
# Buy is triggering, so buying ... # Buy is triggering, so buying ...
patch_get_signal(freqtrade, enter_short=is_short, enter_long=not is_short) patch_get_signal(freqtrade, enter_short=is_short, enter_long=not is_short)
freqtrade.enter_positions() freqtrade.enter_positions()
trades = Trade.query.all() trades = Trade.session.scalars(select(Trade)).all()
for trade in trades: for trade in trades:
trade.is_short = is_short trade.is_short = is_short
nb_trades = len(trades) nb_trades = len(trades)
@ -2235,7 +2236,7 @@ def test_handle_overlapping_signals(
# Buy and Sell are not triggering, so doing nothing ... # Buy and Sell are not triggering, so doing nothing ...
patch_get_signal(freqtrade, enter_long=False) patch_get_signal(freqtrade, enter_long=False)
assert freqtrade.handle_trade(trades[0]) is False assert freqtrade.handle_trade(trades[0]) is False
trades = Trade.query.all() trades = Trade.session.scalars(select(Trade)).all()
for trade in trades: for trade in trades:
trade.is_short = is_short trade.is_short = is_short
nb_trades = len(trades) nb_trades = len(trades)
@ -2248,7 +2249,7 @@ def test_handle_overlapping_signals(
else: else:
patch_get_signal(freqtrade, enter_long=True, exit_long=True) patch_get_signal(freqtrade, enter_long=True, exit_long=True)
assert freqtrade.handle_trade(trades[0]) is False assert freqtrade.handle_trade(trades[0]) is False
trades = Trade.query.all() trades = Trade.session.scalars(select(Trade)).all()
for trade in trades: for trade in trades:
trade.is_short = is_short trade.is_short = is_short
nb_trades = len(trades) nb_trades = len(trades)
@ -2260,7 +2261,7 @@ def test_handle_overlapping_signals(
patch_get_signal(freqtrade, enter_long=False, exit_short=True) patch_get_signal(freqtrade, enter_long=False, exit_short=True)
else: else:
patch_get_signal(freqtrade, enter_long=False, exit_long=True) patch_get_signal(freqtrade, enter_long=False, exit_long=True)
trades = Trade.query.all() trades = Trade.session.scalars(select(Trade)).all()
for trade in trades: for trade in trades:
trade.is_short = is_short trade.is_short = is_short
assert freqtrade.handle_trade(trades[0]) is True assert freqtrade.handle_trade(trades[0]) is True
@ -2291,7 +2292,7 @@ def test_handle_trade_roi(default_conf_usdt, ticker_usdt, limit_order_open, fee,
freqtrade.enter_positions() freqtrade.enter_positions()
trade = Trade.query.first() trade = Trade.session.scalars(select(Trade)).first()
trade.is_short = is_short trade.is_short = is_short
trade.is_open = True trade.is_open = True
@ -2333,7 +2334,7 @@ def test_handle_trade_use_exit_signal(
freqtrade.strategy.min_roi_reached = MagicMock(return_value=False) freqtrade.strategy.min_roi_reached = MagicMock(return_value=False)
freqtrade.enter_positions() freqtrade.enter_positions()
trade = Trade.query.first() trade = Trade.session.scalars(select(Trade)).first()
trade.is_short = is_short trade.is_short = is_short
trade.is_open = True trade.is_open = True
@ -2370,7 +2371,7 @@ def test_close_trade(
# Create trade and sell it # Create trade and sell it
freqtrade.enter_positions() freqtrade.enter_positions()
trade = Trade.query.first() trade = Trade.session.scalars(select(Trade)).first()
trade.is_short = is_short trade.is_short = is_short
assert trade assert trade
@ -2427,7 +2428,7 @@ def test_manage_open_orders_entry_usercustom(
open_trade.is_short = is_short open_trade.is_short = is_short
open_trade.orders[0].side = 'sell' if is_short else 'buy' open_trade.orders[0].side = 'sell' if is_short else 'buy'
open_trade.orders[0].ft_order_side = 'sell' if is_short else 'buy' open_trade.orders[0].ft_order_side = 'sell' if is_short else 'buy'
Trade.query.session.add(open_trade) Trade.session.add(open_trade)
Trade.commit() Trade.commit()
# Ensure default is to return empty (so not mocked yet) # Ensure default is to return empty (so not mocked yet)
@ -2438,7 +2439,8 @@ def test_manage_open_orders_entry_usercustom(
freqtrade.strategy.check_entry_timeout = MagicMock(return_value=False) freqtrade.strategy.check_entry_timeout = MagicMock(return_value=False)
freqtrade.manage_open_orders() freqtrade.manage_open_orders()
assert cancel_order_mock.call_count == 0 assert cancel_order_mock.call_count == 0
trades = Trade.query.filter(Trade.open_order_id.is_(open_trade.open_order_id)).all() trades = Trade.session.scalars(
select(Trade).filter(Trade.open_order_id.is_(open_trade.open_order_id))).all()
nb_trades = len(trades) nb_trades = len(trades)
assert nb_trades == 1 assert nb_trades == 1
assert freqtrade.strategy.check_entry_timeout.call_count == 1 assert freqtrade.strategy.check_entry_timeout.call_count == 1
@ -2446,7 +2448,8 @@ def test_manage_open_orders_entry_usercustom(
freqtrade.manage_open_orders() freqtrade.manage_open_orders()
assert cancel_order_mock.call_count == 0 assert cancel_order_mock.call_count == 0
trades = Trade.query.filter(Trade.open_order_id.is_(open_trade.open_order_id)).all() trades = Trade.session.scalars(
select(Trade).filter(Trade.open_order_id.is_(open_trade.open_order_id))).all()
nb_trades = len(trades) nb_trades = len(trades)
assert nb_trades == 1 assert nb_trades == 1
assert freqtrade.strategy.check_entry_timeout.call_count == 1 assert freqtrade.strategy.check_entry_timeout.call_count == 1
@ -2456,7 +2459,8 @@ def test_manage_open_orders_entry_usercustom(
freqtrade.manage_open_orders() freqtrade.manage_open_orders()
assert cancel_order_wr_mock.call_count == 1 assert cancel_order_wr_mock.call_count == 1
assert rpc_mock.call_count == 2 assert rpc_mock.call_count == 2
trades = Trade.query.filter(Trade.open_order_id.is_(open_trade.open_order_id)).all() trades = Trade.session.scalars(
select(Trade).filter(Trade.open_order_id.is_(open_trade.open_order_id))).all()
nb_trades = len(trades) nb_trades = len(trades)
assert nb_trades == 0 assert nb_trades == 0
assert freqtrade.strategy.check_entry_timeout.call_count == 1 assert freqtrade.strategy.check_entry_timeout.call_count == 1
@ -2486,7 +2490,7 @@ def test_manage_open_orders_entry(
freqtrade = FreqtradeBot(default_conf_usdt) freqtrade = FreqtradeBot(default_conf_usdt)
open_trade.is_short = is_short open_trade.is_short = is_short
Trade.query.session.add(open_trade) Trade.session.add(open_trade)
Trade.commit() Trade.commit()
freqtrade.strategy.check_entry_timeout = MagicMock(return_value=False) freqtrade.strategy.check_entry_timeout = MagicMock(return_value=False)
@ -2524,7 +2528,7 @@ def test_adjust_entry_cancel(
) )
open_trade.is_short = is_short open_trade.is_short = is_short
Trade.query.session.add(open_trade) Trade.session.add(open_trade)
Trade.commit() Trade.commit()
# Timeout to not interfere # Timeout to not interfere
@ -2535,7 +2539,7 @@ def test_adjust_entry_cancel(
freqtrade.manage_open_orders() freqtrade.manage_open_orders()
trades = Trade.query.filter(Trade.open_order_id.is_(open_trade.open_order_id)).all() trades = Trade.query.filter(Trade.open_order_id.is_(open_trade.open_order_id)).all()
assert len(trades) == 0 assert len(trades) == 0
assert len(Order.query.all()) == 0 assert len(Order.session.scalars(select(Order)).all()) == 0
assert log_has_re( assert log_has_re(
f"{'Sell' if is_short else 'Buy'} order user requested order cancel*", caplog) f"{'Sell' if is_short else 'Buy'} order user requested order cancel*", caplog)
assert log_has_re( assert log_has_re(
@ -2565,7 +2569,7 @@ def test_adjust_entry_maintain_replace(
) )
open_trade.is_short = is_short open_trade.is_short = is_short
Trade.query.session.add(open_trade) Trade.session.add(open_trade)
Trade.commit() Trade.commit()
# Timeout to not interfere # Timeout to not interfere
@ -2586,7 +2590,7 @@ def test_adjust_entry_maintain_replace(
freqtrade.manage_open_orders() freqtrade.manage_open_orders()
trades = Trade.query.filter(Trade.open_order_id.is_(open_trade.open_order_id)).all() trades = Trade.query.filter(Trade.open_order_id.is_(open_trade.open_order_id)).all()
assert len(trades) == 1 assert len(trades) == 1
nb_all_orders = len(Order.query.all()) nb_all_orders = len(Order.session.scalars(select(Order)).all())
assert nb_all_orders == 2 assert nb_all_orders == 2
# New order seems to be in closed status? # New order seems to be in closed status?
# nb_open_orders = len(Order.get_open_orders()) # nb_open_orders = len(Order.get_open_orders())
@ -2618,7 +2622,7 @@ def test_check_handle_cancelled_buy(
freqtrade = FreqtradeBot(default_conf_usdt) freqtrade = FreqtradeBot(default_conf_usdt)
open_trade.orders = [] open_trade.orders = []
open_trade.is_short = is_short open_trade.is_short = is_short
Trade.query.session.add(open_trade) Trade.session.add(open_trade)
Trade.commit() Trade.commit()
# check it does cancel buy orders over the time limit # check it does cancel buy orders over the time limit
@ -2649,7 +2653,7 @@ def test_manage_open_orders_buy_exception(
freqtrade = FreqtradeBot(default_conf_usdt) freqtrade = FreqtradeBot(default_conf_usdt)
open_trade.is_short = is_short open_trade.is_short = is_short
Trade.query.session.add(open_trade) Trade.session.add(open_trade)
Trade.commit() Trade.commit()
# check it does cancel buy orders over the time limit # check it does cancel buy orders over the time limit
@ -2691,7 +2695,7 @@ def test_manage_open_orders_exit_usercustom(
open_trade_usdt.close_date = arrow.utcnow().shift(minutes=-601).datetime open_trade_usdt.close_date = arrow.utcnow().shift(minutes=-601).datetime
open_trade_usdt.close_profit_abs = 0.001 open_trade_usdt.close_profit_abs = 0.001
Trade.query.session.add(open_trade_usdt) Trade.session.add(open_trade_usdt)
Trade.commit() Trade.commit()
# Ensure default is false # Ensure default is false
freqtrade.manage_open_orders() freqtrade.manage_open_orders()
@ -2771,7 +2775,7 @@ def test_manage_open_orders_exit(
open_trade_usdt.close_profit_abs = 0.001 open_trade_usdt.close_profit_abs = 0.001
open_trade_usdt.is_short = is_short open_trade_usdt.is_short = is_short
Trade.query.session.add(open_trade_usdt) Trade.session.add(open_trade_usdt)
Trade.commit() Trade.commit()
freqtrade.strategy.check_exit_timeout = MagicMock(return_value=False) freqtrade.strategy.check_exit_timeout = MagicMock(return_value=False)
@ -2811,7 +2815,7 @@ def test_check_handle_cancelled_exit(
open_trade_usdt.close_date = arrow.utcnow().shift(minutes=-601).datetime open_trade_usdt.close_date = arrow.utcnow().shift(minutes=-601).datetime
open_trade_usdt.is_short = is_short open_trade_usdt.is_short = is_short
Trade.query.session.add(open_trade_usdt) Trade.session.add(open_trade_usdt)
Trade.commit() Trade.commit()
# check it does cancel sell orders over the time limit # check it does cancel sell orders over the time limit
@ -2848,7 +2852,7 @@ def test_manage_open_orders_partial(
) )
freqtrade = FreqtradeBot(default_conf_usdt) freqtrade = FreqtradeBot(default_conf_usdt)
prior_stake = open_trade.stake_amount prior_stake = open_trade.stake_amount
Trade.query.session.add(open_trade) Trade.session.add(open_trade)
Trade.commit() Trade.commit()
# check it does cancel buy orders over the time limit # check it does cancel buy orders over the time limit
@ -2893,7 +2897,7 @@ def test_manage_open_orders_partial_fee(
open_trade.fee_open = fee() open_trade.fee_open = fee()
open_trade.fee_close = fee() open_trade.fee_close = fee()
Trade.query.session.add(open_trade) Trade.session.add(open_trade)
Trade.commit() Trade.commit()
# cancelling a half-filled order should update the amount to the bought amount # cancelling a half-filled order should update the amount to the bought amount
# and apply fees if necessary. # and apply fees if necessary.
@ -2943,7 +2947,7 @@ def test_manage_open_orders_partial_except(
open_trade.fee_open = fee() open_trade.fee_open = fee()
open_trade.fee_close = fee() open_trade.fee_close = fee()
Trade.query.session.add(open_trade) Trade.session.add(open_trade)
Trade.commit() Trade.commit()
# cancelling a half-filled order should update the amount to the bought amount # cancelling a half-filled order should update the amount to the bought amount
# and apply fees if necessary. # and apply fees if necessary.
@ -2982,7 +2986,7 @@ def test_manage_open_orders_exception(default_conf_usdt, ticker_usdt, open_trade
) )
freqtrade = FreqtradeBot(default_conf_usdt) freqtrade = FreqtradeBot(default_conf_usdt)
Trade.query.session.add(open_trade_usdt) Trade.session.add(open_trade_usdt)
Trade.commit() Trade.commit()
caplog.clear() caplog.clear()
@ -3011,7 +3015,7 @@ def test_handle_cancel_enter(mocker, caplog, default_conf_usdt, limit_order, is_
freqtrade._notify_enter_cancel = MagicMock() freqtrade._notify_enter_cancel = MagicMock()
trade = mock_trade_usdt_4(fee, is_short) trade = mock_trade_usdt_4(fee, is_short)
Trade.query.session.add(trade) Trade.session.add(trade)
Trade.commit() Trade.commit()
l_order['filled'] = 0.0 l_order['filled'] = 0.0
@ -3061,7 +3065,7 @@ def test_handle_cancel_enter_exchanges(mocker, caplog, default_conf_usdt, is_sho
reason = CANCEL_REASON['TIMEOUT'] reason = CANCEL_REASON['TIMEOUT']
trade = mock_trade_usdt_4(fee, is_short) trade = mock_trade_usdt_4(fee, is_short)
Trade.query.session.add(trade) Trade.session.add(trade)
Trade.commit() Trade.commit()
assert freqtrade.handle_cancel_enter(trade, limit_buy_order_canceled_empty, reason) assert freqtrade.handle_cancel_enter(trade, limit_buy_order_canceled_empty, reason)
assert cancel_order_mock.call_count == 0 assert cancel_order_mock.call_count == 0
@ -3095,7 +3099,7 @@ def test_handle_cancel_enter_corder_empty(mocker, default_conf_usdt, limit_order
freqtrade = FreqtradeBot(default_conf_usdt) freqtrade = FreqtradeBot(default_conf_usdt)
freqtrade._notify_enter_cancel = MagicMock() freqtrade._notify_enter_cancel = MagicMock()
trade = mock_trade_usdt_4(fee, is_short) trade = mock_trade_usdt_4(fee, is_short)
Trade.query.session.add(trade) Trade.session.add(trade)
Trade.commit() Trade.commit()
l_order['filled'] = 0.0 l_order['filled'] = 0.0
l_order['status'] = 'open' l_order['status'] = 'open'
@ -3261,7 +3265,7 @@ def test_execute_trade_exit_up(default_conf_usdt, ticker_usdt, fee, ticker_usdt_
freqtrade.enter_positions() freqtrade.enter_positions()
rpc_mock.reset_mock() rpc_mock.reset_mock()
trade = Trade.query.first() trade = Trade.session.scalars(select(Trade)).first()
assert trade.is_short == is_short assert trade.is_short == is_short
assert trade assert trade
assert freqtrade.strategy.confirm_trade_exit.call_count == 0 assert freqtrade.strategy.confirm_trade_exit.call_count == 0
@ -3342,7 +3346,7 @@ def test_execute_trade_exit_down(default_conf_usdt, ticker_usdt, fee, ticker_usd
# Create some test data # Create some test data
freqtrade.enter_positions() freqtrade.enter_positions()
trade = Trade.query.first() trade = Trade.session.scalars(select(Trade)).first()
trade.is_short = is_short trade.is_short = is_short
assert trade assert trade
@ -3415,7 +3419,7 @@ def test_execute_trade_exit_custom_exit_price(
freqtrade.enter_positions() freqtrade.enter_positions()
rpc_mock.reset_mock() rpc_mock.reset_mock()
trade = Trade.query.first() trade = Trade.session.scalars(select(Trade)).first()
trade.is_short = is_short trade.is_short = is_short
assert trade assert trade
assert freqtrade.strategy.confirm_trade_exit.call_count == 0 assert freqtrade.strategy.confirm_trade_exit.call_count == 0
@ -3492,7 +3496,7 @@ def test_execute_trade_exit_down_stoploss_on_exchange_dry_run(
# Create some test data # Create some test data
freqtrade.enter_positions() freqtrade.enter_positions()
trade = Trade.query.first() trade = Trade.session.scalars(select(Trade)).first()
assert trade.is_short == is_short assert trade.is_short == is_short
assert trade assert trade
@ -3566,7 +3570,7 @@ def test_execute_trade_exit_sloe_cancel_exception(
patch_get_signal(freqtrade) patch_get_signal(freqtrade)
freqtrade.enter_positions() freqtrade.enter_positions()
trade = Trade.query.first() trade = Trade.session.scalars(select(Trade)).first()
PairLock.session = MagicMock() PairLock.session = MagicMock()
freqtrade.config['dry_run'] = False freqtrade.config['dry_run'] = False
@ -3611,7 +3615,7 @@ def test_execute_trade_exit_with_stoploss_on_exchange(
# Create some test data # Create some test data
freqtrade.enter_positions() freqtrade.enter_positions()
trade = Trade.query.first() trade = Trade.session.scalars(select(Trade)).first()
trade.is_short = is_short trade.is_short = is_short
assert trade assert trade
trades = [trade] trades = [trade]
@ -3631,7 +3635,7 @@ def test_execute_trade_exit_with_stoploss_on_exchange(
exit_check=ExitCheckTuple(exit_type=ExitType.STOP_LOSS) exit_check=ExitCheckTuple(exit_type=ExitType.STOP_LOSS)
) )
trade = Trade.query.first() trade = Trade.session.scalars(select(Trade)).first()
trade.is_short = is_short trade.is_short = is_short
assert trade assert trade
assert cancel_order.call_count == 1 assert cancel_order.call_count == 1
@ -3669,7 +3673,7 @@ def test_may_execute_trade_exit_after_stoploss_on_exchange_hit(
# Create some test data # Create some test data
freqtrade.enter_positions() freqtrade.enter_positions()
freqtrade.manage_open_orders() freqtrade.manage_open_orders()
trade = Trade.query.first() trade = Trade.session.scalars(select(Trade)).first()
trades = [trade] trades = [trade]
assert trade.stoploss_order_id is None assert trade.stoploss_order_id is None
@ -3755,7 +3759,7 @@ def test_execute_trade_exit_market_order(
# Create some test data # Create some test data
freqtrade.enter_positions() freqtrade.enter_positions()
trade = Trade.query.first() trade = Trade.session.scalars(select(Trade)).first()
trade.is_short = is_short trade.is_short = is_short
assert trade assert trade
@ -3830,7 +3834,7 @@ def test_execute_trade_exit_insufficient_funds_error(default_conf_usdt, ticker_u
# Create some test data # Create some test data
freqtrade.enter_positions() freqtrade.enter_positions()
trade = Trade.query.first() trade = Trade.session.scalars(select(Trade)).first()
trade.is_short = is_short trade.is_short = is_short
assert trade assert trade
@ -3898,7 +3902,7 @@ def test_exit_profit_only(
exit_type=ExitType.NONE)) exit_type=ExitType.NONE))
freqtrade.enter_positions() freqtrade.enter_positions()
trade = Trade.query.first() trade = Trade.session.scalars(select(Trade)).first()
assert trade.is_short == is_short assert trade.is_short == is_short
oobj = Order.parse_from_ccxt_object(limit_order[eside], limit_order[eside]['symbol'], eside) oobj = Order.parse_from_ccxt_object(limit_order[eside], limit_order[eside]['symbol'], eside)
trade.update_order(limit_order[eside]) trade.update_order(limit_order[eside])
@ -3941,7 +3945,7 @@ def test_sell_not_enough_balance(default_conf_usdt, limit_order, limit_order_ope
freqtrade.enter_positions() freqtrade.enter_positions()
trade = Trade.query.first() trade = Trade.session.scalars(select(Trade)).first()
amnt = trade.amount amnt = trade.amount
oobj = Order.parse_from_ccxt_object(limit_order['buy'], limit_order['buy']['symbol'], 'buy') oobj = Order.parse_from_ccxt_object(limit_order['buy'], limit_order['buy']['symbol'], 'buy')
@ -4009,7 +4013,7 @@ def test_locked_pairs(default_conf_usdt, ticker_usdt, fee,
# Create some test data # Create some test data
freqtrade.enter_positions() freqtrade.enter_positions()
trade = Trade.query.first() trade = Trade.session.scalars(select(Trade)).first()
trade.is_short = is_short trade.is_short = is_short
assert trade assert trade
@ -4064,7 +4068,7 @@ def test_ignore_roi_if_entry_signal(default_conf_usdt, limit_order, limit_order_
freqtrade.enter_positions() freqtrade.enter_positions()
trade = Trade.query.first() trade = Trade.session.scalars(select(Trade)).first()
trade.is_short = is_short trade.is_short = is_short
oobj = Order.parse_from_ccxt_object( oobj = Order.parse_from_ccxt_object(
limit_order[eside], limit_order[eside]['symbol'], eside) limit_order[eside], limit_order[eside]['symbol'], eside)
@ -4114,7 +4118,7 @@ def test_trailing_stop_loss(default_conf_usdt, limit_order_open,
freqtrade.strategy.min_roi_reached = MagicMock(return_value=False) freqtrade.strategy.min_roi_reached = MagicMock(return_value=False)
freqtrade.enter_positions() freqtrade.enter_positions()
trade = Trade.query.first() trade = Trade.session.scalars(select(Trade)).first()
assert trade.is_short == is_short assert trade.is_short == is_short
assert freqtrade.handle_trade(trade) is False assert freqtrade.handle_trade(trade) is False
@ -4189,7 +4193,7 @@ def test_trailing_stop_loss_positive(
freqtrade.strategy.min_roi_reached = MagicMock(return_value=False) freqtrade.strategy.min_roi_reached = MagicMock(return_value=False)
freqtrade.enter_positions() freqtrade.enter_positions()
trade = Trade.query.first() trade = Trade.session.scalars(select(Trade)).first()
assert trade.is_short == is_short assert trade.is_short == is_short
oobj = Order.parse_from_ccxt_object(limit_order[eside], limit_order[eside]['symbol'], eside) oobj = Order.parse_from_ccxt_object(limit_order[eside], limit_order[eside]['symbol'], eside)
trade.update_order(limit_order[eside]) trade.update_order(limit_order[eside])
@ -4286,7 +4290,7 @@ def test_disable_ignore_roi_if_entry_signal(default_conf_usdt, limit_order, limi
freqtrade.enter_positions() freqtrade.enter_positions()
trade = Trade.query.first() trade = Trade.session.scalars(select(Trade)).first()
trade.is_short = is_short trade.is_short = is_short
oobj = Order.parse_from_ccxt_object( oobj = Order.parse_from_ccxt_object(
@ -4752,7 +4756,7 @@ def test_order_book_depth_of_market(
patch_get_signal(freqtrade, enter_short=is_short, enter_long=not is_short) patch_get_signal(freqtrade, enter_short=is_short, enter_long=not is_short)
freqtrade.enter_positions() freqtrade.enter_positions()
trade = Trade.query.first() trade = Trade.session.scalars(select(Trade)).first()
if is_high_delta: if is_high_delta:
assert trade is None assert trade is None
else: else:
@ -4763,7 +4767,7 @@ def test_order_book_depth_of_market(
assert trade.open_date is not None assert trade.open_date is not None
assert trade.exchange == 'binance' assert trade.exchange == 'binance'
assert len(Trade.query.all()) == 1 assert len(Trade.session.scalars(select(Trade)).all()) == 1
# Simulate fulfilled LIMIT_BUY order for trade # Simulate fulfilled LIMIT_BUY order for trade
oobj = Order.parse_from_ccxt_object( oobj = Order.parse_from_ccxt_object(
@ -4860,7 +4864,7 @@ def test_order_book_exit_pricing(
freqtrade.enter_positions() freqtrade.enter_positions()
trade = Trade.query.first() trade = Trade.session.scalars(select(Trade)).first()
assert trade assert trade
time.sleep(0.01) # Race condition fix time.sleep(0.01) # Race condition fix
@ -4932,7 +4936,7 @@ def test_sync_wallet_dry_run(mocker, default_conf_usdt, ticker_usdt, fee, limit_
n = bot.enter_positions() n = bot.enter_positions()
assert n == 2 assert n == 2
trades = Trade.query.all() trades = Trade.session.scalars(select(Trade)).all()
assert len(trades) == 2 assert len(trades) == 2
bot.config['max_open_trades'] = 3 bot.config['max_open_trades'] = 3
@ -4965,7 +4969,7 @@ def test_cancel_all_open_orders(mocker, default_conf_usdt, fee, limit_order, lim
freqtrade = get_patched_freqtradebot(mocker, default_conf_usdt) freqtrade = get_patched_freqtradebot(mocker, default_conf_usdt)
create_mock_trades(fee, is_short=is_short) create_mock_trades(fee, is_short=is_short)
trades = Trade.query.all() trades = Trade.session.scalars(select(Trade)).all()
assert len(trades) == MOCK_TRADE_COUNT assert len(trades) == MOCK_TRADE_COUNT
freqtrade.cancel_all_open_orders() freqtrade.cancel_all_open_orders()
assert buy_mock.call_count == buy_calls assert buy_mock.call_count == buy_calls
@ -4981,7 +4985,7 @@ def test_check_for_open_trades(mocker, default_conf_usdt, fee, is_short):
assert freqtrade.rpc.send_msg.call_count == 0 assert freqtrade.rpc.send_msg.call_count == 0
create_mock_trades(fee, is_short) create_mock_trades(fee, is_short)
trade = Trade.query.first() trade = Trade.session.scalars(select(Trade)).first()
trade.is_short = is_short trade.is_short = is_short
trade.is_open = True trade.is_open = True
@ -5149,7 +5153,7 @@ def test_reupdate_enter_order_fees(mocker, default_conf_usdt, fee, caplog, is_sh
exchange='binance', exchange='binance',
is_short=is_short is_short=is_short
) )
Trade.query.session.add(trade) Trade.session.add(trade)
freqtrade.handle_insufficient_funds(trade) freqtrade.handle_insufficient_funds(trade)
# assert log_has_re(r"Trying to reupdate buy fees for .*", caplog) # assert log_has_re(r"Trying to reupdate buy fees for .*", caplog)
@ -5546,10 +5550,10 @@ def test_position_adjust(mocker, default_conf_usdt, fee) -> None:
assert freqtrade.execute_entry(pair, stake_amount) assert freqtrade.execute_entry(pair, stake_amount)
# Should create an closed trade with an no open order id # Should create an closed trade with an no open order id
# Order is filled and trade is open # Order is filled and trade is open
orders = Order.query.all() orders = Order.session.scalars(select(Order)).all()
assert orders assert orders
assert len(orders) == 1 assert len(orders) == 1
trade = Trade.query.first() trade = Trade.session.scalars(select(Trade)).first()
assert trade assert trade
assert trade.is_open is True assert trade.is_open is True
assert trade.open_order_id is None assert trade.open_order_id is None
@ -5559,7 +5563,7 @@ def test_position_adjust(mocker, default_conf_usdt, fee) -> None:
# Assume it does nothing since order is closed and trade is open # Assume it does nothing since order is closed and trade is open
freqtrade.update_trades_without_assigned_fees() freqtrade.update_trades_without_assigned_fees()
trade = Trade.query.first() trade = Trade.session.scalars(select(Trade)).first()
assert trade assert trade
assert trade.is_open is True assert trade.is_open is True
assert trade.open_order_id is None assert trade.open_order_id is None
@ -5569,7 +5573,7 @@ def test_position_adjust(mocker, default_conf_usdt, fee) -> None:
freqtrade.manage_open_orders() freqtrade.manage_open_orders()
trade = Trade.query.first() trade = Trade.session.scalars(select(Trade)).first()
assert trade assert trade
assert trade.is_open is True assert trade.is_open is True
assert trade.open_order_id is None assert trade.open_order_id is None
@ -5595,10 +5599,10 @@ def test_position_adjust(mocker, default_conf_usdt, fee) -> None:
mocker.patch(f'{EXMS}.fetch_order_or_stoploss_order', MagicMock(return_value=open_dca_order_1)) mocker.patch(f'{EXMS}.fetch_order_or_stoploss_order', MagicMock(return_value=open_dca_order_1))
assert freqtrade.execute_entry(pair, stake_amount, trade=trade) assert freqtrade.execute_entry(pair, stake_amount, trade=trade)
orders = Order.query.all() orders = Order.session.scalars(select(Order)).all()
assert orders assert orders
assert len(orders) == 2 assert len(orders) == 2
trade = Trade.query.first() trade = Trade.session.scalars(select(Trade)).first()
assert trade assert trade
assert trade.open_order_id == '651' assert trade.open_order_id == '651'
assert trade.open_rate == 11 assert trade.open_rate == 11
@ -5628,14 +5632,14 @@ def test_position_adjust(mocker, default_conf_usdt, fee) -> None:
mocker.patch(f'{EXMS}.fetch_order_or_stoploss_order', fetch_order_mm) mocker.patch(f'{EXMS}.fetch_order_or_stoploss_order', fetch_order_mm)
freqtrade.update_trades_without_assigned_fees() freqtrade.update_trades_without_assigned_fees()
orders = Order.query.all() orders = Order.session.scalars(select(Order)).all()
assert orders assert orders
assert len(orders) == 2 assert len(orders) == 2
# Assert that the trade is found as open and without fees # Assert that the trade is found as open and without fees
trades: List[Trade] = Trade.get_open_trades_without_assigned_fees() trades: List[Trade] = Trade.get_open_trades_without_assigned_fees()
assert len(trades) == 1 assert len(trades) == 1
# Assert trade is as expected # Assert trade is as expected
trade = Trade.query.first() trade = Trade.session.scalars(select(Trade)).first()
assert trade assert trade
assert trade.open_order_id == '651' assert trade.open_order_id == '651'
assert trade.open_rate == 11 assert trade.open_rate == 11
@ -5672,14 +5676,14 @@ def test_position_adjust(mocker, default_conf_usdt, fee) -> None:
freqtrade.manage_open_orders() freqtrade.manage_open_orders()
# Assert trade is as expected (averaged dca) # Assert trade is as expected (averaged dca)
trade = Trade.query.first() trade = Trade.session.scalars(select(Trade)).first()
assert trade assert trade
assert trade.open_order_id is None assert trade.open_order_id is None
assert pytest.approx(trade.open_rate) == 9.90909090909 assert pytest.approx(trade.open_rate) == 9.90909090909
assert trade.amount == 22 assert trade.amount == 22
assert pytest.approx(trade.stake_amount) == 218 assert pytest.approx(trade.stake_amount) == 218
orders = Order.query.all() orders = Order.session.scalars(select(Order)).all()
assert orders assert orders
assert len(orders) == 2 assert len(orders) == 2
@ -5714,14 +5718,14 @@ def test_position_adjust(mocker, default_conf_usdt, fee) -> None:
assert freqtrade.execute_entry(pair, stake_amount, trade=trade) assert freqtrade.execute_entry(pair, stake_amount, trade=trade)
# Assert trade is as expected (averaged dca) # Assert trade is as expected (averaged dca)
trade = Trade.query.first() trade = Trade.session.scalars(select(Trade)).first()
assert trade assert trade
assert trade.open_order_id is None assert trade.open_order_id is None
assert pytest.approx(trade.open_rate) == 8.729729729729 assert pytest.approx(trade.open_rate) == 8.729729729729
assert trade.amount == 37 assert trade.amount == 37
assert trade.stake_amount == 323 assert trade.stake_amount == 323
orders = Order.query.all() orders = Order.session.scalars(select(Order)).all()
assert orders assert orders
assert len(orders) == 3 assert len(orders) == 3
@ -5752,7 +5756,7 @@ def test_position_adjust(mocker, default_conf_usdt, fee) -> None:
sub_trade_amt=15) sub_trade_amt=15)
# Assert trade is as expected (averaged dca) # Assert trade is as expected (averaged dca)
trade = Trade.query.first() trade = Trade.session.scalars(select(Trade)).first()
assert trade assert trade
assert trade.open_order_id is None assert trade.open_order_id is None
assert trade.is_open assert trade.is_open
@ -5760,7 +5764,7 @@ def test_position_adjust(mocker, default_conf_usdt, fee) -> None:
assert trade.stake_amount == 192.05405405405406 assert trade.stake_amount == 192.05405405405406
assert pytest.approx(trade.open_rate) == 8.729729729729 assert pytest.approx(trade.open_rate) == 8.729729729729
orders = Order.query.all() orders = Order.session.scalars(select(Order)).all()
assert orders assert orders
assert len(orders) == 4 assert len(orders) == 4
@ -5825,10 +5829,10 @@ def test_position_adjust2(mocker, default_conf_usdt, fee) -> None:
assert freqtrade.execute_entry(pair, amount) assert freqtrade.execute_entry(pair, amount)
# Should create an closed trade with an no open order id # Should create an closed trade with an no open order id
# Order is filled and trade is open # Order is filled and trade is open
orders = Order.query.all() orders = Order.session.scalars(select(Order)).all()
assert orders assert orders
assert len(orders) == 1 assert len(orders) == 1
trade = Trade.query.first() trade = Trade.session.scalars(select(Trade)).first()
assert trade assert trade
assert trade.is_open is True assert trade.is_open is True
assert trade.open_order_id is None assert trade.open_order_id is None
@ -5838,7 +5842,7 @@ def test_position_adjust2(mocker, default_conf_usdt, fee) -> None:
# Assume it does nothing since order is closed and trade is open # Assume it does nothing since order is closed and trade is open
freqtrade.update_trades_without_assigned_fees() freqtrade.update_trades_without_assigned_fees()
trade = Trade.query.first() trade = Trade.session.scalars(select(Trade)).first()
assert trade assert trade
assert trade.is_open is True assert trade.is_open is True
assert trade.open_order_id is None assert trade.open_order_id is None
@ -5848,7 +5852,7 @@ def test_position_adjust2(mocker, default_conf_usdt, fee) -> None:
freqtrade.manage_open_orders() freqtrade.manage_open_orders()
trade = Trade.query.first() trade = Trade.session.scalars(select(Trade)).first()
assert trade assert trade
assert trade.is_open is True assert trade.is_open is True
assert trade.open_order_id is None assert trade.open_order_id is None
@ -5884,7 +5888,7 @@ def test_position_adjust2(mocker, default_conf_usdt, fee) -> None:
assert len(trades) == 1 assert len(trades) == 1
# Assert trade is as expected (averaged dca) # Assert trade is as expected (averaged dca)
trade = Trade.query.first() trade = Trade.session.scalars(select(Trade)).first()
assert trade assert trade
assert trade.open_order_id is None assert trade.open_order_id is None
assert trade.amount == 50 assert trade.amount == 50
@ -5893,7 +5897,7 @@ def test_position_adjust2(mocker, default_conf_usdt, fee) -> None:
assert pytest.approx(trade.realized_profit) == -152.375 assert pytest.approx(trade.realized_profit) == -152.375
assert pytest.approx(trade.close_profit_abs) == -152.375 assert pytest.approx(trade.close_profit_abs) == -152.375
orders = Order.query.all() orders = Order.session.scalars(select(Order)).all()
assert orders assert orders
assert len(orders) == 2 assert len(orders) == 2
# Make sure the closed order is found as the second order. # Make sure the closed order is found as the second order.
@ -5926,7 +5930,7 @@ def test_position_adjust2(mocker, default_conf_usdt, fee) -> None:
sub_trade_amt=amount) sub_trade_amt=amount)
# Assert trade is as expected (averaged dca) # Assert trade is as expected (averaged dca)
trade = Trade.query.first() trade = Trade.session.scalars(select(Trade)).first()
assert trade assert trade
assert trade.open_order_id is None assert trade.open_order_id is None
assert trade.amount == 50 assert trade.amount == 50
@ -5935,7 +5939,7 @@ def test_position_adjust2(mocker, default_conf_usdt, fee) -> None:
# Trade fully realized # Trade fully realized
assert pytest.approx(trade.realized_profit) == 94.25 assert pytest.approx(trade.realized_profit) == 94.25
assert pytest.approx(trade.close_profit_abs) == 94.25 assert pytest.approx(trade.close_profit_abs) == 94.25
orders = Order.query.all() orders = Order.session.scalars(select(Order)).all()
assert orders assert orders
assert len(orders) == 3 assert len(orders) == 3
@ -6020,11 +6024,11 @@ def test_position_adjust3(mocker, default_conf_usdt, fee, data) -> None:
exit_check=ExitCheckTuple(exit_type=ExitType.PARTIAL_EXIT), exit_check=ExitCheckTuple(exit_type=ExitType.PARTIAL_EXIT),
sub_trade_amt=amount) sub_trade_amt=amount)
orders1 = Order.query.all() orders1 = Order.session.scalars(select(Order)).all()
assert orders1 assert orders1
assert len(orders1) == idx + 1 assert len(orders1) == idx + 1
trade = Trade.query.first() trade = Trade.session.scalars(select(Trade)).first()
assert trade assert trade
if idx < len(data) - 1: if idx < len(data) - 1:
assert trade.is_open is True assert trade.is_open is True
@ -6039,7 +6043,7 @@ def test_position_adjust3(mocker, default_conf_usdt, fee, data) -> None:
order_obj = trade.select_order(order[0], False) order_obj = trade.select_order(order[0], False)
assert order_obj.order_id == f'60{idx}' assert order_obj.order_id == f'60{idx}'
trade = Trade.query.first() trade = Trade.session.scalars(select(Trade)).first()
assert trade assert trade
assert trade.open_order_id is None assert trade.open_order_id is None
assert trade.is_open is False assert trade.is_open is False

View File

@ -1,6 +1,7 @@
from unittest.mock import MagicMock from unittest.mock import MagicMock
import pytest import pytest
from sqlalchemy import select
from freqtrade.enums import ExitCheckTuple, ExitType, TradingMode from freqtrade.enums import ExitCheckTuple, ExitType, TradingMode
from freqtrade.persistence import Trade from freqtrade.persistence import Trade
@ -91,7 +92,7 @@ def test_may_execute_exit_stoploss_on_exchange_multi(default_conf, ticker, fee,
assert freqtrade.strategy.confirm_trade_exit.call_count == 0 assert freqtrade.strategy.confirm_trade_exit.call_count == 0
wallets_mock.reset_mock() wallets_mock.reset_mock()
trades = Trade.query.all() trades = Trade.session.scalars(select(Trade)).all()
# Make sure stoploss-order is open and trade is bought (since we mock update_trade_state) # Make sure stoploss-order is open and trade is bought (since we mock update_trade_state)
for trade in trades: for trade in trades:
stoploss_order_closed['id'] = '3' stoploss_order_closed['id'] = '3'
@ -179,13 +180,13 @@ def test_forcebuy_last_unlimited(default_conf, ticker, fee, mocker, balance_rati
n = freqtrade.enter_positions() n = freqtrade.enter_positions()
assert n == 4 assert n == 4
trades = Trade.query.all() trades = Trade.session.scalars(select(Trade)).all()
assert len(trades) == 4 assert len(trades) == 4
assert freqtrade.wallets.get_trade_stake_amount('XRP/BTC') == result1 assert freqtrade.wallets.get_trade_stake_amount('XRP/BTC') == result1
rpc._rpc_force_entry('TKN/BTC', None) rpc._rpc_force_entry('TKN/BTC', None)
trades = Trade.query.all() trades = Trade.session.scalars(select(Trade)).all()
assert len(trades) == 5 assert len(trades) == 5
for trade in trades: for trade in trades: