Improve naming of calculate_profit method
This commit is contained in:
@@ -1731,10 +1731,10 @@ class FreqtradeBot(LoggingMixin):
|
|||||||
amount = order.safe_filled if fill else order.safe_amount
|
amount = order.safe_filled if fill else order.safe_amount
|
||||||
order_rate: float = order.safe_price
|
order_rate: float = order.safe_price
|
||||||
|
|
||||||
profit = trade.calc_profit_combined(order_rate, amount, trade.open_rate)
|
profit = trade.calculate_profit(order_rate, amount, trade.open_rate)
|
||||||
else:
|
else:
|
||||||
order_rate = trade.safe_close_rate
|
order_rate = trade.safe_close_rate
|
||||||
profit = trade.calc_profit_combined(rate=order_rate)
|
profit = trade.calculate_profit(rate=order_rate)
|
||||||
amount = trade.amount
|
amount = trade.amount
|
||||||
gain = "profit" if profit.profit_ratio > 0 else "loss"
|
gain = "profit" if profit.profit_ratio > 0 else "loss"
|
||||||
|
|
||||||
@@ -1787,7 +1787,7 @@ class FreqtradeBot(LoggingMixin):
|
|||||||
order = self.order_obj_or_raise(order_id, order_or_none)
|
order = self.order_obj_or_raise(order_id, order_or_none)
|
||||||
|
|
||||||
profit_rate: float = trade.safe_close_rate
|
profit_rate: float = trade.safe_close_rate
|
||||||
profit = trade.calc_profit_combined(rate=profit_rate)
|
profit = trade.calculate_profit(rate=profit_rate)
|
||||||
current_rate = self.exchange.get_rate(
|
current_rate = self.exchange.get_rate(
|
||||||
trade.pair, side='exit', is_short=trade.is_short, refresh=False)
|
trade.pair, side='exit', is_short=trade.is_short, refresh=False)
|
||||||
gain = "profit" if profit.profit_ratio > 0 else "loss"
|
gain = "profit" if profit.profit_ratio > 0 else "loss"
|
||||||
|
|||||||
@@ -903,11 +903,11 @@ class LocalTrade:
|
|||||||
:param open_rate: open_rate to use. Defaults to self.open_rate if not provided.
|
:param open_rate: open_rate to use. Defaults to self.open_rate if not provided.
|
||||||
:return: profit in stake currency as float
|
:return: profit in stake currency as float
|
||||||
"""
|
"""
|
||||||
prof = self.calc_profit_combined(rate, amount, open_rate)
|
prof = self.calculate_profit(rate, amount, open_rate)
|
||||||
return prof.profit_abs
|
return prof.profit_abs
|
||||||
|
|
||||||
def calc_profit_combined(self, rate: float, amount: Optional[float] = None,
|
def calculate_profit(self, rate: float, amount: Optional[float] = None,
|
||||||
open_rate: Optional[float] = None) -> ProfitStruct:
|
open_rate: Optional[float] = None) -> ProfitStruct:
|
||||||
"""
|
"""
|
||||||
Calculate profit metrics (absolute, ratio, total, total ratio).
|
Calculate profit metrics (absolute, ratio, total, total ratio).
|
||||||
All calculations include fees.
|
All calculations include fees.
|
||||||
@@ -1020,7 +1020,7 @@ class LocalTrade:
|
|||||||
|
|
||||||
exit_rate = o.safe_price
|
exit_rate = o.safe_price
|
||||||
exit_amount = o.safe_amount_after_fee
|
exit_amount = o.safe_amount_after_fee
|
||||||
prof = self.calc_profit_combined(exit_rate, exit_amount, float(avg_price))
|
prof = self.calculate_profit(exit_rate, exit_amount, float(avg_price))
|
||||||
close_profit_abs += prof.profit_abs
|
close_profit_abs += prof.profit_abs
|
||||||
close_profit = prof.profit_ratio
|
close_profit = prof.profit_ratio
|
||||||
else:
|
else:
|
||||||
|
|||||||
@@ -189,7 +189,7 @@ class RPC:
|
|||||||
|
|
||||||
current_profit = current_profit_abs = current_profit_fiat = NAN
|
current_profit = current_profit_abs = current_profit_fiat = NAN
|
||||||
if not isnan(current_rate):
|
if not isnan(current_rate):
|
||||||
prof = trade.calc_profit_combined(current_rate)
|
prof = trade.calculate_profit(current_rate)
|
||||||
current_profit = prof.profit_ratio
|
current_profit = prof.profit_ratio
|
||||||
current_profit_abs = prof.profit_abs
|
current_profit_abs = prof.profit_abs
|
||||||
total_profit_abs = prof.total_profit
|
total_profit_abs = prof.total_profit
|
||||||
@@ -217,7 +217,7 @@ class RPC:
|
|||||||
)
|
)
|
||||||
|
|
||||||
# Calculate guaranteed profit (in case of trailing stop)
|
# Calculate guaranteed profit (in case of trailing stop)
|
||||||
stop_entry = trade.calc_profit_combined(trade.stop_loss)
|
stop_entry = trade.calculate_profit(trade.stop_loss)
|
||||||
|
|
||||||
stoploss_entry_dist = stop_entry.profit_abs
|
stoploss_entry_dist = stop_entry.profit_abs
|
||||||
stoploss_entry_dist_ratio = stop_entry.profit_ratio
|
stoploss_entry_dist_ratio = stop_entry.profit_ratio
|
||||||
@@ -271,7 +271,7 @@ class RPC:
|
|||||||
profit_str = f'{NAN:.2%}'
|
profit_str = f'{NAN:.2%}'
|
||||||
else:
|
else:
|
||||||
if trade.nr_of_successful_entries > 0:
|
if trade.nr_of_successful_entries > 0:
|
||||||
profit = trade.calc_profit_combined(current_rate)
|
profit = trade.calculate_profit(current_rate)
|
||||||
trade_profit = profit.profit_abs
|
trade_profit = profit.profit_abs
|
||||||
profit_str = f'{profit.profit_ratio:.2%}'
|
profit_str = f'{profit.profit_ratio:.2%}'
|
||||||
else:
|
else:
|
||||||
@@ -492,7 +492,7 @@ class RPC:
|
|||||||
profit_ratio = NAN
|
profit_ratio = NAN
|
||||||
profit_abs = NAN
|
profit_abs = NAN
|
||||||
else:
|
else:
|
||||||
profit = trade.calc_profit_combined(trade.close_rate or current_rate)
|
profit = trade.calculate_profit(trade.close_rate or current_rate)
|
||||||
|
|
||||||
profit_ratio = profit.profit_ratio
|
profit_ratio = profit.profit_ratio
|
||||||
profit_abs = profit.total_profit
|
profit_abs = profit.total_profit
|
||||||
|
|||||||
@@ -1158,7 +1158,7 @@ def test_calc_profit(
|
|||||||
)
|
)
|
||||||
trade.open_order_id = 'something'
|
trade.open_order_id = 'something'
|
||||||
|
|
||||||
profit_res = trade.calc_profit_combined(close_rate)
|
profit_res = trade.calculate_profit(close_rate)
|
||||||
assert pytest.approx(profit_res.profit_abs) == round(profit, 8)
|
assert pytest.approx(profit_res.profit_abs) == round(profit, 8)
|
||||||
assert pytest.approx(profit_res.profit_ratio) == round(profit_ratio, 8)
|
assert pytest.approx(profit_res.profit_ratio) == round(profit_ratio, 8)
|
||||||
val = trade.open_trade_value * (profit_res.profit_ratio) / lev
|
val = trade.open_trade_value * (profit_res.profit_ratio) / lev
|
||||||
@@ -1170,7 +1170,7 @@ def test_calc_profit(
|
|||||||
assert pytest.approx(trade.calc_profit(rate=close_rate)) == round(profit, 8)
|
assert pytest.approx(trade.calc_profit(rate=close_rate)) == round(profit, 8)
|
||||||
assert pytest.approx(trade.calc_profit_ratio(rate=close_rate)) == round(profit_ratio, 8)
|
assert pytest.approx(trade.calc_profit_ratio(rate=close_rate)) == round(profit_ratio, 8)
|
||||||
|
|
||||||
profit_res2 = trade.calc_profit_combined(close_rate, trade.amount, trade.open_rate)
|
profit_res2 = trade.calculate_profit(close_rate, trade.amount, trade.open_rate)
|
||||||
assert pytest.approx(profit_res2.profit_abs) == round(profit, 8)
|
assert pytest.approx(profit_res2.profit_abs) == round(profit, 8)
|
||||||
assert pytest.approx(profit_res2.profit_ratio) == round(profit_ratio, 8)
|
assert pytest.approx(profit_res2.profit_ratio) == round(profit_ratio, 8)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user