tests fixed

This commit is contained in:
misagh 2018-11-23 20:28:01 +01:00
parent 5ee2faa182
commit 9144a8f79d
2 changed files with 31 additions and 53 deletions

View File

@ -559,9 +559,13 @@ class FreqtradeBot(object):
# on exchange should be added immediately if stoploss on exchnage
# is on
if self.strategy.stoploss_on_exchange and trade.is_open and \
trade.open_order_id is None and trade.stoploss_order_id is None:
trade.open_order_id is None and trade.stoploss_order_id is None:
if self.edge:
stoploss = self.edge.stoploss(pair=trade.pair)
else:
stoploss = self.strategy.stoploss
stoploss = self.edge.stoploss(pair=trade.pair) if self.edge else self.strategy.stoploss
stop_price = trade.open_rate * (1 + stoploss)
# limit price should be less than stop price.
@ -569,8 +573,10 @@ class FreqtradeBot(object):
limit_price = stop_price * 0.98
stoploss_order_id = self.exchange.stoploss_limit(
pair=trade.pair, amount=trade.amount, stop_price=stop_price, rate=limit_price)['id']
trade.stoploss_order_id = stoploss_order_id
pair=trade.pair, amount=trade.amount, stop_price=stop_price, rate=limit_price
)['id']
trade.stoploss_order_id = str(stoploss_order_id)
# Or Check if there is a stoploss on exchnage and it is hit
elif self.strategy.stoploss_on_exchange and trade.stoploss_order_id:

View File

@ -881,57 +881,29 @@ def test_execute_buy(mocker, default_conf, fee, markets, limit_buy_order) -> Non
assert call_args['amount'] == stake_amount / fix_price
def test_execute_buy_with_stoploss_on_exchange(mocker, default_conf,
fee, markets, limit_buy_order) -> None:
default_conf['exchange']['name'] = 'binance'
def test_add_stoploss_on_exchange(mocker, default_conf, limit_buy_order) -> None:
patch_RPCManager(mocker)
patch_exchange(mocker, id='binance')
mocker.patch('freqtrade.freqtradebot.FreqtradeBot.handle_trade', MagicMock(return_value=True))
mocker.patch('freqtrade.exchange.Exchange.get_order', return_value=limit_buy_order)
mocker.patch('freqtrade.exchange.Exchange.get_trades_for_order', return_value=[])
mocker.patch('freqtrade.freqtradebot.FreqtradeBot.get_real_amount',
return_value=limit_buy_order['amount'])
stoploss_limit = MagicMock(return_value={'id': 13434334})
mocker.patch('freqtrade.exchange.Exchange.stoploss_limit', stoploss_limit)
freqtrade = FreqtradeBot(default_conf)
freqtrade.strategy.stoploss_on_exchange = True
freqtrade.strategy.stoploss = -0.05
stake_amount = 2
bid = 0.11
get_bid = MagicMock(return_value=bid)
mocker.patch.multiple(
'freqtrade.freqtradebot.FreqtradeBot',
get_target_bid=get_bid,
_get_min_pair_stake_amount=MagicMock(return_value=1)
)
buy_mm = MagicMock(return_value={'id': limit_buy_order['id']})
stoploss_limit = MagicMock(return_value={'id': 13434334})
mocker.patch.multiple(
'freqtrade.exchange.Exchange',
get_ticker=MagicMock(return_value={
'bid': 0.00001172,
'ask': 0.00001173,
'last': 0.00001172
}),
buy=buy_mm,
get_fee=fee,
get_markets=markets,
stoploss_limit=stoploss_limit
)
pair = 'ETH/BTC'
print(buy_mm.call_args_list)
assert freqtrade.execute_buy(pair, stake_amount)
assert stoploss_limit.call_count == 1
assert get_bid.call_count == 1
assert buy_mm.call_count == 1
call_args = buy_mm.call_args_list[0][1]
assert call_args['pair'] == pair
assert call_args['rate'] == bid
assert call_args['amount'] == stake_amount / bid
trade = MagicMock()
trade.open_order_id = None
trade.stoploss_order_id = None
trade.is_open = True
call_args = stoploss_limit.call_args_list[0][1]
assert call_args['pair'] == pair
assert call_args['amount'] == stake_amount / bid
assert call_args['stop_price'] == 0.11 * 0.95
assert call_args['rate'] == 0.11 * 0.95 * 0.98
trade = Trade.query.first()
assert trade.is_open is True
freqtrade.process_maybe_execute_sell(trade)
assert trade.stoploss_order_id == '13434334'
assert stoploss_limit.call_count == 1
assert trade.is_open is True
def test_process_maybe_execute_buy(mocker, default_conf) -> None:
@ -1566,6 +1538,8 @@ def test_execute_sell_with_stoploss_on_exchange(default_conf,
trade = Trade.query.first()
assert trade
freqtrade.process_maybe_execute_sell(trade)
# Increase the price and sell it
mocker.patch.multiple(
'freqtrade.exchange.Exchange',
@ -1612,13 +1586,11 @@ def test_may_execute_sell_after_stoploss_on_exchange_hit(default_conf,
# Create some test data
freqtrade.create_trade()
trade = Trade.query.first()
freqtrade.process_maybe_execute_sell(trade)
assert trade
assert trade.stoploss_order_id == '123'
assert trade.open_order_id is not None
trade.update(limit_buy_order)
assert trade.open_order_id is None
# Assuming stoploss on exchnage is hit
# stoploss_order_id should become None