From d8c062188738f15e77a961b735bd60a24f7a8f6f Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 15 Jul 2023 10:14:08 +0200 Subject: [PATCH] Add stake amount property to order object --- docs/strategy-callbacks.md | 2 +- docs/trade-object.md | 3 ++- freqtrade/persistence/trade_model.py | 5 +++++ tests/test_integration.py | 1 + 4 files changed, 9 insertions(+), 2 deletions(-) diff --git a/docs/strategy-callbacks.md b/docs/strategy-callbacks.md index 855f2353b..ab8eb9f98 100644 --- a/docs/strategy-callbacks.md +++ b/docs/strategy-callbacks.md @@ -750,7 +750,7 @@ class DigDeeperStrategy(IStrategy): # Hope you have a deep wallet! try: # This returns first order stake size - stake_amount = filled_entries[0].cost + stake_amount = filled_entries[0].stake_amount # This then calculates current safety order size stake_amount = stake_amount * (1 + (count_of_entries * 0.25)) return stake_amount diff --git a/docs/trade-object.md b/docs/trade-object.md index 7e0db1e3b..15a8b1938 100644 --- a/docs/trade-object.md +++ b/docs/trade-object.md @@ -141,7 +141,8 @@ Most properties here can be None as they are dependant on the exchange response. `amount` | float | Amount in base currency `filled` | float | Filled amount (in base currency) `remaining` | float | Remaining amount -`cost` | float | Cost of the order - usually average * filled +`cost` | float | Cost of the order - usually average * filled (*Exchange dependant on futures, may contain the cost with or without leverage and may be in contracts.*) +`stake_amount` | float | Stake amount used for this order. *Added in 2023.7.* `order_date` | datetime | Order creation date **use `order_date_utc` instead** `order_date_utc` | datetime | Order creation date (in UTC) `order_fill_date` | datetime | Order fill date **use `order_fill_utc` instead** diff --git a/freqtrade/persistence/trade_model.py b/freqtrade/persistence/trade_model.py index bb6837792..718a23fd8 100644 --- a/freqtrade/persistence/trade_model.py +++ b/freqtrade/persistence/trade_model.py @@ -119,6 +119,11 @@ class Order(ModelBase): def safe_amount_after_fee(self) -> float: return self.safe_filled - self.safe_fee_base + @property + def stake_amount(self) -> float: + """ Amount in stake currency used for this order""" + return self.safe_amount * self.safe_price / self.trade.leverage + def __repr__(self): return (f"Order(id={self.id}, trade={self.ft_trade_id}, order_id={self.order_id}, " diff --git a/tests/test_integration.py b/tests/test_integration.py index 2949f1ef2..c9979cdd7 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -429,6 +429,7 @@ def test_dca_order_adjust(default_conf_usdt, ticker_usdt, leverage, fee, mocker) assert pytest.approx(trade.stop_loss) == 1.99 * (1 - 0.1 / leverage) assert pytest.approx(trade.initial_stop_loss) == 1.96 * (1 - 0.1 / leverage) assert trade.initial_stop_loss_pct == -0.1 + assert pytest.approx(trade.orders[-1].stake_amount) == trade.stake_amount # 2nd order - not filling freqtrade.strategy.adjust_trade_position = MagicMock(return_value=120)