Move fiat_converter.convert_amount logic at the end of create_trade()

It is more important to save the trade in the DB than failing
because of the currency convertion. We do not know if
Pymarketcap() will not fail one day. Make sure the trade is saved before.
This commit is contained in:
Gerald Lonlas 2018-01-21 14:40:31 -08:00
parent 31f1fed081
commit de3586d57f

View File

@ -328,13 +328,26 @@ def create_trade(stake_amount: float, interval: int) -> bool:
break
else:
return False
# Calculate amount
buy_limit = get_target_bid(exchange.get_ticker(pair))
amount = stake_amount / buy_limit
order_id = exchange.buy(pair, buy_limit, amount)
# Fee is applied twice because we make a LIMIT_BUY and LIMIT_SELL
trade = Trade(
pair=pair,
stake_amount=stake_amount,
amount=amount,
fee=exchange.get_fee(),
open_rate=buy_limit,
open_date=datetime.utcnow(),
exchange=exchange.get_name().upper(),
open_order_id=order_id
)
Trade.session.add(trade)
Trade.session.flush()
fiat_converter = CryptoToFiatConverter()
stake_amount_fiat = fiat_converter.convert_amount(
stake_amount,
@ -350,19 +363,7 @@ def create_trade(stake_amount: float, interval: int) -> bool:
buy_limit, stake_amount, _CONF['stake_currency'],
stake_amount_fiat, _CONF['fiat_display_currency']
))
# Fee is applied twice because we make a LIMIT_BUY and LIMIT_SELL
trade = Trade(
pair=pair,
stake_amount=stake_amount,
amount=amount,
fee=exchange.get_fee(),
open_rate=buy_limit,
open_date=datetime.utcnow(),
exchange=exchange.get_name().upper(),
open_order_id=order_id
)
Trade.session.add(trade)
Trade.session.flush()
return True