diff --git a/config.json.example b/config.json.example index d2f70d82c..d133cd4df 100644 --- a/config.json.example +++ b/config.json.example @@ -11,6 +11,11 @@ "use_book_order": true, "book_order_top": 6 }, + "ask_strategy":{ + "use_book_order": true, + "book_order_min": 1, + "book_order_max": 30 + }, "exchange": { "name": "bittrex", "key": "your_exchange_key", diff --git a/config_full.json.example b/config_full.json.example index bec845b3b..2b0930049 100644 --- a/config_full.json.example +++ b/config_full.json.example @@ -18,6 +18,11 @@ "use_book_order": true, "book_order_top": 6 }, + "ask_strategy":{ + "use_book_order": true, + "book_order_min": 1, + "book_order_max": 30 + }, "exchange": { "name": "bittrex", "key": "your_exchange_key", diff --git a/freqtrade/constants.py b/freqtrade/constants.py index d56353c6e..9cfa21565 100644 --- a/freqtrade/constants.py +++ b/freqtrade/constants.py @@ -70,6 +70,14 @@ CONF_SCHEMA = { }, 'required': ['ask_last_balance'] }, + 'ask_strategy': { + 'type': 'object', + 'properties': { + 'use_book_order': {'type': 'boolean'}, + 'book_order_min': {'type': 'number', 'minimum':1}, + 'book_order_max': {'type': 'number', 'minimum':1} + }, + }, 'exchange': {'$ref': '#/definitions/exchange'}, 'experimental': { 'type': 'object', diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 203dc2e76..3d302ea50 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -245,9 +245,8 @@ class FreqtradeBot(object): """ if self.config['bid_strategy']['use_book_order']: - logger.info('Using order book ') + logger.info('Getting price from Order Book') orderBook = exchange.get_order_book(pair) - return orderBook['bids'][self.config['bid_strategy']['use_book_order']][0] return orderBook['bids'][self.config['bid_strategy']['book_order_top']][0] else: logger.info('Using Ask / Last Price') @@ -433,19 +432,36 @@ with limit `{buy_limit:.8f} ({stake_amount:.6f} \ raise ValueError(f'attempt to handle closed trade: {trade}') logger.debug('Handling %s ...', trade) - current_rate = exchange.get_ticker(trade.pair)['bid'] + sell_rate = exchange.get_ticker(trade.pair)['bid'] (buy, sell) = (False, False) if self.config.get('experimental', {}).get('use_sell_signal'): (buy, sell) = self.analyze.get_signal(trade.pair, self.analyze.get_ticker_interval()) - if self.analyze.should_sell(trade, current_rate, datetime.utcnow(), buy, sell): - self.execute_sell(trade, current_rate) - return True + if self.config['ask_strategy']['use_book_order']: + logger.info('Using order book for selling...') + orderBook = exchange.get_order_book(trade.pair) + # logger.debug('Order book %s',orderBook) + for i in range(self.config['ask_strategy']['book_order_min'],self.config['ask_strategy']['book_order_max']): + sell_rate = orderBook['asks'][i-1][0] + logger.debug('checking sell rate %s) %.8f > %.8f (%f %%)',i,trade.open_rate,sell_rate,((sell_rate/trade.open_rate)*100)-100) + if self.check_sell(trade, sell_rate, buy, sell): + return True + break + else: + if self.check_sell(trade, sell_rate, buy, sell): + return True + logger.info('Found no sell signals for whitelisted currencies. Trying again..') return False + def check_sell(self, trade: Trade, sell_rate: float, buy: bool, sell: bool ) -> bool: + if self.analyze.should_sell(trade, sell_rate, datetime.utcnow(), buy, sell): + self.execute_sell(trade, sell_rate) + return True + return False + def check_handle_timedout(self, timeoutvalue: int) -> None: """ Check if any orders are timed out and cancel if neccessary diff --git a/freqtrade/tests/conftest.py b/freqtrade/tests/conftest.py index 1311687b7..fd4421d22 100644 --- a/freqtrade/tests/conftest.py +++ b/freqtrade/tests/conftest.py @@ -87,7 +87,14 @@ def default_conf(): "stoploss": -0.10, "unfilledtimeout": 600, "bid_strategy": { - "ask_last_balance": 0.0 + "use_book_order": False, + "book_order_top": 6, + "ask_last_balance": 0.0, + }, + "ask_strategy": { + "use_book_order": False, + "book_order_min": 1, + "book_order_max": 10 }, "exchange": { "name": "bittrex", diff --git a/freqtrade/tests/test_freqtradebot.py b/freqtrade/tests/test_freqtradebot.py index aca6dce2f..a2fd5260a 100644 --- a/freqtrade/tests/test_freqtradebot.py +++ b/freqtrade/tests/test_freqtradebot.py @@ -502,27 +502,27 @@ def test_balance_fully_ask_side(mocker) -> None: """ Test get_target_bid() method """ - freqtrade = get_patched_freqtradebot(mocker, {'bid_strategy': {'ask_last_balance': 0.0}}) + freqtrade = get_patched_freqtradebot(mocker, {'bid_strategy': {'use_book_order':False,'book_order_top':6,'ask_last_balance': 0.0}}) - assert freqtrade.get_target_bid({'ask': 20, 'last': 10}) == 20 + assert freqtrade.get_target_bid('ETH/BTC') >= 0.07 def test_balance_fully_last_side(mocker) -> None: """ Test get_target_bid() method """ - freqtrade = get_patched_freqtradebot(mocker, {'bid_strategy': {'ask_last_balance': 1.0}}) + freqtrade = get_patched_freqtradebot(mocker, {'bid_strategy': {'use_book_order':False,'book_order_top':6,'ask_last_balance': 1.0}}) - assert freqtrade.get_target_bid({'ask': 20, 'last': 10}) == 10 + assert freqtrade.get_target_bid('ETH/BTC') >= 0.07 def test_balance_bigger_last_ask(mocker) -> None: """ Test get_target_bid() method """ - freqtrade = get_patched_freqtradebot(mocker, {'bid_strategy': {'ask_last_balance': 1.0}}) + freqtrade = get_patched_freqtradebot(mocker, {'bid_strategy': {'use_book_order':False,'book_order_top':6,'ask_last_balance': 1.0}}) - assert freqtrade.get_target_bid({'ask': 5, 'last': 10}) == 5 + assert freqtrade.get_target_bid('ETH/BTC') >= 0.07 def test_process_maybe_execute_buy(mocker, default_conf) -> None: