# Buggy code
def handle_timedout_limit_buy(self, trade: Trade, order: Dict) -> bool:
    """Buy timeout - cancel order
    :return: True if order was fully cancelled
    """
    reason = "cancelled due to timeout"
    if order["status"] != "canceled":
        corder = self.exchange.cancel_order(trade.open_order_id, trade.pair)
    else:
        # Order was cancelled already, so we can reuse the existing dict
        corder = order
        reason = "canceled on Exchange"

    if corder["remaining"] == corder["amount"]:
        # if trade is not partially completed, just delete the trade
        self.handle_buy_order_full_cancel(trade, reason)
        return True

    # if trade is partially complete, edit the stake details for the trade
    # and close the order
    trade.amount = corder["amount"] - corder["remaining"]
    trade.stake_amount = trade.amount * trade.open_rate
    # verify if fees were taken from amount to avoid problems during selling
    try:
        new_amount = self.get_real_amount(trade, corder, trade.amount)
        if not isclose(order["amount"], new_amount, abs_tol=constants.MATH_CLOSE_PREC):
            trade.amount = new_amount
            # Fee was applied, so set to 0
            trade.fee_open = 0
    except DependencyException as e:
        logger.warning("Could not update trade amount: %s", e)

    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 (KeyError)
2019-11-20 08:25:42,695 - freqtrade.freqtradebot - INFO - Found open order for Trade(id=5, pair=XTZ/BTC, amount=49.08835905, open_rate=0.00014260, open_since=2019-11-20 06:15:50)
2019-11-20 08:25:51,656 - freqtrade.utils - INFO - worker found ... calling exit
2019-11-20 08:25:51,656 - freqtrade.rpc.rpc_manager - INFO - Sending rpc message: {'type': status, 'status': 'process died'}
2019-11-20 08:25:51,714 - freqtrade.freqtradebot - INFO - Cleaning up modules ...
2019-11-20 08:25:51,714 - freqtrade.rpc.rpc_manager - INFO - Cleaning up rpc modules ...
2019-11-20 08:26:13,304 - freqtrade - ERROR - Fatal exception!
Traceback (most recent call last):
File "/home/dri/miner/freqtrade/freqtrade/main.py", line 36, in main
return_code = args['func'](args)
File "/home/dri/miner/freqtrade/freqtrade/utils.py", line 51, in start_trading
worker.run()
File "/home/dri/miner/freqtrade/freqtrade/worker.py", line 71, in run
state = self._worker(old_state=state)
File "/home/dri/miner/freqtrade/freqtrade/worker.py", line 109, in _worker
self._throttle(func=self._process, min_secs=throttle_secs)
File "/home/dri/miner/freqtrade/freqtrade/worker.py", line 122, in _throttle
result = func(*args, **kwargs)
File "/home/dri/miner/freqtrade/freqtrade/worker.py", line 132, in _process
self.freqtrade.process()
File "/home/dri/miner/freqtrade/freqtrade/freqtradebot.py", line 143, in process
self.check_handle_timedout()
File "/home/dri/miner/freqtrade/freqtrade/freqtradebot.py", line 798, in check_handle_timedout
self.handle_timedout_limit_buy(trade, order)
File "/home/dri/miner/freqtrade/freqtrade/freqtradebot.py", line 828, in handle_timedout_limit_buy
if corder['remaining'] == corder['amount']:
KeyError: 'remaining'

# Fixed code
def handle_timedout_limit_buy(self, trade: Trade, order: Dict) -> bool:
    """
    Buy timeout - cancel order
    :return: True if order was fully cancelled
    """
    reason = "cancelled due to timeout"
    if order["status"] != "canceled":
        corder = self.exchange.cancel_order(trade.open_order_id, trade.pair)
    else:
        # Order was cancelled already, so we can reuse the existing dict
        corder = order
        reason = "canceled on Exchange"

    if corder.get("remaining", order["remaining"]) == order["amount"]:
        # if trade is not partially completed, just delete the trade
        self.handle_buy_order_full_cancel(trade, reason)
        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
    # verify if fees were taken from amount to avoid problems during selling
    try:
        new_amount = self.get_real_amount(
            trade, corder if "fee" in corder else order, trade.amount
        )
        if not isclose(order["amount"], new_amount, abs_tol=constants.MATH_CLOSE_PREC):
            trade.amount = new_amount
            # Fee was applied, so set to 0
            trade.fee_open = 0
    except DependencyException as e:
        logger.warning("Could not update trade amount: %s", e)

    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