[WIP] adding tests for handle_stoploss_on_exchange.

This commit is contained in:
misagh 2018-11-24 19:00:59 +01:00
parent fe8927136c
commit b5192880df

View File

@ -906,6 +906,82 @@ def test_add_stoploss_on_exchange(mocker, default_conf, limit_buy_order) -> None
assert trade.is_open is True assert trade.is_open is True
def test_handle_stoploss_on_exchange(mocker, default_conf, fee,
markets, limit_buy_order, limit_sell_order) -> None:
freqtrade = get_patched_freqtradebot(mocker, default_conf)
stoploss_limit = MagicMock(return_value={'id': 13434334})
mocker.patch('freqtrade.exchange.Exchange.stoploss_limit', stoploss_limit)
# First case: when stoploss is not yet set but the order is open
# should get the stoploss order id immediately
# and should return false as no trade actually happened
trade = MagicMock()
trade.is_open = True
trade.open_order_id = None
trade.stoploss_order_id = None
assert freqtrade.handle_stoploss_on_exchange(trade) is False
assert stoploss_limit.call_count == 1
assert trade.stoploss_order_id == "13434334"
trade.reset_mock()
# Second case: when stoploss is set but it is not yet hit
# should do nothing and return false
trade.is_open = True
trade.open_order_id = None
trade.stoploss_order_id = 100
hanging_stoploss_order = MagicMock(return_value={'status': 'open'})
mocker.patch('freqtrade.exchange.Exchange.get_order', hanging_stoploss_order)
assert freqtrade.handle_stoploss_on_exchange(trade) is False
assert trade.stoploss_order_id == 100
trade.reset_mock()
# Third case: when stoploss is set and it is hit
# should unset stoploss_order_id and return true
# as a trade actually happened
patch_RPCManager(mocker)
patch_exchange(mocker)
mocker.patch.multiple(
'freqtrade.exchange.Exchange',
get_ticker=MagicMock(return_value={
'bid': 0.00001172,
'ask': 0.00001173,
'last': 0.00001172
}),
buy=MagicMock(return_value={'id': limit_buy_order['id']}),
sell=MagicMock(return_value={'id': limit_sell_order['id']}),
get_fee=fee,
get_markets=markets
)
freqtrade = FreqtradeBot(default_conf)
patch_get_signal(freqtrade)
freqtrade.create_trade()
trade = Trade.query.first()
assert trade
# trade = freqtrade.create
# trade.is_open = True
# trade.open_order_id = None
# trade.stoploss_order_id = 100
# stoploss_order_hit = MagicMock(return_value={'status': 'closed'})
# mocker.patch('freqtrade.exchange.Exchange.get_order', stoploss_order_hit)
# # trade = Trade.query.first()
# # assert trade
# assert freqtrade.handle_stoploss_on_exchange(trade) is True
# time.sleep(0.01) # Race condition fix
# assert trade.is_open is True
# assert trade.stoploss_order_id is None
def test_process_maybe_execute_buy(mocker, default_conf) -> None: def test_process_maybe_execute_buy(mocker, default_conf) -> None:
freqtrade = get_patched_freqtradebot(mocker, default_conf) freqtrade = get_patched_freqtradebot(mocker, default_conf)