Add backtesting support for order.stake_amount

This commit is contained in:
Matthias 2023-07-15 10:27:10 +02:00
parent d8c0621887
commit 626ea6b119
4 changed files with 12 additions and 3 deletions

View File

@ -679,6 +679,7 @@ class Backtesting:
remaining=amount,
cost=amount * close_rate,
)
order._trade_bt = trade
trade.orders.append(order)
return trade
@ -903,6 +904,7 @@ class Backtesting:
remaining=amount,
cost=amount * propose_rate + trade.fee_open,
)
order._trade_bt = trade
trade.orders.append(order)
if pos_adjust and self._get_order_filled(order.ft_price, row):
order.close_bt_order(current_time, trade)

View File

@ -38,6 +38,7 @@ class Order(ModelBase):
Mirrors CCXT Order structure
"""
__tablename__ = 'orders'
__allow_unmapped__ = True
session: ClassVar[SessionType]
# Uniqueness should be ensured over pair, order_id
@ -47,7 +48,8 @@ class Order(ModelBase):
id: Mapped[int] = mapped_column(Integer, primary_key=True)
ft_trade_id: Mapped[int] = mapped_column(Integer, ForeignKey('trades.id'), index=True)
trade: Mapped["Trade"] = relationship("Trade", back_populates="orders")
_trade_live: Mapped["Trade"] = relationship("Trade", back_populates="orders")
_trade_bt: "LocalTrade" = None # type: ignore
# order_side can only be 'buy', 'sell' or 'stoploss'
ft_order_side: Mapped[str] = mapped_column(String(25), nullable=False)
@ -119,6 +121,10 @@ class Order(ModelBase):
def safe_amount_after_fee(self) -> float:
return self.safe_filled - self.safe_fee_base
@property
def trade(self) -> "LocalTrade":
return self._trade_bt or self._trade_live
@property
def stake_amount(self) -> float:
""" Amount in stake currency used for this order"""

View File

@ -603,6 +603,7 @@ def test_backtest__enter_trade_futures(default_conf_usdt, fee, mocker) -> None:
assert pytest.approx(trade.liquidation_price) == 0.11787191
assert pytest.approx(trade.orders[0].cost) == (
trade.stake_amount * trade.leverage + trade.fee_open)
assert pytest.approx(trade.orders[-1].stake_amount) == trade.stake_amount
# Stake-amount too high!
mocker.patch(f"{EXMS}.get_min_pair_stake_amount", return_value=600.0)

View File

@ -563,14 +563,14 @@ def test_calc_open_close_trade_price(
trade.open_order_id = f'something-{is_short}-{lev}-{exchange}'
oobj = Order.parse_from_ccxt_object(entry_order, 'ADA/USDT', trade.entry_side)
oobj.trade = trade
oobj._trade_live = trade
oobj.update_from_ccxt_object(entry_order)
trade.update_trade(oobj)
trade.funding_fees = funding_fees
oobj = Order.parse_from_ccxt_object(exit_order, 'ADA/USDT', trade.exit_side)
oobj.trade = trade
oobj._trade_live = trade
oobj.update_from_ccxt_object(exit_order)
trade.update_trade(oobj)