Merge pull request #5924 from freqtrade/feat/leverage
call leverage methods
This commit is contained in:
@@ -698,7 +698,8 @@ def test_backtest_one(default_conf, fee, mocker, testdatadir) -> None:
|
||||
'min_rate': [0.10370188, 0.10300000000000001],
|
||||
'max_rate': [0.10501, 0.1038888],
|
||||
'is_open': [False, False],
|
||||
'buy_tag': [None, None]
|
||||
'buy_tag': [None, None],
|
||||
"is_short": [False, False],
|
||||
})
|
||||
pd.testing.assert_frame_equal(results, expected)
|
||||
data_pair = processed[pair]
|
||||
@@ -1074,6 +1075,8 @@ def test_backtest_start_multi_strat_nomock(default_conf, mocker, caplog, testdat
|
||||
'stake_amount': [0.01, 0.01],
|
||||
'open_rate': [0.104445, 0.10302485],
|
||||
'close_rate': [0.104969, 0.103541],
|
||||
"is_short": [False, False],
|
||||
|
||||
'sell_reason': [SellType.ROI, SellType.ROI]
|
||||
})
|
||||
result2 = pd.DataFrame({'pair': ['XRP/BTC', 'LTC/BTC', 'ETH/BTC'],
|
||||
@@ -1091,6 +1094,7 @@ def test_backtest_start_multi_strat_nomock(default_conf, mocker, caplog, testdat
|
||||
'stake_amount': [0.01, 0.01, 0.01],
|
||||
'open_rate': [0.104445, 0.10302485, 0.122541],
|
||||
'close_rate': [0.104969, 0.103541, 0.123541],
|
||||
"is_short": [False, False, False],
|
||||
'sell_reason': [SellType.ROI, SellType.ROI, SellType.STOP_LOSS]
|
||||
})
|
||||
backtestmock = MagicMock(side_effect=[
|
||||
@@ -1180,6 +1184,7 @@ def test_backtest_start_multi_strat_nomock_detail(default_conf, mocker,
|
||||
'2018-01-30 05:35:00', ], utc=True),
|
||||
'trade_duration': [235, 40],
|
||||
'is_open': [False, False],
|
||||
'is_short': [False, False],
|
||||
'stake_amount': [0.01, 0.01],
|
||||
'open_rate': [0.104445, 0.10302485],
|
||||
'close_rate': [0.104969, 0.103541],
|
||||
@@ -1197,6 +1202,7 @@ def test_backtest_start_multi_strat_nomock_detail(default_conf, mocker,
|
||||
'2018-01-30 08:30:00'], utc=True),
|
||||
'trade_duration': [47, 40, 20],
|
||||
'is_open': [False, False, False],
|
||||
'is_short': [False, False, False],
|
||||
'stake_amount': [0.01, 0.01, 0.01],
|
||||
'open_rate': [0.104445, 0.10302485, 0.122541],
|
||||
'close_rate': [0.104969, 0.103541, 0.123541],
|
||||
|
@@ -344,6 +344,7 @@ def test_hyperopt_format_results(hyperopt):
|
||||
"close_rate": [0.002546, 0.003014, 0.003103, 0.003217],
|
||||
"trade_duration": [123, 34, 31, 14],
|
||||
"is_open": [False, False, False, True],
|
||||
"is_short": [False, False, False, False],
|
||||
"stake_amount": [0.01, 0.01, 0.01, 0.01],
|
||||
"sell_reason": [SellType.ROI, SellType.STOP_LOSS,
|
||||
SellType.ROI, SellType.FORCE_SELL]
|
||||
@@ -412,6 +413,7 @@ def test_generate_optimizer(mocker, hyperopt_conf) -> None:
|
||||
"close_rate": [0.002546, 0.003014, 0.003103, 0.003217],
|
||||
"trade_duration": [123, 34, 31, 14],
|
||||
"is_open": [False, False, False, True],
|
||||
"is_short": [False, False, False, False],
|
||||
"stake_amount": [0.01, 0.01, 0.01, 0.01],
|
||||
"sell_reason": [SellType.ROI, SellType.STOP_LOSS,
|
||||
SellType.ROI, SellType.FORCE_SELL]
|
||||
|
@@ -76,6 +76,7 @@ def test_generate_backtest_stats(default_conf, testdatadir, tmpdir):
|
||||
"close_rate": [0.002546, 0.003014, 0.003103, 0.003217],
|
||||
"trade_duration": [123, 34, 31, 14],
|
||||
"is_open": [False, False, False, True],
|
||||
"is_short": [False, False, False, False],
|
||||
"stake_amount": [0.01, 0.01, 0.01, 0.01],
|
||||
"sell_reason": [SellType.ROI, SellType.STOP_LOSS,
|
||||
SellType.ROI, SellType.FORCE_SELL]
|
||||
@@ -124,6 +125,7 @@ def test_generate_backtest_stats(default_conf, testdatadir, tmpdir):
|
||||
"close_rate": [0.002546, 0.003014, 0.0032903, 0.003217],
|
||||
"trade_duration": [123, 34, 31, 14],
|
||||
"is_open": [False, False, False, True],
|
||||
"is_short": [False, False, False, False],
|
||||
"stake_amount": [0.01, 0.01, 0.01, 0.01],
|
||||
"sell_reason": [SellType.ROI, SellType.ROI,
|
||||
SellType.STOP_LOSS, SellType.FORCE_SELL]
|
||||
|
@@ -701,17 +701,26 @@ def test_process_informative_pairs_added(default_conf_usdt, ticker_usdt, mocker)
|
||||
assert ("ETH/USDT", default_conf_usdt["timeframe"]) in refresh_mock.call_args[0][0]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("trading_mode", [
|
||||
'spot',
|
||||
# TODO-lev: Enable other modes
|
||||
# 'margin', 'futures'
|
||||
]
|
||||
)
|
||||
@pytest.mark.parametrize("is_short", [False, True])
|
||||
def test_execute_entry(mocker, default_conf_usdt, fee, limit_order,
|
||||
limit_order_open, is_short) -> None:
|
||||
limit_order_open, is_short, trading_mode) -> None:
|
||||
|
||||
open_order = limit_order_open[enter_side(is_short)]
|
||||
order = limit_order[enter_side(is_short)]
|
||||
|
||||
default_conf_usdt['trading_mode'] = trading_mode
|
||||
leverage = 1.0 if trading_mode == 'spot' else 3.0
|
||||
default_conf_usdt['collateral'] = 'cross'
|
||||
patch_RPCManager(mocker)
|
||||
patch_exchange(mocker)
|
||||
freqtrade = FreqtradeBot(default_conf_usdt)
|
||||
freqtrade.strategy.confirm_trade_entry = MagicMock(return_value=False)
|
||||
freqtrade.strategy.leverage = MagicMock(return_value=leverage)
|
||||
stake_amount = 2
|
||||
bid = 0.11
|
||||
enter_rate_mock = MagicMock(return_value=bid)
|
||||
@@ -727,6 +736,7 @@ def test_execute_entry(mocker, default_conf_usdt, fee, limit_order,
|
||||
create_order=enter_mm,
|
||||
get_min_pair_stake_amount=MagicMock(return_value=1),
|
||||
get_fee=fee,
|
||||
get_funding_fees=MagicMock(return_value=0),
|
||||
)
|
||||
pair = 'ETH/USDT'
|
||||
|
||||
@@ -744,7 +754,7 @@ def test_execute_entry(mocker, default_conf_usdt, fee, limit_order,
|
||||
call_args = enter_mm.call_args_list[0][1]
|
||||
assert call_args['pair'] == pair
|
||||
assert call_args['rate'] == bid
|
||||
assert call_args['amount'] == round(stake_amount / bid, 8)
|
||||
assert pytest.approx(call_args['amount'], round(stake_amount / bid * leverage, 8))
|
||||
enter_rate_mock.reset_mock()
|
||||
|
||||
# Should create an open trade with an open order id
|
||||
@@ -766,7 +776,7 @@ def test_execute_entry(mocker, default_conf_usdt, fee, limit_order,
|
||||
call_args = enter_mm.call_args_list[1][1]
|
||||
assert call_args['pair'] == pair
|
||||
assert call_args['rate'] == fix_price
|
||||
assert call_args['amount'] == round(stake_amount / fix_price, 8)
|
||||
assert pytest.approx(call_args['amount'], round(stake_amount / fix_price * leverage, 8))
|
||||
|
||||
# In case of closed order
|
||||
order['status'] = 'closed'
|
||||
@@ -824,7 +834,7 @@ def test_execute_entry(mocker, default_conf_usdt, fee, limit_order,
|
||||
|
||||
# In case of the order is rejected and not filled at all
|
||||
order['status'] = 'rejected'
|
||||
order['amount'] = 30.0
|
||||
order['amount'] = 30.0 * leverage
|
||||
order['filled'] = 0.0
|
||||
order['remaining'] = 30.0
|
||||
order['price'] = 0.5
|
||||
@@ -833,6 +843,7 @@ def test_execute_entry(mocker, default_conf_usdt, fee, limit_order,
|
||||
mocker.patch('freqtrade.exchange.Exchange.create_order',
|
||||
MagicMock(return_value=order))
|
||||
assert not freqtrade.execute_entry(pair, stake_amount)
|
||||
assert freqtrade.strategy.leverage.call_count == 0 if trading_mode == 'spot' else 2
|
||||
|
||||
# Fail to get price...
|
||||
mocker.patch('freqtrade.exchange.Exchange.get_rate', MagicMock(return_value=0.0))
|
||||
|
Reference in New Issue
Block a user