total_open_trades_stake should support no-db mode

This commit is contained in:
Matthias 2021-02-10 19:33:39 +01:00
parent 0faa6f84dc
commit 0754a7a78f
4 changed files with 30 additions and 12 deletions

View File

@ -671,9 +671,13 @@ class Trade(_DECL_BASE):
Calculates total invested amount in open trades
in stake currency
"""
total_open_stake_amount = Trade.session.query(func.sum(Trade.stake_amount))\
.filter(Trade.is_open.is_(True))\
.scalar()
if Trade.use_db:
total_open_stake_amount = Trade.session.query(func.sum(Trade.stake_amount))\
.filter(Trade.is_open.is_(True))\
.scalar()
else:
total_open_stake_amount = sum(
t.stake_amount for t in Trade.get_trades_proxy(is_open=True))
return total_open_stake_amount or 0
@staticmethod

View File

@ -183,28 +183,34 @@ def patch_get_signal(freqtrade: FreqtradeBot, value=(True, False)) -> None:
freqtrade.exchange.refresh_latest_ohlcv = lambda p: None
def create_mock_trades(fee):
def create_mock_trades(fee, use_db: bool = True):
"""
Create some fake trades ...
"""
def add_trade(trade):
if use_db:
Trade.session.add(trade)
else:
Trade.trades.append(trade)
# Simulate dry_run entries
trade = mock_trade_1(fee)
Trade.session.add(trade)
add_trade(trade)
trade = mock_trade_2(fee)
Trade.session.add(trade)
add_trade(trade)
trade = mock_trade_3(fee)
Trade.session.add(trade)
add_trade(trade)
trade = mock_trade_4(fee)
Trade.session.add(trade)
add_trade(trade)
trade = mock_trade_5(fee)
Trade.session.add(trade)
add_trade(trade)
trade = mock_trade_6(fee)
Trade.session.add(trade)
add_trade(trade)
@pytest.fixture(autouse=True)

View File

@ -28,6 +28,7 @@ def mock_trade_1(fee):
amount_requested=123.0,
fee_open=fee.return_value,
fee_close=fee.return_value,
is_open=True,
open_rate=0.123,
exchange='bittrex',
open_order_id='dry_run_buy_12345',
@ -180,6 +181,7 @@ def mock_trade_4(fee):
amount_requested=124.0,
fee_open=fee.return_value,
fee_close=fee.return_value,
is_open=True,
open_rate=0.123,
exchange='bittrex',
open_order_id='prod_buy_12345',
@ -230,6 +232,7 @@ def mock_trade_5(fee):
amount_requested=124.0,
fee_open=fee.return_value,
fee_close=fee.return_value,
is_open=True,
open_rate=0.123,
exchange='bittrex',
strategy='SampleStrategy',
@ -281,6 +284,7 @@ def mock_trade_6(fee):
amount_requested=2.0,
fee_open=fee.return_value,
fee_close=fee.return_value,
is_open=True,
open_rate=0.15,
exchange='bittrex',
strategy='SampleStrategy',

View File

@ -1039,14 +1039,18 @@ def test_fee_updated(fee):
@pytest.mark.usefixtures("init_persistence")
def test_total_open_trades_stakes(fee):
@pytest.mark.parametrize('use_db', [True, False])
def test_total_open_trades_stakes(fee, use_db):
Trade.use_db = use_db
res = Trade.total_open_trades_stakes()
assert res == 0
create_mock_trades(fee)
create_mock_trades(fee, use_db)
res = Trade.total_open_trades_stakes()
assert res == 0.004
Trade.use_db = True
@pytest.mark.usefixtures("init_persistence")
def test_get_overall_performance(fee):