Fix a case where the amount was not recalculated. Added additional temporary logging.

This commit is contained in:
Reigo Reinmets 2021-12-16 22:57:56 +02:00
parent 337af44901
commit 462270bc5a
4 changed files with 40 additions and 11 deletions

View File

@ -564,8 +564,8 @@ class FreqtradeBot(LoggingMixin):
logger.info(f"Buy signal found: about create a new trade for {pair} with stake_amount: " logger.info(f"Buy signal found: about create a new trade for {pair} with stake_amount: "
f"{stake_amount} ...") f"{stake_amount} ...")
else: else:
logger.info(f"Position adjust: about create a new order for {pair} with stake_amount: " logger.info(f"Position adjust: about to create a new order for {pair} with stake_amount: "
f"{stake_amount} ...") f"{stake_amount} for {trade}")
amount = stake_amount / enter_limit_requested amount = stake_amount / enter_limit_requested
order_type = ordertype or self.strategy.order_types['buy'] order_type = ordertype or self.strategy.order_types['buy']
@ -582,6 +582,7 @@ class FreqtradeBot(LoggingMixin):
order_obj = Order.parse_from_ccxt_object(order, pair, 'buy') order_obj = Order.parse_from_ccxt_object(order, pair, 'buy')
order_id = order['id'] order_id = order['id']
order_status = order.get('status', None) order_status = order.get('status', None)
logger.info(f"Order #{order_id} was created for {pair} and status is {order_status}.")
# we assume the order is executed at the price requested # we assume the order is executed at the price requested
enter_limit_filled_price = enter_limit_requested enter_limit_filled_price = enter_limit_requested
@ -641,6 +642,22 @@ class FreqtradeBot(LoggingMixin):
Trade.query.session.add(trade) Trade.query.session.add(trade)
Trade.commit() Trade.commit()
if trade is not None:
if order_status == 'closed':
logger.info(f"DCA order closed, trade should be up to date: {trade}")
# First cancelling stoploss on exchange ...
if self.strategy.order_types.get('stoploss_on_exchange') and trade.stoploss_order_id:
try:
logger.info(f"Canceling stoploss on exchange, recreating with new amount for {trade}")
co = self.exchange.cancel_stoploss_order_with_result(trade.stoploss_order_id,
trade.pair, trade.amount)
trade.update_order(co)
except InvalidOrderException:
logger.exception(f"Could not cancel stoploss order {trade.stoploss_order_id}")
else:
logger.info(f"DCA order {order_status}, will wait for resolution: {trade}")
# Updating wallets # Updating wallets
self.wallets.update() self.wallets.update()
@ -1344,7 +1361,9 @@ class FreqtradeBot(LoggingMixin):
logger.warning('Unable to fetch order %s: %s', order_id, exception) logger.warning('Unable to fetch order %s: %s', order_id, exception)
return False return False
logger.info(f"Updating order from exchange: {order}")
trade.update_order(order) trade.update_order(order)
trade.recalc_trade_from_orders()
if self.exchange.check_order_canceled_empty(order): if self.exchange.check_order_canceled_empty(order):
# Trade has been cancelled on exchange # Trade has been cancelled on exchange
@ -1365,6 +1384,7 @@ class FreqtradeBot(LoggingMixin):
trade.update(order) trade.update(order)
trade.recalc_trade_from_orders() trade.recalc_trade_from_orders()
Trade.commit() Trade.commit()
logger.info(f"Trade has been updated: {trade}")
# Updating wallets when order is closed # Updating wallets when order is closed
if not trade.is_open: if not trade.is_open:

View File

@ -428,6 +428,7 @@ class LocalTrade():
if self.is_open: if self.is_open:
logger.info(f'{order_type.upper()}_BUY has been fulfilled for {self}.') logger.info(f'{order_type.upper()}_BUY has been fulfilled for {self}.')
self.open_order_id = None self.open_order_id = None
self.recalc_trade_from_orders()
elif order_type in ('market', 'limit') and order['side'] == 'sell': elif order_type in ('market', 'limit') and order['side'] == 'sell':
if self.is_open: if self.is_open:
logger.info(f'{order_type.upper()}_SELL has been fulfilled for {self}.') logger.info(f'{order_type.upper()}_SELL has been fulfilled for {self}.')
@ -578,11 +579,15 @@ class LocalTrade():
total_amount = 0.0 total_amount = 0.0
total_stake = 0.0 total_stake = 0.0
for temp_order in self.orders: for temp_order in self.orders:
if temp_order.ft_is_open == False and temp_order.status == "closed" and temp_order.ft_order_side == 'buy': if temp_order.ft_is_open or \
temp_order.ft_order_side != 'buy' or \
temp_order.status not in NON_OPEN_EXCHANGE_STATES:
continue
tmp_amount = temp_order.amount tmp_amount = temp_order.amount
if temp_order.filled is not None: if temp_order.filled is not None:
tmp_amount = temp_order.filled tmp_amount = temp_order.filled
if tmp_amount is not None and temp_order.average is not None: if tmp_amount > 0.0 and temp_order.average is not None:
total_amount += tmp_amount total_amount += tmp_amount
total_stake += temp_order.average * tmp_amount total_stake += temp_order.average * tmp_amount

View File

@ -160,6 +160,10 @@ class StrategyTestV2(IStrategy):
current_rate: float, current_profit: float, **kwargs): current_rate: float, current_profit: float, **kwargs):
if current_profit < -0.0075: if current_profit < -0.0075:
for order in trade.orders:
if order.ft_is_open:
return None
return self.wallets.get_trade_stake_amount(pair, None) return self.wallets.get_trade_stake_amount(pair, None)
return None return None

View File

@ -1563,7 +1563,7 @@ def test_recalc_trade_from_orders_ignores_bad_orders(fee):
side="buy", side="buy",
price=1, price=1,
average=2, average=2,
filled=3, filled=0,
remaining=4, remaining=4,
cost=5, cost=5,
order_date=arrow.utcnow().shift(hours=-1).datetime, order_date=arrow.utcnow().shift(hours=-1).datetime,
@ -1590,7 +1590,7 @@ def test_recalc_trade_from_orders_ignores_bad_orders(fee):
side="buy", side="buy",
price=1, price=1,
average=2, average=2,
filled=3, filled=0,
remaining=4, remaining=4,
cost=5, cost=5,
order_date=arrow.utcnow().shift(hours=-1).datetime, order_date=arrow.utcnow().shift(hours=-1).datetime,