From de3586d57f0705411e0e87295b7595da35a6ef95 Mon Sep 17 00:00:00 2001 From: Gerald Lonlas Date: Sun, 21 Jan 2018 14:40:31 -0800 Subject: [PATCH] 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. --- freqtrade/main.py | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/freqtrade/main.py b/freqtrade/main.py index 27f3dfd9a..bcc0d33df 100755 --- a/freqtrade/main.py +++ b/freqtrade/main.py @@ -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