Added feature to bid/buy cryptocurrency using book orders from exchange. The idea, is to get rates from the top 2 (depending on the config settings) of book orders instead of tickers. This way we can offset the lags from the OHLCV data.

This commit is contained in:
Nullart 2018-06-11 16:17:21 +08:00
parent f2fa9ce539
commit 882b30ef17
5 changed files with 36 additions and 8 deletions

View File

@ -7,7 +7,9 @@
"dry_run": false, "dry_run": false,
"unfilledtimeout": 600, "unfilledtimeout": 600,
"bid_strategy": { "bid_strategy": {
"ask_last_balance": 0.0 "ask_last_balance": 0.0,
"use_book_order": true,
"book_order_top": 6
}, },
"exchange": { "exchange": {
"name": "bittrex", "name": "bittrex",

View File

@ -14,7 +14,9 @@
"stoploss": -0.10, "stoploss": -0.10,
"unfilledtimeout": 600, "unfilledtimeout": 600,
"bid_strategy": { "bid_strategy": {
"ask_last_balance": 0.0 "ask_last_balance": 0.0,
"use_book_order": true,
"book_order_top": 6
}, },
"exchange": { "exchange": {
"name": "bittrex", "name": "bittrex",

View File

@ -65,6 +65,8 @@ CONF_SCHEMA = {
'maximum': 1, 'maximum': 1,
'exclusiveMaximum': False 'exclusiveMaximum': False
}, },
'use_book_order': {'type': 'boolean'},
'book_order_top': {'type': 'number', 'maximum':20,'minimum':1}
}, },
'required': ['ask_last_balance'] 'required': ['ask_last_balance']
}, },

View File

@ -239,6 +239,19 @@ def get_balances() -> dict:
except ccxt.BaseError as e: except ccxt.BaseError as e:
raise OperationalException(e) raise OperationalException(e)
@retrier
def get_order_book(pair: str, refresh: Optional[bool] = True) -> dict:
try:
return _API.fetch_order_book(pair)
except ccxt.NotSupported as e:
raise OperationalException(
f'Exchange {_API.name} does not support fetching order book.'
f'Message: {e}')
except (ccxt.NetworkError, ccxt.ExchangeError) as e:
raise TemporaryError(
f'Could not load order book due to {e.__class__.__name__}. Message: {e}')
except ccxt.BaseError as e:
raise OperationalException(e)
@retrier @retrier
def get_tickers() -> Dict: def get_tickers() -> Dict:

View File

@ -237,17 +237,26 @@ class FreqtradeBot(object):
return final_list return final_list
def get_target_bid(self, ticker: Dict[str, float]) -> float: def get_target_bid(self, pair: str) -> float:
""" """
Calculates bid target between current ask price and last price Calculates bid target between current ask price and last price
:param ticker: Ticker to use for getting Ask and Last Price :param ticker: Ticker to use for getting Ask and Last Price
:return: float: Price :return: float: Price
""" """
if self.config['bid_strategy']['use_book_order']:
logger.info('Using order book ')
orderBook = exchange.get_order_book(pair)
return orderBook['bids'][self.config['bid_strategy']['use_book_order']][0]
else:
logger.info('Using Ask / Last Price')
ticker = exchange.get_ticker(pair);
if ticker['ask'] < ticker['last']: if ticker['ask'] < ticker['last']:
return ticker['ask'] return ticker['ask']
balance = self.config['bid_strategy']['ask_last_balance'] balance = self.config['bid_strategy']['ask_last_balance']
return ticker['ask'] + balance * (ticker['last'] - ticker['ask']) return ticker['ask'] + balance * (ticker['last'] - ticker['ask'])
def create_trade(self) -> bool: def create_trade(self) -> bool:
""" """
Checks the implemented trading indicator(s) for a randomly picked pair, Checks the implemented trading indicator(s) for a randomly picked pair,
@ -290,7 +299,7 @@ class FreqtradeBot(object):
pair_s = pair.replace('_', '/') pair_s = pair.replace('_', '/')
pair_url = exchange.get_pair_detail_url(pair) pair_url = exchange.get_pair_detail_url(pair)
# Calculate amount # Calculate amount
buy_limit = self.get_target_bid(exchange.get_ticker(pair)) buy_limit = self.get_target_bid(pair)
amount = stake_amount / buy_limit amount = stake_amount / buy_limit
order_id = exchange.buy(pair, buy_limit, amount)['id'] order_id = exchange.buy(pair, buy_limit, amount)['id']