Added leverage brackets dry run test

This commit is contained in:
Sam Germain 2021-09-17 00:41:00 -06:00
parent 798a0c9827
commit 32e52cd460
4 changed files with 1283 additions and 74 deletions

View File

@ -33,7 +33,7 @@ class Binance(Exchange):
# TradingMode.SPOT always supported and not required in this list
# (TradingMode.MARGIN, Collateral.CROSS), # TODO-lev: Uncomment once supported
# (TradingMode.FUTURES, Collateral.CROSS), # TODO-lev: Uncomment once supported
# (TradingMode.FUTURES, Collateral.ISOLATED) # TODO-lev: Uncomment once supported
(TradingMode.FUTURES, Collateral.ISOLATED)
]
def stoploss_adjust(self, stop_loss: float, order: Dict, side: str) -> bool:

View File

@ -2,16 +2,14 @@ from datetime import datetime, timezone
from random import randint
from unittest.mock import MagicMock, PropertyMock
import json
from pathlib import Path
import ccxt
import pytest
from freqtrade.enums import TradingMode
from freqtrade.enums import Collateral, TradingMode
from freqtrade.exceptions import DependencyException, InvalidOrderException, OperationalException
from tests.conftest import get_mock_coro, get_patched_exchange, log_has_re
from tests.exchange.test_exchange import ccxt_exceptionhandlers
from tests.leverage_brackets import leverage_brackets
@pytest.mark.parametrize('limitratio,expected,side', [
@ -206,76 +204,61 @@ def test_get_max_leverage_binance(default_conf, mocker, pair, nominal_value, max
def test_fill_leverage_brackets_binance(default_conf, mocker):
api_mock = MagicMock()
# api_mock.load_leverage_brackets = MagicMock(return_value={
# 'ADA/BUSD': [[0.0, 0.025],
# [100000.0, 0.05],
# [500000.0, 0.1],
# [1000000.0, 0.15],
# [2000000.0, 0.25],
# [5000000.0, 0.5]],
# 'BTC/USDT': [[0.0, 0.004],
# [50000.0, 0.005],
# [250000.0, 0.01],
# [1000000.0, 0.025],
# [5000000.0, 0.05],
# [20000000.0, 0.1],
# [50000000.0, 0.125],
# [100000000.0, 0.15],
# [200000000.0, 0.25],
# [300000000.0, 0.5]],
# "ZEC/USDT": [[0.0, 0.01],
# [5000.0, 0.025],
# [25000.0, 0.05],
# [100000.0, 0.1],
# [250000.0, 0.125],
# [1000000.0, 0.5]],
api_mock.load_leverage_brackets = MagicMock(return_value={
'ADA/BUSD': [[0.0, 0.025],
[100000.0, 0.05],
[500000.0, 0.1],
[1000000.0, 0.15],
[2000000.0, 0.25],
[5000000.0, 0.5]],
'BTC/USDT': [[0.0, 0.004],
[50000.0, 0.005],
[250000.0, 0.01],
[1000000.0, 0.025],
[5000000.0, 0.05],
[20000000.0, 0.1],
[50000000.0, 0.125],
[100000000.0, 0.15],
[200000000.0, 0.25],
[300000000.0, 0.5]],
"ZEC/USDT": [[0.0, 0.01],
[5000.0, 0.025],
[25000.0, 0.05],
[100000.0, 0.1],
[250000.0, 0.125],
[1000000.0, 0.5]],
# })
})
default_conf['dry_run'] = False
default_conf['trading_mode'] = TradingMode.FUTURES
default_conf['collateral'] = Collateral.ISOLATED
exchange = get_patched_exchange(mocker, default_conf, api_mock, id="binance")
exchange.trading_mode = TradingMode.FUTURES
exchange.fill_leverage_brackets()
leverage_brackets_path = Path('freqtrade/data') / 'leverage_brackets.json'
with open(leverage_brackets_path) as json_file:
leverage_brackets = json.load(json_file)
for pair, brackets in leverage_brackets.items():
leverage_brackets[pair] = [
[
min_amount,
float(margin_req)
] for [
min_amount,
margin_req
] in brackets
]
assert exchange._leverage_brackets == leverage_brackets
# assert exchange._leverage_brackets == {
# 'ADA/BUSD': [[0.0, 0.025],
# [100000.0, 0.05],
# [500000.0, 0.1],
# [1000000.0, 0.15],
# [2000000.0, 0.25],
# [5000000.0, 0.5]],
# 'BTC/USDT': [[0.0, 0.004],
# [50000.0, 0.005],
# [250000.0, 0.01],
# [1000000.0, 0.025],
# [5000000.0, 0.05],
# [20000000.0, 0.1],
# [50000000.0, 0.125],
# [100000000.0, 0.15],
# [200000000.0, 0.25],
# [300000000.0, 0.5]],
# "ZEC/USDT": [[0.0, 0.01],
# [5000.0, 0.025],
# [25000.0, 0.05],
# [100000.0, 0.1],
# [250000.0, 0.125],
# [1000000.0, 0.5]],
# }
assert exchange._leverage_brackets == {
'ADA/BUSD': [[0.0, 0.025],
[100000.0, 0.05],
[500000.0, 0.1],
[1000000.0, 0.15],
[2000000.0, 0.25],
[5000000.0, 0.5]],
'BTC/USDT': [[0.0, 0.004],
[50000.0, 0.005],
[250000.0, 0.01],
[1000000.0, 0.025],
[5000000.0, 0.05],
[20000000.0, 0.1],
[50000000.0, 0.125],
[100000000.0, 0.15],
[200000000.0, 0.25],
[300000000.0, 0.5]],
"ZEC/USDT": [[0.0, 0.01],
[5000.0, 0.025],
[25000.0, 0.05],
[100000.0, 0.1],
[250000.0, 0.125],
[1000000.0, 0.5]],
}
api_mock = MagicMock()
api_mock.load_leverage_brackets = MagicMock()
@ -291,12 +274,22 @@ def test_fill_leverage_brackets_binance(default_conf, mocker):
)
def test_fill_leverage_brackets_binance_dryrun(default_conf, mocker):
api_mock = MagicMock()
default_conf['trading_mode'] = TradingMode.FUTURES
default_conf['collateral'] = Collateral.ISOLATED
exchange = get_patched_exchange(mocker, default_conf, api_mock, id="binance")
exchange.fill_leverage_brackets()
assert exchange._leverage_brackets == leverage_brackets()
def test__set_leverage_binance(mocker, default_conf):
api_mock = MagicMock()
api_mock.set_leverage = MagicMock()
type(api_mock).has = PropertyMock(return_value={'setLeverage': True})
default_conf['dry_run'] = False
exchange = get_patched_exchange(mocker, default_conf, id="binance")
exchange._set_leverage(3.0, trading_mode=TradingMode.MARGIN)

View File

@ -3076,6 +3076,7 @@ def test__set_leverage(mocker, default_conf, exchange_name, trading_mode):
api_mock = MagicMock()
api_mock.set_leverage = MagicMock()
type(api_mock).has = PropertyMock(return_value={'setLeverage': True})
default_conf['dry_run'] = False
ccxt_exceptionhandlers(
mocker,
@ -3099,6 +3100,7 @@ def test_set_margin_mode(mocker, default_conf, collateral):
api_mock = MagicMock()
api_mock.set_margin_mode = MagicMock()
type(api_mock).has = PropertyMock(return_value={'setMarginMode': True})
default_conf['dry_run'] = False
ccxt_exceptionhandlers(
mocker,
@ -3130,7 +3132,6 @@ def test_set_margin_mode(mocker, default_conf, collateral):
# TODO-lev: Remove once implemented
("binance", TradingMode.MARGIN, Collateral.CROSS, True),
("binance", TradingMode.FUTURES, Collateral.CROSS, True),
("binance", TradingMode.FUTURES, Collateral.ISOLATED, True),
("kraken", TradingMode.MARGIN, Collateral.CROSS, True),
("kraken", TradingMode.FUTURES, Collateral.CROSS, True),
("ftx", TradingMode.MARGIN, Collateral.CROSS, True),
@ -3139,7 +3140,7 @@ def test_set_margin_mode(mocker, default_conf, collateral):
# TODO-lev: Uncomment once implemented
# ("binance", TradingMode.MARGIN, Collateral.CROSS, False),
# ("binance", TradingMode.FUTURES, Collateral.CROSS, False),
# ("binance", TradingMode.FUTURES, Collateral.ISOLATED, False),
("binance", TradingMode.FUTURES, Collateral.ISOLATED, False),
# ("kraken", TradingMode.MARGIN, Collateral.CROSS, False),
# ("kraken", TradingMode.FUTURES, Collateral.CROSS, False),
# ("ftx", TradingMode.MARGIN, Collateral.CROSS, False),

1215
tests/leverage_brackets.py Normal file

File diff suppressed because it is too large Load Diff