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

View File

@ -209,15 +209,17 @@ def check_handle_timedout(timeoutvalue: int) -> None:
except (NetworkException, DependencyException):
logger.info('Cannot query order for %s due to %s', trade, traceback.format_exc())
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
if int(order['remaining']) == 0:
continue
if order['type'] == "LIMIT_BUY" and ordertime < timeoutthreashold:
if order['side'] == 'buy' and ordertime < timeoutthreashold:
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)

View File

@ -104,19 +104,19 @@ class Trade(_DECL_BASE):
:return: None
"""
# Ignore open and cancelled orders
if not order['closed'] or order['rate'] is None:
if order['status'] == 'open' or order['rate'] is None:
return
logger.info('Updating trade (id=%d) ...', self.id)
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
self.open_rate = Decimal(order['rate'])
self.amount = Decimal(order['amount'])
logger.info('LIMIT_BUY has been fulfilled for %s.', self)
self.open_order_id = None
elif order['type'] == 'LIMIT_SELL':
elif order['type'] == 'limit' and order['side'] == 'sell':
self.close(order['rate'])
else:
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),
close_profit=fmt_close_profit,
current_profit=round(current_profit * 100, 2),
open_order='({} rem={:.8f})'.format(
order['type'], order['remaining']
open_order='({} {} rem={:.8f})'.format(
order['type'], order['side'], order['remaining']
) if order else None,
total_trades=len(trades)
)
@ -602,14 +602,18 @@ def _exec_forcesell(trade: Trade) -> None:
order = exchange.get_order(trade.open_order_id, trade.pair)
# 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)
trade.close(order.get('rate') or trade.open_rate)
# TODO: sell amount which has been bought already
return
# 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
# Get current rate and execute sell

View File

@ -137,13 +137,14 @@ def health():
def limit_buy_order():
return {
'id': 'mocked_limit_buy',
'type': 'LIMIT_BUY',
'type': 'limit',
'side': 'buy',
'pair': 'mocked',
'opened': str(arrow.utcnow().datetime),
'datetime': arrow.utcnow().isoformat(),
'rate': 0.00001099,
'amount': 90.99181073,
'remaining': 0.0,
'closed': str(arrow.utcnow().datetime),
'status': 'closed'
}
@ -151,12 +152,14 @@ def limit_buy_order():
def limit_buy_order_old():
return {
'id': 'mocked_limit_buy_old',
'type': 'LIMIT_BUY',
'type': 'limit',
'side': 'buy',
'pair': 'ETH/BTC',
'opened': str(arrow.utcnow().shift(minutes=-601).datetime),
'datetime': arrow.utcnow().shift(minutes=-601).isoformat(),
'rate': 0.00001099,
'amount': 90.99181073,
'remaining': 90.99181073,
'status': 'open'
}
@ -164,12 +167,14 @@ def limit_buy_order_old():
def limit_sell_order_old():
return {
'id': 'mocked_limit_sell_old',
'type': 'LIMIT_SELL',
'type': 'limit',
'side': 'sell',
'pair': 'ETH/BTC',
'opened': str(arrow.utcnow().shift(minutes=-601).datetime),
'datetime': arrow.utcnow().shift(minutes=-601).isoformat(),
'rate': 0.00001099,
'amount': 90.99181073,
'remaining': 90.99181073,
'status': 'open'
}
@ -177,12 +182,14 @@ def limit_sell_order_old():
def limit_buy_order_old_partial():
return {
'id': 'mocked_limit_buy_old_partial',
'type': 'LIMIT_BUY',
'type': 'limit',
'side': 'buy',
'pair': 'ETH/BTC',
'opened': str(arrow.utcnow().shift(minutes=-601).datetime),
'datetime': arrow.utcnow().shift(minutes=-601).isoformat(),
'rate': 0.00001099,
'amount': 90.99181073,
'remaining': 67.99181073,
'status': 'open'
}
@ -190,13 +197,14 @@ def limit_buy_order_old_partial():
def limit_sell_order():
return {
'id': 'mocked_limit_sell',
'type': 'LIMIT_SELL',
'type': 'limit',
'side': 'sell',
'pair': 'mocked',
'opened': str(arrow.utcnow().datetime),
'datetime': arrow.utcnow().isoformat(),
'rate': 0.00001173,
'amount': 90.99181073,
'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',
get_ticker=ticker,
get_order=MagicMock(return_value={
'closed': None,
'type': 'LIMIT_BUY',
'type': 'limit',
'side': 'buy',
'status': 'open'
}),
cancel_order=cancel_order_mock)
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_date is None
limit_buy_order['closed'] = False
limit_buy_order['status'] = 'open'
trade.update(limit_buy_order)
assert trade.open_order_id is None