From 3b7e05e07b15c1df1bfe62bfbf264feac9877784 Mon Sep 17 00:00:00 2001 From: misagh Date: Thu, 22 Nov 2018 16:26:24 +0100 Subject: [PATCH] stop loss order added right after a buy order is executued --- freqtrade/freqtradebot.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 8a2db84a9..f1aae3c3f 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -508,6 +508,37 @@ class FreqtradeBot(object): Trade.session.add(trade) Trade.session.flush() + # Check if stoploss should be added on exchange + # If True then here immediately after buy we should + # Add the stoploss order + if self.strategy.stoploss_on_exchange: + stoploss = self.edge.stoploss if self.edge else self.strategy.stoploss + stop_price = buy_limit * (1 + stoploss) + + # limit price should be less than stop price. + # 0.98 is arbitrary here. + limit_price = stop_price * 0.98 + + order_id = self.exchange.stoploss_limit(pair=pair, amount=amount, + stop_price=stop_price, rate=limit_price)['id'] + + trade = Trade( + pair=pair, + stake_amount=stake_amount, + amount=amount, + fee_open=fee, + fee_close=fee, + stoploss=stop_price, + open_date=datetime.utcnow(), + exchange=self.exchange.id, + open_order_id=order_id, + strategy=self.strategy.get_strategy_name(), + ticker_interval=constants.TICKER_INTERVAL_MINUTES[self.config['ticker_interval']] + ) + + Trade.session.add(trade) + Trade.session.flush() + # Updating wallets self.wallets.update()