From a2ff6326470afde76cdfe9d70966d83151afbded Mon Sep 17 00:00:00 2001 From: Matthias Date: Fri, 1 May 2020 16:00:42 +0200 Subject: [PATCH] Add update_fee method to persistence --- freqtrade/persistence.py | 18 ++++++++++++++++++ tests/test_persistence.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/freqtrade/persistence.py b/freqtrade/persistence.py index 36e2c7ffd..fec095daa 100644 --- a/freqtrade/persistence.py +++ b/freqtrade/persistence.py @@ -376,6 +376,24 @@ class Trade(_DECL_BASE): self ) + def update_fee(self, fee_cost: float, fee_currency: Optional[str], fee_rate: Optional[float], + side: str) -> None: + """ + Update Fee parameters. Only acts once per side + """ + if side == 'buy' and self.fee_open_currency is None: + self.fee_open_cost = fee_cost + self.fee_open_currency = fee_currency + if fee_rate is not None: + self.fee_open = fee_rate + # Assume close-fee will fall into the same fee category and take an educated guess + self.fee_close = fee_rate + elif side == 'sell' and self.fee_close_currency is None: + self.fee_close_cost = fee_cost + self.fee_close_currency = fee_currency + if fee_rate is not None: + self.fee_close = fee_rate + def _calc_open_trade_price(self) -> float: """ Calculate the open_rate including open_fee. diff --git a/tests/test_persistence.py b/tests/test_persistence.py index aa3a59b87..fa6f84ce0 100644 --- a/tests/test_persistence.py +++ b/tests/test_persistence.py @@ -874,6 +874,39 @@ def test_stoploss_reinitialization(default_conf, fee): assert trade_adj.initial_stop_loss_pct == -0.04 +def test_update_fee(fee): + trade = Trade( + pair='ETH/BTC', + stake_amount=0.001, + fee_open=fee.return_value, + open_date=arrow.utcnow().shift(hours=-2).datetime, + amount=10, + fee_close=fee.return_value, + exchange='bittrex', + open_rate=1, + max_rate=1, + ) + fee_cost = 0.15 + fee_currency = 'BTC' + fee_rate = 0.0075 + assert trade.fee_open_currency is None + + trade.update_fee(fee_cost, fee_currency, fee_rate, 'buy') + assert trade.fee_open_currency == fee_currency + assert trade.fee_open_cost == fee_cost + assert trade.fee_open == fee_rate + # Setting buy rate should "guess" close rate + assert trade.fee_close == fee_rate + assert trade.fee_close_currency is None + assert trade.fee_close_cost is None + + fee_rate = 0.0076 + trade.update_fee(fee_cost, fee_currency, fee_rate, 'sell') + assert trade.fee_close == 0.0076 + assert trade.fee_close_cost == fee_cost + assert trade.fee_close == fee_rate + + @pytest.mark.usefixtures("init_persistence") def test_total_open_trades_stakes(fee):