Use ccxt order format

This commit is contained in:
enenn 2018-03-13 22:29:22 +01:00
parent a6ce6ef2c4
commit 421cd76272
7 changed files with 48 additions and 31 deletions

View File

@ -98,10 +98,11 @@ def buy(pair: str, rate: float, amount: float) -> Dict:
'pair': pair, 'pair': pair,
'rate': rate, 'rate': rate,
'amount': amount, 'amount': amount,
'type': 'LIMIT_BUY', 'type': 'limit',
'side': 'buy',
'remaining': 0.0, 'remaining': 0.0,
'opened': arrow.utcnow().datetime, 'datetime': arrow.utcnow().isoformat(),
'closed': arrow.utcnow().datetime, 'status': 'closed'
} }
return {'id': order_id} return {'id': order_id}
@ -135,10 +136,11 @@ def sell(pair: str, rate: float, amount: float) -> Dict:
'pair': pair, 'pair': pair,
'rate': rate, 'rate': rate,
'amount': amount, 'amount': amount,
'type': 'LIMIT_SELL', 'type': 'limit',
'side': 'sell',
'remaining': 0.0, 'remaining': 0.0,
'opened': arrow.utcnow().datetime, 'datetime': arrow.utcnow().isoformat(),
'closed': arrow.utcnow().datetime, 'status': 'closed'
} }
return {'id': order_id} return {'id': order_id}

View File

@ -209,15 +209,17 @@ def check_handle_timedout(timeoutvalue: int) -> None:
except (NetworkException, DependencyException): except (NetworkException, DependencyException):
logger.info('Cannot query order for %s due to %s', trade, traceback.format_exc()) logger.info('Cannot query order for %s due to %s', trade, traceback.format_exc())
continue continue
ordertime = arrow.get(order['opened'])
# divide by 1000 since timestemp is unix timestamp in ms
ordertime = arrow.get(order['datetime']).datetime
# Check if trade is still actually open # Check if trade is still actually open
if int(order['remaining']) == 0: if int(order['remaining']) == 0:
continue continue
if order['type'] == "LIMIT_BUY" and ordertime < timeoutthreashold: if order['side'] == 'buy' and ordertime < timeoutthreashold:
handle_timedout_limit_buy(trade, order) handle_timedout_limit_buy(trade, order)
elif order['type'] == "LIMIT_SELL" and ordertime < timeoutthreashold: elif order['side'] == 'sell' and ordertime < timeoutthreashold:
handle_timedout_limit_sell(trade, order) handle_timedout_limit_sell(trade, order)

View File

@ -104,19 +104,19 @@ class Trade(_DECL_BASE):
:return: None :return: None
""" """
# Ignore open and cancelled orders # Ignore open and cancelled orders
if not order['closed'] or order['rate'] is None: if order['status'] == 'open' or order['rate'] is None:
return return
logger.info('Updating trade (id=%d) ...', self.id) logger.info('Updating trade (id=%d) ...', self.id)
getcontext().prec = 8 # Bittrex do not go above 8 decimal getcontext().prec = 8 # Bittrex do not go above 8 decimal
if order['type'] == 'LIMIT_BUY': if order['type'] == 'limit' and order['side'] == 'buy':
# Update open rate and actual amount # Update open rate and actual amount
self.open_rate = Decimal(order['rate']) self.open_rate = Decimal(order['rate'])
self.amount = Decimal(order['amount']) self.amount = Decimal(order['amount'])
logger.info('LIMIT_BUY has been fulfilled for %s.', self) logger.info('LIMIT_BUY has been fulfilled for %s.', self)
self.open_order_id = None self.open_order_id = None
elif order['type'] == 'LIMIT_SELL': elif order['type'] == 'limit' and order['side'] == 'sell':
self.close(order['rate']) self.close(order['rate'])
else: else:
raise ValueError('Unknown order type: {}'.format(order['type'])) raise ValueError('Unknown order type: {}'.format(order['type']))

View File

@ -170,8 +170,8 @@ def _status(bot: Bot, update: Update) -> None:
amount=round(trade.amount, 8), amount=round(trade.amount, 8),
close_profit=fmt_close_profit, close_profit=fmt_close_profit,
current_profit=round(current_profit * 100, 2), current_profit=round(current_profit * 100, 2),
open_order='({} rem={:.8f})'.format( open_order='({} {} rem={:.8f})'.format(
order['type'], order['remaining'] order['type'], order['side'], order['remaining']
) if order else None, ) if order else None,
total_trades=len(trades) total_trades=len(trades)
) )
@ -602,14 +602,18 @@ def _exec_forcesell(trade: Trade) -> None:
order = exchange.get_order(trade.open_order_id, trade.pair) order = exchange.get_order(trade.open_order_id, trade.pair)
# Cancel open LIMIT_BUY orders and close trade # Cancel open LIMIT_BUY orders and close trade
if order and not order['closed'] and order['type'] == 'LIMIT_BUY': if order and order['status'] == 'open' \
and order['type'] == 'limit' \
and order['side'] == 'buy':
exchange.cancel_order(trade.open_order_id, trade.pair) exchange.cancel_order(trade.open_order_id, trade.pair)
trade.close(order.get('rate') or trade.open_rate) trade.close(order.get('rate') or trade.open_rate)
# TODO: sell amount which has been bought already # TODO: sell amount which has been bought already
return return
# Ignore trades with an attached LIMIT_SELL order # Ignore trades with an attached LIMIT_SELL order
if order and not order['closed'] and order['type'] == 'LIMIT_SELL': if order and order['status'] == 'open' \
and order['type'] == 'limit' \
and order['side'] == 'sell':
return return
# Get current rate and execute sell # Get current rate and execute sell

