initial test added for TSL on exchange
This commit is contained in:
parent
e025ad3918
commit
cfe00c2f0c
@ -662,18 +662,16 @@ class FreqtradeBot(object):
|
||||
self.exchange.get_ticker(trade.pair)['bid'], trade.stop_loss)
|
||||
return result
|
||||
|
||||
print(trade.stop_loss)
|
||||
print(order['info']['stopPrice'])
|
||||
if trade.stop_loss > order['info']['stopPrice']:
|
||||
# we check also if the update is neccesary
|
||||
update_beat = self.strategy.order_types['stoploss_on_exchange_interval']
|
||||
if (datetime.now() - trade.stoploss_last_update).total_seconds > update_beat:
|
||||
if (datetime.now() - trade.stoploss_last_update).total_seconds() > update_beat:
|
||||
# cancelling the current stoploss on exchange first
|
||||
if self.exchange.cancel_order(order['id'], trade.pair):
|
||||
# creating the new one
|
||||
stoploss_order_id = self.exchange.stoploss_limit(
|
||||
pair=trade.pair, amount=trade.amount,
|
||||
stop_price=stop_price, rate=limit_price
|
||||
stop_price=trade.stop_loss, rate=trade.stop_loss * 0.99
|
||||
)['id']
|
||||
trade.stoploss_order_id = str(stoploss_order_id)
|
||||
|
||||
|
@ -222,11 +222,13 @@ class Trade(_DECL_BASE):
|
||||
logger.debug("assigning new stop loss")
|
||||
self.stop_loss = new_loss
|
||||
self.initial_stop_loss = new_loss
|
||||
self.stoploss_last_update = datetime.utcnow()
|
||||
|
||||
# evaluate if the stop loss needs to be updated
|
||||
else:
|
||||
if new_loss > self.stop_loss: # stop losses only walk up, never down!
|
||||
self.stop_loss = new_loss
|
||||
self.stoploss_last_update = datetime.utcnow()
|
||||
logger.debug("adjusted stop loss")
|
||||
else:
|
||||
logger.debug("keeping current stop loss")
|
||||
|
@ -1033,9 +1033,21 @@ def test_handle_stoploss_on_exchange_trailing(mocker, default_conf, fee, caplog,
|
||||
get_markets=markets,
|
||||
stoploss_limit=stoploss_limit
|
||||
)
|
||||
|
||||
# enabling TSL
|
||||
default_conf['trailing_stop'] = True
|
||||
|
||||
# disabling ROI
|
||||
default_conf['minimal_roi']['0'] = 999999999
|
||||
|
||||
freqtrade = FreqtradeBot(default_conf)
|
||||
|
||||
# setting stoploss
|
||||
freqtrade.strategy.stoploss = -0.05
|
||||
|
||||
# setting stoploss_on_exchange_interval
|
||||
freqtrade.strategy.order_types['stoploss_on_exchange_interval'] = 0
|
||||
|
||||
patch_get_signal(freqtrade)
|
||||
|
||||
freqtrade.create_trade()
|
||||
@ -1051,7 +1063,7 @@ def test_handle_stoploss_on_exchange_trailing(mocker, default_conf, fee, caplog,
|
||||
'price': 3,
|
||||
'average': 2,
|
||||
'info' : {
|
||||
'stopPrice': 1.113399999
|
||||
'stopPrice': 0.000011134
|
||||
}
|
||||
})
|
||||
|
||||
@ -1061,23 +1073,22 @@ def test_handle_stoploss_on_exchange_trailing(mocker, default_conf, fee, caplog,
|
||||
assert freqtrade.handle_trade(trade) is False
|
||||
|
||||
# price jumped 2x
|
||||
trade.max_rate = trade.open_rate * 2
|
||||
mocker.patch('freqtrade.exchange.Exchange.get_ticker', MagicMock(return_value={
|
||||
'bid': 0.00002344,
|
||||
'ask': 0.00002346,
|
||||
'last': 0.00002344
|
||||
}))
|
||||
|
||||
assert freqtrade.handle_trade(trade) is False
|
||||
assert freqtrade.handle_stoploss_on_exchange(trade) is False
|
||||
assert trade.stop_loss == 0.00002344 * 0.95
|
||||
|
||||
assert trade.stop_loss == (trade.open_rate * 2) * 0.95
|
||||
mocker.patch('freqtrade.exchange.Exchange.cancel_order', MagicMock(return_value={
|
||||
"orderId": 100,
|
||||
"status": "CANCELED",
|
||||
}))
|
||||
|
||||
stoploss_order_hit = MagicMock(return_value={
|
||||
'status': 'open',
|
||||
'type': 'stop_loss_limit',
|
||||
'price': 3,
|
||||
'average': 2
|
||||
})
|
||||
mocker.patch('freqtrade.exchange.Exchange.get_order', stoploss_order_hit)
|
||||
assert freqtrade.handle_stoploss_on_exchange(trade) is False
|
||||
assert log_has('STOP_LOSS_LIMIT is hit for {}.'.format(trade), caplog.record_tuples)
|
||||
assert trade.stoploss_order_id is None
|
||||
assert trade.is_open is False
|
||||
assert freqtrade.exchange.cancel_order.call_count == 1
|
||||
|
||||
|
||||
def test_process_maybe_execute_buy(mocker, default_conf) -> None:
|
||||
|
Loading…
Reference in New Issue
Block a user