working on trailing stop loss, needs tests and verification that it works

This commit is contained in:
Gert Wohlgemuth 2018-05-10 10:03:48 -07:00
parent 1dbdb880e6
commit 867fac7719
2 changed files with 39 additions and 2 deletions

View File

@ -14,7 +14,6 @@ from freqtrade.exchange import get_ticker_history
from freqtrade.persistence import Trade
from freqtrade.strategy.resolver import StrategyResolver
logger = logging.getLogger(__name__)
@ -31,6 +30,7 @@ class Analyze(object):
Analyze class contains everything the bot need to determine if the situation is good for
buying or selling.
"""
def __init__(self, config: dict) -> None:
"""
Init Analyze
@ -195,10 +195,19 @@ class Analyze(object):
:return True if bot should sell at current rate
"""
current_profit = trade.calc_profit_percent(current_rate)
if self.strategy.stoploss is not None and current_profit < self.strategy.stoploss:
if trade.stop_loss is None:
# initially adjust the stop loss to it's default value
trade.adjust_stop_loss(current_rate, self.strategy.stoploss)
# evaluate stop loss, before we continue
if self.strategy.stoploss is not None and trade.stop_loss >= current_rate:
logger.debug('Stop loss hit.')
return True
# update the stop loss afterwards, after all by definition it's supposed to be hanging
trade.adjust_stop_loss(current_rate, self.strategy.stoploss)
# Check if time matches and current rate is above threshold
time_diff = (current_time.timestamp() - trade.open_date.timestamp()) / 60
for duration, threshold in self.strategy.minimal_roi.items():

View File

@ -95,6 +95,7 @@ class Trade(_DECL_BASE):
open_date = Column(DateTime, nullable=False, default=datetime.utcnow)
close_date = Column(DateTime)
open_order_id = Column(String)
stop_loss = Column(Float, nullable=False, default=0.0) # absolute value of the stop loss
def __repr__(self):
return 'Trade(id={}, pair={}, amount={:.8f}, open_rate={:.8f}, open_since={})'.format(
@ -105,6 +106,33 @@ class Trade(_DECL_BASE):
arrow.get(self.open_date).humanize() if self.is_open else 'closed'
)
def adjust_stop_loss(self, current_price, stoploss):
"""
this adjusts the stop loss to it's most recently observed
setting
:param current_price:
:param stoploss:
:return:
"""
new_loss = Decimal(current_price * (1 - abs(stoploss)))
logger.debug("calculated stop loss at: {:.6f}".format(new_loss))
if self.stop_loss is None:
logger.debug("assigning new stop loss")
self.stop_loss = new_loss # no stop loss assigned yet
else:
if _CONF.get('trailing_stop', True):
if new_loss > self.stop_loss: # stop losses only walk up, never down!
self.stop_loss = new_loss
logger.debug("adjusted stop loss for {:.6f} and {:.6f} to {:.6f}".format(
current_price, stoploss, self.stop_loss)
)
else:
logger.debug("keeping current stop loss of {:.6f}".format(self.stop_loss))
else:
print("utilizing fixed stop")
def update(self, order: Dict) -> None:
"""
Updates this entity with amount and actual open/close rates.