# Buggy code
def handle_timedout_limit_buy(self, trade: Trade, order: Dict) -> bool:
    """
    Buy timeout - cancel order
    :return: True if order was fully cancelled
    """
    if order["status"] != "canceled":
        reason = "cancelled due to timeout"
        corder = self.exchange.cancel_order(trade.open_order_id, trade.pair)
        # Some exchanges don't return a dict here.
        if not isinstance(corder, dict):
            corder = {}
        logger.info("Buy order %s for %s.", reason, trade)
    else:
        # Order was cancelled already, so we can reuse the existing dict
        corder = order
        reason = "cancelled on exchange"
        logger.info("Buy order %s for %s.", reason, trade)

    if corder.get("remaining", order["remaining"]) == order["amount"]:
        # if trade is not partially completed, just delete the trade
        Trade.session.delete(trade)
        Trade.session.flush()
        return True

    # if trade is partially complete, edit the stake details for the trade
    # and close the order
    # cancel_order may not contain the full order dict, so we need to fallback
    # to the order dict aquired before cancelling.
    # we need to fall back to the values from order if corder does not contain these keys.
    trade.amount = order["amount"] - corder.get("remaining", order["remaining"])
    trade.stake_amount = trade.amount * trade.open_rate
    self.update_trade_state(trade, corder if "fee" in corder else order, trade.amount)

    trade.open_order_id = None
    logger.info("Partial buy order timeout for %s.", trade)
    self.rpc.send_msg(
        {
            "type": RPCMessageType.STATUS_NOTIFICATION,
            "status": f"Remaining buy order for {trade.pair} cancelled due to timeout",
        }
    )
    return False


# Traceback (TypeError)
2020-04-01 18:20:37,872 - freqtrade.freqtradebot - INFO - Buy order cancelled due to timeout for Trade(id=23, pair=HBAR/USD, amount=1286.13227870, open_rate=0.03110100, open_since=2020-04-01 16:10:35).
2020-04-01 18:20:37,873 - freqtrade.commands.trade_commands - INFO - worker found ... calling exit
2020-04-01 18:20:37,873 - freqtrade.rpc.rpc_manager - INFO - Sending rpc message: {'type': status, 'status': 'process died'}
2020-04-01 18:20:37,937 - freqtrade.freqtradebot - INFO - Cleaning up modules ...
2020-04-01 18:20:37,938 - freqtrade.rpc.rpc_manager - INFO - Cleaning up rpc modules ...
2020-04-01 18:21:05,174 - freqtrade - ERROR - Fatal exception!
Traceback (most recent call last):
File "/root/ft-bt-l-hbar-usd-Strategy005/freqtrade/main.py", line 36, in main
return_code = args['func'](args)
File "/root/ft-bt-l-hbar-usd-Strategy005/freqtrade/commands/trade_commands.py", line 20, in start_trading
worker.run()
File "/root/ft-bt-l-hbar-usd-Strategy005/freqtrade/worker.py", line 66, in run
state = self._worker(old_state=state)
File "/root/ft-bt-l-hbar-usd-Strategy005/freqtrade/worker.py", line 104, in _worker
self._throttle(func=self._process_running, throttle_secs=self._throttle_secs)
File "/root/ft-bt-l-hbar-usd-Strategy005/freqtrade/worker.py", line 125, in _throttle
result = func(*args, **kwargs)
File "/root/ft-bt-l-hbar-usd-Strategy005/freqtrade/worker.py", line 139, in _process_running
self.freqtrade.process()
File "/root/ft-bt-l-hbar-usd-Strategy005/freqtrade/freqtradebot.py", line 158, in process
self.check_handle_timedout()
File "/root/ft-bt-l-hbar-usd-Strategy005/freqtrade/freqtradebot.py", line 876, in check_handle_timedout
self.handle_timedout_limit_buy(trade, order)
File "/root/ft-bt-l-hbar-usd-Strategy005/freqtrade/freqtradebot.py", line 917, in handle_timedout_limit_buy
trade.amount = order['amount'] - corder.get('remaining', order['remaining'])
TypeError: unsupported operand type(s) for -: 'float' and 'NoneType'

# Fixed code
def handle_timedout_limit_buy(self, trade: Trade, order: Dict) -> bool:
    """
    Buy timeout - cancel order
    :return: True if order was fully cancelled
    """
    if order["status"] != "canceled":
        reason = "cancelled due to timeout"
        try:
            corder = self.exchange.cancel_order(trade.open_order_id, trade.pair)
            # Some exchanges don't return a dict here.
            if not isinstance(corder, dict):
                corder = {}
            logger.info("Buy order %s for %s.", reason, trade)
        except InvalidOrderException:
            corder = {}
            logger.exception(
                f"Could not cancel buy order {trade.open_order_id} for pair {trade.pair}"
            )
    else:
        # Order was cancelled already, so we can reuse the existing dict
        corder = order
        reason = "cancelled on exchange"
        logger.info("Buy order %s for %s.", reason, trade)

    if safe_value_fallback(corder, order, "remaining", "remaining") == order["amount"]:
        logger.info("Buy order fully cancelled. Removing %s from database.", trade)
        # if trade is not partially completed, just delete the trade
        Trade.session.delete(trade)
        Trade.session.flush()
        return True

    # if trade is partially complete, edit the stake details for the trade
    # and close the order
    # cancel_order may not contain the full order dict, so we need to fallback
    # to the order dict aquired before cancelling.
    # we need to fall back to the values from order if corder does not contain these keys.
    trade.amount = order["amount"] - safe_value_fallback(
        corder, order, "remaining", "remaining"
    )
    trade.stake_amount = trade.amount * trade.open_rate
    self.update_trade_state(trade, corder if "fee" in corder else order, trade.amount)

    trade.open_order_id = None
    logger.info("Partial buy order timeout for %s.", trade)
    self.rpc.send_msg(
        {
            "type": RPCMessageType.STATUS_NOTIFICATION,
            "status": f"Remaining buy order for {trade.pair} cancelled due to timeout",
        }
    )
    return False