From e8ef36fb6ed8d984711531313487692eba9669c3 Mon Sep 17 00:00:00 2001 From: hroff-1902 <47309513+hroff-1902@users.noreply.github.com> Date: Wed, 13 Feb 2019 02:55:55 +0300 Subject: [PATCH] execute_buy: do not use ticker if use_order_book:true is set in config This PR corresponds to: https://github.com/freqtrade/freqtrade/issues/1377#issue-386200394 in understanfing that pair Ticker is mostly statistics, but on the other side, create_trade/execute_buy. It resolves problem with some exchanges (BitMex) where ticker structure returned by ccxt does not contain bid and ask values. 1. On exchanges like Bitmex, set use_order_book: true for buys. FT won't request ticker and will use data from order book only. 2. On exchanges where order book is not available, set use_order_book: false, ticker data (including ask/last balance logic) will be used. 3. On other exchanges, either approach may be used in the config. Performance: current implementation fetches ticker every time even if order book data will be later used. With this change it's eliminated. Comparison of order book rate and ticker rate is removed (in order to split fetching order book and ticker completely in execute_buy), so some tests that touch this code may require adjustments. --- freqtrade/freqtradebot.py | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 5413c57f9..4671b6053 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -204,19 +204,11 @@ class FreqtradeBot(object): self.state = State.STOPPED return state_changed - def get_target_bid(self, pair: str, ticker: Dict[str, float]) -> float: + def get_target_bid(self, pair: str) -> float: """ Calculates bid target between current ask price and last price - :param ticker: Ticker to use for getting Ask and Last Price :return: float: Price """ - if ticker['ask'] < ticker['last']: - ticker_rate = ticker['ask'] - else: - balance = self.config['bid_strategy']['ask_last_balance'] - ticker_rate = ticker['ask'] + balance * (ticker['last'] - ticker['ask']) - - used_rate = ticker_rate config_bid_strategy = self.config.get('bid_strategy', {}) if 'use_order_book' in config_bid_strategy and\ config_bid_strategy.get('use_order_book', False): @@ -226,15 +218,16 @@ class FreqtradeBot(object): logger.debug('order_book %s', order_book) # top 1 = index 0 order_book_rate = order_book['bids'][order_book_top - 1][0] - # if ticker has lower rate, then use ticker ( usefull if down trending ) logger.info('...top %s order book buy rate %0.8f', order_book_top, order_book_rate) - if ticker_rate < order_book_rate: - logger.info('...using ticker rate instead %0.8f', ticker_rate) - used_rate = ticker_rate - else: - used_rate = order_book_rate + used_rate = order_book_rate else: logger.info('Using Last Ask / Last Price') + ticker = self.exchange.get_ticker(pair) + if ticker['ask'] < ticker['last']: + ticker_rate = ticker['ask'] + else: + balance = self.config['bid_strategy']['ask_last_balance'] + ticker_rate = ticker['ask'] + balance * (ticker['last'] - ticker['ask']) used_rate = ticker_rate return used_rate @@ -380,7 +373,7 @@ class FreqtradeBot(object): buy_limit_requested = price else: # Calculate amount - buy_limit_requested = self.get_target_bid(pair, self.exchange.get_ticker(pair)) + buy_limit_requested = self.get_target_bid(pair) min_stake_amount = self._get_min_pair_stake_amount(pair_s, buy_limit_requested) if min_stake_amount is not None and min_stake_amount > stake_amount: