# Buggy code
def check_handle_timedout(self) -> None:
    """
    Check if any orders are timed out and cancel if neccessary
    :param timeoutvalue: Number of minutes until order is considered timed out
    :return: None
    """

    for trade in Trade.get_open_order_trades():
        try:
            if not trade.open_order_id:
                continue
            order = self.exchange.get_order(trade.open_order_id, trade.pair)
        except (RequestException, DependencyException, InvalidOrderException):
            logger.info(
                "Cannot query order for %s due to %s", trade, traceback.format_exc()
            )
            continue

        # Check if trade is still actually open
        if float(order["remaining"]) == 0.0:
            self.wallets.update()
            continue

        if (order["side"] == "buy" and order["status"] == "canceled") or (
            self._check_timed_out("buy", order)
        ):
            self.handle_timedout_limit_buy(trade, order)
            self.wallets.update()

        elif (order["side"] == "sell" and order["status"] == "canceled") or (
            self._check_timed_out("sell", order)
        ):
            self.handle_timedout_limit_sell(trade, order)
            self.wallets.update()


# 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 check_handle_timedout(self) -> None:
    """
    Check if any orders are timed out and cancel if neccessary
    :param timeoutvalue: Number of minutes until order is considered timed out
    :return: None
    """

    for trade in Trade.get_open_order_trades():
        try:
            if not trade.open_order_id:
                continue
            order = self.exchange.get_order(trade.open_order_id, trade.pair)
        except (RequestException, DependencyException, InvalidOrderException):
            logger.info(
                "Cannot query order for %s due to %s", trade, traceback.format_exc()
            )
            continue

        # Check if trade is still actually open
        if float(order.get("remaining", 0.0)) == 0.0:
            self.wallets.update()
            continue

        if (order["side"] == "buy" and order["status"] == "canceled") or (
            self._check_timed_out("buy", order)
        ):
            self.handle_timedout_limit_buy(trade, order)
            self.wallets.update()

        elif (order["side"] == "sell" and order["status"] == "canceled") or (
            self._check_timed_out("sell", order)
        ):
            self.handle_timedout_limit_sell(trade, order)
            self.wallets.update()