Merged feat/short into lev-strat
This commit is contained in:
@@ -18,7 +18,7 @@ from freqtrade import constants
|
||||
from freqtrade.commands import Arguments
|
||||
from freqtrade.data.converter import ohlcv_to_dataframe
|
||||
from freqtrade.edge import Edge, PairInfo
|
||||
from freqtrade.enums import RunMode
|
||||
from freqtrade.enums import Collateral, RunMode, TradingMode
|
||||
from freqtrade.enums.signaltype import SignalDirection
|
||||
from freqtrade.exchange import Exchange
|
||||
from freqtrade.freqtradebot import FreqtradeBot
|
||||
@@ -82,7 +82,13 @@ def patched_configuration_load_config_file(mocker, config) -> None:
|
||||
)
|
||||
|
||||
|
||||
def patch_exchange(mocker, api_mock=None, id='binance', mock_markets=True) -> None:
|
||||
def patch_exchange(
|
||||
mocker,
|
||||
api_mock=None,
|
||||
id='binance',
|
||||
mock_markets=True,
|
||||
mock_supported_modes=True
|
||||
) -> None:
|
||||
mocker.patch('freqtrade.exchange.Exchange._load_async_markets', MagicMock(return_value={}))
|
||||
mocker.patch('freqtrade.exchange.Exchange.validate_pairs', MagicMock())
|
||||
mocker.patch('freqtrade.exchange.Exchange.validate_timeframes', MagicMock())
|
||||
@@ -91,10 +97,22 @@ def patch_exchange(mocker, api_mock=None, id='binance', mock_markets=True) -> No
|
||||
mocker.patch('freqtrade.exchange.Exchange.id', PropertyMock(return_value=id))
|
||||
mocker.patch('freqtrade.exchange.Exchange.name', PropertyMock(return_value=id.title()))
|
||||
mocker.patch('freqtrade.exchange.Exchange.precisionMode', PropertyMock(return_value=2))
|
||||
|
||||
if mock_markets:
|
||||
mocker.patch('freqtrade.exchange.Exchange.markets',
|
||||
PropertyMock(return_value=get_markets()))
|
||||
|
||||
if mock_supported_modes:
|
||||
mocker.patch(
|
||||
f'freqtrade.exchange.{id.capitalize()}._supported_trading_mode_collateral_pairs',
|
||||
PropertyMock(return_value=[
|
||||
(TradingMode.MARGIN, Collateral.CROSS),
|
||||
(TradingMode.MARGIN, Collateral.ISOLATED),
|
||||
(TradingMode.FUTURES, Collateral.CROSS),
|
||||
(TradingMode.FUTURES, Collateral.ISOLATED)
|
||||
])
|
||||
)
|
||||
|
||||
if api_mock:
|
||||
mocker.patch('freqtrade.exchange.Exchange._init_ccxt', MagicMock(return_value=api_mock))
|
||||
else:
|
||||
@@ -102,8 +120,8 @@ def patch_exchange(mocker, api_mock=None, id='binance', mock_markets=True) -> No
|
||||
|
||||
|
||||
def get_patched_exchange(mocker, config, api_mock=None, id='binance',
|
||||
mock_markets=True) -> Exchange:
|
||||
patch_exchange(mocker, api_mock, id, mock_markets)
|
||||
mock_markets=True, mock_supported_modes=True) -> Exchange:
|
||||
patch_exchange(mocker, api_mock, id, mock_markets, mock_supported_modes)
|
||||
config['exchange']['name'] = id
|
||||
try:
|
||||
exchange = ExchangeResolver.load_exchange(id, config)
|
||||
@@ -465,7 +483,10 @@ def get_markets():
|
||||
'max': 500000,
|
||||
},
|
||||
},
|
||||
'info': {},
|
||||
'info': {
|
||||
'leverage_buy': ['2'],
|
||||
'leverage_sell': ['2'],
|
||||
},
|
||||
},
|
||||
'TKN/BTC': {
|
||||
'id': 'tknbtc',
|
||||
@@ -491,7 +512,10 @@ def get_markets():
|
||||
'max': 500000,
|
||||
},
|
||||
},
|
||||
'info': {},
|
||||
'info': {
|
||||
'leverage_buy': ['2', '3', '4', '5'],
|
||||
'leverage_sell': ['2', '3', '4', '5'],
|
||||
},
|
||||
},
|
||||
'BLK/BTC': {
|
||||
'id': 'blkbtc',
|
||||
@@ -516,7 +540,10 @@ def get_markets():
|
||||
'max': 500000,
|
||||
},
|
||||
},
|
||||
'info': {},
|
||||
'info': {
|
||||
'leverage_buy': ['2', '3'],
|
||||
'leverage_sell': ['2', '3'],
|
||||
},
|
||||
},
|
||||
'LTC/BTC': {
|
||||
'id': 'ltcbtc',
|
||||
@@ -541,7 +568,10 @@ def get_markets():
|
||||
'max': 500000,
|
||||
},
|
||||
},
|
||||
'info': {},
|
||||
'info': {
|
||||
'leverage_buy': [],
|
||||
'leverage_sell': [],
|
||||
},
|
||||
},
|
||||
'XRP/BTC': {
|
||||
'id': 'xrpbtc',
|
||||
@@ -619,7 +649,10 @@ def get_markets():
|
||||
'max': None
|
||||
}
|
||||
},
|
||||
'info': {},
|
||||
'info': {
|
||||
'leverage_buy': [],
|
||||
'leverage_sell': [],
|
||||
},
|
||||
},
|
||||
'ETH/USDT': {
|
||||
'id': 'USDT-ETH',
|
||||
@@ -735,6 +768,8 @@ def get_markets():
|
||||
'max': None
|
||||
}
|
||||
},
|
||||
'info': {
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
|
@@ -1,21 +1,31 @@
|
||||
from datetime import datetime, timezone
|
||||
from random import randint
|
||||
from unittest.mock import MagicMock
|
||||
from unittest.mock import MagicMock, PropertyMock
|
||||
|
||||
import ccxt
|
||||
import pytest
|
||||
|
||||
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
|
||||
|
||||
|
||||
@pytest.mark.parametrize('limitratio,expected', [
|
||||
(None, 220 * 0.99),
|
||||
(0.99, 220 * 0.99),
|
||||
(0.98, 220 * 0.98),
|
||||
@pytest.mark.parametrize('limitratio,expected,side', [
|
||||
(None, 220 * 0.99, "sell"),
|
||||
(0.99, 220 * 0.99, "sell"),
|
||||
(0.98, 220 * 0.98, "sell"),
|
||||
(None, 220 * 1.01, "buy"),
|
||||
(0.99, 220 * 1.01, "buy"),
|
||||
(0.98, 220 * 1.02, "buy"),
|
||||
])
|
||||
def test_stoploss_order_binance(default_conf, mocker, limitratio, expected):
|
||||
def test_stoploss_order_binance(
|
||||
default_conf,
|
||||
mocker,
|
||||
limitratio,
|
||||
expected,
|
||||
side
|
||||
):
|
||||
api_mock = MagicMock()
|
||||
order_id = 'test_prod_buy_{}'.format(randint(0, 10 ** 6))
|
||||
order_type = 'stop_loss_limit'
|
||||
@@ -33,19 +43,32 @@ def test_stoploss_order_binance(default_conf, mocker, limitratio, expected):
|
||||
exchange = get_patched_exchange(mocker, default_conf, api_mock, 'binance')
|
||||
|
||||
with pytest.raises(OperationalException):
|
||||
order = exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=190,
|
||||
order_types={'stoploss_on_exchange_limit_ratio': 1.05})
|
||||
order = exchange.stoploss(
|
||||
pair='ETH/BTC',
|
||||
amount=1,
|
||||
stop_price=190,
|
||||
side=side,
|
||||
order_types={'stoploss_on_exchange_limit_ratio': 1.05},
|
||||
leverage=1.0
|
||||
)
|
||||
|
||||
api_mock.create_order.reset_mock()
|
||||
order_types = {} if limitratio is None else {'stoploss_on_exchange_limit_ratio': limitratio}
|
||||
order = exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, order_types=order_types)
|
||||
order = exchange.stoploss(
|
||||
pair='ETH/BTC',
|
||||
amount=1,
|
||||
stop_price=220,
|
||||
order_types=order_types,
|
||||
side=side,
|
||||
leverage=1.0
|
||||
)
|
||||
|
||||
assert 'id' in order
|
||||
assert 'info' in order
|
||||
assert order['id'] == order_id
|
||||
assert api_mock.create_order.call_args_list[0][1]['symbol'] == 'ETH/BTC'
|
||||
assert api_mock.create_order.call_args_list[0][1]['type'] == order_type
|
||||
assert api_mock.create_order.call_args_list[0][1]['side'] == 'sell'
|
||||
assert api_mock.create_order.call_args_list[0][1]['side'] == side
|
||||
assert api_mock.create_order.call_args_list[0][1]['amount'] == 1
|
||||
# Price should be 1% below stopprice
|
||||
assert api_mock.create_order.call_args_list[0][1]['price'] == expected
|
||||
@@ -55,17 +78,31 @@ def test_stoploss_order_binance(default_conf, mocker, limitratio, expected):
|
||||
with pytest.raises(DependencyException):
|
||||
api_mock.create_order = MagicMock(side_effect=ccxt.InsufficientFunds("0 balance"))
|
||||
exchange = get_patched_exchange(mocker, default_conf, api_mock, 'binance')
|
||||
exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, order_types={})
|
||||
exchange.stoploss(
|
||||
pair='ETH/BTC',
|
||||
amount=1,
|
||||
stop_price=220,
|
||||
order_types={},
|
||||
side=side,
|
||||
leverage=1.0)
|
||||
|
||||
with pytest.raises(InvalidOrderException):
|
||||
api_mock.create_order = MagicMock(
|
||||
side_effect=ccxt.InvalidOrder("binance Order would trigger immediately."))
|
||||
exchange = get_patched_exchange(mocker, default_conf, api_mock, 'binance')
|
||||
exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, order_types={})
|
||||
exchange.stoploss(
|
||||
pair='ETH/BTC',
|
||||
amount=1,
|
||||
stop_price=220,
|
||||
order_types={},
|
||||
side=side,
|
||||
leverage=1.0
|
||||
)
|
||||
|
||||
ccxt_exceptionhandlers(mocker, default_conf, api_mock, "binance",
|
||||
"stoploss", "create_order", retries=1,
|
||||
pair='ETH/BTC', amount=1, stop_price=220, order_types={})
|
||||
pair='ETH/BTC', amount=1, stop_price=220, order_types={},
|
||||
side=side, leverage=1.0)
|
||||
|
||||
|
||||
def test_stoploss_order_dry_run_binance(default_conf, mocker):
|
||||
@@ -78,12 +115,25 @@ def test_stoploss_order_dry_run_binance(default_conf, mocker):
|
||||
exchange = get_patched_exchange(mocker, default_conf, api_mock, 'binance')
|
||||
|
||||
with pytest.raises(OperationalException):
|
||||
order = exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=190,
|
||||
order_types={'stoploss_on_exchange_limit_ratio': 1.05})
|
||||
order = exchange.stoploss(
|
||||
pair='ETH/BTC',
|
||||
amount=1,
|
||||
stop_price=190,
|
||||
side="sell",
|
||||
order_types={'stoploss_on_exchange_limit_ratio': 1.05},
|
||||
leverage=1.0
|
||||
)
|
||||
|
||||
api_mock.create_order.reset_mock()
|
||||
|
||||
order = exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, order_types={})
|
||||
order = exchange.stoploss(
|
||||
pair='ETH/BTC',
|
||||
amount=1,
|
||||
stop_price=220,
|
||||
order_types={},
|
||||
side="sell",
|
||||
leverage=1.0
|
||||
)
|
||||
|
||||
assert 'id' in order
|
||||
assert 'info' in order
|
||||
@@ -94,18 +144,202 @@ def test_stoploss_order_dry_run_binance(default_conf, mocker):
|
||||
assert order['amount'] == 1
|
||||
|
||||
|
||||
def test_stoploss_adjust_binance(mocker, default_conf):
|
||||
@pytest.mark.parametrize('sl1,sl2,sl3,side', [
|
||||
(1501, 1499, 1501, "sell"),
|
||||
(1499, 1501, 1499, "buy")
|
||||
])
|
||||
def test_stoploss_adjust_binance(mocker, default_conf, sl1, sl2, sl3, side):
|
||||
exchange = get_patched_exchange(mocker, default_conf, id='binance')
|
||||
order = {
|
||||
'type': 'stop_loss_limit',
|
||||
'price': 1500,
|
||||
'info': {'stopPrice': 1500},
|
||||
}
|
||||
assert exchange.stoploss_adjust(1501, order)
|
||||
assert not exchange.stoploss_adjust(1499, order)
|
||||
assert exchange.stoploss_adjust(sl1, order, side=side)
|
||||
assert not exchange.stoploss_adjust(sl2, order, side=side)
|
||||
# Test with invalid order case
|
||||
order['type'] = 'stop_loss'
|
||||
assert not exchange.stoploss_adjust(1501, order)
|
||||
assert not exchange.stoploss_adjust(sl3, order, side=side)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('pair,nominal_value,max_lev', [
|
||||
("BNB/BUSD", 0.0, 40.0),
|
||||
("BNB/USDT", 100.0, 153.84615384615384),
|
||||
("BTC/USDT", 170.30, 250.0),
|
||||
("BNB/BUSD", 999999.9, 10.0),
|
||||
("BNB/USDT", 5000000.0, 6.666666666666667),
|
||||
("BTC/USDT", 300000000.1, 2.0),
|
||||
])
|
||||
def test_get_max_leverage_binance(default_conf, mocker, pair, nominal_value, max_lev):
|
||||
exchange = get_patched_exchange(mocker, default_conf, id="binance")
|
||||
exchange._leverage_brackets = {
|
||||
'BNB/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]],
|
||||
'BNB/USDT': [[0.0, 0.0065],
|
||||
[10000.0, 0.01],
|
||||
[50000.0, 0.02],
|
||||
[250000.0, 0.05],
|
||||
[1000000.0, 0.1],
|
||||
[2000000.0, 0.125],
|
||||
[5000000.0, 0.15],
|
||||
[10000000.0, 0.25]],
|
||||
'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]],
|
||||
}
|
||||
assert exchange.get_max_leverage(pair, nominal_value) == max_lev
|
||||
|
||||
|
||||
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]],
|
||||
|
||||
})
|
||||
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.fill_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]],
|
||||
}
|
||||
|
||||
api_mock = MagicMock()
|
||||
api_mock.load_leverage_brackets = MagicMock()
|
||||
type(api_mock).has = PropertyMock(return_value={'loadLeverageBrackets': True})
|
||||
|
||||
ccxt_exceptionhandlers(
|
||||
mocker,
|
||||
default_conf,
|
||||
api_mock,
|
||||
"binance",
|
||||
"fill_leverage_brackets",
|
||||
"load_leverage_brackets"
|
||||
)
|
||||
|
||||
|
||||
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()
|
||||
|
||||
leverage_brackets = {
|
||||
"1000SHIB/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]
|
||||
],
|
||||
"1INCH/USDT": [
|
||||
[0.0, 0.012],
|
||||
[5000.0, 0.025],
|
||||
[25000.0, 0.05],
|
||||
[100000.0, 0.1],
|
||||
[250000.0, 0.125],
|
||||
[1000000.0, 0.5]
|
||||
],
|
||||
"AAVE/USDT": [
|
||||
[0.0, 0.01],
|
||||
[50000.0, 0.02],
|
||||
[250000.0, 0.05],
|
||||
[1000000.0, 0.1],
|
||||
[2000000.0, 0.125],
|
||||
[5000000.0, 0.1665],
|
||||
[10000000.0, 0.25]
|
||||
],
|
||||
"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]
|
||||
]
|
||||
}
|
||||
|
||||
for key, value in leverage_brackets.items():
|
||||
assert exchange._leverage_brackets[key] == value
|
||||
|
||||
|
||||
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)
|
||||
|
||||
ccxt_exceptionhandlers(
|
||||
mocker,
|
||||
default_conf,
|
||||
api_mock,
|
||||
"binance",
|
||||
"_set_leverage",
|
||||
"set_leverage",
|
||||
pair="XRP/USDT",
|
||||
leverage=5.0,
|
||||
trading_mode=TradingMode.FUTURES
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -138,3 +372,15 @@ async def test__async_get_historic_ohlcv_binance(default_conf, mocker, caplog):
|
||||
assert exchange._api_async.fetch_ohlcv.call_count == 2
|
||||
assert res == ohlcv
|
||||
assert log_has_re(r"Candle-data for ETH/BTC available starting with .*", caplog)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("trading_mode,collateral,config", [
|
||||
("", "", {}),
|
||||
("margin", "cross", {"options": {"defaultType": "margin"}}),
|
||||
("futures", "isolated", {"options": {"defaultType": "future"}}),
|
||||
])
|
||||
def test__ccxt_config(default_conf, mocker, trading_mode, collateral, config):
|
||||
default_conf['trading_mode'] = trading_mode
|
||||
default_conf['collateral'] = collateral
|
||||
exchange = get_patched_exchange(mocker, default_conf, id="binance")
|
||||
assert exchange._ccxt_config == config
|
||||
|
@@ -11,6 +11,7 @@ import ccxt
|
||||
import pytest
|
||||
from pandas import DataFrame
|
||||
|
||||
from freqtrade.enums import Collateral, TradingMode
|
||||
from freqtrade.exceptions import (DDosProtection, DependencyException, InvalidOrderException,
|
||||
OperationalException, PricingError, TemporaryError)
|
||||
from freqtrade.exchange import Binance, Bittrex, Exchange, Kraken
|
||||
@@ -131,6 +132,7 @@ def test_init_ccxt_kwargs(default_conf, mocker, caplog):
|
||||
|
||||
assert log_has("Applying additional ccxt config: {'TestKWARG': 11, 'TestKWARG44': 11}", caplog)
|
||||
assert ex._api.headers == {'hello': 'world'}
|
||||
assert ex._ccxt_config == {}
|
||||
Exchange._headers = {}
|
||||
|
||||
|
||||
@@ -395,7 +397,11 @@ def test_get_min_pair_stake_amount(mocker, default_conf) -> None:
|
||||
PropertyMock(return_value=markets)
|
||||
)
|
||||
result = exchange.get_min_pair_stake_amount('ETH/BTC', 1, stoploss)
|
||||
assert isclose(result, 2 * (1+0.05) / (1-abs(stoploss)))
|
||||
expected_result = 2 * (1+0.05) / (1-abs(stoploss))
|
||||
assert isclose(result, expected_result)
|
||||
# With Leverage
|
||||
result = exchange.get_min_pair_stake_amount('ETH/BTC', 1, stoploss, 3.0)
|
||||
assert isclose(result, expected_result/3)
|
||||
|
||||
# min amount is set
|
||||
markets["ETH/BTC"]["limits"] = {
|
||||
@@ -407,7 +413,11 @@ def test_get_min_pair_stake_amount(mocker, default_conf) -> None:
|
||||
PropertyMock(return_value=markets)
|
||||
)
|
||||
result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, stoploss)
|
||||
assert isclose(result, 2 * 2 * (1+0.05) / (1-abs(stoploss)))
|
||||
expected_result = 2 * 2 * (1+0.05) / (1-abs(stoploss))
|
||||
assert isclose(result, expected_result)
|
||||
# With Leverage
|
||||
result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, stoploss, 5.0)
|
||||
assert isclose(result, expected_result/5)
|
||||
|
||||
# min amount and cost are set (cost is minimal)
|
||||
markets["ETH/BTC"]["limits"] = {
|
||||
@@ -419,7 +429,11 @@ def test_get_min_pair_stake_amount(mocker, default_conf) -> None:
|
||||
PropertyMock(return_value=markets)
|
||||
)
|
||||
result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, stoploss)
|
||||
assert isclose(result, max(2, 2 * 2) * (1+0.05) / (1-abs(stoploss)))
|
||||
expected_result = max(2, 2 * 2) * (1+0.05) / (1-abs(stoploss))
|
||||
assert isclose(result, expected_result)
|
||||
# With Leverage
|
||||
result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, stoploss, 10)
|
||||
assert isclose(result, expected_result/10)
|
||||
|
||||
# min amount and cost are set (amount is minial)
|
||||
markets["ETH/BTC"]["limits"] = {
|
||||
@@ -431,14 +445,26 @@ def test_get_min_pair_stake_amount(mocker, default_conf) -> None:
|
||||
PropertyMock(return_value=markets)
|
||||
)
|
||||
result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, stoploss)
|
||||
assert isclose(result, max(8, 2 * 2) * (1+0.05) / (1-abs(stoploss)))
|
||||
expected_result = max(8, 2 * 2) * (1+0.05) / (1-abs(stoploss))
|
||||
assert isclose(result, expected_result)
|
||||
# With Leverage
|
||||
result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, stoploss, 7.0)
|
||||
assert isclose(result, expected_result/7.0)
|
||||
|
||||
result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, -0.4)
|
||||
assert isclose(result, max(8, 2 * 2) * 1.5)
|
||||
expected_result = max(8, 2 * 2) * 1.5
|
||||
assert isclose(result, expected_result)
|
||||
# With Leverage
|
||||
result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, -0.4, 8.0)
|
||||
assert isclose(result, expected_result/8.0)
|
||||
|
||||
# Really big stoploss
|
||||
result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, -1)
|
||||
assert isclose(result, max(8, 2 * 2) * 1.5)
|
||||
expected_result = max(8, 2 * 2) * 1.5
|
||||
assert isclose(result, expected_result)
|
||||
# With Leverage
|
||||
result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, -1, 12.0)
|
||||
assert isclose(result, expected_result/12)
|
||||
|
||||
|
||||
def test_get_min_pair_stake_amount_real_data(mocker, default_conf) -> None:
|
||||
@@ -456,10 +482,10 @@ def test_get_min_pair_stake_amount_real_data(mocker, default_conf) -> None:
|
||||
PropertyMock(return_value=markets)
|
||||
)
|
||||
result = exchange.get_min_pair_stake_amount('ETH/BTC', 0.020405, stoploss)
|
||||
assert round(result, 8) == round(
|
||||
max(0.0001, 0.001 * 0.020405) * (1+0.05) / (1-abs(stoploss)),
|
||||
8
|
||||
)
|
||||
expected_result = max(0.0001, 0.001 * 0.020405) * (1+0.05) / (1-abs(stoploss))
|
||||
assert round(result, 8) == round(expected_result, 8)
|
||||
result = exchange.get_min_pair_stake_amount('ETH/BTC', 0.020405, stoploss, 3.0)
|
||||
assert round(result, 8) == round(expected_result/3, 8)
|
||||
|
||||
|
||||
def test_set_sandbox(default_conf, mocker):
|
||||
@@ -970,7 +996,13 @@ def test_create_dry_run_order(default_conf, mocker, side, exchange_name):
|
||||
exchange = get_patched_exchange(mocker, default_conf, id=exchange_name)
|
||||
|
||||
order = exchange.create_dry_run_order(
|
||||
pair='ETH/BTC', ordertype='limit', side=side, amount=1, rate=200)
|
||||
pair='ETH/BTC',
|
||||
ordertype='limit',
|
||||
side=side,
|
||||
amount=1,
|
||||
rate=200,
|
||||
leverage=1.0
|
||||
)
|
||||
assert 'id' in order
|
||||
assert f'dry_run_{side}_' in order["id"]
|
||||
assert order["side"] == side
|
||||
@@ -993,7 +1025,13 @@ def test_create_dry_run_order_limit_fill(default_conf, mocker, side, startprice,
|
||||
)
|
||||
|
||||
order = exchange.create_dry_run_order(
|
||||
pair='LTC/USDT', ordertype='limit', side=side, amount=1, rate=startprice)
|
||||
pair='LTC/USDT',
|
||||
ordertype='limit',
|
||||
side=side,
|
||||
amount=1,
|
||||
rate=startprice,
|
||||
leverage=1.0
|
||||
)
|
||||
assert order_book_l2_usd.call_count == 1
|
||||
assert 'id' in order
|
||||
assert f'dry_run_{side}_' in order["id"]
|
||||
@@ -1039,7 +1077,13 @@ def test_create_dry_run_order_market_fill(default_conf, mocker, side, rate, amou
|
||||
)
|
||||
|
||||
order = exchange.create_dry_run_order(
|
||||
pair='LTC/USDT', ordertype='market', side=side, amount=amount, rate=rate)
|
||||
pair='LTC/USDT',
|
||||
ordertype='market',
|
||||
side=side,
|
||||
amount=amount,
|
||||
rate=rate,
|
||||
leverage=1.0
|
||||
)
|
||||
assert 'id' in order
|
||||
assert f'dry_run_{side}_' in order["id"]
|
||||
assert order["side"] == side
|
||||
@@ -1049,10 +1093,7 @@ def test_create_dry_run_order_market_fill(default_conf, mocker, side, rate, amou
|
||||
assert round(order["average"], 4) == round(endprice, 4)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("side", [
|
||||
("buy"),
|
||||
("sell")
|
||||
])
|
||||
@pytest.mark.parametrize("side", ["buy", "sell"])
|
||||
@pytest.mark.parametrize("ordertype,rate,marketprice", [
|
||||
("market", None, None),
|
||||
("market", 200, True),
|
||||
@@ -1074,9 +1115,17 @@ def test_create_order(default_conf, mocker, side, ordertype, rate, marketprice,
|
||||
mocker.patch('freqtrade.exchange.Exchange.amount_to_precision', lambda s, x, y: y)
|
||||
mocker.patch('freqtrade.exchange.Exchange.price_to_precision', lambda s, x, y: y)
|
||||
exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name)
|
||||
exchange._set_leverage = MagicMock()
|
||||
exchange.set_margin_mode = MagicMock()
|
||||
|
||||
order = exchange.create_order(
|
||||
pair='ETH/BTC', ordertype=ordertype, side=side, amount=1, rate=200)
|
||||
pair='ETH/BTC',
|
||||
ordertype=ordertype,
|
||||
side=side,
|
||||
amount=1,
|
||||
rate=200,
|
||||
leverage=1.0
|
||||
)
|
||||
|
||||
assert 'id' in order
|
||||
assert 'info' in order
|
||||
@@ -1086,6 +1135,21 @@ def test_create_order(default_conf, mocker, side, ordertype, rate, marketprice,
|
||||
assert api_mock.create_order.call_args[0][2] == side
|
||||
assert api_mock.create_order.call_args[0][3] == 1
|
||||
assert api_mock.create_order.call_args[0][4] is rate
|
||||
assert exchange._set_leverage.call_count == 0
|
||||
assert exchange.set_margin_mode.call_count == 0
|
||||
|
||||
exchange.trading_mode = TradingMode.FUTURES
|
||||
order = exchange.create_order(
|
||||
pair='ETH/BTC',
|
||||
ordertype=ordertype,
|
||||
side=side,
|
||||
amount=1,
|
||||
rate=200,
|
||||
leverage=3.0
|
||||
)
|
||||
|
||||
assert exchange._set_leverage.call_count == 1
|
||||
assert exchange.set_margin_mode.call_count == 1
|
||||
|
||||
|
||||
def test_buy_dry_run(default_conf, mocker):
|
||||
@@ -2624,10 +2688,17 @@ def test_get_fee(default_conf, mocker, exchange_name):
|
||||
def test_stoploss_order_unsupported_exchange(default_conf, mocker):
|
||||
exchange = get_patched_exchange(mocker, default_conf, id='bittrex')
|
||||
with pytest.raises(OperationalException, match=r"stoploss is not implemented .*"):
|
||||
exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, order_types={})
|
||||
exchange.stoploss(
|
||||
pair='ETH/BTC',
|
||||
amount=1,
|
||||
stop_price=220,
|
||||
order_types={},
|
||||
side="sell",
|
||||
leverage=1.0
|
||||
)
|
||||
|
||||
with pytest.raises(OperationalException, match=r"stoploss is not implemented .*"):
|
||||
exchange.stoploss_adjust(1, {})
|
||||
exchange.stoploss_adjust(1, {}, side="sell")
|
||||
|
||||
|
||||
def test_merge_ft_has_dict(default_conf, mocker):
|
||||
@@ -2972,7 +3043,123 @@ def test_calculate_fee_rate(mocker, default_conf, order, expected) -> None:
|
||||
(3, 5, 5),
|
||||
(4, 5, 2),
|
||||
(5, 5, 1),
|
||||
|
||||
])
|
||||
def test_calculate_backoff(retrycount, max_retries, expected):
|
||||
assert calculate_backoff(retrycount, max_retries) == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize('exchange', ['binance', 'kraken', 'ftx'])
|
||||
@pytest.mark.parametrize('stake_amount,leverage,min_stake_with_lev', [
|
||||
(9.0, 3.0, 3.0),
|
||||
(20.0, 5.0, 4.0),
|
||||
(100.0, 100.0, 1.0)
|
||||
])
|
||||
def test_get_stake_amount_considering_leverage(
|
||||
exchange,
|
||||
stake_amount,
|
||||
leverage,
|
||||
min_stake_with_lev,
|
||||
mocker,
|
||||
default_conf
|
||||
):
|
||||
exchange = get_patched_exchange(mocker, default_conf, id=exchange)
|
||||
assert exchange._get_stake_amount_considering_leverage(
|
||||
stake_amount, leverage) == min_stake_with_lev
|
||||
|
||||
|
||||
@pytest.mark.parametrize("exchange_name,trading_mode", [
|
||||
("binance", TradingMode.FUTURES),
|
||||
("ftx", TradingMode.MARGIN),
|
||||
("ftx", TradingMode.FUTURES)
|
||||
])
|
||||
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,
|
||||
default_conf,
|
||||
api_mock,
|
||||
exchange_name,
|
||||
"_set_leverage",
|
||||
"set_leverage",
|
||||
pair="XRP/USDT",
|
||||
leverage=5.0,
|
||||
trading_mode=trading_mode
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("collateral", [
|
||||
(Collateral.CROSS),
|
||||
(Collateral.ISOLATED)
|
||||
])
|
||||
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,
|
||||
default_conf,
|
||||
api_mock,
|
||||
"binance",
|
||||
"set_margin_mode",
|
||||
"set_margin_mode",
|
||||
pair="XRP/USDT",
|
||||
collateral=collateral
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("exchange_name, trading_mode, collateral, exception_thrown", [
|
||||
("binance", TradingMode.SPOT, None, False),
|
||||
("binance", TradingMode.MARGIN, Collateral.ISOLATED, True),
|
||||
("kraken", TradingMode.SPOT, None, False),
|
||||
("kraken", TradingMode.MARGIN, Collateral.ISOLATED, True),
|
||||
("kraken", TradingMode.FUTURES, Collateral.ISOLATED, True),
|
||||
("ftx", TradingMode.SPOT, None, False),
|
||||
("ftx", TradingMode.MARGIN, Collateral.ISOLATED, True),
|
||||
("ftx", TradingMode.FUTURES, Collateral.ISOLATED, True),
|
||||
("bittrex", TradingMode.SPOT, None, False),
|
||||
("bittrex", TradingMode.MARGIN, Collateral.CROSS, True),
|
||||
("bittrex", TradingMode.MARGIN, Collateral.ISOLATED, True),
|
||||
("bittrex", TradingMode.FUTURES, Collateral.CROSS, True),
|
||||
("bittrex", TradingMode.FUTURES, Collateral.ISOLATED, True),
|
||||
|
||||
# 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),
|
||||
("ftx", TradingMode.FUTURES, Collateral.CROSS, True),
|
||||
|
||||
# TODO-lev: Uncomment once implemented
|
||||
# ("binance", TradingMode.MARGIN, Collateral.CROSS, False),
|
||||
# ("binance", TradingMode.FUTURES, Collateral.CROSS, 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),
|
||||
# ("ftx", TradingMode.FUTURES, Collateral.CROSS, False)
|
||||
])
|
||||
def test_validate_trading_mode_and_collateral(
|
||||
default_conf,
|
||||
mocker,
|
||||
exchange_name,
|
||||
trading_mode,
|
||||
collateral,
|
||||
exception_thrown
|
||||
):
|
||||
exchange = get_patched_exchange(
|
||||
mocker, default_conf, id=exchange_name, mock_supported_modes=False)
|
||||
if (exception_thrown):
|
||||
with pytest.raises(OperationalException):
|
||||
exchange.validate_trading_mode_and_collateral(trading_mode, collateral)
|
||||
else:
|
||||
exchange.validate_trading_mode_and_collateral(trading_mode, collateral)
|
||||
|
@@ -14,7 +14,11 @@ from .test_exchange import ccxt_exceptionhandlers
|
||||
STOPLOSS_ORDERTYPE = 'stop'
|
||||
|
||||
|
||||
def test_stoploss_order_ftx(default_conf, mocker):
|
||||
@pytest.mark.parametrize('order_price,exchangelimitratio,side', [
|
||||
(217.8, 1.05, "sell"),
|
||||
(222.2, 0.95, "buy"),
|
||||
])
|
||||
def test_stoploss_order_ftx(default_conf, mocker, order_price, exchangelimitratio, side):
|
||||
api_mock = MagicMock()
|
||||
order_id = 'test_prod_buy_{}'.format(randint(0, 10 ** 6))
|
||||
|
||||
@@ -32,12 +36,18 @@ def test_stoploss_order_ftx(default_conf, mocker):
|
||||
exchange = get_patched_exchange(mocker, default_conf, api_mock, 'ftx')
|
||||
|
||||
# stoploss_on_exchange_limit_ratio is irrelevant for ftx market orders
|
||||
order = exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=190,
|
||||
order_types={'stoploss_on_exchange_limit_ratio': 1.05})
|
||||
order = exchange.stoploss(
|
||||
pair='ETH/BTC',
|
||||
amount=1,
|
||||
stop_price=190,
|
||||
side=side,
|
||||
order_types={'stoploss_on_exchange_limit_ratio': exchangelimitratio},
|
||||
leverage=1.0
|
||||
)
|
||||
|
||||
assert api_mock.create_order.call_args_list[0][1]['symbol'] == 'ETH/BTC'
|
||||
assert api_mock.create_order.call_args_list[0][1]['type'] == STOPLOSS_ORDERTYPE
|
||||
assert api_mock.create_order.call_args_list[0][1]['side'] == 'sell'
|
||||
assert api_mock.create_order.call_args_list[0][1]['side'] == side
|
||||
assert api_mock.create_order.call_args_list[0][1]['amount'] == 1
|
||||
assert 'orderPrice' not in api_mock.create_order.call_args_list[0][1]['params']
|
||||
assert 'stopPrice' in api_mock.create_order.call_args_list[0][1]['params']
|
||||
@@ -47,51 +57,79 @@ def test_stoploss_order_ftx(default_conf, mocker):
|
||||
|
||||
api_mock.create_order.reset_mock()
|
||||
|
||||
order = exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, order_types={})
|
||||
order = exchange.stoploss(
|
||||
pair='ETH/BTC',
|
||||
amount=1,
|
||||
stop_price=220,
|
||||
order_types={},
|
||||
side=side,
|
||||
leverage=1.0
|
||||
)
|
||||
|
||||
assert 'id' in order
|
||||
assert 'info' in order
|
||||
assert order['id'] == order_id
|
||||
assert api_mock.create_order.call_args_list[0][1]['symbol'] == 'ETH/BTC'
|
||||
assert api_mock.create_order.call_args_list[0][1]['type'] == STOPLOSS_ORDERTYPE
|
||||
assert api_mock.create_order.call_args_list[0][1]['side'] == 'sell'
|
||||
assert api_mock.create_order.call_args_list[0][1]['side'] == side
|
||||
assert api_mock.create_order.call_args_list[0][1]['amount'] == 1
|
||||
assert 'orderPrice' not in api_mock.create_order.call_args_list[0][1]['params']
|
||||
assert api_mock.create_order.call_args_list[0][1]['params']['stopPrice'] == 220
|
||||
|
||||
api_mock.create_order.reset_mock()
|
||||
order = exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220,
|
||||
order_types={'stoploss': 'limit'})
|
||||
order = exchange.stoploss(
|
||||
pair='ETH/BTC',
|
||||
amount=1,
|
||||
stop_price=220,
|
||||
order_types={'stoploss': 'limit'}, side=side,
|
||||
leverage=1.0
|
||||
)
|
||||
|
||||
assert 'id' in order
|
||||
assert 'info' in order
|
||||
assert order['id'] == order_id
|
||||
assert api_mock.create_order.call_args_list[0][1]['symbol'] == 'ETH/BTC'
|
||||
assert api_mock.create_order.call_args_list[0][1]['type'] == STOPLOSS_ORDERTYPE
|
||||
assert api_mock.create_order.call_args_list[0][1]['side'] == 'sell'
|
||||
assert api_mock.create_order.call_args_list[0][1]['side'] == side
|
||||
assert api_mock.create_order.call_args_list[0][1]['amount'] == 1
|
||||
assert 'orderPrice' in api_mock.create_order.call_args_list[0][1]['params']
|
||||
assert api_mock.create_order.call_args_list[0][1]['params']['orderPrice'] == 217.8
|
||||
assert api_mock.create_order.call_args_list[0][1]['params']['orderPrice'] == order_price
|
||||
assert api_mock.create_order.call_args_list[0][1]['params']['stopPrice'] == 220
|
||||
|
||||
# test exception handling
|
||||
with pytest.raises(DependencyException):
|
||||
api_mock.create_order = MagicMock(side_effect=ccxt.InsufficientFunds("0 balance"))
|
||||
exchange = get_patched_exchange(mocker, default_conf, api_mock, 'ftx')
|
||||
exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, order_types={})
|
||||
exchange.stoploss(
|
||||
pair='ETH/BTC',
|
||||
amount=1,
|
||||
stop_price=220,
|
||||
order_types={},
|
||||
side=side,
|
||||
leverage=1.0
|
||||
)
|
||||
|
||||
with pytest.raises(InvalidOrderException):
|
||||
api_mock.create_order = MagicMock(
|
||||
side_effect=ccxt.InvalidOrder("ftx Order would trigger immediately."))
|
||||
exchange = get_patched_exchange(mocker, default_conf, api_mock, 'ftx')
|
||||
exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, order_types={})
|
||||
exchange.stoploss(
|
||||
pair='ETH/BTC',
|
||||
amount=1,
|
||||
stop_price=220,
|
||||
order_types={},
|
||||
side=side,
|
||||
leverage=1.0
|
||||
)
|
||||
|
||||
ccxt_exceptionhandlers(mocker, default_conf, api_mock, "ftx",
|
||||
"stoploss", "create_order", retries=1,
|
||||
pair='ETH/BTC', amount=1, stop_price=220, order_types={})
|
||||
pair='ETH/BTC', amount=1, stop_price=220, order_types={},
|
||||
side=side, leverage=1.0)
|
||||
|
||||
|
||||
def test_stoploss_order_dry_run_ftx(default_conf, mocker):
|
||||
@pytest.mark.parametrize('side', [("sell"), ("buy")])
|
||||
def test_stoploss_order_dry_run_ftx(default_conf, mocker, side):
|
||||
api_mock = MagicMock()
|
||||
default_conf['dry_run'] = True
|
||||
mocker.patch('freqtrade.exchange.Exchange.amount_to_precision', lambda s, x, y: y)
|
||||
@@ -101,7 +139,14 @@ def test_stoploss_order_dry_run_ftx(default_conf, mocker):
|
||||
|
||||
api_mock.create_order.reset_mock()
|
||||
|
||||
order = exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, order_types={})
|
||||
order = exchange.stoploss(
|
||||
pair='ETH/BTC',
|
||||
amount=1,
|
||||
stop_price=220,
|
||||
order_types={},
|
||||
side=side,
|
||||
leverage=1.0
|
||||
)
|
||||
|
||||
assert 'id' in order
|
||||
assert 'info' in order
|
||||
@@ -112,20 +157,24 @@ def test_stoploss_order_dry_run_ftx(default_conf, mocker):
|
||||
assert order['amount'] == 1
|
||||
|
||||
|
||||
def test_stoploss_adjust_ftx(mocker, default_conf):
|
||||
@pytest.mark.parametrize('sl1,sl2,sl3,side', [
|
||||
(1501, 1499, 1501, "sell"),
|
||||
(1499, 1501, 1499, "buy")
|
||||
])
|
||||
def test_stoploss_adjust_ftx(mocker, default_conf, sl1, sl2, sl3, side):
|
||||
exchange = get_patched_exchange(mocker, default_conf, id='ftx')
|
||||
order = {
|
||||
'type': STOPLOSS_ORDERTYPE,
|
||||
'price': 1500,
|
||||
}
|
||||
assert exchange.stoploss_adjust(1501, order)
|
||||
assert not exchange.stoploss_adjust(1499, order)
|
||||
assert exchange.stoploss_adjust(sl1, order, side=side)
|
||||
assert not exchange.stoploss_adjust(sl2, order, side=side)
|
||||
# Test with invalid order case ...
|
||||
order['type'] = 'stop_loss_limit'
|
||||
assert not exchange.stoploss_adjust(1501, order)
|
||||
assert not exchange.stoploss_adjust(sl3, order, side=side)
|
||||
|
||||
|
||||
def test_fetch_stoploss_order(default_conf, mocker, limit_sell_order):
|
||||
def test_fetch_stoploss_order(default_conf, mocker, limit_sell_order, limit_buy_order):
|
||||
default_conf['dry_run'] = True
|
||||
order = MagicMock()
|
||||
order.myid = 123
|
||||
@@ -158,6 +207,16 @@ def test_fetch_stoploss_order(default_conf, mocker, limit_sell_order):
|
||||
assert resp['type'] == 'stop'
|
||||
assert resp['status_stop'] == 'triggered'
|
||||
|
||||
api_mock.fetch_order = MagicMock(return_value=limit_buy_order)
|
||||
|
||||
resp = exchange.fetch_stoploss_order('X', 'TKN/BTC')
|
||||
assert resp
|
||||
assert api_mock.fetch_order.call_count == 1
|
||||
assert resp['id_stop'] == 'mocked_limit_buy'
|
||||
assert resp['id'] == 'X'
|
||||
assert resp['type'] == 'stop'
|
||||
assert resp['status_stop'] == 'triggered'
|
||||
|
||||
with pytest.raises(InvalidOrderException):
|
||||
api_mock.fetch_orders = MagicMock(side_effect=ccxt.InvalidOrder("Order not found"))
|
||||
exchange = get_patched_exchange(mocker, default_conf, api_mock, id='ftx')
|
||||
@@ -191,3 +250,20 @@ def test_get_order_id(mocker, default_conf):
|
||||
}
|
||||
}
|
||||
assert exchange.get_order_id_conditional(order) == '1111'
|
||||
|
||||
|
||||
@pytest.mark.parametrize('pair,nominal_value,max_lev', [
|
||||
("ADA/BTC", 0.0, 20.0),
|
||||
("BTC/EUR", 100.0, 20.0),
|
||||
("ZEC/USD", 173.31, 20.0),
|
||||
])
|
||||
def test_get_max_leverage_ftx(default_conf, mocker, pair, nominal_value, max_lev):
|
||||
exchange = get_patched_exchange(mocker, default_conf, id="ftx")
|
||||
assert exchange.get_max_leverage(pair, nominal_value) == max_lev
|
||||
|
||||
|
||||
def test_fill_leverage_brackets_ftx(default_conf, mocker):
|
||||
# FTX only has one account wide leverage, so there's no leverage brackets
|
||||
exchange = get_patched_exchange(mocker, default_conf, id="ftx")
|
||||
exchange.fill_leverage_brackets()
|
||||
assert exchange._leverage_brackets == {}
|
||||
|
@@ -166,7 +166,11 @@ def test_get_balances_prod(default_conf, mocker):
|
||||
|
||||
|
||||
@pytest.mark.parametrize('ordertype', ['market', 'limit'])
|
||||
def test_stoploss_order_kraken(default_conf, mocker, ordertype):
|
||||
@pytest.mark.parametrize('side,adjustedprice', [
|
||||
("sell", 217.8),
|
||||
("buy", 222.2),
|
||||
])
|
||||
def test_stoploss_order_kraken(default_conf, mocker, ordertype, side, adjustedprice):
|
||||
api_mock = MagicMock()
|
||||
order_id = 'test_prod_buy_{}'.format(randint(0, 10 ** 6))
|
||||
|
||||
@@ -183,10 +187,17 @@ def test_stoploss_order_kraken(default_conf, mocker, ordertype):
|
||||
|
||||
exchange = get_patched_exchange(mocker, default_conf, api_mock, 'kraken')
|
||||
|
||||
order = exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220,
|
||||
order_types={'stoploss': ordertype,
|
||||
'stoploss_on_exchange_limit_ratio': 0.99
|
||||
})
|
||||
order = exchange.stoploss(
|
||||
pair='ETH/BTC',
|
||||
amount=1,
|
||||
stop_price=220,
|
||||
side=side,
|
||||
order_types={
|
||||
'stoploss': ordertype,
|
||||
'stoploss_on_exchange_limit_ratio': 0.99
|
||||
},
|
||||
leverage=1.0
|
||||
)
|
||||
|
||||
assert 'id' in order
|
||||
assert 'info' in order
|
||||
@@ -195,12 +206,14 @@ def test_stoploss_order_kraken(default_conf, mocker, ordertype):
|
||||
if ordertype == 'limit':
|
||||
assert api_mock.create_order.call_args_list[0][1]['type'] == STOPLOSS_LIMIT_ORDERTYPE
|
||||
assert api_mock.create_order.call_args_list[0][1]['params'] == {
|
||||
'trading_agreement': 'agree', 'price2': 217.8}
|
||||
'trading_agreement': 'agree',
|
||||
'price2': adjustedprice
|
||||
}
|
||||
else:
|
||||
assert api_mock.create_order.call_args_list[0][1]['type'] == STOPLOSS_ORDERTYPE
|
||||
assert api_mock.create_order.call_args_list[0][1]['params'] == {
|
||||
'trading_agreement': 'agree'}
|
||||
assert api_mock.create_order.call_args_list[0][1]['side'] == 'sell'
|
||||
assert api_mock.create_order.call_args_list[0][1]['side'] == side
|
||||
assert api_mock.create_order.call_args_list[0][1]['amount'] == 1
|
||||
assert api_mock.create_order.call_args_list[0][1]['price'] == 220
|
||||
|
||||
@@ -208,20 +221,36 @@ def test_stoploss_order_kraken(default_conf, mocker, ordertype):
|
||||
with pytest.raises(DependencyException):
|
||||
api_mock.create_order = MagicMock(side_effect=ccxt.InsufficientFunds("0 balance"))
|
||||
exchange = get_patched_exchange(mocker, default_conf, api_mock, 'kraken')
|
||||
exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, order_types={})
|
||||
exchange.stoploss(
|
||||
pair='ETH/BTC',
|
||||
amount=1,
|
||||
stop_price=220,
|
||||
order_types={},
|
||||
side=side,
|
||||
leverage=1.0
|
||||
)
|
||||
|
||||
with pytest.raises(InvalidOrderException):
|
||||
api_mock.create_order = MagicMock(
|
||||
side_effect=ccxt.InvalidOrder("kraken Order would trigger immediately."))
|
||||
exchange = get_patched_exchange(mocker, default_conf, api_mock, 'kraken')
|
||||
exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, order_types={})
|
||||
exchange.stoploss(
|
||||
pair='ETH/BTC',
|
||||
amount=1,
|
||||
stop_price=220,
|
||||
order_types={},
|
||||
side=side,
|
||||
leverage=1.0
|
||||
)
|
||||
|
||||
ccxt_exceptionhandlers(mocker, default_conf, api_mock, "kraken",
|
||||
"stoploss", "create_order", retries=1,
|
||||
pair='ETH/BTC', amount=1, stop_price=220, order_types={})
|
||||
pair='ETH/BTC', amount=1, stop_price=220, order_types={},
|
||||
side=side, leverage=1.0)
|
||||
|
||||
|
||||
def test_stoploss_order_dry_run_kraken(default_conf, mocker):
|
||||
@pytest.mark.parametrize('side', ['buy', 'sell'])
|
||||
def test_stoploss_order_dry_run_kraken(default_conf, mocker, side):
|
||||
api_mock = MagicMock()
|
||||
default_conf['dry_run'] = True
|
||||
mocker.patch('freqtrade.exchange.Exchange.amount_to_precision', lambda s, x, y: y)
|
||||
@@ -231,7 +260,14 @@ def test_stoploss_order_dry_run_kraken(default_conf, mocker):
|
||||
|
||||
api_mock.create_order.reset_mock()
|
||||
|
||||
order = exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, order_types={})
|
||||
order = exchange.stoploss(
|
||||
pair='ETH/BTC',
|
||||
amount=1,
|
||||
stop_price=220,
|
||||
order_types={},
|
||||
side=side,
|
||||
leverage=1.0
|
||||
)
|
||||
|
||||
assert 'id' in order
|
||||
assert 'info' in order
|
||||
@@ -242,14 +278,54 @@ def test_stoploss_order_dry_run_kraken(default_conf, mocker):
|
||||
assert order['amount'] == 1
|
||||
|
||||
|
||||
def test_stoploss_adjust_kraken(mocker, default_conf):
|
||||
@pytest.mark.parametrize('sl1,sl2,sl3,side', [
|
||||
(1501, 1499, 1501, "sell"),
|
||||
(1499, 1501, 1499, "buy")
|
||||
])
|
||||
def test_stoploss_adjust_kraken(mocker, default_conf, sl1, sl2, sl3, side):
|
||||
exchange = get_patched_exchange(mocker, default_conf, id='kraken')
|
||||
order = {
|
||||
'type': STOPLOSS_ORDERTYPE,
|
||||
'price': 1500,
|
||||
}
|
||||
assert exchange.stoploss_adjust(1501, order)
|
||||
assert not exchange.stoploss_adjust(1499, order)
|
||||
assert exchange.stoploss_adjust(sl1, order, side=side)
|
||||
assert not exchange.stoploss_adjust(sl2, order, side=side)
|
||||
# Test with invalid order case ...
|
||||
order['type'] = 'stop_loss_limit'
|
||||
assert not exchange.stoploss_adjust(1501, order)
|
||||
assert not exchange.stoploss_adjust(sl3, order, side=side)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('pair,nominal_value,max_lev', [
|
||||
("ADA/BTC", 0.0, 3.0),
|
||||
("BTC/EUR", 100.0, 5.0),
|
||||
("ZEC/USD", 173.31, 2.0),
|
||||
])
|
||||
def test_get_max_leverage_kraken(default_conf, mocker, pair, nominal_value, max_lev):
|
||||
exchange = get_patched_exchange(mocker, default_conf, id="kraken")
|
||||
exchange._leverage_brackets = {
|
||||
'ADA/BTC': ['2', '3'],
|
||||
'BTC/EUR': ['2', '3', '4', '5'],
|
||||
'ZEC/USD': ['2']
|
||||
}
|
||||
assert exchange.get_max_leverage(pair, nominal_value) == max_lev
|
||||
|
||||
|
||||
def test_fill_leverage_brackets_kraken(default_conf, mocker):
|
||||
api_mock = MagicMock()
|
||||
exchange = get_patched_exchange(mocker, default_conf, api_mock, id="kraken")
|
||||
exchange.fill_leverage_brackets()
|
||||
|
||||
assert exchange._leverage_brackets == {
|
||||
'BLK/BTC': [1, 2, 3],
|
||||
'TKN/BTC': [1, 2, 3, 4, 5],
|
||||
'ETH/BTC': [1, 2],
|
||||
'LTC/BTC': [1],
|
||||
'XRP/BTC': [1],
|
||||
'NEO/BTC': [1],
|
||||
'BTT/BTC': [1],
|
||||
'ETH/USDT': [1],
|
||||
'LTC/USDT': [1],
|
||||
'LTC/USD': [1],
|
||||
'XLTCUSDT': [1],
|
||||
'LTC/ETH': [1]
|
||||
}
|
||||
|
@@ -22,9 +22,10 @@ twentyfive_hours = Decimal(25.0)
|
||||
('kraken', 0.00025, five_hours, 0.045),
|
||||
('kraken', 0.00025, twentyfive_hours, 0.12),
|
||||
# FTX
|
||||
# TODO-lev: - implement FTX tests
|
||||
# ('ftx', Decimal(0.0005), ten_mins, 0.06),
|
||||
# ('ftx', Decimal(0.0005), five_hours, 0.045),
|
||||
('ftx', 0.0005, ten_mins, 0.00125),
|
||||
('ftx', 0.00025, ten_mins, 0.000625),
|
||||
('ftx', 0.00025, five_hours, 0.003125),
|
||||
('ftx', 0.00025, twentyfive_hours, 0.015625),
|
||||
])
|
||||
def test_interest(exchange, interest_rate, hours, expected):
|
||||
borrowed = Decimal(60.0)
|
@@ -12,7 +12,8 @@ from freqtrade.persistence import Trade
|
||||
from freqtrade.plugins.pairlist.pairlist_helpers import expand_pairlist
|
||||
from freqtrade.plugins.pairlistmanager import PairListManager
|
||||
from freqtrade.resolvers import PairListResolver
|
||||
from tests.conftest import get_patched_exchange, get_patched_freqtradebot, log_has, log_has_re
|
||||
from tests.conftest import (create_mock_trades, get_patched_exchange, get_patched_freqtradebot,
|
||||
log_has, log_has_re)
|
||||
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
@@ -663,6 +664,31 @@ def test_PerformanceFilter_error(mocker, whitelist_conf, caplog) -> None:
|
||||
assert log_has("PerformanceFilter is not available in this mode.", caplog)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("init_persistence")
|
||||
def test_PerformanceFilter_lookback(mocker, whitelist_conf, fee) -> None:
|
||||
whitelist_conf['exchange']['pair_whitelist'].append('XRP/BTC')
|
||||
whitelist_conf['pairlists'] = [
|
||||
{"method": "StaticPairList"},
|
||||
{"method": "PerformanceFilter", "minutes": 60}
|
||||
]
|
||||
mocker.patch('freqtrade.exchange.Exchange.exchange_has', MagicMock(return_value=True))
|
||||
exchange = get_patched_exchange(mocker, whitelist_conf)
|
||||
pm = PairListManager(exchange, whitelist_conf)
|
||||
pm.refresh_pairlist()
|
||||
|
||||
assert pm.whitelist == ['ETH/BTC', 'TKN/BTC', 'XRP/BTC']
|
||||
|
||||
with time_machine.travel("2021-09-01 05:00:00 +00:00") as t:
|
||||
create_mock_trades(fee)
|
||||
pm.refresh_pairlist()
|
||||
assert pm.whitelist == ['XRP/BTC', 'ETH/BTC', 'TKN/BTC']
|
||||
|
||||
# Move to "outside" of lookback window, so original sorting is restored.
|
||||
t.move_to("2021-09-01 07:00:00 +00:00")
|
||||
pm.refresh_pairlist()
|
||||
assert pm.whitelist == ['ETH/BTC', 'TKN/BTC', 'XRP/BTC']
|
||||
|
||||
|
||||
def test_gen_pair_whitelist_not_supported(mocker, default_conf, tickers) -> None:
|
||||
default_conf['pairlists'] = [{'method': 'VolumePairList', 'number_assets': 10}]
|
||||
|
||||
|
@@ -422,20 +422,22 @@ def test_api_stopbuy(botclient):
|
||||
assert ftbot.config['max_open_trades'] == 0
|
||||
|
||||
|
||||
def test_api_balance(botclient, mocker, rpc_balance):
|
||||
def test_api_balance(botclient, mocker, rpc_balance, tickers):
|
||||
ftbot, client = botclient
|
||||
|
||||
ftbot.config['dry_run'] = False
|
||||
mocker.patch('freqtrade.exchange.Exchange.get_balances', return_value=rpc_balance)
|
||||
mocker.patch('freqtrade.exchange.Exchange.get_tickers', tickers)
|
||||
mocker.patch('freqtrade.exchange.Exchange.get_valid_pair_combination',
|
||||
side_effect=lambda a, b: f"{a}/{b}")
|
||||
ftbot.wallets.update()
|
||||
|
||||
rc = client_get(client, f"{BASE_URI}/balance")
|
||||
assert_response(rc)
|
||||
assert "currencies" in rc.json()
|
||||
assert len(rc.json()["currencies"]) == 5
|
||||
assert rc.json()['currencies'][0] == {
|
||||
response = rc.json()
|
||||
assert "currencies" in response
|
||||
assert len(response["currencies"]) == 5
|
||||
assert response['currencies'][0] == {
|
||||
'currency': 'BTC',
|
||||
'free': 12.0,
|
||||
'balance': 12.0,
|
||||
@@ -443,6 +445,10 @@ def test_api_balance(botclient, mocker, rpc_balance):
|
||||
'est_stake': 12.0,
|
||||
'stake': 'BTC',
|
||||
}
|
||||
assert 'starting_capital' in response
|
||||
assert 'starting_capital_fiat' in response
|
||||
assert 'starting_capital_pct' in response
|
||||
assert 'starting_capital_ratio' in response
|
||||
|
||||
|
||||
def test_api_count(botclient, mocker, ticker, fee, markets):
|
||||
@@ -1218,6 +1224,7 @@ def test_api_strategies(botclient):
|
||||
assert_response(rc)
|
||||
assert rc.json() == {'strategies': [
|
||||
'HyperoptableStrategy',
|
||||
'InformativeDecoratorTest',
|
||||
'StrategyTestV2',
|
||||
'TestStrategyLegacyV1'
|
||||
]}
|
||||
|
@@ -576,6 +576,8 @@ def test_balance_handle_too_large_response(default_conf, update, mocker) -> None
|
||||
'total': 100.0,
|
||||
'symbol': 100.0,
|
||||
'value': 1000.0,
|
||||
'starting_capital': 1000,
|
||||
'starting_capital_fiat': 1000,
|
||||
})
|
||||
|
||||
telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf)
|
||||
|
75
tests/strategy/strats/informative_decorator_strategy.py
Normal file
75
tests/strategy/strats/informative_decorator_strategy.py
Normal file
@@ -0,0 +1,75 @@
|
||||
# pragma pylint: disable=missing-docstring, invalid-name, pointless-string-statement
|
||||
|
||||
from pandas import DataFrame
|
||||
|
||||
from freqtrade.strategy import informative, merge_informative_pair
|
||||
from freqtrade.strategy.interface import IStrategy
|
||||
|
||||
|
||||
class InformativeDecoratorTest(IStrategy):
|
||||
"""
|
||||
Strategy used by tests freqtrade bot.
|
||||
Please do not modify this strategy, it's intended for internal use only.
|
||||
Please look at the SampleStrategy in the user_data/strategy directory
|
||||
or strategy repository https://github.com/freqtrade/freqtrade-strategies
|
||||
for samples and inspiration.
|
||||
"""
|
||||
INTERFACE_VERSION = 2
|
||||
stoploss = -0.10
|
||||
timeframe = '5m'
|
||||
startup_candle_count: int = 20
|
||||
|
||||
def informative_pairs(self):
|
||||
return [('BTC/USDT', '5m')]
|
||||
|
||||
def populate_buy_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
|
||||
dataframe['buy'] = 0
|
||||
return dataframe
|
||||
|
||||
def populate_sell_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
|
||||
dataframe['sell'] = 0
|
||||
return dataframe
|
||||
|
||||
# Decorator stacking test.
|
||||
@informative('30m')
|
||||
@informative('1h')
|
||||
def populate_indicators_1h(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
|
||||
dataframe['rsi'] = 14
|
||||
return dataframe
|
||||
|
||||
# Simple informative test.
|
||||
@informative('1h', 'BTC/{stake}')
|
||||
def populate_indicators_btc_1h(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
|
||||
dataframe['rsi'] = 14
|
||||
return dataframe
|
||||
|
||||
# Quote currency different from stake currency test.
|
||||
@informative('1h', 'ETH/BTC')
|
||||
def populate_indicators_eth_btc_1h(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
|
||||
dataframe['rsi'] = 14
|
||||
return dataframe
|
||||
|
||||
# Formatting test.
|
||||
@informative('30m', 'BTC/{stake}', '{column}_{BASE}_{QUOTE}_{base}_{quote}_{asset}_{timeframe}')
|
||||
def populate_indicators_btc_1h_2(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
|
||||
dataframe['rsi'] = 14
|
||||
return dataframe
|
||||
|
||||
# Custom formatter test
|
||||
@informative('30m', 'ETH/{stake}', fmt=lambda column, **kwargs: column + '_from_callable')
|
||||
def populate_indicators_eth_30m(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
|
||||
dataframe['rsi'] = 14
|
||||
return dataframe
|
||||
|
||||
def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
|
||||
# Strategy timeframe indicators for current pair.
|
||||
dataframe['rsi'] = 14
|
||||
# Informative pairs are available in this method.
|
||||
dataframe['rsi_less'] = dataframe['rsi'] < dataframe['rsi_1h']
|
||||
|
||||
# Mixing manual informative pairs with decorators.
|
||||
informative = self.dp.get_pair_dataframe('BTC/USDT', '5m')
|
||||
informative['rsi'] = 14
|
||||
dataframe = merge_informative_pair(dataframe, informative, self.timeframe, '5m', ffill=True)
|
||||
|
||||
return dataframe
|
@@ -648,7 +648,7 @@ def test_is_informative_pairs_callback(default_conf):
|
||||
strategy = StrategyResolver.load_strategy(default_conf)
|
||||
# Should return empty
|
||||
# Uses fallback to base implementation
|
||||
assert [] == strategy.informative_pairs()
|
||||
assert [] == strategy.gather_informative_pairs()
|
||||
|
||||
|
||||
@pytest.mark.parametrize('error', [
|
||||
|
@@ -4,7 +4,9 @@ import numpy as np
|
||||
import pandas as pd
|
||||
import pytest
|
||||
|
||||
from freqtrade.strategy import merge_informative_pair, stoploss_from_open, timeframe_to_minutes
|
||||
from freqtrade.data.dataprovider import DataProvider
|
||||
from freqtrade.strategy import (merge_informative_pair, stoploss_from_absolute, stoploss_from_open,
|
||||
timeframe_to_minutes)
|
||||
|
||||
|
||||
def generate_test_data(timeframe: str, size: int):
|
||||
@@ -132,3 +134,65 @@ def test_stoploss_from_open():
|
||||
assert stoploss == 0
|
||||
else:
|
||||
assert isclose(stop_price, expected_stop_price, rel_tol=0.00001)
|
||||
|
||||
|
||||
def test_stoploss_from_absolute():
|
||||
assert stoploss_from_absolute(90, 100) == 1 - (90 / 100)
|
||||
assert stoploss_from_absolute(100, 100) == 0
|
||||
assert stoploss_from_absolute(110, 100) == 0
|
||||
assert stoploss_from_absolute(100, 0) == 1
|
||||
assert stoploss_from_absolute(0, 100) == 1
|
||||
|
||||
|
||||
def test_informative_decorator(mocker, default_conf):
|
||||
test_data_5m = generate_test_data('5m', 40)
|
||||
test_data_30m = generate_test_data('30m', 40)
|
||||
test_data_1h = generate_test_data('1h', 40)
|
||||
data = {
|
||||
('XRP/USDT', '5m'): test_data_5m,
|
||||
('XRP/USDT', '30m'): test_data_30m,
|
||||
('XRP/USDT', '1h'): test_data_1h,
|
||||
('LTC/USDT', '5m'): test_data_5m,
|
||||
('LTC/USDT', '30m'): test_data_30m,
|
||||
('LTC/USDT', '1h'): test_data_1h,
|
||||
('BTC/USDT', '30m'): test_data_30m,
|
||||
('BTC/USDT', '5m'): test_data_5m,
|
||||
('BTC/USDT', '1h'): test_data_1h,
|
||||
('ETH/USDT', '1h'): test_data_1h,
|
||||
('ETH/USDT', '30m'): test_data_30m,
|
||||
('ETH/BTC', '1h'): test_data_1h,
|
||||
}
|
||||
from .strats.informative_decorator_strategy import InformativeDecoratorTest
|
||||
default_conf['stake_currency'] = 'USDT'
|
||||
strategy = InformativeDecoratorTest(config=default_conf)
|
||||
strategy.dp = DataProvider({}, None, None)
|
||||
mocker.patch.object(strategy.dp, 'current_whitelist', return_value=[
|
||||
'XRP/USDT', 'LTC/USDT', 'BTC/USDT'
|
||||
])
|
||||
|
||||
assert len(strategy._ft_informative) == 6 # Equal to number of decorators used
|
||||
informative_pairs = [('XRP/USDT', '1h'), ('LTC/USDT', '1h'), ('XRP/USDT', '30m'),
|
||||
('LTC/USDT', '30m'), ('BTC/USDT', '1h'), ('BTC/USDT', '30m'),
|
||||
('BTC/USDT', '5m'), ('ETH/BTC', '1h'), ('ETH/USDT', '30m')]
|
||||
for inf_pair in informative_pairs:
|
||||
assert inf_pair in strategy.gather_informative_pairs()
|
||||
|
||||
def test_historic_ohlcv(pair, timeframe):
|
||||
return data[(pair, timeframe or strategy.timeframe)].copy()
|
||||
mocker.patch('freqtrade.data.dataprovider.DataProvider.historic_ohlcv',
|
||||
side_effect=test_historic_ohlcv)
|
||||
|
||||
analyzed = strategy.advise_all_indicators(
|
||||
{p: data[(p, strategy.timeframe)] for p in ('XRP/USDT', 'LTC/USDT')})
|
||||
expected_columns = [
|
||||
'rsi_1h', 'rsi_30m', # Stacked informative decorators
|
||||
'btc_usdt_rsi_1h', # BTC 1h informative
|
||||
'rsi_BTC_USDT_btc_usdt_BTC/USDT_30m', # Column formatting
|
||||
'rsi_from_callable', # Custom column formatter
|
||||
'eth_btc_rsi_1h', # Quote currency not matching stake currency
|
||||
'rsi', 'rsi_less', # Non-informative columns
|
||||
'rsi_5m', # Manual informative dataframe
|
||||
]
|
||||
for _, dataframe in analyzed.items():
|
||||
for col in expected_columns:
|
||||
assert col in dataframe.columns
|
||||
|
@@ -35,7 +35,7 @@ def test_search_all_strategies_no_failed():
|
||||
directory = Path(__file__).parent / "strats"
|
||||
strategies = StrategyResolver.search_all_objects(directory, enum_failed=False)
|
||||
assert isinstance(strategies, list)
|
||||
assert len(strategies) == 3
|
||||
assert len(strategies) == 4
|
||||
assert isinstance(strategies[0], dict)
|
||||
|
||||
|
||||
@@ -43,10 +43,10 @@ def test_search_all_strategies_with_failed():
|
||||
directory = Path(__file__).parent / "strats"
|
||||
strategies = StrategyResolver.search_all_objects(directory, enum_failed=True)
|
||||
assert isinstance(strategies, list)
|
||||
assert len(strategies) == 4
|
||||
assert len(strategies) == 5
|
||||
# with enum_failed=True search_all_objects() shall find 2 good strategies
|
||||
# and 1 which fails to load
|
||||
assert len([x for x in strategies if x['class'] is not None]) == 3
|
||||
assert len([x for x in strategies if x['class'] is not None]) == 4
|
||||
assert len([x for x in strategies if x['class'] is None]) == 1
|
||||
|
||||
|
||||
|
@@ -78,11 +78,15 @@ def test_bot_cleanup(mocker, default_conf, caplog) -> None:
|
||||
assert coo_mock.call_count == 1
|
||||
|
||||
|
||||
def test_order_dict_dry_run(default_conf, mocker, caplog) -> None:
|
||||
@pytest.mark.parametrize('runmode', [
|
||||
RunMode.DRY_RUN,
|
||||
RunMode.LIVE
|
||||
])
|
||||
def test_order_dict(default_conf, mocker, runmode, caplog) -> None:
|
||||
patch_RPCManager(mocker)
|
||||
patch_exchange(mocker)
|
||||
conf = default_conf.copy()
|
||||
conf['runmode'] = RunMode.DRY_RUN
|
||||
conf['runmode'] = runmode
|
||||
conf['order_types'] = {
|
||||
'buy': 'market',
|
||||
'sell': 'limit',
|
||||
@@ -92,45 +96,14 @@ def test_order_dict_dry_run(default_conf, mocker, caplog) -> None:
|
||||
conf['bid_strategy']['price_side'] = 'ask'
|
||||
|
||||
freqtrade = FreqtradeBot(conf)
|
||||
if runmode == RunMode.LIVE:
|
||||
assert not log_has_re(".*stoploss_on_exchange .* dry-run", caplog)
|
||||
assert freqtrade.strategy.order_types['stoploss_on_exchange']
|
||||
|
||||
caplog.clear()
|
||||
# is left untouched
|
||||
conf = default_conf.copy()
|
||||
conf['runmode'] = RunMode.DRY_RUN
|
||||
conf['order_types'] = {
|
||||
'buy': 'market',
|
||||
'sell': 'limit',
|
||||
'stoploss': 'limit',
|
||||
'stoploss_on_exchange': False,
|
||||
}
|
||||
freqtrade = FreqtradeBot(conf)
|
||||
assert not freqtrade.strategy.order_types['stoploss_on_exchange']
|
||||
assert not log_has_re(".*stoploss_on_exchange .* dry-run", caplog)
|
||||
|
||||
|
||||
def test_order_dict_live(default_conf, mocker, caplog) -> None:
|
||||
patch_RPCManager(mocker)
|
||||
patch_exchange(mocker)
|
||||
|
||||
conf = default_conf.copy()
|
||||
conf['runmode'] = RunMode.LIVE
|
||||
conf['order_types'] = {
|
||||
'buy': 'market',
|
||||
'sell': 'limit',
|
||||
'stoploss': 'limit',
|
||||
'stoploss_on_exchange': True,
|
||||
}
|
||||
conf['bid_strategy']['price_side'] = 'ask'
|
||||
|
||||
freqtrade = FreqtradeBot(conf)
|
||||
assert not log_has_re(".*stoploss_on_exchange .* dry-run", caplog)
|
||||
assert freqtrade.strategy.order_types['stoploss_on_exchange']
|
||||
|
||||
caplog.clear()
|
||||
# is left untouched
|
||||
conf = default_conf.copy()
|
||||
conf['runmode'] = RunMode.LIVE
|
||||
conf['runmode'] = runmode
|
||||
conf['order_types'] = {
|
||||
'buy': 'market',
|
||||
'sell': 'limit',
|
||||
@@ -219,8 +192,14 @@ def test_edge_overrides_stake_amount(mocker, edge_conf) -> None:
|
||||
'LTC/BTC', freqtrade.edge) == (999.9 * 0.5 * 0.01) / 0.21
|
||||
|
||||
|
||||
def test_edge_overrides_stoploss(limit_buy_order, fee, caplog, mocker, edge_conf) -> None:
|
||||
|
||||
@pytest.mark.parametrize('buy_price_mult,ignore_strat_sl', [
|
||||
# Override stoploss
|
||||
(0.79, False),
|
||||
# Override strategy stoploss
|
||||
(0.85, True)
|
||||
])
|
||||
def test_edge_overrides_stoploss(limit_buy_order, fee, caplog, mocker,
|
||||
buy_price_mult, ignore_strat_sl, edge_conf) -> None:
|
||||
patch_RPCManager(mocker)
|
||||
patch_exchange(mocker)
|
||||
patch_edge(mocker)
|
||||
@@ -234,9 +213,9 @@ def test_edge_overrides_stoploss(limit_buy_order, fee, caplog, mocker, edge_conf
|
||||
mocker.patch.multiple(
|
||||
'freqtrade.exchange.Exchange',
|
||||
fetch_ticker=MagicMock(return_value={
|
||||
'bid': buy_price * 0.79,
|
||||
'ask': buy_price * 0.79,
|
||||
'last': buy_price * 0.79
|
||||
'bid': buy_price * buy_price_mult,
|
||||
'ask': buy_price * buy_price_mult,
|
||||
'last': buy_price * buy_price_mult,
|
||||
}),
|
||||
get_fee=fee,
|
||||
)
|
||||
@@ -253,46 +232,10 @@ def test_edge_overrides_stoploss(limit_buy_order, fee, caplog, mocker, edge_conf
|
||||
#############################################
|
||||
|
||||
# stoploss shoud be hit
|
||||
assert freqtrade.handle_trade(trade) is True
|
||||
assert log_has('Exit for NEO/BTC detected. Reason: stop_loss', caplog)
|
||||
assert trade.sell_reason == SellType.STOP_LOSS.value
|
||||
|
||||
|
||||
def test_edge_should_ignore_strategy_stoploss(limit_buy_order, fee,
|
||||
mocker, edge_conf) -> None:
|
||||
patch_RPCManager(mocker)
|
||||
patch_exchange(mocker)
|
||||
patch_edge(mocker)
|
||||
edge_conf['max_open_trades'] = float('inf')
|
||||
|
||||
# Strategy stoploss is -0.1 but Edge imposes a stoploss at -0.2
|
||||
# Thus, if price falls 15%, stoploss should not be triggered
|
||||
#
|
||||
# mocking the ticker: price is falling ...
|
||||
buy_price = limit_buy_order['price']
|
||||
mocker.patch.multiple(
|
||||
'freqtrade.exchange.Exchange',
|
||||
fetch_ticker=MagicMock(return_value={
|
||||
'bid': buy_price * 0.85,
|
||||
'ask': buy_price * 0.85,
|
||||
'last': buy_price * 0.85
|
||||
}),
|
||||
get_fee=fee,
|
||||
)
|
||||
#############################################
|
||||
|
||||
# Create a trade with "limit_buy_order" price
|
||||
freqtrade = FreqtradeBot(edge_conf)
|
||||
freqtrade.active_pair_whitelist = ['NEO/BTC']
|
||||
patch_get_signal(freqtrade)
|
||||
freqtrade.strategy.min_roi_reached = MagicMock(return_value=False)
|
||||
freqtrade.enter_positions()
|
||||
trade = Trade.query.first()
|
||||
trade.update(limit_buy_order)
|
||||
#############################################
|
||||
|
||||
# stoploss shoud not be hit
|
||||
assert freqtrade.handle_trade(trade) is False
|
||||
assert freqtrade.handle_trade(trade) is not ignore_strat_sl
|
||||
if not ignore_strat_sl:
|
||||
assert log_has('Exit for NEO/BTC detected. Reason: stop_loss', caplog)
|
||||
assert trade.sell_reason == SellType.STOP_LOSS.value
|
||||
|
||||
|
||||
def test_total_open_trades_stakes(mocker, default_conf, ticker, fee) -> None:
|
||||
@@ -376,8 +319,16 @@ def test_create_trade_no_stake_amount(default_conf, ticker, limit_buy_order,
|
||||
freqtrade.create_trade('ETH/BTC')
|
||||
|
||||
|
||||
def test_create_trade_minimal_amount(default_conf, ticker, limit_buy_order_open,
|
||||
fee, mocker) -> None:
|
||||
@pytest.mark.parametrize('stake_amount,create,amount_enough,max_open_trades', [
|
||||
(0.0005, True, True, 99),
|
||||
(0.000000005, True, False, 99),
|
||||
(0, False, True, 99),
|
||||
(UNLIMITED_STAKE_AMOUNT, False, True, 0),
|
||||
])
|
||||
def test_create_trade_minimal_amount(
|
||||
default_conf, ticker, limit_buy_order_open, fee, mocker,
|
||||
stake_amount, create, amount_enough, max_open_trades, caplog
|
||||
) -> None:
|
||||
patch_RPCManager(mocker)
|
||||
patch_exchange(mocker)
|
||||
buy_mock = MagicMock(return_value=limit_buy_order_open)
|
||||
@@ -387,78 +338,33 @@ def test_create_trade_minimal_amount(default_conf, ticker, limit_buy_order_open,
|
||||
create_order=buy_mock,
|
||||
get_fee=fee,
|
||||
)
|
||||
default_conf['stake_amount'] = 0.0005
|
||||
default_conf['max_open_trades'] = max_open_trades
|
||||
freqtrade = FreqtradeBot(default_conf)
|
||||
freqtrade.config['stake_amount'] = stake_amount
|
||||
patch_get_signal(freqtrade)
|
||||
|
||||
freqtrade.create_trade('ETH/BTC')
|
||||
rate, amount = buy_mock.call_args[1]['rate'], buy_mock.call_args[1]['amount']
|
||||
assert rate * amount <= default_conf['stake_amount']
|
||||
|
||||
|
||||
def test_create_trade_too_small_stake_amount(default_conf, ticker, limit_buy_order_open,
|
||||
fee, mocker, caplog) -> None:
|
||||
patch_RPCManager(mocker)
|
||||
patch_exchange(mocker)
|
||||
buy_mock = MagicMock(return_value=limit_buy_order_open)
|
||||
mocker.patch.multiple(
|
||||
'freqtrade.exchange.Exchange',
|
||||
fetch_ticker=ticker,
|
||||
create_order=buy_mock,
|
||||
get_fee=fee,
|
||||
)
|
||||
|
||||
freqtrade = FreqtradeBot(default_conf)
|
||||
freqtrade.config['stake_amount'] = 0.000000005
|
||||
|
||||
patch_get_signal(freqtrade)
|
||||
|
||||
assert freqtrade.create_trade('ETH/BTC')
|
||||
assert log_has_re(r"Stake amount for pair .* is too small.*", caplog)
|
||||
|
||||
|
||||
def test_create_trade_zero_stake_amount(default_conf, ticker, limit_buy_order_open,
|
||||
fee, mocker) -> None:
|
||||
patch_RPCManager(mocker)
|
||||
patch_exchange(mocker)
|
||||
buy_mock = MagicMock(return_value=limit_buy_order_open)
|
||||
mocker.patch.multiple(
|
||||
'freqtrade.exchange.Exchange',
|
||||
fetch_ticker=ticker,
|
||||
create_order=buy_mock,
|
||||
get_fee=fee,
|
||||
)
|
||||
|
||||
freqtrade = FreqtradeBot(default_conf)
|
||||
freqtrade.config['stake_amount'] = 0
|
||||
|
||||
patch_get_signal(freqtrade)
|
||||
|
||||
assert not freqtrade.create_trade('ETH/BTC')
|
||||
|
||||
|
||||
def test_create_trade_limit_reached(default_conf, ticker, limit_buy_order_open,
|
||||
fee, mocker) -> None:
|
||||
patch_RPCManager(mocker)
|
||||
patch_exchange(mocker)
|
||||
mocker.patch.multiple(
|
||||
'freqtrade.exchange.Exchange',
|
||||
fetch_ticker=ticker,
|
||||
create_order=MagicMock(return_value=limit_buy_order_open),
|
||||
get_fee=fee,
|
||||
)
|
||||
default_conf['max_open_trades'] = 0
|
||||
default_conf['stake_amount'] = UNLIMITED_STAKE_AMOUNT
|
||||
|
||||
freqtrade = FreqtradeBot(default_conf)
|
||||
patch_get_signal(freqtrade)
|
||||
|
||||
assert not freqtrade.create_trade('ETH/BTC')
|
||||
assert freqtrade.wallets.get_trade_stake_amount('ETH/BTC', freqtrade.edge) == 0
|
||||
if create:
|
||||
assert freqtrade.create_trade('ETH/BTC')
|
||||
if amount_enough:
|
||||
rate, amount = buy_mock.call_args[1]['rate'], buy_mock.call_args[1]['amount']
|
||||
assert rate * amount <= default_conf['stake_amount']
|
||||
else:
|
||||
assert log_has_re(
|
||||
r"Stake amount for pair .* is too small.*",
|
||||
caplog
|
||||
)
|
||||
else:
|
||||
assert not freqtrade.create_trade('ETH/BTC')
|
||||
if not max_open_trades:
|
||||
assert freqtrade.wallets.get_trade_stake_amount('ETH/BTC', freqtrade.edge) == 0
|
||||
|
||||
|
||||
@pytest.mark.parametrize('whitelist,positions', [
|
||||
(["ETH/BTC"], 1), # No pairs left
|
||||
([], 0), # No pairs in whitelist
|
||||
])
|
||||
def test_enter_positions_no_pairs_left(default_conf, ticker, limit_buy_order_open, fee,
|
||||
mocker, caplog) -> None:
|
||||
whitelist, positions, mocker, caplog) -> None:
|
||||
patch_RPCManager(mocker)
|
||||
patch_exchange(mocker)
|
||||
mocker.patch.multiple(
|
||||
@@ -467,36 +373,20 @@ def test_enter_positions_no_pairs_left(default_conf, ticker, limit_buy_order_ope
|
||||
create_order=MagicMock(return_value=limit_buy_order_open),
|
||||
get_fee=fee,
|
||||
)
|
||||
|
||||
default_conf['exchange']['pair_whitelist'] = ["ETH/BTC"]
|
||||
default_conf['exchange']['pair_whitelist'] = whitelist
|
||||
freqtrade = FreqtradeBot(default_conf)
|
||||
patch_get_signal(freqtrade)
|
||||
|
||||
n = freqtrade.enter_positions()
|
||||
assert n == 1
|
||||
assert not log_has_re(r"No currency pair in active pair whitelist.*", caplog)
|
||||
n = freqtrade.enter_positions()
|
||||
assert n == 0
|
||||
assert log_has_re(r"No currency pair in active pair whitelist.*", caplog)
|
||||
|
||||
|
||||
def test_enter_positions_no_pairs_in_whitelist(default_conf, ticker, limit_buy_order, fee,
|
||||
mocker, caplog) -> None:
|
||||
patch_RPCManager(mocker)
|
||||
patch_exchange(mocker)
|
||||
mocker.patch.multiple(
|
||||
'freqtrade.exchange.Exchange',
|
||||
fetch_ticker=ticker,
|
||||
create_order=MagicMock(return_value={'id': limit_buy_order['id']}),
|
||||
get_fee=fee,
|
||||
)
|
||||
default_conf['exchange']['pair_whitelist'] = []
|
||||
freqtrade = FreqtradeBot(default_conf)
|
||||
patch_get_signal(freqtrade)
|
||||
|
||||
n = freqtrade.enter_positions()
|
||||
assert n == 0
|
||||
assert log_has("Active pair whitelist is empty.", caplog)
|
||||
assert n == positions
|
||||
if positions:
|
||||
assert not log_has_re(r"No currency pair in active pair whitelist.*", caplog)
|
||||
n = freqtrade.enter_positions()
|
||||
assert n == 0
|
||||
assert log_has_re(r"No currency pair in active pair whitelist.*", caplog)
|
||||
else:
|
||||
assert n == 0
|
||||
assert log_has("Active pair whitelist is empty.", caplog)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("init_persistence")
|
||||
@@ -1253,6 +1143,7 @@ def test_create_stoploss_order_insufficient_funds(mocker, default_conf, caplog,
|
||||
@pytest.mark.usefixtures("init_persistence")
|
||||
def test_handle_stoploss_on_exchange_trailing(mocker, default_conf, fee,
|
||||
limit_buy_order, limit_sell_order) -> None:
|
||||
# TODO-lev: test for short
|
||||
# When trailing stoploss is set
|
||||
stoploss = MagicMock(return_value={'id': 13434334})
|
||||
patch_RPCManager(mocker)
|
||||
@@ -1344,10 +1235,14 @@ def test_handle_stoploss_on_exchange_trailing(mocker, default_conf, fee,
|
||||
assert freqtrade.handle_stoploss_on_exchange(trade) is False
|
||||
|
||||
cancel_order_mock.assert_called_once_with(100, 'ETH/BTC')
|
||||
stoploss_order_mock.assert_called_once_with(amount=85.32423208,
|
||||
pair='ETH/BTC',
|
||||
order_types=freqtrade.strategy.order_types,
|
||||
stop_price=0.00002346 * 0.95)
|
||||
stoploss_order_mock.assert_called_once_with(
|
||||
amount=85.32423208,
|
||||
pair='ETH/BTC',
|
||||
order_types=freqtrade.strategy.order_types,
|
||||
stop_price=0.00002346 * 0.95,
|
||||
side="sell",
|
||||
leverage=1.0
|
||||
)
|
||||
|
||||
# price fell below stoploss, so dry-run sells trade.
|
||||
mocker.patch('freqtrade.exchange.Exchange.fetch_ticker', MagicMock(return_value={
|
||||
@@ -1360,6 +1255,7 @@ def test_handle_stoploss_on_exchange_trailing(mocker, default_conf, fee,
|
||||
|
||||
def test_handle_stoploss_on_exchange_trailing_error(mocker, default_conf, fee, caplog,
|
||||
limit_buy_order, limit_sell_order) -> None:
|
||||
# TODO-lev: test for short
|
||||
# When trailing stoploss is set
|
||||
stoploss = MagicMock(return_value={'id': 13434334})
|
||||
patch_exchange(mocker)
|
||||
@@ -1418,7 +1314,7 @@ def test_handle_stoploss_on_exchange_trailing_error(mocker, default_conf, fee, c
|
||||
side_effect=InvalidOrderException())
|
||||
mocker.patch('freqtrade.exchange.Binance.fetch_stoploss_order',
|
||||
return_value=stoploss_order_hanging)
|
||||
freqtrade.handle_trailing_stoploss_on_exchange(trade, stoploss_order_hanging)
|
||||
freqtrade.handle_trailing_stoploss_on_exchange(trade, stoploss_order_hanging, side="sell")
|
||||
assert log_has_re(r"Could not cancel stoploss order abcd for pair ETH/BTC.*", caplog)
|
||||
|
||||
# Still try to create order
|
||||
@@ -1428,7 +1324,7 @@ def test_handle_stoploss_on_exchange_trailing_error(mocker, default_conf, fee, c
|
||||
caplog.clear()
|
||||
cancel_mock = mocker.patch("freqtrade.exchange.Binance.cancel_stoploss_order", MagicMock())
|
||||
mocker.patch("freqtrade.exchange.Binance.stoploss", side_effect=ExchangeError())
|
||||
freqtrade.handle_trailing_stoploss_on_exchange(trade, stoploss_order_hanging)
|
||||
freqtrade.handle_trailing_stoploss_on_exchange(trade, stoploss_order_hanging, side="sell")
|
||||
assert cancel_mock.call_count == 1
|
||||
assert log_has_re(r"Could not create trailing stoploss order for pair ETH/BTC\..*", caplog)
|
||||
|
||||
@@ -1437,6 +1333,7 @@ def test_handle_stoploss_on_exchange_trailing_error(mocker, default_conf, fee, c
|
||||
def test_handle_stoploss_on_exchange_custom_stop(mocker, default_conf, fee,
|
||||
limit_buy_order, limit_sell_order) -> None:
|
||||
# When trailing stoploss is set
|
||||
# TODO-lev: test for short
|
||||
stoploss = MagicMock(return_value={'id': 13434334})
|
||||
patch_RPCManager(mocker)
|
||||
mocker.patch.multiple(
|
||||
@@ -1527,10 +1424,14 @@ def test_handle_stoploss_on_exchange_custom_stop(mocker, default_conf, fee,
|
||||
assert freqtrade.handle_stoploss_on_exchange(trade) is False
|
||||
|
||||
cancel_order_mock.assert_called_once_with(100, 'ETH/BTC')
|
||||
stoploss_order_mock.assert_called_once_with(amount=85.32423208,
|
||||
pair='ETH/BTC',
|
||||
order_types=freqtrade.strategy.order_types,
|
||||
stop_price=0.00002346 * 0.96)
|
||||
stoploss_order_mock.assert_called_once_with(
|
||||
amount=85.32423208,
|
||||
pair='ETH/BTC',
|
||||
order_types=freqtrade.strategy.order_types,
|
||||
stop_price=0.00002346 * 0.96,
|
||||
side="sell",
|
||||
leverage=1.0
|
||||
)
|
||||
|
||||
# price fell below stoploss, so dry-run sells trade.
|
||||
mocker.patch('freqtrade.exchange.Exchange.fetch_ticker', MagicMock(return_value={
|
||||
@@ -1543,7 +1444,7 @@ def test_handle_stoploss_on_exchange_custom_stop(mocker, default_conf, fee,
|
||||
|
||||
def test_tsl_on_exchange_compatible_with_edge(mocker, edge_conf, fee, caplog,
|
||||
limit_buy_order, limit_sell_order) -> None:
|
||||
|
||||
# TODO-lev: test for short
|
||||
# When trailing stoploss is set
|
||||
stoploss = MagicMock(return_value={'id': 13434334})
|
||||
patch_RPCManager(mocker)
|
||||
@@ -1648,36 +1549,37 @@ def test_tsl_on_exchange_compatible_with_edge(mocker, edge_conf, fee, caplog,
|
||||
# stoploss should be set to 1% as trailing is on
|
||||
assert trade.stop_loss == 0.00002346 * 0.99
|
||||
cancel_order_mock.assert_called_once_with(100, 'NEO/BTC')
|
||||
stoploss_order_mock.assert_called_once_with(amount=2132892.49146757,
|
||||
pair='NEO/BTC',
|
||||
order_types=freqtrade.strategy.order_types,
|
||||
stop_price=0.00002346 * 0.99)
|
||||
stoploss_order_mock.assert_called_once_with(
|
||||
amount=2132892.49146757,
|
||||
pair='NEO/BTC',
|
||||
order_types=freqtrade.strategy.order_types,
|
||||
stop_price=0.00002346 * 0.99,
|
||||
side="sell",
|
||||
leverage=1.0
|
||||
)
|
||||
|
||||
|
||||
def test_enter_positions(mocker, default_conf, caplog) -> None:
|
||||
@pytest.mark.parametrize('return_value,side_effect,log_message', [
|
||||
(False, None, 'Found no enter signals for whitelisted currencies. Trying again...'),
|
||||
(None, DependencyException, 'Unable to create trade for ETH/BTC: ')
|
||||
])
|
||||
def test_enter_positions(mocker, default_conf, return_value, side_effect,
|
||||
log_message, caplog) -> None:
|
||||
caplog.set_level(logging.DEBUG)
|
||||
freqtrade = get_patched_freqtradebot(mocker, default_conf)
|
||||
|
||||
mock_ct = mocker.patch('freqtrade.freqtradebot.FreqtradeBot.create_trade',
|
||||
MagicMock(return_value=False))
|
||||
n = freqtrade.enter_positions()
|
||||
assert n == 0
|
||||
assert log_has('Found no enter signals for whitelisted currencies. Trying again...', caplog)
|
||||
# create_trade should be called once for every pair in the whitelist.
|
||||
assert mock_ct.call_count == len(default_conf['exchange']['pair_whitelist'])
|
||||
|
||||
|
||||
def test_enter_positions_exception(mocker, default_conf, caplog) -> None:
|
||||
freqtrade = get_patched_freqtradebot(mocker, default_conf)
|
||||
|
||||
mock_ct = mocker.patch(
|
||||
'freqtrade.freqtradebot.FreqtradeBot.create_trade',
|
||||
MagicMock(side_effect=DependencyException)
|
||||
MagicMock(
|
||||
return_value=return_value,
|
||||
side_effect=side_effect
|
||||
)
|
||||
)
|
||||
n = freqtrade.enter_positions()
|
||||
assert n == 0
|
||||
assert log_has(log_message, caplog)
|
||||
# create_trade should be called once for every pair in the whitelist.
|
||||
assert mock_ct.call_count == len(default_conf['exchange']['pair_whitelist'])
|
||||
assert log_has('Unable to create trade for ETH/BTC: ', caplog)
|
||||
|
||||
|
||||
def test_exit_positions(mocker, default_conf, limit_buy_order, caplog) -> None:
|
||||
@@ -1771,8 +1673,13 @@ def test_update_trade_state(mocker, default_conf, limit_buy_order, caplog) -> No
|
||||
assert log_has_re('Found open order for.*', caplog)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('initial_amount,has_rounding_fee', [
|
||||
(90.99181073 + 1e-14, True),
|
||||
(8.0, False)
|
||||
])
|
||||
def test_update_trade_state_withorderdict(default_conf, trades_for_order, limit_buy_order, fee,
|
||||
mocker):
|
||||
mocker, initial_amount, has_rounding_fee, caplog):
|
||||
trades_for_order[0]['amount'] = initial_amount
|
||||
mocker.patch('freqtrade.exchange.Exchange.get_trades_for_order', return_value=trades_for_order)
|
||||
# fetch_order should not be called!!
|
||||
mocker.patch('freqtrade.exchange.Exchange.fetch_order', MagicMock(side_effect=ValueError))
|
||||
@@ -1793,32 +1700,8 @@ def test_update_trade_state_withorderdict(default_conf, trades_for_order, limit_
|
||||
freqtrade.update_trade_state(trade, '123456', limit_buy_order)
|
||||
assert trade.amount != amount
|
||||
assert trade.amount == limit_buy_order['amount']
|
||||
|
||||
|
||||
def test_update_trade_state_withorderdict_rounding_fee(default_conf, trades_for_order, fee,
|
||||
limit_buy_order, mocker, caplog):
|
||||
trades_for_order[0]['amount'] = limit_buy_order['amount'] + 1e-14
|
||||
mocker.patch('freqtrade.exchange.Exchange.get_trades_for_order', return_value=trades_for_order)
|
||||
# fetch_order should not be called!!
|
||||
mocker.patch('freqtrade.exchange.Exchange.fetch_order', MagicMock(side_effect=ValueError))
|
||||
patch_exchange(mocker)
|
||||
amount = sum(x['amount'] for x in trades_for_order)
|
||||
freqtrade = get_patched_freqtradebot(mocker, default_conf)
|
||||
trade = Trade(
|
||||
pair='LTC/ETH',
|
||||
amount=amount,
|
||||
exchange='binance',
|
||||
open_rate=0.245441,
|
||||
fee_open=fee.return_value,
|
||||
fee_close=fee.return_value,
|
||||
open_order_id='123456',
|
||||
is_open=True,
|
||||
open_date=arrow.utcnow().datetime,
|
||||
)
|
||||
freqtrade.update_trade_state(trade, '123456', limit_buy_order)
|
||||
assert trade.amount != amount
|
||||
assert trade.amount == limit_buy_order['amount']
|
||||
assert log_has_re(r'Applying fee on amount for .*', caplog)
|
||||
if has_rounding_fee:
|
||||
assert log_has_re(r'Applying fee on amount for .*', caplog)
|
||||
|
||||
|
||||
def test_update_trade_state_exception(mocker, default_conf,
|
||||
@@ -3130,16 +3013,28 @@ def test_execute_trade_exit_insufficient_funds_error(default_conf, ticker, fee,
|
||||
assert mock_insuf.call_count == 1
|
||||
|
||||
|
||||
def test_sell_profit_only_enable_profit(default_conf, limit_buy_order, limit_buy_order_open,
|
||||
fee, mocker) -> None:
|
||||
@pytest.mark.parametrize('profit_only,bid,ask,handle_first,handle_second,sell_type', [
|
||||
# Enable profit
|
||||
(True, 0.00001172, 0.00001173, False, True, SellType.SELL_SIGNAL.value),
|
||||
# Disable profit
|
||||
(False, 0.00002172, 0.00002173, True, False, SellType.SELL_SIGNAL.value),
|
||||
# Enable loss
|
||||
# * Shouldn't this be SellType.STOP_LOSS.value
|
||||
(True, 0.00000172, 0.00000173, False, False, None),
|
||||
# Disable loss
|
||||
(False, 0.00000172, 0.00000173, True, False, SellType.SELL_SIGNAL.value),
|
||||
])
|
||||
def test_sell_profit_only(
|
||||
default_conf, limit_buy_order, limit_buy_order_open,
|
||||
fee, mocker, profit_only, bid, ask, handle_first, handle_second, sell_type) -> None:
|
||||
patch_RPCManager(mocker)
|
||||
patch_exchange(mocker)
|
||||
mocker.patch.multiple(
|
||||
'freqtrade.exchange.Exchange',
|
||||
fetch_ticker=MagicMock(return_value={
|
||||
'bid': 0.00001172,
|
||||
'ask': 0.00001173,
|
||||
'last': 0.00001172
|
||||
'bid': bid,
|
||||
'ask': ask,
|
||||
'last': bid
|
||||
}),
|
||||
create_order=MagicMock(side_effect=[
|
||||
limit_buy_order_open,
|
||||
@@ -3149,128 +3044,29 @@ def test_sell_profit_only_enable_profit(default_conf, limit_buy_order, limit_buy
|
||||
)
|
||||
default_conf.update({
|
||||
'use_sell_signal': True,
|
||||
'sell_profit_only': True,
|
||||
'sell_profit_only': profit_only,
|
||||
'sell_profit_offset': 0.1,
|
||||
})
|
||||
freqtrade = FreqtradeBot(default_conf)
|
||||
patch_get_signal(freqtrade)
|
||||
freqtrade.strategy.min_roi_reached = MagicMock(return_value=False)
|
||||
|
||||
if sell_type == SellType.SELL_SIGNAL.value:
|
||||
freqtrade.strategy.min_roi_reached = MagicMock(return_value=False)
|
||||
else:
|
||||
freqtrade.strategy.stop_loss_reached = MagicMock(return_value=SellCheckTuple(
|
||||
sell_type=SellType.NONE))
|
||||
freqtrade.enter_positions()
|
||||
|
||||
trade = Trade.query.first()
|
||||
trade.update(limit_buy_order)
|
||||
freqtrade.wallets.update()
|
||||
patch_get_signal(freqtrade, enter_long=False, exit_long=True)
|
||||
assert freqtrade.handle_trade(trade) is False
|
||||
assert freqtrade.handle_trade(trade) is handle_first
|
||||
|
||||
freqtrade.strategy.sell_profit_offset = 0.0
|
||||
assert freqtrade.handle_trade(trade) is True
|
||||
if handle_second:
|
||||
freqtrade.strategy.sell_profit_offset = 0.0
|
||||
assert freqtrade.handle_trade(trade) is True
|
||||
|
||||
assert trade.sell_reason == SellType.SELL_SIGNAL.value
|
||||
|
||||
|
||||
def test_sell_profit_only_disable_profit(default_conf, limit_buy_order, limit_buy_order_open,
|
||||
fee, mocker) -> None:
|
||||
patch_RPCManager(mocker)
|
||||
patch_exchange(mocker)
|
||||
mocker.patch.multiple(
|
||||
'freqtrade.exchange.Exchange',
|
||||
fetch_ticker=MagicMock(return_value={
|
||||
'bid': 0.00002172,
|
||||
'ask': 0.00002173,
|
||||
'last': 0.00002172
|
||||
}),
|
||||
create_order=MagicMock(side_effect=[
|
||||
limit_buy_order_open,
|
||||
{'id': 1234553382},
|
||||
]),
|
||||
get_fee=fee,
|
||||
)
|
||||
default_conf.update({
|
||||
'use_sell_signal': True,
|
||||
'sell_profit_only': False,
|
||||
})
|
||||
freqtrade = FreqtradeBot(default_conf)
|
||||
patch_get_signal(freqtrade)
|
||||
freqtrade.strategy.min_roi_reached = MagicMock(return_value=False)
|
||||
freqtrade.enter_positions()
|
||||
|
||||
trade = Trade.query.first()
|
||||
trade.update(limit_buy_order)
|
||||
freqtrade.wallets.update()
|
||||
patch_get_signal(freqtrade, enter_long=False, exit_long=True)
|
||||
assert freqtrade.handle_trade(trade) is True
|
||||
assert trade.sell_reason == SellType.SELL_SIGNAL.value
|
||||
|
||||
|
||||
def test_sell_profit_only_enable_loss(default_conf, limit_buy_order, limit_buy_order_open,
|
||||
fee, mocker) -> None:
|
||||
patch_RPCManager(mocker)
|
||||
patch_exchange(mocker)
|
||||
mocker.patch.multiple(
|
||||
'freqtrade.exchange.Exchange',
|
||||
fetch_ticker=MagicMock(return_value={
|
||||
'bid': 0.00000172,
|
||||
'ask': 0.00000173,
|
||||
'last': 0.00000172
|
||||
}),
|
||||
create_order=MagicMock(side_effect=[
|
||||
limit_buy_order_open,
|
||||
{'id': 1234553382},
|
||||
]),
|
||||
get_fee=fee,
|
||||
)
|
||||
default_conf.update({
|
||||
'use_sell_signal': True,
|
||||
'sell_profit_only': True,
|
||||
})
|
||||
freqtrade = FreqtradeBot(default_conf)
|
||||
patch_get_signal(freqtrade)
|
||||
freqtrade.strategy.stop_loss_reached = MagicMock(return_value=SellCheckTuple(
|
||||
sell_type=SellType.NONE))
|
||||
freqtrade.enter_positions()
|
||||
|
||||
trade = Trade.query.first()
|
||||
trade.update(limit_buy_order)
|
||||
patch_get_signal(freqtrade, enter_long=False, exit_long=True)
|
||||
assert freqtrade.handle_trade(trade) is False
|
||||
|
||||
|
||||
def test_sell_profit_only_disable_loss(default_conf, limit_buy_order, limit_buy_order_open,
|
||||
fee, mocker) -> None:
|
||||
patch_RPCManager(mocker)
|
||||
patch_exchange(mocker)
|
||||
mocker.patch.multiple(
|
||||
'freqtrade.exchange.Exchange',
|
||||
fetch_ticker=MagicMock(return_value={
|
||||
'bid': 0.0000172,
|
||||
'ask': 0.0000173,
|
||||
'last': 0.0000172
|
||||
}),
|
||||
create_order=MagicMock(side_effect=[
|
||||
limit_buy_order_open,
|
||||
{'id': 1234553382},
|
||||
]),
|
||||
get_fee=fee,
|
||||
)
|
||||
default_conf.update({
|
||||
'use_sell_signal': True,
|
||||
'sell_profit_only': False,
|
||||
})
|
||||
|
||||
freqtrade = FreqtradeBot(default_conf)
|
||||
patch_get_signal(freqtrade)
|
||||
freqtrade.strategy.min_roi_reached = MagicMock(return_value=False)
|
||||
|
||||
freqtrade.enter_positions()
|
||||
|
||||
trade = Trade.query.first()
|
||||
trade.update(limit_buy_order)
|
||||
freqtrade.wallets.update()
|
||||
patch_get_signal(freqtrade, enter_long=False, exit_long=True)
|
||||
assert freqtrade.handle_trade(trade) is True
|
||||
assert trade.sell_reason == SellType.SELL_SIGNAL.value
|
||||
assert trade.sell_reason == sell_type
|
||||
|
||||
|
||||
def test_sell_not_enough_balance(default_conf, limit_buy_order, limit_buy_order_open,
|
||||
@@ -3308,11 +3104,15 @@ def test_sell_not_enough_balance(default_conf, limit_buy_order, limit_buy_order_
|
||||
assert trade.amount != amnt
|
||||
|
||||
|
||||
def test__safe_exit_amount(default_conf, fee, caplog, mocker):
|
||||
@pytest.mark.parametrize('amount_wallet,has_err', [
|
||||
(95.29, False),
|
||||
(91.29, True)
|
||||
])
|
||||
def test__safe_exit_amount(default_conf, fee, caplog, mocker, amount_wallet, has_err):
|
||||
patch_RPCManager(mocker)
|
||||
patch_exchange(mocker)
|
||||
amount = 95.33
|
||||
amount_wallet = 95.29
|
||||
amount_wallet = amount_wallet
|
||||
mocker.patch('freqtrade.wallets.Wallets.get_free', MagicMock(return_value=amount_wallet))
|
||||
wallet_update = mocker.patch('freqtrade.wallets.Wallets.update')
|
||||
trade = Trade(
|
||||
@@ -3326,37 +3126,19 @@ def test__safe_exit_amount(default_conf, fee, caplog, mocker):
|
||||
)
|
||||
freqtrade = FreqtradeBot(default_conf)
|
||||
patch_get_signal(freqtrade)
|
||||
|
||||
wallet_update.reset_mock()
|
||||
assert freqtrade._safe_exit_amount(trade.pair, trade.amount) == amount_wallet
|
||||
assert log_has_re(r'.*Falling back to wallet-amount.', caplog)
|
||||
assert wallet_update.call_count == 1
|
||||
caplog.clear()
|
||||
wallet_update.reset_mock()
|
||||
assert freqtrade._safe_exit_amount(trade.pair, amount_wallet) == amount_wallet
|
||||
assert not log_has_re(r'.*Falling back to wallet-amount.', caplog)
|
||||
assert wallet_update.call_count == 1
|
||||
|
||||
|
||||
def test__safe_exit_amount_error(default_conf, fee, caplog, mocker):
|
||||
patch_RPCManager(mocker)
|
||||
patch_exchange(mocker)
|
||||
amount = 95.33
|
||||
amount_wallet = 91.29
|
||||
mocker.patch('freqtrade.wallets.Wallets.get_free', MagicMock(return_value=amount_wallet))
|
||||
trade = Trade(
|
||||
pair='LTC/ETH',
|
||||
amount=amount,
|
||||
exchange='binance',
|
||||
open_rate=0.245441,
|
||||
open_order_id="123456",
|
||||
fee_open=fee.return_value,
|
||||
fee_close=fee.return_value,
|
||||
)
|
||||
freqtrade = FreqtradeBot(default_conf)
|
||||
patch_get_signal(freqtrade)
|
||||
with pytest.raises(DependencyException, match=r"Not enough amount to exit."):
|
||||
assert freqtrade._safe_exit_amount(trade.pair, trade.amount)
|
||||
if has_err:
|
||||
with pytest.raises(DependencyException, match=r"Not enough amount to exit trade."):
|
||||
assert freqtrade._safe_exit_amount(trade.pair, trade.amount)
|
||||
else:
|
||||
wallet_update.reset_mock()
|
||||
assert freqtrade._safe_exit_amount(trade.pair, trade.amount) == amount_wallet
|
||||
assert log_has_re(r'.*Falling back to wallet-amount.', caplog)
|
||||
assert wallet_update.call_count == 1
|
||||
caplog.clear()
|
||||
wallet_update.reset_mock()
|
||||
assert freqtrade._safe_exit_amount(trade.pair, amount_wallet) == amount_wallet
|
||||
assert not log_has_re(r'.*Falling back to wallet-amount.', caplog)
|
||||
assert wallet_update.call_count == 1
|
||||
|
||||
|
||||
def test_locked_pairs(default_conf, ticker, fee, ticker_sell_down, mocker, caplog) -> None:
|
||||
@@ -4144,50 +3926,37 @@ def test_order_book_depth_of_market_high_delta(default_conf, ticker, limit_buy_o
|
||||
assert trade is None
|
||||
|
||||
|
||||
def test_order_book_bid_strategy1(mocker, default_conf, order_book_l2) -> None:
|
||||
@pytest.mark.parametrize('exception_thrown,ask,last,order_book_top,order_book', [
|
||||
(False, 0.045, 0.046, 2, None),
|
||||
(True, 0.042, 0.046, 1, {'bids': [[]], 'asks': [[]]})
|
||||
])
|
||||
def test_order_book_bid_strategy1(mocker, default_conf, order_book_l2, exception_thrown,
|
||||
ask, last, order_book_top, order_book, caplog) -> None:
|
||||
"""
|
||||
test if function get_rate will return the order book price
|
||||
instead of the ask rate
|
||||
test if function get_rate will return the order book price instead of the ask rate
|
||||
"""
|
||||
patch_exchange(mocker)
|
||||
ticker_mock = MagicMock(return_value={'ask': 0.045, 'last': 0.046})
|
||||
ticker_mock = MagicMock(return_value={'ask': ask, 'last': last})
|
||||
mocker.patch.multiple(
|
||||
'freqtrade.exchange.Exchange',
|
||||
fetch_l2_order_book=order_book_l2,
|
||||
fetch_l2_order_book=MagicMock(return_value=order_book) if order_book else order_book_l2,
|
||||
fetch_ticker=ticker_mock,
|
||||
|
||||
)
|
||||
default_conf['exchange']['name'] = 'binance'
|
||||
default_conf['bid_strategy']['use_order_book'] = True
|
||||
default_conf['bid_strategy']['order_book_top'] = 2
|
||||
default_conf['bid_strategy']['order_book_top'] = order_book_top
|
||||
default_conf['bid_strategy']['ask_last_balance'] = 0
|
||||
default_conf['telegram']['enabled'] = False
|
||||
|
||||
freqtrade = FreqtradeBot(default_conf)
|
||||
assert freqtrade.exchange.get_rate('ETH/BTC', refresh=True, side="buy") == 0.043935
|
||||
assert ticker_mock.call_count == 0
|
||||
|
||||
|
||||
def test_order_book_bid_strategy_exception(mocker, default_conf, caplog) -> None:
|
||||
patch_exchange(mocker)
|
||||
ticker_mock = MagicMock(return_value={'ask': 0.042, 'last': 0.046})
|
||||
mocker.patch.multiple(
|
||||
'freqtrade.exchange.Exchange',
|
||||
fetch_l2_order_book=MagicMock(return_value={'bids': [[]], 'asks': [[]]}),
|
||||
fetch_ticker=ticker_mock,
|
||||
|
||||
)
|
||||
default_conf['exchange']['name'] = 'binance'
|
||||
default_conf['bid_strategy']['use_order_book'] = True
|
||||
default_conf['bid_strategy']['order_book_top'] = 1
|
||||
default_conf['bid_strategy']['ask_last_balance'] = 0
|
||||
default_conf['telegram']['enabled'] = False
|
||||
|
||||
freqtrade = FreqtradeBot(default_conf)
|
||||
# orderbook shall be used even if tickers would be lower.
|
||||
with pytest.raises(PricingError):
|
||||
freqtrade.exchange.get_rate('ETH/BTC', refresh=True, side="buy")
|
||||
assert log_has_re(r'Buy Price at location 1 from orderbook could not be determined.', caplog)
|
||||
if exception_thrown:
|
||||
with pytest.raises(PricingError):
|
||||
freqtrade.exchange.get_rate('ETH/BTC', refresh=True, side="buy")
|
||||
assert log_has_re(
|
||||
r'Buy Price at location 1 from orderbook could not be determined.', caplog)
|
||||
else:
|
||||
assert freqtrade.exchange.get_rate('ETH/BTC', refresh=True, side="buy") == 0.043935
|
||||
assert ticker_mock.call_count == 0
|
||||
|
||||
|
||||
def test_check_depth_of_market_buy(default_conf, mocker, order_book_l2) -> None:
|
||||
|
Reference in New Issue
Block a user