Rename profit_percent to profit_ratio to be consistent
This commit is contained in:
		| @@ -108,7 +108,7 @@ def load_trades_from_db(db_url: str) -> pd.DataFrame: | |||||||
|     trades = pd.DataFrame([(t.pair, |     trades = pd.DataFrame([(t.pair, | ||||||
|                             t.open_date.replace(tzinfo=timezone.utc), |                             t.open_date.replace(tzinfo=timezone.utc), | ||||||
|                             t.close_date.replace(tzinfo=timezone.utc) if t.close_date else None, |                             t.close_date.replace(tzinfo=timezone.utc) if t.close_date else None, | ||||||
|                             t.calc_profit(), t.calc_profit_percent(), |                             t.calc_profit(), t.calc_profit_ratio(), | ||||||
|                             t.open_rate, t.close_rate, t.amount, |                             t.open_rate, t.close_rate, t.amount, | ||||||
|                             (round((t.close_date.timestamp() - t.open_date.timestamp()) / 60, 2) |                             (round((t.close_date.timestamp() - t.open_date.timestamp()) / 60, 2) | ||||||
|                                 if t.close_date else None), |                                 if t.close_date else None), | ||||||
|   | |||||||
| @@ -950,7 +950,7 @@ class FreqtradeBot: | |||||||
|         profit_trade = trade.calc_profit(rate=profit_rate) |         profit_trade = trade.calc_profit(rate=profit_rate) | ||||||
|         # Use cached ticker here - it was updated seconds ago. |         # Use cached ticker here - it was updated seconds ago. | ||||||
|         current_rate = self.get_sell_rate(trade.pair, False) |         current_rate = self.get_sell_rate(trade.pair, False) | ||||||
|         profit_percent = trade.calc_profit_percent(profit_rate) |         profit_percent = trade.calc_profit_ratio(profit_rate) | ||||||
|         gain = "profit" if profit_percent > 0 else "loss" |         gain = "profit" if profit_percent > 0 else "loss" | ||||||
|  |  | ||||||
|         msg = { |         msg = { | ||||||
|   | |||||||
| @@ -329,7 +329,7 @@ class Backtesting: | |||||||
|                 closerate = self._get_close_rate(sell_row, trade, sell, trade_dur) |                 closerate = self._get_close_rate(sell_row, trade, sell, trade_dur) | ||||||
|  |  | ||||||
|                 return BacktestResult(pair=pair, |                 return BacktestResult(pair=pair, | ||||||
|                                       profit_percent=trade.calc_profit_percent(rate=closerate), |                                       profit_percent=trade.calc_profit_ratio(rate=closerate), | ||||||
|                                       profit_abs=trade.calc_profit(rate=closerate), |                                       profit_abs=trade.calc_profit(rate=closerate), | ||||||
|                                       open_time=buy_row.date, |                                       open_time=buy_row.date, | ||||||
|                                       close_time=sell_row.date, |                                       close_time=sell_row.date, | ||||||
| @@ -345,7 +345,7 @@ class Backtesting: | |||||||
|             # no sell condition found - trade stil open at end of backtest period |             # no sell condition found - trade stil open at end of backtest period | ||||||
|             sell_row = partial_ticker[-1] |             sell_row = partial_ticker[-1] | ||||||
|             bt_res = BacktestResult(pair=pair, |             bt_res = BacktestResult(pair=pair, | ||||||
|                                     profit_percent=trade.calc_profit_percent(rate=sell_row.open), |                                     profit_percent=trade.calc_profit_ratio(rate=sell_row.open), | ||||||
|                                     profit_abs=trade.calc_profit(rate=sell_row.open), |                                     profit_abs=trade.calc_profit(rate=sell_row.open), | ||||||
|                                     open_time=buy_row.date, |                                     open_time=buy_row.date, | ||||||
|                                     close_time=sell_row.date, |                                     close_time=sell_row.date, | ||||||
|   | |||||||
| @@ -185,6 +185,7 @@ class Trade(_DECL_BASE): | |||||||
|     fee_close = Column(Float, nullable=False, default=0.0) |     fee_close = Column(Float, nullable=False, default=0.0) | ||||||
|     open_rate = Column(Float) |     open_rate = Column(Float) | ||||||
|     open_rate_requested = Column(Float) |     open_rate_requested = Column(Float) | ||||||
|  |     # open_trade_price - calcuated via _calc_open_trade_price | ||||||
|     open_trade_price = Column(Float) |     open_trade_price = Column(Float) | ||||||
|     close_rate = Column(Float) |     close_rate = Column(Float) | ||||||
|     close_rate_requested = Column(Float) |     close_rate_requested = Column(Float) | ||||||
| @@ -331,7 +332,7 @@ class Trade(_DECL_BASE): | |||||||
|         and marks trade as closed |         and marks trade as closed | ||||||
|         """ |         """ | ||||||
|         self.close_rate = Decimal(rate) |         self.close_rate = Decimal(rate) | ||||||
|         self.close_profit = self.calc_profit_percent() |         self.close_profit = self.calc_profit_ratio() | ||||||
|         self.close_date = datetime.utcnow() |         self.close_date = datetime.utcnow() | ||||||
|         self.is_open = False |         self.is_open = False | ||||||
|         self.open_order_id = None |         self.open_order_id = None | ||||||
| @@ -390,14 +391,14 @@ class Trade(_DECL_BASE): | |||||||
|         profit = close_trade_price - self.open_trade_price |         profit = close_trade_price - self.open_trade_price | ||||||
|         return float(f"{profit:.8f}") |         return float(f"{profit:.8f}") | ||||||
|  |  | ||||||
|     def calc_profit_percent(self, rate: Optional[float] = None, |     def calc_profit_ratio(self, rate: Optional[float] = None, | ||||||
|                           fee: Optional[float] = None) -> float: |                           fee: Optional[float] = None) -> float: | ||||||
|         """ |         """ | ||||||
|         Calculates the profit in percentage (including fee). |         Calculates the profit as ratio (including fee). | ||||||
|         :param rate: rate to compare with (optional). |         :param rate: rate to compare with (optional). | ||||||
|             If rate is not set self.close_rate will be used |             If rate is not set self.close_rate will be used | ||||||
|         :param fee: fee to use on the close rate (optional). |         :param fee: fee to use on the close rate (optional). | ||||||
|         :return: profit in percentage as float |         :return: profit ratio as float | ||||||
|         """ |         """ | ||||||
|         close_trade_price = self.calc_close_trade_price( |         close_trade_price = self.calc_close_trade_price( | ||||||
|             rate=(rate or self.close_rate), |             rate=(rate or self.close_rate), | ||||||
|   | |||||||
| @@ -123,7 +123,7 @@ class RPC: | |||||||
|                     current_rate = self._freqtrade.get_sell_rate(trade.pair, False) |                     current_rate = self._freqtrade.get_sell_rate(trade.pair, False) | ||||||
|                 except DependencyException: |                 except DependencyException: | ||||||
|                     current_rate = NAN |                     current_rate = NAN | ||||||
|                 current_profit = trade.calc_profit_percent(current_rate) |                 current_profit = trade.calc_profit_ratio(current_rate) | ||||||
|                 fmt_close_profit = (f'{round(trade.close_profit * 100, 2):.2f}%' |                 fmt_close_profit = (f'{round(trade.close_profit * 100, 2):.2f}%' | ||||||
|                                     if trade.close_profit else None) |                                     if trade.close_profit else None) | ||||||
|                 trade_dict = trade.to_json() |                 trade_dict = trade.to_json() | ||||||
| @@ -151,7 +151,7 @@ class RPC: | |||||||
|                     current_rate = self._freqtrade.get_sell_rate(trade.pair, False) |                     current_rate = self._freqtrade.get_sell_rate(trade.pair, False) | ||||||
|                 except DependencyException: |                 except DependencyException: | ||||||
|                     current_rate = NAN |                     current_rate = NAN | ||||||
|                 trade_perc = (100 * trade.calc_profit_percent(current_rate)) |                 trade_perc = (100 * trade.calc_profit_ratio(current_rate)) | ||||||
|                 trade_profit = trade.calc_profit(current_rate) |                 trade_profit = trade.calc_profit(current_rate) | ||||||
|                 profit_str = f'{trade_perc:.2f}%' |                 profit_str = f'{trade_perc:.2f}%' | ||||||
|                 if self._fiat_converter: |                 if self._fiat_converter: | ||||||
| @@ -240,7 +240,7 @@ class RPC: | |||||||
|                 durations.append((trade.close_date - trade.open_date).total_seconds()) |                 durations.append((trade.close_date - trade.open_date).total_seconds()) | ||||||
|  |  | ||||||
|             if not trade.is_open: |             if not trade.is_open: | ||||||
|                 profit_percent = trade.calc_profit_percent() |                 profit_percent = trade.calc_profit_ratio() | ||||||
|                 profit_closed_coin.append(trade.calc_profit()) |                 profit_closed_coin.append(trade.calc_profit()) | ||||||
|                 profit_closed_perc.append(profit_percent) |                 profit_closed_perc.append(profit_percent) | ||||||
|             else: |             else: | ||||||
| @@ -249,7 +249,7 @@ class RPC: | |||||||
|                     current_rate = self._freqtrade.get_sell_rate(trade.pair, False) |                     current_rate = self._freqtrade.get_sell_rate(trade.pair, False) | ||||||
|                 except DependencyException: |                 except DependencyException: | ||||||
|                     current_rate = NAN |                     current_rate = NAN | ||||||
|                 profit_percent = trade.calc_profit_percent(rate=current_rate) |                 profit_percent = trade.calc_profit_ratio(rate=current_rate) | ||||||
|  |  | ||||||
|             profit_all_coin.append( |             profit_all_coin.append( | ||||||
|                 trade.calc_profit(rate=trade.close_rate or current_rate) |                 trade.calc_profit(rate=trade.close_rate or current_rate) | ||||||
|   | |||||||
| @@ -296,7 +296,7 @@ class IStrategy(ABC): | |||||||
|         """ |         """ | ||||||
|         # Set current rate to low for backtesting sell |         # Set current rate to low for backtesting sell | ||||||
|         current_rate = low or rate |         current_rate = low or rate | ||||||
|         current_profit = trade.calc_profit_percent(current_rate) |         current_profit = trade.calc_profit_ratio(current_rate) | ||||||
|  |  | ||||||
|         trade.adjust_min_max_rates(high or current_rate) |         trade.adjust_min_max_rates(high or current_rate) | ||||||
|  |  | ||||||
| @@ -311,7 +311,7 @@ class IStrategy(ABC): | |||||||
|  |  | ||||||
|         # Set current rate to high for backtesting sell |         # Set current rate to high for backtesting sell | ||||||
|         current_rate = high or rate |         current_rate = high or rate | ||||||
|         current_profit = trade.calc_profit_percent(current_rate) |         current_profit = trade.calc_profit_ratio(current_rate) | ||||||
|         config_ask_strategy = self.config.get('ask_strategy', {}) |         config_ask_strategy = self.config.get('ask_strategy', {}) | ||||||
|  |  | ||||||
|         if buy and config_ask_strategy.get('ignore_roi_if_buy_signal', False): |         if buy and config_ask_strategy.get('ignore_roi_if_buy_signal', False): | ||||||
| @@ -360,7 +360,7 @@ class IStrategy(ABC): | |||||||
|             sl_offset = self.trailing_stop_positive_offset |             sl_offset = self.trailing_stop_positive_offset | ||||||
|  |  | ||||||
|             # Make sure current_profit is calculated using high for backtesting. |             # Make sure current_profit is calculated using high for backtesting. | ||||||
|             high_profit = current_profit if not high else trade.calc_profit_percent(high) |             high_profit = current_profit if not high else trade.calc_profit_ratio(high) | ||||||
|  |  | ||||||
|             # Don't update stoploss if trailing_only_offset_is_reached is true. |             # Don't update stoploss if trailing_only_offset_is_reached is true. | ||||||
|             if not (self.trailing_only_offset_is_reached and high_profit < sl_offset): |             if not (self.trailing_only_offset_is_reached and high_profit < sl_offset): | ||||||
|   | |||||||
| @@ -381,7 +381,7 @@ def test_api_performance(botclient, mocker, ticker, fee): | |||||||
|         close_rate=0.265441, |         close_rate=0.265441, | ||||||
|  |  | ||||||
|     ) |     ) | ||||||
|     trade.close_profit = trade.calc_profit_percent() |     trade.close_profit = trade.calc_profit_ratio() | ||||||
|     Trade.session.add(trade) |     Trade.session.add(trade) | ||||||
|  |  | ||||||
|     trade = Trade( |     trade = Trade( | ||||||
| @@ -396,7 +396,7 @@ def test_api_performance(botclient, mocker, ticker, fee): | |||||||
|         fee_open=fee.return_value, |         fee_open=fee.return_value, | ||||||
|         close_rate=0.391 |         close_rate=0.391 | ||||||
|     ) |     ) | ||||||
|     trade.close_profit = trade.calc_profit_percent() |     trade.close_profit = trade.calc_profit_ratio() | ||||||
|     Trade.session.add(trade) |     Trade.session.add(trade) | ||||||
|     Trade.session.flush() |     Trade.session.flush() | ||||||
|  |  | ||||||
|   | |||||||
| @@ -1543,7 +1543,8 @@ def test_update_trade_state(mocker, default_conf, limit_buy_order, caplog) -> No | |||||||
|     assert log_has_re('Found open order for.*', caplog) |     assert log_has_re('Found open order for.*', caplog) | ||||||
|  |  | ||||||
|  |  | ||||||
| def test_update_trade_state_withorderdict(default_conf, trades_for_order, limit_buy_order, fee, mocker): | def test_update_trade_state_withorderdict(default_conf, trades_for_order, limit_buy_order, fee, | ||||||
|  |                                           mocker): | ||||||
|     mocker.patch('freqtrade.exchange.Exchange.get_trades_for_order', return_value=trades_for_order) |     mocker.patch('freqtrade.exchange.Exchange.get_trades_for_order', return_value=trades_for_order) | ||||||
|     # get_order should not be called!! |     # get_order should not be called!! | ||||||
|     mocker.patch('freqtrade.exchange.Exchange.get_order', MagicMock(side_effect=ValueError)) |     mocker.patch('freqtrade.exchange.Exchange.get_order', MagicMock(side_effect=ValueError)) | ||||||
|   | |||||||
| @@ -226,7 +226,7 @@ def test_calc_open_close_trade_price(limit_buy_order, limit_sell_order, fee): | |||||||
|     assert trade.calc_profit() == 0.00006217 |     assert trade.calc_profit() == 0.00006217 | ||||||
|  |  | ||||||
|     # Profit in percent |     # Profit in percent | ||||||
|     assert trade.calc_profit_percent() == 0.06201058 |     assert trade.calc_profit_ratio() == 0.06201058 | ||||||
|  |  | ||||||
|  |  | ||||||
| @pytest.mark.usefixtures("init_persistence") | @pytest.mark.usefixtures("init_persistence") | ||||||
| @@ -367,7 +367,7 @@ def test_calc_profit(limit_buy_order, limit_sell_order, fee): | |||||||
|  |  | ||||||
|  |  | ||||||
| @pytest.mark.usefixtures("init_persistence") | @pytest.mark.usefixtures("init_persistence") | ||||||
| def test_calc_profit_percent(limit_buy_order, limit_sell_order, fee): | def test_calc_profit_ratio(limit_buy_order, limit_sell_order, fee): | ||||||
|     trade = Trade( |     trade = Trade( | ||||||
|         pair='ETH/BTC', |         pair='ETH/BTC', | ||||||
|         stake_amount=0.001, |         stake_amount=0.001, | ||||||
| @@ -381,17 +381,17 @@ def test_calc_profit_percent(limit_buy_order, limit_sell_order, fee): | |||||||
|     trade.update(limit_buy_order)  # Buy @ 0.00001099 |     trade.update(limit_buy_order)  # Buy @ 0.00001099 | ||||||
|  |  | ||||||
|     # Get percent of profit with a custom rate (Higher than open rate) |     # Get percent of profit with a custom rate (Higher than open rate) | ||||||
|     assert trade.calc_profit_percent(rate=0.00001234) == 0.11723875 |     assert trade.calc_profit_ratio(rate=0.00001234) == 0.11723875 | ||||||
|  |  | ||||||
|     # Get percent of profit with a custom rate (Lower than open rate) |     # Get percent of profit with a custom rate (Lower than open rate) | ||||||
|     assert trade.calc_profit_percent(rate=0.00000123) == -0.88863828 |     assert trade.calc_profit_ratio(rate=0.00000123) == -0.88863828 | ||||||
|  |  | ||||||
|     # Test when we apply a Sell order. Sell higher than open rate @ 0.00001173 |     # Test when we apply a Sell order. Sell higher than open rate @ 0.00001173 | ||||||
|     trade.update(limit_sell_order) |     trade.update(limit_sell_order) | ||||||
|     assert trade.calc_profit_percent() == 0.06201058 |     assert trade.calc_profit_ratio() == 0.06201058 | ||||||
|  |  | ||||||
|     # Test with a custom fee rate on the close trade |     # Test with a custom fee rate on the close trade | ||||||
|     assert trade.calc_profit_percent(fee=0.003) == 0.06147824 |     assert trade.calc_profit_ratio(fee=0.003) == 0.06147824 | ||||||
|  |  | ||||||
|  |  | ||||||
| @pytest.mark.usefixtures("init_persistence") | @pytest.mark.usefixtures("init_persistence") | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user