move initial logic to persistence
This commit is contained in:
parent
a91d75b3b2
commit
c997aa9864
@ -212,9 +212,8 @@ class Analyze(object):
|
|||||||
|
|
||||||
current_profit = trade.calc_profit_percent(current_rate)
|
current_profit = trade.calc_profit_percent(current_rate)
|
||||||
trailing_stop = self.config.get('trailing_stop', False)
|
trailing_stop = self.config.get('trailing_stop', False)
|
||||||
if trade.stop_loss is None or trade.stop_loss == 0:
|
|
||||||
# initially adjust the stop loss to the base value
|
trade.adjust_stop_loss(trade.open_rate, self.strategy.stoploss, initial=True)
|
||||||
trade.adjust_stop_loss(trade.open_rate, self.strategy.stoploss)
|
|
||||||
|
|
||||||
# evaluate if the stoploss was hit
|
# evaluate if the stoploss was hit
|
||||||
if self.strategy.stoploss is not None and trade.stop_loss >= current_rate:
|
if self.strategy.stoploss is not None and trade.stop_loss >= current_rate:
|
||||||
|
@ -167,15 +167,19 @@ class Trade(_DECL_BASE):
|
|||||||
arrow.get(self.open_date).humanize() if self.is_open else 'closed'
|
arrow.get(self.open_date).humanize() if self.is_open else 'closed'
|
||||||
)
|
)
|
||||||
|
|
||||||
def adjust_stop_loss(self, current_price, stoploss):
|
def adjust_stop_loss(self, current_price: float, stoploss: float, initial: bool =False):
|
||||||
"""
|
"""
|
||||||
this adjusts the stop loss to it's most recently observed
|
this adjusts the stop loss to it's most recently observed setting
|
||||||
setting
|
|
||||||
:param current_price:
|
:param current_price:
|
||||||
:param stoploss:
|
:param stoploss in percent:
|
||||||
|
:param initial:
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
if initial and not (self.stop_loss is None or self.stop_loss == 0):
|
||||||
|
# Don't modify if called with initial and nothing to do
|
||||||
|
return
|
||||||
|
|
||||||
new_loss = float(current_price * (1 - abs(stoploss)))
|
new_loss = float(current_price * (1 - abs(stoploss)))
|
||||||
|
|
||||||
# keeping track of the highest observed rate for this trade
|
# keeping track of the highest observed rate for this trade
|
||||||
|
@ -465,18 +465,24 @@ def test_adjust_stop_loss(limit_buy_order, limit_sell_order, fee):
|
|||||||
open_rate=1,
|
open_rate=1,
|
||||||
)
|
)
|
||||||
|
|
||||||
# Get percent of profit with a custom rate (Higher than open rate)
|
trade.adjust_stop_loss(trade.open_rate, 0.05, True)
|
||||||
trade.adjust_stop_loss(1, 0.05)
|
|
||||||
assert trade.stop_loss == 0.95
|
assert trade.stop_loss == 0.95
|
||||||
assert trade.max_rate == 1
|
assert trade.max_rate == 1
|
||||||
assert trade.initial_stop_loss == 0.95
|
assert trade.initial_stop_loss == 0.95
|
||||||
|
|
||||||
|
# Get percent of profit with a lowre rate
|
||||||
|
trade.adjust_stop_loss(0.96, 0.05)
|
||||||
|
assert trade.stop_loss == 0.95
|
||||||
|
assert trade.max_rate == 1
|
||||||
|
assert trade.initial_stop_loss == 0.95
|
||||||
|
|
||||||
|
# Get percent of profit with a custom rate (Higher than open rate)
|
||||||
trade.adjust_stop_loss(1.3, -0.1)
|
trade.adjust_stop_loss(1.3, -0.1)
|
||||||
assert round(trade.stop_loss, 8) == 1.17
|
assert round(trade.stop_loss, 8) == 1.17
|
||||||
assert trade.max_rate == 1.3
|
assert trade.max_rate == 1.3
|
||||||
assert trade.initial_stop_loss == 0.95
|
assert trade.initial_stop_loss == 0.95
|
||||||
|
|
||||||
# current rate lower ... should not change
|
# current rate lower again ... should not change
|
||||||
trade.adjust_stop_loss(1.2, 0.1)
|
trade.adjust_stop_loss(1.2, 0.1)
|
||||||
assert round(trade.stop_loss, 8) == 1.17
|
assert round(trade.stop_loss, 8) == 1.17
|
||||||
assert trade.max_rate == 1.3
|
assert trade.max_rate == 1.3
|
||||||
@ -487,3 +493,9 @@ def test_adjust_stop_loss(limit_buy_order, limit_sell_order, fee):
|
|||||||
assert round(trade.stop_loss, 8) == 1.26
|
assert round(trade.stop_loss, 8) == 1.26
|
||||||
assert trade.max_rate == 1.4
|
assert trade.max_rate == 1.4
|
||||||
assert trade.initial_stop_loss == 0.95
|
assert trade.initial_stop_loss == 0.95
|
||||||
|
|
||||||
|
# Initial is true but stop_loss set - so doesn't do anything
|
||||||
|
trade.adjust_stop_loss(1.7, 0.1, True)
|
||||||
|
assert round(trade.stop_loss, 8) == 1.26
|
||||||
|
assert trade.max_rate == 1.4
|
||||||
|
assert trade.initial_stop_loss == 0.95
|
||||||
|
Loading…
Reference in New Issue
Block a user