# Buggy code
def get_buy_rate(self, pair: str, refresh: bool) -> float:
    """
    Calculates bid target between current ask price and last price
    :param pair: Pair to get rate for
    :param refresh: allow cached data
    :return: float: Price
    """
    if not refresh:
        rate = self._buy_rate_cache.get(pair)
        # Check if cache has been invalidated
        if rate:
            logger.info(f"Using cached buy rate for {pair}.")
            return rate

    bid_strategy = self.config.get("bid_strategy", {})
    if "use_order_book" in bid_strategy and bid_strategy.get("use_order_book", False):
        logger.info(
            f"Getting price from order book {bid_strategy['price_side'].capitalize()} side."
        )
        order_book_top = bid_strategy.get("order_book_top", 1)
        order_book = self.exchange.get_order_book(pair, order_book_top)
        logger.debug("order_book %s", order_book)
        # top 1 = index 0
        order_book_rate = order_book[f"{bid_strategy['price_side']}s"][
            order_book_top - 1
        ][0]
        logger.info(
            f"...top {order_book_top} order book buy rate {order_book_rate:.8f}"
        )
        used_rate = order_book_rate
    else:
        logger.info(
            f"Using Last {bid_strategy['price_side'].capitalize()} / Last Price"
        )
        ticker = self.exchange.fetch_ticker(pair)
        ticker_rate = ticker[bid_strategy["price_side"]]
        if ticker["last"] and ticker_rate > ticker["last"]:
            balance = self.config["bid_strategy"]["ask_last_balance"]
            ticker_rate = ticker_rate + balance * (ticker["last"] - ticker_rate)
        used_rate = ticker_rate

    self._buy_rate_cache[pair] = used_rate

    return used_rate


# Traceback (IndexError)
2020-05-23 16:03:03,076 - freqtrade - ERROR - Fatal exception!
Traceback (most recent call last):
File "/freqtrade/freqtrade/main.py", line 36, in main
return_code = args['func'](args)
File "/freqtrade/freqtrade/commands/trade_commands.py", line 20, in start_trading
worker.run()
File "/freqtrade/freqtrade/worker.py", line 66, in run
state = self._worker(old_state=state)
File "/freqtrade/freqtrade/worker.py", line 104, in _worker
self._throttle(func=self._process_running, throttle_secs=self._throttle_secs)
File "/freqtrade/freqtrade/worker.py", line 125, in _throttle
result = func(*args, **kwargs)
File "/freqtrade/freqtrade/worker.py", line 139, in _process_running
self.freqtrade.process()
File "/freqtrade/freqtrade/freqtradebot.py", line 157, in process
self.exit_positions(trades)
File "/freqtrade/freqtrade/freqtradebot.py", line 613, in exit_positions
if trade.open_order_id is None and self.handle_trade(trade):
File "/freqtrade/freqtrade/freqtradebot.py", line 694, in handle_trade
sell_rate = next(order_book)
File "/freqtrade/freqtrade/freqtradebot.py", line 632, in _order_book_gen
yield order_book[side][i - 1][0]
IndexError: list index out of range
2020-05-23 16:03:13,989 - freqtrade.loggers - INFO - Verbosity set to 0

# Fixed code
def get_buy_rate(self, pair: str, refresh: bool) -> float:
    """
    Calculates bid target between current ask price and last price
    :param pair: Pair to get rate for
    :param refresh: allow cached data
    :return: float: Price
    """
    if not refresh:
        rate = self._buy_rate_cache.get(pair)
        # Check if cache has been invalidated
        if rate:
            logger.info(f"Using cached buy rate for {pair}.")
            return rate

    bid_strategy = self.config.get("bid_strategy", {})
    if "use_order_book" in bid_strategy and bid_strategy.get("use_order_book", False):
        logger.info(
            f"Getting price from order book {bid_strategy['price_side'].capitalize()} side."
        )
        order_book_top = bid_strategy.get("order_book_top", 1)
        order_book = self.exchange.fetch_l2_order_book(pair, order_book_top)
        logger.debug("order_book %s", order_book)
        # top 1 = index 0
        try:
            rate_from_l2 = order_book[f"{bid_strategy['price_side']}s"][
                order_book_top - 1
            ][0]
        except (IndexError, KeyError) as e:
            logger.warning(
                "Buy Price from orderbook could not be determined."
                f"Orderbook: {order_book}"
            )
            raise PricingError from e
        logger.info(f"...top {order_book_top} order book buy rate {rate_from_l2:.8f}")
        used_rate = rate_from_l2
    else:
        logger.info(
            f"Using Last {bid_strategy['price_side'].capitalize()} / Last Price"
        )
        ticker = self.exchange.fetch_ticker(pair)
        ticker_rate = ticker[bid_strategy["price_side"]]
        if ticker["last"] and ticker_rate > ticker["last"]:
            balance = self.config["bid_strategy"]["ask_last_balance"]
            ticker_rate = ticker_rate + balance * (ticker["last"] - ticker_rate)
        used_rate = ticker_rate

    self._buy_rate_cache[pair] = used_rate

    return used_rate