tests fixed
This commit is contained in:
parent
5ee2faa182
commit
9144a8f79d
@ -559,9 +559,13 @@ class FreqtradeBot(object):
|
|||||||
# on exchange should be added immediately if stoploss on exchnage
|
# on exchange should be added immediately if stoploss on exchnage
|
||||||
# is on
|
# is on
|
||||||
if self.strategy.stoploss_on_exchange and trade.is_open and \
|
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)
|
stop_price = trade.open_rate * (1 + stoploss)
|
||||||
|
|
||||||
# limit price should be less than stop price.
|
# limit price should be less than stop price.
|
||||||
@ -569,8 +573,10 @@ class FreqtradeBot(object):
|
|||||||
limit_price = stop_price * 0.98
|
limit_price = stop_price * 0.98
|
||||||
|
|
||||||
stoploss_order_id = self.exchange.stoploss_limit(
|
stoploss_order_id = self.exchange.stoploss_limit(
|
||||||
pair=trade.pair, amount=trade.amount, stop_price=stop_price, rate=limit_price)['id']
|
pair=trade.pair, amount=trade.amount, stop_price=stop_price, rate=limit_price
|
||||||
trade.stoploss_order_id = stoploss_order_id
|
)['id']
|
||||||
|
|
||||||
|
trade.stoploss_order_id = str(stoploss_order_id)
|
||||||
|
|
||||||
# Or Check if there is a stoploss on exchnage and it is hit
|
# Or Check if there is a stoploss on exchnage and it is hit
|
||||||
elif self.strategy.stoploss_on_exchange and trade.stoploss_order_id:
|
elif self.strategy.stoploss_on_exchange and trade.stoploss_order_id:
|
||||||
|
@ -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
|
assert call_args['amount'] == stake_amount / fix_price
|
||||||
|
|
||||||
|
|
||||||
def test_execute_buy_with_stoploss_on_exchange(mocker, default_conf,
|
def test_add_stoploss_on_exchange(mocker, default_conf, limit_buy_order) -> None:
|
||||||
fee, markets, limit_buy_order) -> None:
|
|
||||||
default_conf['exchange']['name'] = 'binance'
|
|
||||||
patch_RPCManager(mocker)
|
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 = FreqtradeBot(default_conf)
|
||||||
freqtrade.strategy.stoploss_on_exchange = True
|
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)
|
trade = MagicMock()
|
||||||
assert stoploss_limit.call_count == 1
|
trade.open_order_id = None
|
||||||
assert get_bid.call_count == 1
|
trade.stoploss_order_id = None
|
||||||
assert buy_mm.call_count == 1
|
trade.is_open = True
|
||||||
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
|
|
||||||
|
|
||||||
call_args = stoploss_limit.call_args_list[0][1]
|
freqtrade.process_maybe_execute_sell(trade)
|
||||||
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
|
|
||||||
assert trade.stoploss_order_id == '13434334'
|
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:
|
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()
|
trade = Trade.query.first()
|
||||||
assert trade
|
assert trade
|
||||||
|
|
||||||
|
freqtrade.process_maybe_execute_sell(trade)
|
||||||
|
|
||||||
# Increase the price and sell it
|
# Increase the price and sell it
|
||||||
mocker.patch.multiple(
|
mocker.patch.multiple(
|
||||||
'freqtrade.exchange.Exchange',
|
'freqtrade.exchange.Exchange',
|
||||||
@ -1612,13 +1586,11 @@ def test_may_execute_sell_after_stoploss_on_exchange_hit(default_conf,
|
|||||||
|
|
||||||
# Create some test data
|
# Create some test data
|
||||||
freqtrade.create_trade()
|
freqtrade.create_trade()
|
||||||
|
|
||||||
trade = Trade.query.first()
|
trade = Trade.query.first()
|
||||||
|
freqtrade.process_maybe_execute_sell(trade)
|
||||||
assert trade
|
assert trade
|
||||||
assert trade.stoploss_order_id == '123'
|
assert trade.stoploss_order_id == '123'
|
||||||
assert trade.open_order_id is not None
|
assert trade.open_order_id is None
|
||||||
|
|
||||||
trade.update(limit_buy_order)
|
|
||||||
|
|
||||||
# Assuming stoploss on exchnage is hit
|
# Assuming stoploss on exchnage is hit
|
||||||
# stoploss_order_id should become None
|
# stoploss_order_id should become None
|
||||||
|
Loading…
Reference in New Issue
Block a user