Merge pull request #2124 from freqtrade/fix/sell_order_hanging
Fix/sell order hanging
This commit is contained in:
commit
3af5691b91
@ -376,7 +376,7 @@ class Exchange(object):
|
|||||||
'side': side,
|
'side': side,
|
||||||
'remaining': amount,
|
'remaining': amount,
|
||||||
'datetime': arrow.utcnow().isoformat(),
|
'datetime': arrow.utcnow().isoformat(),
|
||||||
'status': "open",
|
'status': "closed" if ordertype == "market" else "open",
|
||||||
'fee': None,
|
'fee': None,
|
||||||
"info": {}
|
"info": {}
|
||||||
}
|
}
|
||||||
|
@ -871,15 +871,18 @@ class FreqtradeBot(object):
|
|||||||
logger.exception(f"Could not cancel stoploss order {trade.stoploss_order_id}")
|
logger.exception(f"Could not cancel stoploss order {trade.stoploss_order_id}")
|
||||||
|
|
||||||
# Execute sell and update trade record
|
# Execute sell and update trade record
|
||||||
order_id = self.exchange.sell(pair=str(trade.pair),
|
order = self.exchange.sell(pair=str(trade.pair),
|
||||||
ordertype=self.strategy.order_types[sell_type],
|
ordertype=self.strategy.order_types[sell_type],
|
||||||
amount=trade.amount, rate=limit,
|
amount=trade.amount, rate=limit,
|
||||||
time_in_force=self.strategy.order_time_in_force['sell']
|
time_in_force=self.strategy.order_time_in_force['sell']
|
||||||
)['id']
|
)
|
||||||
|
|
||||||
trade.open_order_id = order_id
|
trade.open_order_id = order['id']
|
||||||
trade.close_rate_requested = limit
|
trade.close_rate_requested = limit
|
||||||
trade.sell_reason = sell_reason.value
|
trade.sell_reason = sell_reason.value
|
||||||
|
# In case of market sell orders the order can be closed immediately
|
||||||
|
if order.get('status', 'unknown') == 'closed':
|
||||||
|
trade.update(order)
|
||||||
Trade.session.flush()
|
Trade.session.flush()
|
||||||
self._notify_sell(trade)
|
self._notify_sell(trade)
|
||||||
|
|
||||||
|
@ -2421,8 +2421,8 @@ def test_may_execute_sell_after_stoploss_on_exchange_hit(default_conf,
|
|||||||
assert rpc_mock.call_count == 2
|
assert rpc_mock.call_count == 2
|
||||||
|
|
||||||
|
|
||||||
def test_execute_sell_without_conf_sell_up(default_conf, ticker, fee,
|
def test_execute_sell_market_order(default_conf, ticker, fee,
|
||||||
ticker_sell_up, markets, mocker) -> None:
|
ticker_sell_up, markets, mocker) -> None:
|
||||||
rpc_mock = patch_RPCManager(mocker)
|
rpc_mock = patch_RPCManager(mocker)
|
||||||
mocker.patch.multiple(
|
mocker.patch.multiple(
|
||||||
'freqtrade.exchange.Exchange',
|
'freqtrade.exchange.Exchange',
|
||||||
@ -2445,10 +2445,13 @@ def test_execute_sell_without_conf_sell_up(default_conf, ticker, fee,
|
|||||||
'freqtrade.exchange.Exchange',
|
'freqtrade.exchange.Exchange',
|
||||||
get_ticker=ticker_sell_up
|
get_ticker=ticker_sell_up
|
||||||
)
|
)
|
||||||
freqtrade.config = {}
|
freqtrade.config['order_types']['sell'] = 'market'
|
||||||
|
|
||||||
freqtrade.execute_sell(trade=trade, limit=ticker_sell_up()['bid'], sell_reason=SellType.ROI)
|
freqtrade.execute_sell(trade=trade, limit=ticker_sell_up()['bid'], sell_reason=SellType.ROI)
|
||||||
|
|
||||||
|
assert not trade.is_open
|
||||||
|
assert trade.close_profit == 0.0611052
|
||||||
|
|
||||||
assert rpc_mock.call_count == 2
|
assert rpc_mock.call_count == 2
|
||||||
last_msg = rpc_mock.call_args_list[-1][0][0]
|
last_msg = rpc_mock.call_args_list[-1][0][0]
|
||||||
assert {
|
assert {
|
||||||
@ -2458,63 +2461,18 @@ def test_execute_sell_without_conf_sell_up(default_conf, ticker, fee,
|
|||||||
'gain': 'profit',
|
'gain': 'profit',
|
||||||
'limit': 1.172e-05,
|
'limit': 1.172e-05,
|
||||||
'amount': 90.99181073703367,
|
'amount': 90.99181073703367,
|
||||||
'order_type': 'limit',
|
'order_type': 'market',
|
||||||
'open_rate': 1.099e-05,
|
'open_rate': 1.099e-05,
|
||||||
'current_rate': 1.172e-05,
|
'current_rate': 1.172e-05,
|
||||||
'profit_amount': 6.126e-05,
|
'profit_amount': 6.126e-05,
|
||||||
'profit_percent': 0.0611052,
|
'profit_percent': 0.0611052,
|
||||||
|
'stake_currency': 'BTC',
|
||||||
|
'fiat_currency': 'USD',
|
||||||
'sell_reason': SellType.ROI.value
|
'sell_reason': SellType.ROI.value
|
||||||
|
|
||||||
} == last_msg
|
} == last_msg
|
||||||
|
|
||||||
|
|
||||||
def test_execute_sell_without_conf_sell_down(default_conf, ticker, fee,
|
|
||||||
ticker_sell_down, markets, mocker) -> None:
|
|
||||||
rpc_mock = patch_RPCManager(mocker)
|
|
||||||
mocker.patch.multiple(
|
|
||||||
'freqtrade.exchange.Exchange',
|
|
||||||
_load_markets=MagicMock(return_value={}),
|
|
||||||
get_ticker=ticker,
|
|
||||||
get_fee=fee,
|
|
||||||
markets=PropertyMock(return_value=markets)
|
|
||||||
)
|
|
||||||
freqtrade = FreqtradeBot(default_conf)
|
|
||||||
patch_get_signal(freqtrade)
|
|
||||||
|
|
||||||
# Create some test data
|
|
||||||
freqtrade.create_trades()
|
|
||||||
|
|
||||||
trade = Trade.query.first()
|
|
||||||
assert trade
|
|
||||||
|
|
||||||
# Decrease the price and sell it
|
|
||||||
mocker.patch.multiple(
|
|
||||||
'freqtrade.exchange.Exchange',
|
|
||||||
get_ticker=ticker_sell_down
|
|
||||||
)
|
|
||||||
|
|
||||||
freqtrade.config = {}
|
|
||||||
freqtrade.execute_sell(trade=trade, limit=ticker_sell_down()['bid'],
|
|
||||||
sell_reason=SellType.STOP_LOSS)
|
|
||||||
|
|
||||||
assert rpc_mock.call_count == 2
|
|
||||||
last_msg = rpc_mock.call_args_list[-1][0][0]
|
|
||||||
assert {
|
|
||||||
'type': RPCMessageType.SELL_NOTIFICATION,
|
|
||||||
'exchange': 'Bittrex',
|
|
||||||
'pair': 'ETH/BTC',
|
|
||||||
'gain': 'loss',
|
|
||||||
'limit': 1.044e-05,
|
|
||||||
'amount': 90.99181073703367,
|
|
||||||
'order_type': 'limit',
|
|
||||||
'open_rate': 1.099e-05,
|
|
||||||
'current_rate': 1.044e-05,
|
|
||||||
'profit_amount': -5.492e-05,
|
|
||||||
'profit_percent': -0.05478342,
|
|
||||||
'sell_reason': SellType.STOP_LOSS.value
|
|
||||||
} == last_msg
|
|
||||||
|
|
||||||
|
|
||||||
def test_sell_profit_only_enable_profit(default_conf, limit_buy_order,
|
def test_sell_profit_only_enable_profit(default_conf, limit_buy_order,
|
||||||
fee, markets, mocker) -> None:
|
fee, markets, mocker) -> None:
|
||||||
patch_RPCManager(mocker)
|
patch_RPCManager(mocker)
|
||||||
|
Loading…
Reference in New Issue
Block a user