Merge pull request #33 from nullart/nullartHFT
Order book updates, percent from top feature and documentation updates
This commit is contained in:
@@ -10,7 +10,7 @@ import arrow
|
||||
from pandas import DataFrame, to_datetime
|
||||
|
||||
from freqtrade import constants
|
||||
from freqtrade.exchange import get_ticker_history
|
||||
from freqtrade.exchange import get_fee, get_ticker_history
|
||||
from freqtrade.persistence import Trade
|
||||
from freqtrade.strategy.resolver import StrategyResolver, IStrategy
|
||||
|
||||
@@ -197,7 +197,6 @@ class Analyze(object):
|
||||
:return True if bot should sell at current rate
|
||||
"""
|
||||
current_profit = trade.calc_profit_percent(current_rate)
|
||||
|
||||
if trade.stop_loss is None:
|
||||
# initially adjust the stop loss to the base value
|
||||
trade.adjust_stop_loss(trade.open_rate, self.strategy.stoploss)
|
||||
@@ -248,3 +247,15 @@ class Analyze(object):
|
||||
"""
|
||||
return {pair: self.populate_indicators(self.parse_ticker_dataframe(pair_data))
|
||||
for pair, pair_data in tickerdata.items()}
|
||||
|
||||
def trunc_num(self, f, n):
|
||||
import math
|
||||
return math.floor(f * 10 ** n) / 10 ** n
|
||||
|
||||
def get_roi_rate(self, trade: Trade) -> float:
|
||||
current_time = datetime.utcnow()
|
||||
time_diff = (current_time.timestamp() - trade.open_date.timestamp()) / 60
|
||||
for duration, threshold in self.strategy.minimal_roi.items():
|
||||
if time_diff > duration:
|
||||
roi_rate = (trade.open_rate * (1 + threshold)) * (1+(2.1*get_fee(trade.pair)))
|
||||
return self.trunc_num(roi_rate, 8)
|
||||
@@ -58,10 +58,10 @@ CONF_SCHEMA = {
|
||||
'unfilledtimeout': {
|
||||
'type': 'object',
|
||||
'properties': {
|
||||
'use_book_order': {'type': 'boolean'},
|
||||
'buy': {'type': 'number', 'minimum': 3},
|
||||
'sell': {'type': 'number', 'minimum': 10}
|
||||
}
|
||||
'buy': {'type': 'number', 'minimum': 1},
|
||||
'sell': {'type': 'number', 'minimum': 1}
|
||||
},
|
||||
'required': ['buy', 'sell']
|
||||
},
|
||||
'bid_strategy': {
|
||||
'type': 'object',
|
||||
@@ -73,24 +73,27 @@ CONF_SCHEMA = {
|
||||
'exclusiveMaximum': False
|
||||
},
|
||||
'use_book_order': {'type': 'boolean'},
|
||||
'book_order_top': {'type': 'number', 'maximum': 20, 'minimum': 1}
|
||||
'book_order_top': {'type': 'number', 'maximum': 20, 'minimum': 1},
|
||||
'percent_from_top': {'type': 'number', 'minimum': 0}
|
||||
},
|
||||
'required': ['ask_last_balance']
|
||||
'required': ['ask_last_balance', 'use_book_order']
|
||||
},
|
||||
'ask_strategy': {
|
||||
'type': 'object',
|
||||
'properties': {
|
||||
'use_book_order': {'type': 'boolean'},
|
||||
'book_order_min': {'type': 'number', 'minimum': 1},
|
||||
'book_order_max': {'type': 'number', 'minimum': 1}
|
||||
}
|
||||
'book_order_max': {'type': 'number', 'minimum': 1, 'maximum': 50}
|
||||
},
|
||||
'required': ['use_book_order']
|
||||
},
|
||||
'exchange': {'$ref': '#/definitions/exchange'},
|
||||
'experimental': {
|
||||
'type': 'object',
|
||||
'properties': {
|
||||
'use_sell_signal': {'type': 'boolean'},
|
||||
'sell_profit_only': {'type': 'boolean'}
|
||||
'sell_profit_only': {'type': 'boolean'},
|
||||
'sell_fullfilled_at_roi' : {'type': 'boolean'}
|
||||
}
|
||||
},
|
||||
'telegram': {
|
||||
|
||||
@@ -242,8 +242,18 @@ def get_balances() -> dict:
|
||||
|
||||
|
||||
@retrier
|
||||
def get_order_book(pair: str, limit: Optional[int] = 1000) -> dict:
|
||||
def get_order_book(pair: str, limit: Optional[int] = 100) -> dict:
|
||||
try:
|
||||
params = {}
|
||||
# 20180619: bittrex doesnt support limits -.-
|
||||
# 20180619: binance limit fix.. binance currently has valid range
|
||||
if _API.name == 'Binance':
|
||||
limit_range = [5, 10, 20, 50, 100, 500, 1000]
|
||||
for limitx in limit_range:
|
||||
if limit < limitx:
|
||||
limit = limitx
|
||||
break
|
||||
|
||||
return _API.fetch_order_book(pair, limit)
|
||||
except ccxt.NotSupported as e:
|
||||
raise OperationalException(
|
||||
|
||||
@@ -164,8 +164,9 @@ class FreqtradeBot(object):
|
||||
|
||||
if 'unfilledtimeout' in self.config:
|
||||
# Check and handle any timed out open orders
|
||||
self.check_handle_timedout()
|
||||
Trade.session.flush()
|
||||
if not self.config['dry_run']:
|
||||
self.check_handle_timedout()
|
||||
Trade.session.flush()
|
||||
|
||||
except TemporaryError as error:
|
||||
logger.warning('%s, retrying in 30 seconds...', error)
|
||||
@@ -243,24 +244,40 @@ class FreqtradeBot(object):
|
||||
:param ticker: Ticker to use for getting Ask and Last Price
|
||||
:return: float: Price
|
||||
"""
|
||||
|
||||
ticker = exchange.get_ticker(pair)
|
||||
logger.info('ticker data %s', ticker)
|
||||
|
||||
if ticker['ask'] < ticker['last']:
|
||||
return ticker['ask']
|
||||
balance = self.config['bid_strategy']['ask_last_balance']
|
||||
ticker_rate = ticker['ask'] + balance * (ticker['last'] - ticker['ask'])
|
||||
ticker_rate = ticker['ask']
|
||||
else:
|
||||
balance = self.config['bid_strategy']['ask_last_balance']
|
||||
ticker_rate = ticker['ask'] + balance * (ticker['last'] - ticker['ask'])
|
||||
|
||||
used_rate = ticker_rate
|
||||
|
||||
if self.config['bid_strategy'].get('use_book_order', False):
|
||||
logger.info('Getting price from Order Book')
|
||||
orderBook = exchange.get_order_book(pair)
|
||||
orderBook_rate = orderBook['bids'][self.config['bid_strategy']['book_order_top']][0]
|
||||
orderBook_top = self.config.get('bid_strategy',{}).get('book_order_top',1)
|
||||
orderBook = exchange.get_order_book(pair, orderBook_top)
|
||||
# top 1 = index 0
|
||||
orderBook_rate = orderBook['bids'][orderBook_top-1][0]
|
||||
orderBook_rate = orderBook_rate+0.00000001
|
||||
# if ticker has lower rate, then use ticker ( usefull if down trending )
|
||||
logger.info('...book order buy rate %0.8f', orderBook_rate)
|
||||
if ticker_rate < orderBook_rate:
|
||||
return ticker_rate
|
||||
return orderBook_rate
|
||||
logger.info('...using ticker rate instead %0.8f', ticker_rate)
|
||||
used_rate = ticker_rate
|
||||
used_rate = orderBook_rate
|
||||
else:
|
||||
logger.info('Using Ask / Last Price')
|
||||
return ticker_rate
|
||||
logger.info('Using Last Ask / Last Price')
|
||||
used_rate = ticker_rate
|
||||
percent_from_top = self.config.get('bid_strategy',{}).get('percent_from_top',0)
|
||||
if percent_from_top > 0:
|
||||
used_rate = used_rate - (used_rate * percent_from_top)
|
||||
used_rate = self.analyze.trunc_num(used_rate, 8)
|
||||
logger.info('...percent_from_top enabled, new buy rate %0.8f', used_rate)
|
||||
|
||||
return used_rate
|
||||
|
||||
def create_trade(self) -> bool:
|
||||
"""
|
||||
@@ -269,6 +286,7 @@ class FreqtradeBot(object):
|
||||
:return: True if a trade object has been created and persisted, False otherwise
|
||||
"""
|
||||
stake_amount = self.config['stake_amount']
|
||||
|
||||
interval = self.analyze.get_ticker_interval()
|
||||
stake_currency = self.config['stake_currency']
|
||||
fiat_currency = self.config['fiat_display_currency']
|
||||
@@ -279,8 +297,10 @@ class FreqtradeBot(object):
|
||||
stake_amount
|
||||
)
|
||||
whitelist = copy.deepcopy(self.config['exchange']['pair_whitelist'])
|
||||
|
||||
# Check if stake_amount is fulfilled
|
||||
if exchange.get_balance(stake_currency) < stake_amount:
|
||||
current_balance = exchange.get_balance(self.config['stake_currency'])
|
||||
if current_balance < stake_amount:
|
||||
raise DependencyException(
|
||||
f'stake amount is not fulfilled (currency={stake_currency})')
|
||||
|
||||
@@ -436,24 +456,34 @@ with limit `{buy_limit:.8f} ({stake_amount:.6f} \
|
||||
if not trade.is_open:
|
||||
raise ValueError(f'attempt to handle closed trade: {trade}')
|
||||
|
||||
logger.debug('Handling %s ...', trade)
|
||||
logger.info('Handling %s ...', trade)
|
||||
sell_rate = exchange.get_ticker(trade.pair)['bid']
|
||||
|
||||
logger.info(' ticker rate %0.8f', sell_rate)
|
||||
(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())
|
||||
|
||||
is_set_fullfilled_at_roi = self.config.get('experimental', {}).get('sell_fullfilled_at_roi', False)
|
||||
if is_set_fullfilled_at_roi:
|
||||
sell_rate = self.analyze.get_roi_rate(trade)
|
||||
logger.info('trying to selling at roi rate %0.8f', sell_rate)
|
||||
|
||||
if 'ask_strategy' in self.config and self.config['ask_strategy'].get('use_book_order', False):
|
||||
logger.info('Using order book for selling...')
|
||||
orderBook = exchange.get_order_book(trade.pair)
|
||||
|
||||
# logger.debug('Order book %s',orderBook)
|
||||
orderBook_min = self.config['ask_strategy']['book_order_min']
|
||||
orderBook_max = self.config['ask_strategy']['book_order_max']
|
||||
for i in range(orderBook_min, orderBook_max + 1):
|
||||
orderBook_rate = orderBook['asks'][i - 1][0]
|
||||
|
||||
orderBook = exchange.get_order_book(trade.pair, orderBook_max)
|
||||
|
||||
for i in range(orderBook_min, orderBook_max+1):
|
||||
orderBook_rate = orderBook['asks'][i-1][0]
|
||||
|
||||
# if orderbook has higher rate (high profit),
|
||||
# use orderbook, otherwise just use sell rate
|
||||
# use orderbook, otherwise just use bids rate
|
||||
logger.info(' order book asks top %s: %0.8f', i, orderBook_rate)
|
||||
if (sell_rate < orderBook_rate):
|
||||
sell_rate = orderBook_rate
|
||||
|
||||
@@ -461,6 +491,7 @@ with limit `{buy_limit:.8f} ({stake_amount:.6f} \
|
||||
return True
|
||||
break
|
||||
else:
|
||||
logger.info('checking sell')
|
||||
if self.check_sell(trade, sell_rate, buy, sell):
|
||||
return True
|
||||
|
||||
@@ -502,7 +533,7 @@ with limit `{buy_limit:.8f} ({stake_amount:.6f} \
|
||||
ordertime = arrow.get(order['datetime']).datetime
|
||||
|
||||
# Check if trade is still actually open
|
||||
if order['status'] == 'open':
|
||||
if (order['status'] == 'open'):
|
||||
if order['side'] == 'buy' and ordertime < buy_timeoutthreashold:
|
||||
self.handle_timedout_limit_buy(trade, order)
|
||||
elif order['side'] == 'sell' and ordertime < sell_timeoutthreashold:
|
||||
|
||||
@@ -180,7 +180,8 @@ class Telegram(RPC):
|
||||
headers=[
|
||||
'Day',
|
||||
'Profit {}'.format(self._config['stake_currency']),
|
||||
'Profit {}'.format(self._config['fiat_display_currency'])
|
||||
'Profit {}'.format(self._config['fiat_display_currency']),
|
||||
'Trades'
|
||||
],
|
||||
tablefmt='simple')
|
||||
message = '<b>Daily Profit over the last {} days</b>:\n<pre>{}</pre>'\
|
||||
|
||||
@@ -98,6 +98,7 @@ def default_conf():
|
||||
"use_book_order": False,
|
||||
"book_order_top": 6,
|
||||
"ask_last_balance": 0.0,
|
||||
"percent_from_top": 0.0
|
||||
},
|
||||
"ask_strategy": {
|
||||
"use_book_order": False,
|
||||
|
||||
@@ -499,7 +499,12 @@ def test_balance_fully_ask_side(mocker) -> None:
|
||||
"""
|
||||
Test get_target_bid() method
|
||||
"""
|
||||
param = {'use_book_order': False, 'book_order_top': 6, 'ask_last_balance': 0.0}
|
||||
param = {
|
||||
'use_book_order': False,
|
||||
'book_order_top': 6,
|
||||
'ask_last_balance': 0.0,
|
||||
'percent_from_top': 0
|
||||
}
|
||||
freqtrade = get_patched_freqtradebot(mocker, {'bid_strategy': param})
|
||||
|
||||
assert freqtrade.get_target_bid('ETH/BTC') >= 0.07
|
||||
@@ -509,7 +514,12 @@ def test_balance_fully_last_side(mocker) -> None:
|
||||
"""
|
||||
Test get_target_bid() method
|
||||
"""
|
||||
param = {'use_book_order': False, 'book_order_top': 6, 'ask_last_balance': 0.0}
|
||||
param = {
|
||||
'use_book_order': False,
|
||||
'book_order_top': 6,
|
||||
'ask_last_balance': 0.0,
|
||||
'percent_from_top': 0
|
||||
}
|
||||
freqtrade = get_patched_freqtradebot(mocker, {'bid_strategy': param})
|
||||
|
||||
assert freqtrade.get_target_bid('ETH/BTC') >= 0.07
|
||||
@@ -519,7 +529,12 @@ def test_balance_bigger_last_ask(mocker) -> None:
|
||||
"""
|
||||
Test get_target_bid() method
|
||||
"""
|
||||
param = {'use_book_order': False, 'book_order_top': 6, 'ask_last_balance': 0.0}
|
||||
param = {
|
||||
'use_book_order': False,
|
||||
'book_order_top': 6,
|
||||
'ask_last_balance': 0.0,
|
||||
'percent_from_top': 0.00
|
||||
}
|
||||
freqtrade = get_patched_freqtradebot(mocker, {'bid_strategy': param})
|
||||
|
||||
assert freqtrade.get_target_bid('ETH/BTC') >= 0.07
|
||||
|
||||
Reference in New Issue
Block a user