set initial open_rate and amount in create_trade

This is mostly needed by dry_run
This commit is contained in:
gcarq 2017-11-01 00:54:16 +01:00
parent 1e5b0e8726
commit 543857ddb2
5 changed files with 28 additions and 47 deletions

View File

@ -106,9 +106,7 @@ def cancel_order(order_id: str) -> None:
def get_order(order_id: str) -> Dict:
if _CONF['dry_run']:
return {
}
return {}
return _API.get_order(order_id)

View File

@ -51,7 +51,7 @@ def _process() -> None:
logger.info('Got open order for %s', trade)
trade.update(exchange.get_order(trade.open_order_id))
if not close_trade_if_fulfilled(trade) and trade.open_rate:
if not close_trade_if_fulfilled(trade):
# Check if we can sell our current pair
handle_trade(trade)
@ -198,6 +198,8 @@ def create_trade(stake_amount: float) -> Optional[Trade]:
telegram.send_msg(message)
return Trade(pair=pair,
stake_amount=stake_amount,
amount=amount,
open_rate=buy_limit,
open_date=datetime.utcnow(),
exchange=exchange.get_name().upper(),
open_order_id=order_id,

View File

@ -78,12 +78,12 @@ class Trade(Base):
:param order: order retrieved by exchange.get_order()
:return: None
"""
if not order['closed']:
if not order.get('closed'):
return
logger.debug('Updating trade (id=%d) ...', self.id)
if order['type'] == 'LIMIT_BUY':
# Set open rate and actual amount
# Update open rate and actual amount
self.open_rate = order['rate']
self.amount = order['amount']
elif order['type'] == 'LIMIT_SELL':

View File

@ -118,14 +118,13 @@ def _status(bot: Bot, update: Update) -> None:
else:
for trade in trades:
order = exchange.get_order(trade.open_order_id)
if trade.open_rate:
# calculate profit and send message to user
current_rate = exchange.get_ticker(trade.pair)['bid']
current_profit = trade.calc_profit(current_rate)
fmt_close_profit = '{:.2f}%'.format(
round(trade.close_profit * 100, 2)
) if trade.close_profit else None
message = """
# calculate profit and send message to user
current_rate = exchange.get_ticker(trade.pair)['bid']
current_profit = trade.calc_profit(current_rate)
fmt_close_profit = '{:.2f}%'.format(
round(trade.close_profit * 100, 2)
) if trade.close_profit else None
message = """
*Trade ID:* `{trade_id}`
*Current Pair:* [{pair}]({market_url})
*Open Since:* `{date}`
@ -136,38 +135,21 @@ def _status(bot: Bot, update: Update) -> None:
*Close Profit:* `{close_profit}`
*Current Profit:* `{current_profit:.2f}%`
*Open Order:* `{open_order}`
""".format(
trade_id=trade.id,
pair=trade.pair,
market_url=exchange.get_pair_detail_url(trade.pair),
date=arrow.get(trade.open_date).humanize(),
open_rate=trade.open_rate,
close_rate=trade.close_rate,
current_rate=current_rate,
amount=round(trade.amount, 8),
close_profit=fmt_close_profit,
current_profit=round(current_profit * 100, 2),
open_order='{} ({})'.format(
order['remaining'], order['type']
) if order else None,
)
else:
message = """
*Trade ID:* `{trade_id}`
*Current Pair:* [{pair}]({market_url})
*Open Since:* `{date}`
*Open Order:* `{open_order}`
`Waiting until order is fulfilled.`
""".format(
trade_id=trade.id,
pair=trade.pair,
market_url=exchange.get_pair_detail_url(trade.pair),
date=arrow.get(trade.open_date).humanize(),
open_order='{} ({})'.format(
order['remaining'], order['type']
) if order else None,
)
""".format(
trade_id=trade.id,
pair=trade.pair,
market_url=exchange.get_pair_detail_url(trade.pair),
date=arrow.get(trade.open_date).humanize(),
open_rate=trade.open_rate,
close_rate=trade.close_rate,
current_rate=current_rate,
amount=round(trade.amount, 8),
close_profit=fmt_close_profit,
current_profit=round(current_profit * 100, 2),
open_order='{} ({})'.format(
order['remaining'], order['type']
) if order else None,
)
send_msg(message, bot=bot)

View File

@ -99,7 +99,6 @@ def test_status_handle(conf, update, mocker):
_status(bot=MagicBot(), update=update)
assert msg_mock.call_count == 3
assert 'Waiting until order is fulfilled' in msg_mock.call_args_list[-2][0][0]
assert '[BTC_ETH]' in msg_mock.call_args_list[-1][0][0]