Skip to content

Commit

Permalink
Move rate-calcuation for stoploss-limit order to exchange
Browse files Browse the repository at this point in the history
  • Loading branch information
xmatthias committed Jan 19, 2020
1 parent 2f82122 commit 8d2e0bf
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
8 changes: 6 additions & 2 deletions freqtrade/exchange/binance.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,17 @@ def get_order_book(self, pair: str, limit: int = 100) -> dict:

return super().get_order_book(pair, limit)

def stoploss_limit(self, pair: str, amount: float, stop_price: float, rate: float) -> Dict:
def stoploss_limit(self, pair: str, amount: float, stop_price: float,
order_types: Dict) -> Dict:
"""
creates a stoploss limit order.
this stoploss-limit is binance-specific.
It may work with a limited number of other exchanges, but this has not been tested yet.
"""
# Limit price threshold: As limit price should always be below stop-price
LIMIT_PRICE_PCT = order_types.get('stoploss_on_exchange_limit_ratio', 0.99)
rate = stop_price * LIMIT_PRICE_PCT

ordertype = "stop_loss_limit"

stop_price = self.price_to_precision(pair, stop_price)
Expand Down
3 changes: 2 additions & 1 deletion freqtrade/exchange/exchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,8 @@ def sell(self, pair: str, ordertype: str, amount: float,

return self.create_order(pair, ordertype, 'sell', amount, rate, params)

def stoploss_limit(self, pair: str, amount: float, stop_price: float, rate: float) -> Dict:
def stoploss_limit(self, pair: str, amount: float, stop_price: float,
order_types: Dict) -> Dict:
"""
creates a stoploss limit order.
Since ccxt does not unify stoploss-limit orders yet, this needs to be implemented in each
Expand Down
5 changes: 1 addition & 4 deletions freqtrade/freqtradebot.py
Original file line number Diff line number Diff line change
Expand Up @@ -636,13 +636,10 @@ def create_stoploss_order(self, trade: Trade, stop_price: float, rate: float) ->
Force-sells the pair (using EmergencySell reason) in case of Problems creating the order.
:return: True if the order succeeded, and False in case of problems.
"""
# Limit price threshold: As limit price should always be below stop-price
LIMIT_PRICE_PCT = self.strategy.order_types.get('stoploss_on_exchange_limit_ratio', 0.99)

try:
stoploss_order = self.exchange.stoploss_limit(pair=trade.pair, amount=trade.amount,
stop_price=stop_price,
rate=rate * LIMIT_PRICE_PCT)
order_types=self.strategy.order_types)
trade.stoploss_order_id = str(stoploss_order['id'])
return True
except InvalidOrderException as e:
Expand Down

0 comments on commit 8d2e0bf

Please sign in to comment.