View File

@ -137,13 +137,14 @@ def health():
def limit_buy_order(): def limit_buy_order():
return { return {
'id': 'mocked_limit_buy', 'id': 'mocked_limit_buy',
'type': 'LIMIT_BUY', 'type': 'limit',
'side': 'buy',
'pair': 'mocked', 'pair': 'mocked',
'opened': str(arrow.utcnow().datetime), 'datetime': arrow.utcnow().isoformat(),
'rate': 0.00001099, 'rate': 0.00001099,
'amount': 90.99181073, 'amount': 90.99181073,
'remaining': 0.0, 'remaining': 0.0,
'closed': str(arrow.utcnow().datetime), 'status': 'closed'
} }
@ -151,12 +152,14 @@ def limit_buy_order():
def limit_buy_order_old(): def limit_buy_order_old():
return { return {
'id': 'mocked_limit_buy_old', 'id': 'mocked_limit_buy_old',
'type': 'LIMIT_BUY', 'type': 'limit',
'side': 'buy',
'pair': 'ETH/BTC', 'pair': 'ETH/BTC',
'opened': str(arrow.utcnow().shift(minutes=-601).datetime), 'datetime': arrow.utcnow().shift(minutes=-601).isoformat(),
'rate': 0.00001099, 'rate': 0.00001099,
'amount': 90.99181073, 'amount': 90.99181073,
'remaining': 90.99181073, 'remaining': 90.99181073,
'status': 'open'
} }
@ -164,12 +167,14 @@ def limit_buy_order_old():
def limit_sell_order_old(): def limit_sell_order_old():
return { return {
'id': 'mocked_limit_sell_old', 'id': 'mocked_limit_sell_old',
'type': 'LIMIT_SELL', 'type': 'limit',
'side': 'sell',
'pair': 'ETH/BTC', 'pair': 'ETH/BTC',
'opened': str(arrow.utcnow().shift(minutes=-601).datetime), 'datetime': arrow.utcnow().shift(minutes=-601).isoformat(),
'rate': 0.00001099, 'rate': 0.00001099,
'amount': 90.99181073, 'amount': 90.99181073,
'remaining': 90.99181073, 'remaining': 90.99181073,
'status': 'open'
} }
@ -177,12 +182,14 @@ def limit_sell_order_old():
def limit_buy_order_old_partial(): def limit_buy_order_old_partial():
return { return {
'id': 'mocked_limit_buy_old_partial', 'id': 'mocked_limit_buy_old_partial',
'type': 'LIMIT_BUY', 'type': 'limit',
'side': 'buy',
'pair': 'ETH/BTC', 'pair': 'ETH/BTC',
'opened': str(arrow.utcnow().shift(minutes=-601).datetime), 'datetime': arrow.utcnow().shift(minutes=-601).isoformat(),
'rate': 0.00001099, 'rate': 0.00001099,
'amount': 90.99181073, 'amount': 90.99181073,
'remaining': 67.99181073, 'remaining': 67.99181073,
'status': 'open'
} }
@ -190,13 +197,14 @@ def limit_buy_order_old_partial():
def limit_sell_order(): def limit_sell_order():
return { return {
'id': 'mocked_limit_sell', 'id': 'mocked_limit_sell',
'type': 'LIMIT_SELL', 'type': 'limit',
'side': 'sell',
'pair': 'mocked', 'pair': 'mocked',
'opened': str(arrow.utcnow().datetime), 'datetime': arrow.utcnow().isoformat(),
'rate': 0.00001173, 'rate': 0.00001173,
'amount': 90.99181073, 'amount': 90.99181073,
'remaining': 0.0, 'remaining': 0.0,
'closed': str(arrow.utcnow().datetime), 'status': 'closed'
} }

View File

@ -294,8 +294,9 @@ def test_exec_forcesell_open_orders(default_conf, ticker, mocker):
mocker.patch.multiple('freqtrade.main.exchange', mocker.patch.multiple('freqtrade.main.exchange',
get_ticker=ticker, get_ticker=ticker,
get_order=MagicMock(return_value={ get_order=MagicMock(return_value={
'closed': None, 'type': 'limit',
'type': 'LIMIT_BUY', 'side': 'buy',
'status': 'open'
}), }),
cancel_order=cancel_order_mock) cancel_order=cancel_order_mock)
trade = Trade( trade = Trade(

View File

@ -189,7 +189,7 @@ def test_update_open_order(limit_buy_order):
assert trade.close_profit is None assert trade.close_profit is None
assert trade.close_date is None assert trade.close_date is None
limit_buy_order['closed'] = False limit_buy_order['status'] = 'open'
trade.update(limit_buy_order) trade.update(limit_buy_order)
assert trade.open_order_id is None assert trade.open_order_id is None