This commit is contained in:
Nullart2 2018-08-07 18:29:37 +08:00
parent 1309c2b14f
commit c9c0e108ab
3 changed files with 51 additions and 27 deletions

View File

@ -411,15 +411,23 @@ class Exchange(object):
@retrier
def get_order_book(self, pair: str, limit: int = 100) -> dict:
"""
get order book level 2 from exchange
Notes:
20180619: bittrex doesnt support limits -.-
20180619: binance support limits but only on specific range
"""
try:
# 20180619: bittrex doesnt support limits -.-
# 20180619: binance support limits but only on specific range
if self._api.name == 'Binance':
limit_range = [5, 10, 20, 50, 100, 500, 1000]
for limitx in limit_range:
if limit <= limitx:
limit = limitx
break
# get next-higher step in the limit_range list
limit = min(list(filter(lambda x: limit <= x, limit_range)))
# above script works like loop below (but with slightly better performance):
# for limitx in limit_range:
# if limit <= limitx:
# limit = limitx
# break
return self._api.fetch_l2_order_book(pair, limit)
except ccxt.NotSupported as e:

View File

@ -268,10 +268,6 @@ class FreqtradeBot(object):
return used_rate
def _trunc_num(self, f, n):
import math
return math.floor(f * 10 ** n) / 10 ** n
def _get_trade_stake_amount(self) -> Optional[float]:
"""
Check if stake amount can be fulfilled with the available balance
@ -366,20 +362,18 @@ class FreqtradeBot(object):
get('check_depth_of_market', {})
if (experimental_check_depth_of_market.get('enabled', False)) and\
(experimental_check_depth_of_market.get('bids_to_ask_delta', 0) > 0):
if self._check_depth_of_market_buy(_pair):
if self._check_depth_of_market_buy(_pair, experimental_check_depth_of_market):
return self.execute_buy(_pair, stake_amount)
else:
return False
return self.execute_buy(_pair, stake_amount)
return False
def _check_depth_of_market_buy(self, pair: str) -> bool:
def _check_depth_of_market_buy(self, pair: str, conf: Dict) -> bool:
"""
Checks depth of market before executing a buy
"""
experimental_check_depth_of_market = self.config.get('experimental', {}).\
get('check_depth_of_market', {})
conf_bids_to_ask_delta = experimental_check_depth_of_market.get('bids_to_ask_delta', 0)
conf_bids_to_ask_delta = conf.get('bids_to_ask_delta', 0)
logger.info('checking depth of market for %s', pair)
order_book = self.exchange.get_order_book(pair, 1000)
order_book_data_frame = order_book_to_dataframe(order_book['bids'], order_book['asks'])
@ -387,8 +381,7 @@ class FreqtradeBot(object):
order_book_asks = order_book_data_frame['a_size'].sum()
bids_ask_delta = order_book_bids / order_book_asks
logger.info('bids: %s, asks: %s, delta: %s', order_book_bids,
order_book_asks,
order_book_bids / order_book_asks)
order_book_asks, bids_ask_delta)
if bids_ask_delta >= conf_bids_to_ask_delta:
return True
return False

View File

@ -159,6 +159,15 @@ def test_gen_pair_whitelist(mocker, default_conf, tickers) -> None:
assert whitelist == []
def test_gen_pair_whitelist_not_supported(mocker, default_conf, tickers) -> None:
freqtrade = get_patched_freqtradebot(mocker, default_conf)
mocker.patch('freqtrade.exchange.Exchange.get_tickers', tickers)
mocker.patch('freqtrade.exchange.Exchange.exchange_has', MagicMock(return_value=False))
with pytest.raises(OperationalException):
freqtrade._gen_pair_whitelist(base_currency='BTC')
@pytest.mark.skip(reason="Test not implemented")
def test_refresh_whitelist() -> None:
pass
@ -1914,6 +1923,7 @@ def test_order_book_depth_of_market(default_conf, ticker, limit_buy_order, fee,
def test_order_book_depth_of_market_high_delta(default_conf, ticker,
limit_buy_order, fee, markets, mocker):
default_conf['experimental']['check_depth_of_market']['enabled'] = True
# delta is 100 which is impossible to reach. hence check_depth_of_market will return false
default_conf['experimental']['check_depth_of_market']['bids_to_ask_delta'] = 100
patch_RPCManager(mocker)
mocker.patch.multiple(
@ -1924,7 +1934,6 @@ def test_order_book_depth_of_market_high_delta(default_conf, ticker,
get_fee=fee,
get_markets=markets
)
# Save state of current whitelist
freqtrade = FreqtradeBot(default_conf)
patch_get_signal(freqtrade)
@ -1934,33 +1943,47 @@ def test_order_book_depth_of_market_high_delta(default_conf, ticker,
assert trade is None
def test_order_book_bid_strategy(default_conf) -> None:
def test_order_book_bid_strategy1(default_conf) -> None:
"""
test if function get_target_bid will return the order book price
instead of the ask rate
"""
default_conf['exchange']['name'] = 'binance'
default_conf['experimental']['bid_strategy']['use_order_book'] = True
default_conf['experimental']['bid_strategy']['order_book_top'] = 2
default_conf['bid_strategy']['ask_last_balance'] = 0
default_conf['telegram']['enabled'] = False
freqtrade = FreqtradeBot(default_conf)
assert freqtrade.get_target_bid('BTC/USDT', {'ask': 200000, 'last': 200000}) != 200000
def test_order_book_bid_strategy2(default_conf) -> None:
"""
test if function get_target_bid will return ask rate instead
of the order book rate
"""
default_conf['exchange']['name'] = 'binance'
default_conf['experimental']['bid_strategy']['use_order_book'] = True
default_conf['experimental']['bid_strategy']['order_book_top'] = 1
default_conf['bid_strategy']['ask_last_balance'] = 0
default_conf['telegram']['enabled'] = False
freqtrade = FreqtradeBot(default_conf)
assert freqtrade.get_target_bid('BTC/USDT', {'ask': 2, 'last': 2}) == 2
def test_trunc_num(default_conf) -> None:
default_conf['telegram']['enabled'] = False
freqtrade = FreqtradeBot(default_conf)
assert freqtrade._trunc_num(10.1111, 2) == 10.11
def test_check_depth_of_market_buy(default_conf) -> None:
default_conf['telegram']['enabled'] = False
default_conf['exchange']['name'] = 'binance'
default_conf['experimental']['check_depth_of_market']['enabled'] = True
# delta is 100 which is impossible to reach. hence function will return false
default_conf['experimental']['check_depth_of_market']['bids_to_ask_delta'] = 100
freqtrade = FreqtradeBot(default_conf)
assert freqtrade._check_depth_of_market_buy('ETH/BTC') is False
conf = default_conf['experimental']['check_depth_of_market']
assert freqtrade._check_depth_of_market_buy('ETH/BTC', conf) is False
def test_order_book_ask_strategy(default_conf, limit_buy_order, limit_sell_order,