Skip to content

Commit

Permalink
Add forcesell market/limit distinction
Browse files Browse the repository at this point in the history
  • Loading branch information
xmatthias committed Nov 27, 2021
1 parent 338fe33 commit 80ed528
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 16 deletions.
18 changes: 8 additions & 10 deletions freqtrade/freqtradebot.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ def _check_depth_of_market_buy(self, pair: str, conf: Dict) -> bool:
return False

def execute_entry(self, pair: str, stake_amount: float, price: Optional[float] = None, *,
order_type: Optional[str] = None, buy_tag: Optional[str] = None) -> bool:
ordertype: Optional[str] = None, buy_tag: Optional[str] = None) -> bool:
"""
Executes a limit buy for the given pair
:param pair: pair for which we want to create a LIMIT_BUY
Expand Down Expand Up @@ -510,8 +510,7 @@ def execute_entry(self, pair: str, stake_amount: float, price: Optional[float] =
f"{stake_amount} ...")

amount = stake_amount / enter_limit_requested
if not order_type:
order_type = self.strategy.order_types['buy']
order_type = ordertype or self.strategy.order_types['buy']

if not strategy_safe_wrapper(self.strategy.confirm_trade_entry, default_retval=True)(
pair=pair, order_type=order_type, amount=amount, rate=enter_limit_requested,
Expand Down Expand Up @@ -866,7 +865,7 @@ def _check_and_execute_exit(self, trade: Trade, exit_rate: float,
logger.info(
f'Executing Sell for {trade.pair}. Reason: {should_sell.sell_type}. '
f'Tag: {exit_tag if exit_tag is not None else "None"}')
self.execute_trade_exit(trade, exit_rate, should_sell, exit_tag)
self.execute_trade_exit(trade, exit_rate, should_sell, exit_tag=exit_tag)
return True
return False

Expand Down Expand Up @@ -1079,7 +1078,10 @@ def execute_trade_exit(
trade: Trade,
limit: float,
sell_reason: SellCheckTuple,
exit_tag: Optional[str] = None) -> bool:
*,
exit_tag: Optional[str] = None,
ordertype: Optional[str] = None,
) -> bool:
"""
Executes a trade exit for the given trade and limit
:param trade: Trade instance
Expand Down Expand Up @@ -1117,14 +1119,10 @@ def execute_trade_exit(
except InvalidOrderException:
logger.exception(f"Could not cancel stoploss order {trade.stoploss_order_id}")

order_type = self.strategy.order_types[sell_type]
order_type = ordertype or self.strategy.order_types[sell_type]
if sell_reason.sell_type == SellType.EMERGENCY_SELL:
# Emergency sells (default to market!)
order_type = self.strategy.order_types.get("emergencysell", "market")
if sell_reason.sell_type == SellType.FORCE_SELL:
# Force sells (default to the sell_type defined in the strategy,
# but we allow this value to be changed)
order_type = self.strategy.order_types.get("forcesell", order_type)

amount = self._safe_exit_amount(trade.pair, trade.amount)
time_in_force = self.strategy.order_time_in_force['sell']
Expand Down
1 change: 1 addition & 0 deletions freqtrade/rpc/api_server/api_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ class ForceBuyPayload(BaseModel):

class ForceSellPayload(BaseModel):
tradeid: str
ordertype: Optional[OrderTypeValues]


class BlacklistPayload(BaseModel):
Expand Down
6 changes: 3 additions & 3 deletions freqtrade/rpc/api_server/api_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
# API version
# Pre-1.1, no version was provided
# Version increments should happen in "small" steps (1.1, 1.12, ...) unless big changes happen.
# 1.11: forcebuy accepts new option with ordertype
# 1.11: forcebuy and forcesell accept ordertype
API_VERSION = 1.11

# Public API, requires no auth.
Expand Down Expand Up @@ -130,7 +130,7 @@ def show_config(rpc: Optional[RPC] = Depends(get_rpc_optional), config=Depends(g

@router.post('/forcebuy', response_model=ForceBuyResponse, tags=['trading'])
def forcebuy(payload: ForceBuyPayload, rpc: RPC = Depends(get_rpc)):
trade = rpc._rpc_forcebuy(payload.pair, payload.price, payload.ordertype)
trade = rpc._rpc_forcebuy(payload.pair, payload.price, payload.ordertype.value)

if trade:
return ForceBuyResponse.parse_obj(trade.to_json())
Expand All @@ -140,7 +140,7 @@ def forcebuy(payload: ForceBuyPayload, rpc: RPC = Depends(get_rpc)):

@router.post('/forcesell', response_model=ResultMsg, tags=['trading'])
def forcesell(payload: ForceSellPayload, rpc: RPC = Depends(get_rpc)):
return rpc._rpc_forcesell(payload.tradeid)
return rpc._rpc_forcesell(payload.tradeid, payload.ordertype.value)


@router.get('/blacklist', response_model=BlacklistResponse, tags=['info', 'pairlist'])
Expand Down
10 changes: 7 additions & 3 deletions freqtrade/rpc/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ def _rpc_stopbuy(self) -> Dict[str, str]:

return {'status': 'No more buy will occur from now. Run /reload_config to reset.'}

def _rpc_forcesell(self, trade_id: str) -> Dict[str, str]:
def _rpc_forcesell(self, trade_id: str, ordertype: Optional[str] = None) -> Dict[str, str]:
"""
Handler for forcesell <id>.
Sells the given trade at current price
Expand All @@ -664,7 +664,11 @@ def _exec_forcesell(trade: Trade) -> None:
current_rate = self._freqtrade.exchange.get_rate(
trade.pair, refresh=False, side="sell")
sell_reason = SellCheckTuple(sell_type=SellType.FORCE_SELL)
self._freqtrade.execute_trade_exit(trade, current_rate, sell_reason)
order_type = ordertype or self._freqtrade.strategy.order_types.get(
"forcesell", self._freqtrade.strategy.order_types["sell"])

self._freqtrade.execute_trade_exit(
trade, current_rate, sell_reason, ordertype=order_type)
# ---- EOF def _exec_forcesell ----

if self._freqtrade.state != State.RUNNING:
Expand Down Expand Up @@ -724,7 +728,7 @@ def _rpc_forcebuy(self, pair: str, price: Optional[float],
if not order_type:
order_type = self._freqtrade.strategy.order_types.get(
'forcebuy', self._freqtrade.strategy.order_types['buy'])
if self._freqtrade.execute_entry(pair, stakeamount, price, order_type=order_type):
if self._freqtrade.execute_entry(pair, stakeamount, price, ordertype=order_type):
Trade.commit()
trade = Trade.get_trades([Trade.is_open.is_(True), Trade.pair == pair]).first()
return trade
Expand Down

0 comments on commit 80ed528

Please sign in to comment.