Adding templates for leverage/short tests

All previous pytests pass
This commit is contained in:
Sam Germain
2021-06-21 21:26:31 -06:00
parent 10979361c1
commit 000932eed0
6 changed files with 197 additions and 39 deletions

View File

@@ -221,6 +221,8 @@ def create_mock_trades(fee, use_db: bool = True):
trade = mock_trade_6(fee)
add_trade(trade)
# TODO-mg: Add margin trades
if use_db:
Trade.query.session.flush()
@@ -250,6 +252,7 @@ def patch_coingekko(mocker) -> None:
@pytest.fixture(scope='function')
def init_persistence(default_conf):
init_db(default_conf['db_url'], default_conf['dry_run'])
# TODO-mg: margin with leverage and/or borrowed?
@pytest.fixture(scope="function")
@@ -812,7 +815,7 @@ def shitcoinmarkets(markets):
"future": False,
"active": True
},
})
})
return shitmarkets
@@ -914,18 +917,17 @@ def limit_sell_order_old():
@pytest.fixture
def limit_buy_order_old_partial():
return {
'id': 'mocked_limit_buy_old_partial',
'type': 'limit',
'side': 'buy',
'symbol': 'ETH/BTC',
'datetime': arrow.utcnow().shift(minutes=-601).isoformat(),
'price': 0.00001099,
'amount': 90.99181073,
'filled': 23.0,
'remaining': 67.99181073,
'status': 'open'
}
return {'id': 'mocked_limit_buy_old_partial',
'type': 'limit',
'side': 'buy',
'symbol': 'ETH/BTC',
'datetime': arrow.utcnow().shift(minutes=-601).isoformat(),
'price': 0.00001099,
'amount': 90.99181073,
'filled': 23.0,
'remaining': 67.99181073,
'status': 'open'
}
@pytest.fixture
@@ -1769,6 +1771,7 @@ def rpc_balance():
'used': 0.0
},
}
# TODO-mg: Add shorts and leverage?
@pytest.fixture
@@ -2084,3 +2087,95 @@ def saved_hyperopt_results():
].total_seconds()
return hyperopt_res
# * Margin Tests
@pytest.fixture
def leveraged_fee():
return
@pytest.fixture
def short_fee():
return
@pytest.fixture
def ticker_short():
return
@pytest.fixture
def ticker_exit_short_up():
return
@pytest.fixture
def ticker_exit_short_down():
return
@pytest.fixture
def leveraged_markets():
return
@pytest.fixture(scope='function')
def limit_short_order_open():
return
@pytest.fixture(scope='function')
def limit_short_order(limit_short_order_open):
return
@pytest.fixture(scope='function')
def market_short_order():
return
@pytest.fixture
def market_short_exit_order():
return
@pytest.fixture
def limit_short_order_old():
return
@pytest.fixture
def limit_exit_short_order_old():
return
@pytest.fixture
def limit_short_order_old_partial():
return
@pytest.fixture
def limit_short_order_old_partial_canceled(limit_short_order_old_partial):
return
@pytest.fixture(scope='function')
def limit_short_order_canceled_empty(request):
return
@pytest.fixture
def limit_exit_short_order_open():
return
@pytest.fixture
def limit_exit_short_order(limit_sell_order_open):
return
@pytest.fixture
def short_order_fee():
return

View File

@@ -3,7 +3,7 @@ from datetime import datetime, timedelta, timezone
from freqtrade.persistence.models import Order, Trade
MOCK_TRADE_COUNT = 6
MOCK_TRADE_COUNT = 6 # TODO-mg: Increase for short and leverage
def mock_order_1():
@@ -303,3 +303,5 @@ def mock_trade_6(fee):
o = Order.parse_from_ccxt_object(mock_order_6_sell(), 'LTC/BTC', 'sell')
trade.orders.append(o)
return trade
# TODO-mg: Mock orders for leveraged and short trades

View File

@@ -107,6 +107,14 @@ def test_rpc_trade_status(default_conf, ticker, fee, mocker) -> None:
'stoploss_entry_dist_ratio': -0.10448878,
'open_order': None,
'exchange': 'binance',
'leverage': 1.0,
'borrowed': 0.0,
'borrowed_currency': None,
'collateral_currency': None,
'interest_rate': 0.0,
'liquidation_price': None,
'is_short': False,
}
mocker.patch('freqtrade.exchange.Exchange.get_rate',
@@ -173,6 +181,15 @@ def test_rpc_trade_status(default_conf, ticker, fee, mocker) -> None:
'stoploss_entry_dist_ratio': -0.10448878,
'open_order': None,
'exchange': 'binance',
'leverage': 1.0,
'borrowed': 0.0,
'borrowed_currency': None,
'collateral_currency': None,
'interest_rate': 0.0,
'liquidation_price': None,
'is_short': False,
}

View File

@@ -129,6 +129,9 @@ def test_update_with_binance(limit_buy_order, limit_sell_order, fee, caplog):
r"pair=ETH/BTC, amount=90.99181073, open_rate=0.00001099, open_since=.*\).",
caplog)
# TODO-mg: create a short order
# TODO-mg: create a leveraged long order
@pytest.mark.usefixtures("init_persistence")
def test_update_market_order(market_buy_order, market_sell_order, fee, caplog):
@@ -167,6 +170,9 @@ def test_update_market_order(market_buy_order, market_sell_order, fee, caplog):
r"pair=ETH/BTC, amount=91.99181073, open_rate=0.00004099, open_since=.*\).",
caplog)
# TODO-mg: market short
# TODO-mg: market leveraged long
@pytest.mark.usefixtures("init_persistence")
def test_calc_open_close_trade_price(limit_buy_order, limit_sell_order, fee):
@@ -659,11 +665,13 @@ def test_migrate_new(mocker, default_conf, fee, caplog):
order_date DATETIME,
order_filled_date DATETIME,
order_update_date DATETIME,
leverage FLOAT,
PRIMARY KEY (id),
CONSTRAINT _order_pair_order_id UNIQUE (ft_pair, order_id),
FOREIGN KEY(ft_trade_id) REFERENCES trades (id)
)
"""))
# TODO-mg: Had to add field leverage to this table, check that this is correct
connection.execute(text("""
insert into orders ( id, ft_trade_id, ft_order_side, ft_pair, ft_is_open, order_id, status,
@@ -912,6 +920,14 @@ def test_to_json(default_conf, fee):
'strategy': None,
'timeframe': None,
'exchange': 'binance',
'leverage': None,
'borrowed': None,
'borrowed_currency': None,
'collateral_currency': None,
'interest_rate': None,
'liquidation_price': None,
'is_short': None,
}
# Simulate dry_run entries
@@ -977,6 +993,14 @@ def test_to_json(default_conf, fee):
'strategy': None,
'timeframe': None,
'exchange': 'binance',
'leverage': None,
'borrowed': None,
'borrowed_currency': None,
'collateral_currency': None,
'interest_rate': None,
'liquidation_price': None,
'is_short': None,
}
@@ -1315,11 +1339,11 @@ def test_Trade_object_idem():
'get_overall_performance',
'get_total_closed_profit',
'total_open_trades_stakes',
'get_sold_trades_without_assigned_fees',
'get_closed_trades_without_assigned_fees',
'get_open_trades_without_assigned_fees',
'get_open_order_trades',
'get_trades',
)
)
# Parent (LocalTrade) should have the same attributes
for item in trade: