Initial steps to change bid/ask pricing to enter/exit

This commit is contained in:
Matthias
2022-03-27 18:03:49 +02:00
parent d1f61c4cf9
commit bcf326a035
30 changed files with 193 additions and 131 deletions

View File

@@ -419,7 +419,7 @@ def get_default_conf(testdatadir):
"entry": 10,
"exit": 30
},
"bid_strategy": {
"entry_pricing": {
"ask_last_balance": 0.0,
"use_order_book": False,
"order_book_top": 1,
@@ -428,7 +428,7 @@ def get_default_conf(testdatadir):
"bids_to_ask_delta": 1
}
},
"ask_strategy": {
"exit_pricing": {
"use_order_book": False,
"order_book_top": 1,
},

View File

@@ -107,8 +107,8 @@ def exchange_conf():
config['exchange']['key'] = ''
config['exchange']['secret'] = ''
config['dry_run'] = False
config['bid_strategy']['use_order_book'] = True
config['ask_strategy']['use_order_book'] = True
config['entry_pricing']['use_order_book'] = True
config['exit_pricing']['use_order_book'] = True
return config

View File

@@ -971,7 +971,7 @@ def test_validate_pricing(default_conf, mocker):
has.update({'fetchTicker': True})
default_conf['ask_strategy']['use_order_book'] = True
default_conf['exit_pricing']['use_order_book'] = True
ExchangeResolver.load_exchange('binance', default_conf)
has.update({'fetchL2OrderBook': False})
@@ -2305,10 +2305,10 @@ def test_get_buy_rate(mocker, default_conf, caplog, side, ask, bid,
last, last_ab, expected) -> None:
caplog.set_level(logging.DEBUG)
if last_ab is None:
del default_conf['bid_strategy']['ask_last_balance']
del default_conf['entry_pricing']['ask_last_balance']
else:
default_conf['bid_strategy']['ask_last_balance'] = last_ab
default_conf['bid_strategy']['price_side'] = side
default_conf['entry_pricing']['ask_last_balance'] = last_ab
default_conf['entry_pricing']['price_side'] = side
exchange = get_patched_exchange(mocker, default_conf)
mocker.patch('freqtrade.exchange.Exchange.fetch_ticker',
return_value={'ask': ask, 'last': last, 'bid': bid})
@@ -2349,9 +2349,9 @@ def test_get_sell_rate(default_conf, mocker, caplog, side, bid, ask,
last, last_ab, expected) -> None:
caplog.set_level(logging.DEBUG)
default_conf['ask_strategy']['price_side'] = side
default_conf['exit_pricing']['price_side'] = side
if last_ab is not None:
default_conf['ask_strategy']['bid_last_balance'] = last_ab
default_conf['exit_pricing']['bid_last_balance'] = last_ab
mocker.patch('freqtrade.exchange.Exchange.fetch_ticker',
return_value={'ask': ask, 'bid': bid, 'last': last})
pair = "ETH/BTC"
@@ -2381,10 +2381,10 @@ def test_get_sell_rate(default_conf, mocker, caplog, side, bid, ask,
def test_get_ticker_rate_error(mocker, entry, default_conf, caplog, side, ask, bid,
last, last_ab, expected) -> None:
caplog.set_level(logging.DEBUG)
default_conf['bid_strategy']['ask_last_balance'] = last_ab
default_conf['bid_strategy']['price_side'] = side
default_conf['ask_strategy']['price_side'] = side
default_conf['ask_strategy']['ask_last_balance'] = last_ab
default_conf['entry_pricing']['ask_last_balance'] = last_ab
default_conf['entry_pricing']['price_side'] = side
default_conf['exit_pricing']['price_side'] = side
default_conf['exit_pricing']['ask_last_balance'] = last_ab
exchange = get_patched_exchange(mocker, default_conf)
mocker.patch('freqtrade.exchange.Exchange.fetch_ticker',
return_value={'ask': ask, 'last': last, 'bid': bid})
@@ -2400,9 +2400,9 @@ def test_get_ticker_rate_error(mocker, entry, default_conf, caplog, side, ask, b
def test_get_sell_rate_orderbook(default_conf, mocker, caplog, side, expected, order_book_l2):
caplog.set_level(logging.DEBUG)
# Test orderbook mode
default_conf['ask_strategy']['price_side'] = side
default_conf['ask_strategy']['use_order_book'] = True
default_conf['ask_strategy']['order_book_top'] = 1
default_conf['exit_pricing']['price_side'] = side
default_conf['exit_pricing']['use_order_book'] = True
default_conf['exit_pricing']['order_book_top'] = 1
pair = "ETH/BTC"
mocker.patch('freqtrade.exchange.Exchange.fetch_l2_order_book', order_book_l2)
exchange = get_patched_exchange(mocker, default_conf)
@@ -2417,9 +2417,9 @@ def test_get_sell_rate_orderbook(default_conf, mocker, caplog, side, expected, o
def test_get_sell_rate_orderbook_exception(default_conf, mocker, caplog):
# Test orderbook mode
default_conf['ask_strategy']['price_side'] = 'ask'
default_conf['ask_strategy']['use_order_book'] = True
default_conf['ask_strategy']['order_book_top'] = 1
default_conf['exit_pricing']['price_side'] = 'ask'
default_conf['exit_pricing']['use_order_book'] = True
default_conf['exit_pricing']['order_book_top'] = 1
pair = "ETH/BTC"
# Test What happens if the exchange returns an empty orderbook.
mocker.patch('freqtrade.exchange.Exchange.fetch_l2_order_book',
@@ -2433,7 +2433,7 @@ def test_get_sell_rate_orderbook_exception(default_conf, mocker, caplog):
def test_get_sell_rate_exception(default_conf, mocker, caplog):
# Ticker on one side can be empty in certain circumstances.
default_conf['ask_strategy']['price_side'] = 'ask'
default_conf['exit_pricing']['price_side'] = 'ask'
pair = "ETH/BTC"
mocker.patch('freqtrade.exchange.Exchange.fetch_ticker',
return_value={'ask': None, 'bid': 0.12, 'last': None})
@@ -2441,7 +2441,7 @@ def test_get_sell_rate_exception(default_conf, mocker, caplog):
with pytest.raises(PricingError, match=r"Sell-Rate for ETH/BTC was empty."):
exchange.get_rate(pair, refresh=True, side="sell")
exchange._config['ask_strategy']['price_side'] = 'bid'
exchange._config['exit_pricing']['price_side'] = 'bid'
assert exchange.get_rate(pair, refresh=True, side="sell") == 0.12
# Reverse sides
mocker.patch('freqtrade.exchange.Exchange.fetch_ticker',
@@ -2449,7 +2449,7 @@ def test_get_sell_rate_exception(default_conf, mocker, caplog):
with pytest.raises(PricingError, match=r"Sell-Rate for ETH/BTC was empty."):
exchange.get_rate(pair, refresh=True, side="sell")
exchange._config['ask_strategy']['price_side'] = 'ask'
exchange._config['exit_pricing']['price_side'] = 'ask'
assert exchange.get_rate(pair, refresh=True, side="sell") == 0.13

View File

@@ -543,8 +543,8 @@ def test_api_show_config(botclient):
assert response['trading_mode'] == 'spot'
assert response['strategy_version'] is None
assert not response['trailing_stop']
assert 'bid_strategy' in response
assert 'ask_strategy' in response
assert 'entry_pricing' in response
assert 'exit_pricing' in response
assert 'unfilledtimeout' in response
assert 'version' in response
assert 'api_version' in response

View File

@@ -809,21 +809,21 @@ def test_validate_price_side(default_conf):
conf = deepcopy(default_conf)
conf['order_types']['entry'] = 'market'
with pytest.raises(OperationalException,
match='Market buy orders require bid_strategy.price_side = "ask".'):
match='Market buy orders require entry_pricing.price_side = "ask".'):
validate_config_consistency(conf)
conf = deepcopy(default_conf)
conf['order_types']['exit'] = 'market'
with pytest.raises(OperationalException,
match='Market sell orders require ask_strategy.price_side = "bid".'):
match='Market sell orders require exit_pricing.price_side = "bid".'):
validate_config_consistency(conf)
# Validate inversed case
conf = deepcopy(default_conf)
conf['order_types']['exit'] = 'market'
conf['order_types']['entry'] = 'market'
conf['ask_strategy']['price_side'] = 'bid'
conf['bid_strategy']['price_side'] = 'ask'
conf['exit_pricing']['price_side'] = 'bid'
conf['entry_pricing']['price_side'] = 'ask'
validate_config_consistency(conf)
@@ -926,18 +926,18 @@ def test_validate_protections(default_conf, protconf, expected):
def test_validate_ask_orderbook(default_conf, caplog) -> None:
conf = deepcopy(default_conf)
conf['ask_strategy']['use_order_book'] = True
conf['ask_strategy']['order_book_min'] = 2
conf['ask_strategy']['order_book_max'] = 2
conf['exit_pricing']['use_order_book'] = True
conf['exit_pricing']['order_book_min'] = 2
conf['exit_pricing']['order_book_max'] = 2
validate_config_consistency(conf)
assert log_has_re(r"DEPRECATED: Please use `order_book_top` instead of.*", caplog)
assert conf['ask_strategy']['order_book_top'] == 2
assert conf['exit_pricing']['order_book_top'] == 2
conf['ask_strategy']['order_book_max'] = 5
conf['exit_pricing']['order_book_max'] = 5
with pytest.raises(OperationalException,
match=r"Using order_book_max != order_book_min in ask_strategy.*"):
match=r"Using order_book_max != order_book_min in exit_pricing.*"):
validate_config_consistency(conf)
@@ -1200,15 +1200,15 @@ def test_pairlist_resolving_fallback(mocker):
@pytest.mark.parametrize("setting", [
("ask_strategy", "use_sell_signal", True,
("exit_pricing", "use_sell_signal", True,
None, "use_sell_signal", False),
("ask_strategy", "sell_profit_only", True,
("exit_pricing", "sell_profit_only", True,
None, "sell_profit_only", False),
("ask_strategy", "sell_profit_offset", 0.1,
("exit_pricing", "sell_profit_offset", 0.1,
None, "sell_profit_offset", 0.01),
("ask_strategy", "ignore_roi_if_buy_signal", True,
("exit_pricing", "ignore_roi_if_buy_signal", True,
None, "ignore_roi_if_buy_signal", False),
("ask_strategy", "ignore_buying_expired_candle_after", 5,
("exit_pricing", "ignore_buying_expired_candle_after", 5,
None, "ignore_buying_expired_candle_after", 6),
])
def test_process_temporary_deprecated_settings(mocker, default_conf, setting, caplog):
@@ -1434,15 +1434,15 @@ def test_flat_vars_to_nested_dict(caplog):
'FREQTRADE__EXCHANGE__SOME_SETTING': 'true',
'FREQTRADE__EXCHANGE__SOME_FALSE_SETTING': 'false',
'FREQTRADE__EXCHANGE__CONFIG__whatever': 'sometime',
'FREQTRADE__ASK_STRATEGY__PRICE_SIDE': 'bid',
'FREQTRADE__ASK_STRATEGY__cccc': '500',
'FREQTRADE__EXIT_PRICING__PRICE_SIDE': 'bid',
'FREQTRADE__EXIT_PRICING__cccc': '500',
'FREQTRADE__STAKE_AMOUNT': '200.05',
'FREQTRADE__TELEGRAM__CHAT_ID': '2151',
'NOT_RELEVANT': '200.0', # Will be ignored
}
expected = {
'stake_amount': 200.05,
'ask_strategy': {
'exit_pricing': {
'price_side': 'bid',
'cccc': 500,
},

View File

@@ -96,7 +96,7 @@ def test_order_dict(default_conf_usdt, mocker, runmode, caplog) -> None:
'stoploss': 'limit',
'stoploss_on_exchange': True,
}
conf['bid_strategy']['price_side'] = 'ask'
conf['entry_pricing']['price_side'] = 'ask'
freqtrade = FreqtradeBot(conf)
if runmode == RunMode.LIVE:
@@ -4052,7 +4052,7 @@ def test_disable_ignore_roi_if_buy_signal(default_conf_usdt, limit_order, limit_
get_fee=fee,
_is_dry_limit_order_filled=MagicMock(return_value=False),
)
default_conf_usdt['ask_strategy'] = {
default_conf_usdt['exit_pricing'] = {
'ignore_roi_if_buy_signal': False
}
freqtrade = FreqtradeBot(default_conf_usdt)
@@ -4432,8 +4432,8 @@ def test_order_book_depth_of_market(
):
ticker_side = 'ask' if is_short else 'bid'
default_conf_usdt['bid_strategy']['check_depth_of_market']['enabled'] = True
default_conf_usdt['bid_strategy']['check_depth_of_market']['bids_to_ask_delta'] = delta
default_conf_usdt['entry_pricing']['check_depth_of_market']['enabled'] = True
default_conf_usdt['entry_pricing']['check_depth_of_market']['bids_to_ask_delta'] = delta
patch_RPCManager(mocker)
patch_exchange(mocker)
mocker.patch('freqtrade.exchange.Exchange.fetch_l2_order_book', order_book_l2)
@@ -4476,8 +4476,8 @@ def test_order_book_depth_of_market(
(False, 0.045, 0.046, 2, None),
(True, 0.042, 0.046, 1, {'bids': [[]], 'asks': [[]]})
])
def test_order_book_bid_strategy1(mocker, default_conf_usdt, order_book_l2, exception_thrown,
ask, last, order_book_top, order_book, caplog) -> None:
def test_order_book_entry_pricing1(mocker, default_conf_usdt, 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
"""
@@ -4489,9 +4489,9 @@ def test_order_book_bid_strategy1(mocker, default_conf_usdt, order_book_l2, exce
fetch_ticker=ticker_usdt_mock,
)
default_conf_usdt['exchange']['name'] = 'binance'
default_conf_usdt['bid_strategy']['use_order_book'] = True
default_conf_usdt['bid_strategy']['order_book_top'] = order_book_top
default_conf_usdt['bid_strategy']['ask_last_balance'] = 0
default_conf_usdt['entry_pricing']['use_order_book'] = True
default_conf_usdt['entry_pricing']['order_book_top'] = order_book_top
default_conf_usdt['entry_pricing']['ask_last_balance'] = 0
default_conf_usdt['telegram']['enabled'] = False
freqtrade = FreqtradeBot(default_conf_usdt)
@@ -4516,17 +4516,17 @@ def test_check_depth_of_market(default_conf_usdt, mocker, order_book_l2) -> None
)
default_conf_usdt['telegram']['enabled'] = False
default_conf_usdt['exchange']['name'] = 'binance'
default_conf_usdt['bid_strategy']['check_depth_of_market']['enabled'] = True
default_conf_usdt['entry_pricing']['check_depth_of_market']['enabled'] = True
# delta is 100 which is impossible to reach. hence function will return false
default_conf_usdt['bid_strategy']['check_depth_of_market']['bids_to_ask_delta'] = 100
default_conf_usdt['entry_pricing']['check_depth_of_market']['bids_to_ask_delta'] = 100
freqtrade = FreqtradeBot(default_conf_usdt)
conf = default_conf_usdt['bid_strategy']['check_depth_of_market']
conf = default_conf_usdt['entry_pricing']['check_depth_of_market']
assert freqtrade._check_depth_of_market('ETH/BTC', conf, side=SignalDirection.LONG) is False
@pytest.mark.parametrize('is_short', [False, True])
def test_order_book_ask_strategy(
def test_order_book_exit_pricing(
default_conf_usdt, limit_buy_order_usdt_open, limit_buy_order_usdt, fee, is_short,
limit_sell_order_usdt_open, mocker, order_book_l2, caplog) -> None:
"""
@@ -4534,8 +4534,8 @@ def test_order_book_ask_strategy(
"""
mocker.patch('freqtrade.exchange.Exchange.fetch_l2_order_book', order_book_l2)
default_conf_usdt['exchange']['name'] = 'binance'
default_conf_usdt['ask_strategy']['use_order_book'] = True
default_conf_usdt['ask_strategy']['order_book_top'] = 1
default_conf_usdt['exit_pricing']['use_order_book'] = True
default_conf_usdt['exit_pricing']['order_book_top'] = 1
default_conf_usdt['telegram']['enabled'] = False
patch_RPCManager(mocker)
patch_exchange(mocker)