# Buggy code
def calculate_fee_rate(self, order: Dict) -> Optional[float]:
    """
    Calculate fee rate if it's not given by the exchange.
    :param order: Order or trade (one trade) dict
    """
    if order["fee"].get("rate") is not None:
        return order["fee"].get("rate")
    fee_curr = order["fee"]["currency"]
    # Calculate fee based on order details
    if fee_curr in self.get_pair_base_currency(order["symbol"]):
        # Base currency - divide by amount
        return round(
            order["fee"]["cost"]
            / safe_value_fallback(order, order, "filled", "amount"),
            8,
        )
    elif fee_curr in self.get_pair_quote_currency(order["symbol"]):
        # Quote currency - divide by cost
        return round(order["fee"]["cost"] / order["cost"], 8)
    else:
        # If Fee currency is a different currency
        try:
            comb = self.get_valid_pair_combination(
                fee_curr, self._config["stake_currency"]
            )
            tick = self.fetch_ticker(comb)

            fee_to_quote_rate = safe_value_fallback(tick, tick, "last", "ask")
            return round((order["fee"]["cost"] * fee_to_quote_rate) / order["cost"], 8)
        except DependencyException:
            return None


# Traceback (ZeroDivisionError)
2020-06-02 15:54:45,752 - freqtrade.persistence - INFO - Found open trade: Trade(id=24, pair=XMR/USD, amount=0.30576364, open_rate=65.41000000, open_since=2020-06-02 15:20:27)
2020-06-02 15:54:45,754 - freqtrade.persistence - INFO - Found open trade: Trade(id=25, pair=ETH/USD, amount=0.08561643, open_rate=233.60000000, open_since=2020-06-02 15:25:31)
2020-06-02 15:54:45,755 - freqtrade.persistence - INFO - Found open trade: Trade(id=26, pair=XRP/USD, amount=100.10511036, open_rate=0.19979000, open_since=2020-06-02 15:35:34)
2020-06-02 15:54:45,756 - freqtrade.persistence - INFO - Found open trade: Trade(id=27, pair=BCH/USD, amount=0.00004993, open_rate=247.40000000, open_since=2020-06-02 15:40:46)
2020-06-02 15:54:45,758 - freqtrade.persistence - INFO - Found open trade: Trade(id=28, pair=LTC/USD, amount=0.44023772, open_rate=45.43000000, open_since=2020-06-02 15:41:04)
2020-06-02 15:54:59,369 - freqtrade.freqtradebot - INFO - Found open order for Trade(id=27, pair=BCH/USD, amount=0.00004993, open_rate=247.40000000, open_since=2020-06-02 15:40:46)
2020-06-02 15:54:59,370 - freqtrade.commands.trade_commands - ERROR - float division by zero
2020-06-02 15:54:59,371 - freqtrade.commands.trade_commands - ERROR - Fatal exception!
Traceback (most recent call last):
File "/freqtrade/freqtrade/commands/trade_commands.py", line 20, in start_trading
worker.run()
File "/freqtrade/freqtrade/worker.py", line 73, in run
state = self._worker(old_state=state)
File "/freqtrade/freqtrade/worker.py", line 107, in _worker
self._throttle(func=self._process_running, throttle_secs=self._throttle_secs)
File "/freqtrade/freqtrade/worker.py", line 128, in _throttle
result = func(*args, **kwargs)
File "/freqtrade/freqtrade/worker.py", line 141, in _process_running
self.freqtrade.process()
File "/freqtrade/freqtrade/freqtradebot.py", line 156, in process
self.check_handle_timedout()
File "/freqtrade/freqtrade/freqtradebot.py", line 894, in check_handle_timedout
fully_cancelled = self.update_trade_state(trade, order)
File "/freqtrade/freqtrade/freqtradebot.py", line 1207, in update_trade_state
new_amount = self.get_real_amount(trade, order, order_amount)
File "/freqtrade/freqtrade/freqtradebot.py", line 1262, in get_real_amount
fee_cost, fee_currency, fee_rate = self.exchange.extract_cost_curr_rate(order)
File "/freqtrade/freqtrade/exchange/exchange.py", line 1128, in extract_cost_curr_rate
self.calculate_fee_rate(order))
File "/freqtrade/freqtrade/exchange/exchange.py", line 1107, in calculate_fee_rate
return round(order['fee']['cost'] / order['cost'], 8)
ZeroDivisionError: float division by zero

# Fixed code
def calculate_fee_rate(self, order: Dict) -> Optional[float]:
    """
    Calculate fee rate if it's not given by the exchange.
    :param order: Order or trade (one trade) dict
    """
    if order["fee"].get("rate") is not None:
        return order["fee"].get("rate")
    fee_curr = order["fee"]["currency"]
    # Calculate fee based on order details
    if fee_curr in self.get_pair_base_currency(order["symbol"]):
        # Base currency - divide by amount
        return round(
            order["fee"]["cost"]
            / safe_value_fallback(order, order, "filled", "amount"),
            8,
        )
    elif fee_curr in self.get_pair_quote_currency(order["symbol"]):
        # Quote currency - divide by cost
        return round(order["fee"]["cost"] / order["cost"], 8) if order["cost"] else None
    else:
        # If Fee currency is a different currency
        if not order["cost"]:
            # If cost is None or 0.0 -> falsy, return None
            return None
        try:
            comb = self.get_valid_pair_combination(
                fee_curr, self._config["stake_currency"]
            )
            tick = self.fetch_ticker(comb)

            fee_to_quote_rate = safe_value_fallback(tick, tick, "last", "ask")
            return round((order["fee"]["cost"] * fee_to_quote_rate) / order["cost"], 8)
        except DependencyException:
            return None