From b4d6250d55e76f678c231b72613ad9d4f0e78f78 Mon Sep 17 00:00:00 2001 From: Stephen Dade Date: Wed, 3 Jan 2018 14:30:10 +1100 Subject: [PATCH 1/3] Added order timeout handling --- README.md | 2 + config.json.example | 1 + freqtrade/main.py | 50 ++++++++++- freqtrade/misc.py | 1 + freqtrade/tests/conftest.py | 1 + freqtrade/tests/test_main.py | 160 ++++++++++++++++++++++++++++++++++- 6 files changed, 213 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 045d6b624..b6c7043f8 100644 --- a/README.md +++ b/README.md @@ -160,6 +160,7 @@ optional arguments: "tradesv3.dry_run.sqlite" instead of memory DB. Work only if dry_run is enabled. ``` + More details on: - [How to run the bot](https://github.com/gcarq/freqtrade/blob/develop/docs/bot-usage.md#bot-commands) - [How to use Backtesting](https://github.com/gcarq/freqtrade/blob/develop/docs/bot-usage.md#backtesting-commands) @@ -196,3 +197,4 @@ To run this bot we recommend you a cloud instance with a minimum of: - [TA-Lib](https://mrjbq7.github.io/ta-lib/install.html) - [virtualenv](https://virtualenv.pypa.io/en/stable/installation/) (Recommended) - [Docker](https://www.docker.com/products/docker) (Recommended) + diff --git a/config.json.example b/config.json.example index e5cb2fbfa..3678ccd8c 100644 --- a/config.json.example +++ b/config.json.example @@ -11,6 +11,7 @@ "0": 0.04 }, "stoploss": -0.10, + "opentradetimeout": 600, "bid_strategy": { "ask_last_balance": 0.0 }, diff --git a/freqtrade/main.py b/freqtrade/main.py index 08d42c379..cd19af008 100755 --- a/freqtrade/main.py +++ b/freqtrade/main.py @@ -5,7 +5,7 @@ import logging import sys import time import traceback -from datetime import datetime +from datetime import datetime, timedelta from typing import Dict, Optional, List import requests @@ -98,6 +98,10 @@ def _process(nb_assets: Optional[int] = 0) -> bool: # Check if we can sell our current pair state_changed = handle_trade(trade) or state_changed + if 'opentradetimeout' in _CONF and trade.open_order_id: + # Check and handle any timed out trades + check_handle_timedout(trade) + Trade.session.flush() except (requests.exceptions.RequestException, json.JSONDecodeError) as error: logger.warning( @@ -115,6 +119,50 @@ def _process(nb_assets: Optional[int] = 0) -> bool: return state_changed +def check_handle_timedout(trade: Trade) -> bool: + """ + Check if a trade is timed out and cancel if neccessary + :param trade: Trade instance + :return: True if the trade is timed out, false otherwise + """ + timeoutthreashold = datetime.utcnow() - timedelta(minutes=_CONF['opentradetimeout']) + order = exchange.get_order(trade.open_order_id) + + if trade.open_date < timeoutthreashold: + # Buy timeout - cancel order + exchange.cancel_order(trade.open_order_id) + if order['remaining'] == order['amount']: + # if trade is not partially completed, just delete the trade + Trade.session.delete(trade) + Trade.session.flush() + logger.info('Buy order timeout for %s.', trade) + else: + # if trade is partially complete, edit the stake details for the trade + # and close the order + trade.amount = order['amount'] - order['remaining'] + trade.stake_amount = trade.amount * trade.open_rate + trade.open_order_id = None + logger.info('Partial buy order timeout for %s.', trade) + return True + elif trade.close_date is not None and trade.close_date < timeoutthreashold: + # Sell timeout - cancel order and update trade + if order['remaining'] == order['amount']: + # if trade is not partially completed, just cancel the trade + exchange.cancel_order(trade.open_order_id) + trade.close_rate = None + trade.close_profit = None + trade.close_date = None + trade.is_open = True + trade.open_order_id = None + logger.info('Sell order timeout for %s.', trade) + return True + else: + # TODO: figure out how to handle partially complete sell orders + return False + else: + return False + + def execute_sell(trade: Trade, limit: float) -> None: """ Executes a limit sell for the given trade and limit diff --git a/freqtrade/misc.py b/freqtrade/misc.py index 8364863f1..c8f1dcbcd 100644 --- a/freqtrade/misc.py +++ b/freqtrade/misc.py @@ -218,6 +218,7 @@ CONF_SCHEMA = { 'minProperties': 1 }, 'stoploss': {'type': 'number', 'maximum': 0, 'exclusiveMaximum': True}, + 'opentradetimeout': {'type': 'integer', 'minimum': 0}, 'bid_strategy': { 'type': 'object', 'properties': { diff --git a/freqtrade/tests/conftest.py b/freqtrade/tests/conftest.py index c8ecd39c7..857f46f33 100644 --- a/freqtrade/tests/conftest.py +++ b/freqtrade/tests/conftest.py @@ -25,6 +25,7 @@ def default_conf(): "0": 0.04 }, "stoploss": -0.10, + "opentradetimeout": 600, "bid_strategy": { "ask_last_balance": 0.0 }, diff --git a/freqtrade/tests/test_main.py b/freqtrade/tests/test_main.py index 09b3460c1..3ba62dbea 100644 --- a/freqtrade/tests/test_main.py +++ b/freqtrade/tests/test_main.py @@ -5,13 +5,14 @@ from unittest.mock import MagicMock import pytest import requests import logging +from datetime import timedelta, datetime from sqlalchemy import create_engine from freqtrade import DependencyException, OperationalException from freqtrade.analyze import SignalType from freqtrade.exchange import Exchanges from freqtrade.main import create_trade, handle_trade, init, \ - get_target_bid, _process, execute_sell + get_target_bid, _process, execute_sell, check_handle_timedout from freqtrade.misc import get_state, State from freqtrade.persistence import Trade @@ -318,6 +319,163 @@ def test_close_trade(default_conf, ticker, limit_buy_order, limit_sell_order, mo handle_trade(trade) +def test_check_handle_timedout(default_conf, ticker, health, mocker): + mocker.patch.dict('freqtrade.main._CONF', default_conf) + cancel_order_mock = MagicMock() + mocker.patch.multiple('freqtrade.rpc', init=MagicMock(), send_msg=MagicMock()) + mocker.patch.multiple('freqtrade.main.exchange', + validate_pairs=MagicMock(), + get_ticker=ticker, + get_order=MagicMock(return_value={ + 'closed': None, + 'type': 'LIMIT_BUY', + 'remaining': 1.0, + 'amount': 1.0, + }), + cancel_order=cancel_order_mock) + init(default_conf, create_engine('sqlite://')) + + tradeBuy = Trade( + pair='BTC_ETH', + open_rate=1, + exchange='BITTREX', + open_order_id='123456789', + amount=1, + fee=0.0, + stake_amount=1, + open_date=datetime.utcnow(), + is_open=True + ) + + tradeSell = Trade( + pair='BTC_BCC', + open_rate=1, + exchange='BITTREX', + open_order_id='678901234', + amount=1, + fee=0.0, + stake_amount=1, + open_date=datetime.utcnow(), + close_date=datetime.utcnow(), + is_open=True + ) + + Trade.session.add(tradeBuy) + Trade.session.add(tradeSell) + + # check it doesn't cancel any buy trades under the time limit + ret = check_handle_timedout(tradeBuy) + assert ret is False + assert cancel_order_mock.call_count == 0 + trades = Trade.query.filter(Trade.open_order_id.is_(tradeBuy.open_order_id)).all() + assert len(trades) == 1 + + # change the trade open datetime to 601 minutes in the past + tradeBuy.open_date = datetime.utcnow() - timedelta(minutes=601) + + # check it does cancel buy orders over the time limit + ret = check_handle_timedout(tradeBuy) + assert ret is True + assert cancel_order_mock.call_count == 1 + trades = Trade.query.filter(Trade.open_order_id.is_(tradeBuy.open_order_id)).all() + assert len(trades) == 0 + + # check it doesn't cancel any sell trades under the time limit + ret = check_handle_timedout(tradeSell) + assert ret is False + assert cancel_order_mock.call_count == 1 + assert tradeSell.is_open is True + + # change the trade close datetime to 601 minutes in the past + tradeSell.close_date = datetime.utcnow() - timedelta(minutes=601) + + # check it does cancel sell orders over the time limit + ret = check_handle_timedout(tradeSell) + assert ret is True + assert cancel_order_mock.call_count == 2 + assert tradeSell.is_open is True + + +def test_check_handle_timedout_partial(default_conf, ticker, health, mocker): + mocker.patch.dict('freqtrade.main._CONF', default_conf) + cancel_order_mock = MagicMock() + mocker.patch.multiple('freqtrade.rpc', init=MagicMock(), send_msg=MagicMock()) + mocker.patch.multiple('freqtrade.main.exchange', + validate_pairs=MagicMock(), + get_ticker=ticker, + get_order=MagicMock(return_value={ + 'closed': None, + 'type': 'LIMIT_BUY', + 'remaining': 0.5, + 'amount': 1.0, + }), + cancel_order=cancel_order_mock) + init(default_conf, create_engine('sqlite://')) + + tradeBuy = Trade( + pair='BTC_ETH', + open_rate=1, + exchange='BITTREX', + open_order_id='123456789', + amount=1, + fee=0.0, + stake_amount=1, + open_date=datetime.utcnow(), + is_open=True + ) + + tradeSell = Trade( + pair='BTC_BCC', + open_rate=1, + exchange='BITTREX', + open_order_id='678901234', + amount=1, + fee=0.0, + stake_amount=1, + open_date=datetime.utcnow(), + close_date=datetime.utcnow(), + is_open=True + ) + + Trade.session.add(tradeBuy) + Trade.session.add(tradeSell) + + # check it doesn't cancel any buy trades under the time limit + ret = check_handle_timedout(tradeBuy) + assert ret is False + assert cancel_order_mock.call_count == 0 + trades = Trade.query.filter(Trade.open_order_id.is_(tradeBuy.open_order_id)).all() + assert len(trades) == 1 + + # change the trade open datetime to 601 minutes in the past + tradeBuy.open_date = datetime.utcnow() - timedelta(minutes=601) + + # check it does cancel buy orders over the time limit + # note this is for a partially-complete buy order + ret = check_handle_timedout(tradeBuy) + assert ret is True + assert cancel_order_mock.call_count == 1 + trades = Trade.query.filter(Trade.open_order_id.is_(tradeBuy.open_order_id)).all() + assert len(trades) == 1 + assert trades[0].amount == 0.5 + assert trades[0].stake_amount == 0.5 + + # check it doesn't cancel any sell trades under the time limit + ret = check_handle_timedout(tradeSell) + assert ret is False + assert cancel_order_mock.call_count == 1 + assert tradeSell.is_open is True + + # change the trade close datetime to 601 minutes in the past + tradeSell.close_date = datetime.utcnow() - timedelta(minutes=601) + + # check it does not cancel partially-complete sell orders over the time limit + ret = check_handle_timedout(tradeSell) + assert ret is False + assert cancel_order_mock.call_count == 1 + assert tradeSell.open_order_id is not None + + def test_balance_fully_ask_side(mocker): mocker.patch.dict('freqtrade.main._CONF', {'bid_strategy': {'ask_last_balance': 0.0}}) assert get_target_bid({'ask': 20, 'last': 10}) == 20 From 7169ad557f36b4bc92359f488beeffeb28691aa2 Mon Sep 17 00:00:00 2001 From: Stephen Dade Date: Wed, 3 Jan 2018 21:24:42 +1100 Subject: [PATCH 2/3] Correct documentation for opentradetimeout --- README.md | 2 -- docs/configuration.md | 1 + 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index b6c7043f8..045d6b624 100644 --- a/README.md +++ b/README.md @@ -160,7 +160,6 @@ optional arguments: "tradesv3.dry_run.sqlite" instead of memory DB. Work only if dry_run is enabled. ``` - More details on: - [How to run the bot](https://github.com/gcarq/freqtrade/blob/develop/docs/bot-usage.md#bot-commands) - [How to use Backtesting](https://github.com/gcarq/freqtrade/blob/develop/docs/bot-usage.md#backtesting-commands) @@ -197,4 +196,3 @@ To run this bot we recommend you a cloud instance with a minimum of: - [TA-Lib](https://mrjbq7.github.io/ta-lib/install.html) - [virtualenv](https://virtualenv.pypa.io/en/stable/installation/) (Recommended) - [Docker](https://www.docker.com/products/docker) (Recommended) - diff --git a/docs/configuration.md b/docs/configuration.md index bfd169aea..6dcb34da7 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -21,6 +21,7 @@ The table below will list all configuration parameters. | `dry_run` | true | Yes | Define if the bot must be in Dry-run or production mode. | `minimal_roi` | See below | Yes | Set the threshold in percent the bot will use to sell a trade. More information below. | `stoploss` | -0.10 | No | Value of the stoploss in percent used by the bot. More information below. +| `opentradetimeout` | 0 | No | The number of minutes until an open trade will be cancelled. | `bid_strategy.ask_last_balance` | 0.0 | Yes | Set the bidding price. More information below. | `exchange.name` | bittrex | Yes | Name of the exchange class to use. | `exchange.key` | key | No | API key to use for the exchange. Only required when you are in production mode. From b5d2cfecc76ab184faf540836ed61876726766fe Mon Sep 17 00:00:00 2001 From: Stephen Dade Date: Thu, 4 Jan 2018 10:35:57 +1100 Subject: [PATCH 3/3] Unfilled Order timeout - better documentation and variable naming --- config.json.example | 2 +- docs/configuration.md | 2 +- freqtrade/main.py | 4 ++-- freqtrade/misc.py | 2 +- freqtrade/tests/conftest.py | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/config.json.example b/config.json.example index 3678ccd8c..c68e854e2 100644 --- a/config.json.example +++ b/config.json.example @@ -11,7 +11,7 @@ "0": 0.04 }, "stoploss": -0.10, - "opentradetimeout": 600, + "unfilledtimeout": 600, "bid_strategy": { "ask_last_balance": 0.0 }, diff --git a/docs/configuration.md b/docs/configuration.md index 6dcb34da7..384775765 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -21,7 +21,7 @@ The table below will list all configuration parameters. | `dry_run` | true | Yes | Define if the bot must be in Dry-run or production mode. | `minimal_roi` | See below | Yes | Set the threshold in percent the bot will use to sell a trade. More information below. | `stoploss` | -0.10 | No | Value of the stoploss in percent used by the bot. More information below. -| `opentradetimeout` | 0 | No | The number of minutes until an open trade will be cancelled. +| `unfilledtimeout` | 0 | No | How long (in minutes) the bot will wait for an unfilled order to complete, after which the order will be cancelled. | `bid_strategy.ask_last_balance` | 0.0 | Yes | Set the bidding price. More information below. | `exchange.name` | bittrex | Yes | Name of the exchange class to use. | `exchange.key` | key | No | API key to use for the exchange. Only required when you are in production mode. diff --git a/freqtrade/main.py b/freqtrade/main.py index cd19af008..ca77f1c93 100755 --- a/freqtrade/main.py +++ b/freqtrade/main.py @@ -98,7 +98,7 @@ def _process(nb_assets: Optional[int] = 0) -> bool: # Check if we can sell our current pair state_changed = handle_trade(trade) or state_changed - if 'opentradetimeout' in _CONF and trade.open_order_id: + if 'unfilledtimeout' in _CONF and trade.open_order_id: # Check and handle any timed out trades check_handle_timedout(trade) @@ -125,7 +125,7 @@ def check_handle_timedout(trade: Trade) -> bool: :param trade: Trade instance :return: True if the trade is timed out, false otherwise """ - timeoutthreashold = datetime.utcnow() - timedelta(minutes=_CONF['opentradetimeout']) + timeoutthreashold = datetime.utcnow() - timedelta(minutes=_CONF['unfilledtimeout']) order = exchange.get_order(trade.open_order_id) if trade.open_date < timeoutthreashold: diff --git a/freqtrade/misc.py b/freqtrade/misc.py index c8f1dcbcd..9f414a72f 100644 --- a/freqtrade/misc.py +++ b/freqtrade/misc.py @@ -218,7 +218,7 @@ CONF_SCHEMA = { 'minProperties': 1 }, 'stoploss': {'type': 'number', 'maximum': 0, 'exclusiveMaximum': True}, - 'opentradetimeout': {'type': 'integer', 'minimum': 0}, + 'unfilledtimeout': {'type': 'integer', 'minimum': 0}, 'bid_strategy': { 'type': 'object', 'properties': { diff --git a/freqtrade/tests/conftest.py b/freqtrade/tests/conftest.py index 857f46f33..0420b5074 100644 --- a/freqtrade/tests/conftest.py +++ b/freqtrade/tests/conftest.py @@ -25,7 +25,7 @@ def default_conf(): "0": 0.04 }, "stoploss": -0.10, - "opentradetimeout": 600, + "unfilledtimeout": 600, "bid_strategy": { "ask_last_balance": 0.0 },