updated ratio_calc_profit function

This commit is contained in:
Sam Germain 2021-07-08 05:37:54 -06:00
parent 006a60e5a4
commit 8e52a3a29c
2 changed files with 62 additions and 37 deletions

View File

@ -609,12 +609,14 @@ class LocalTrade():
def update_order(self, order: Dict) -> None:
Order.update_orders(self.orders, order)
def _calc_open_trade_value(self) -> float:
def _calc_open_trade_value(self, amount: Optional[float] = None) -> float:
"""
Calculate the open_rate including open_fee.
:return: Price in of the open trade incl. Fees
"""
open_trade = Decimal(self.amount) * Decimal(self.open_rate)
if amount is None:
amount = self.amount
open_trade = Decimal(amount) * Decimal(self.open_rate)
fees = open_trade * Decimal(self.fee_open)
if self.is_short:
return float(open_trade - fees)
@ -651,6 +653,7 @@ class LocalTrade():
return self.interest_mode(borrowed=borrowed, rate=rate, hours=hours)
def calc_close_trade_value(self, rate: Optional[float] = None,
fee: Optional[float] = None,
interest_rate: Optional[float] = None) -> float:
"""
@ -718,23 +721,30 @@ class LocalTrade():
If interest_rate is not set self.interest_rate will be used
:return: profit ratio as float
"""
close_trade_value = self.calc_close_trade_value(
rate=(rate or self.close_rate),
fee=(fee or self.fee_close),
interest_rate=(interest_rate or self.interest_rate)
)
if ((self.is_short and close_trade_value == 0.0) or
(not self.is_short and self.open_trade_value == 0.0)):
if self.leverage is None:
leverage = 1.0
else:
leverage = self.leverage
stake_value = self._calc_open_trade_value(amount=(self.amount/leverage))
short_close_zero = (self.is_short and close_trade_value == 0.0)
long_close_zero = (not self.is_short and self.open_trade_value == 0.0)
if (short_close_zero or long_close_zero):
return 0.0
else:
if self.has_no_leverage:
# TODO-mg: Use one profit_ratio calculation
profit_ratio = (close_trade_value/self.open_trade_value) - 1
else:
if self.is_short:
profit_ratio = ((self.open_trade_value - close_trade_value) / self.stake_amount)
profit_ratio = ((self.open_trade_value - close_trade_value) / stake_value)
else:
profit_ratio = ((close_trade_value - self.open_trade_value) / self.stake_amount)
profit_ratio = ((close_trade_value - self.open_trade_value) / stake_value)
return float(f"{profit_ratio:.8f}")

View File

@ -228,12 +228,15 @@ def test_calc_open_close_trade_price_lev(limit_lev_buy_order, limit_lev_sell_ord
- (272.97543219 * 0.00001173 * 0.0025)
- 2.0833333331722917e-07
= 0.003193788481706411
stake_value: (amount/lev * open_rate) + (amount/lev * open_rate * fee)
= (272.97543219/3 * 0.00001099) + (272.97543219/3 * 0.00001099 * 0.0025)
= 0.0010024999999225066
total_profit = close_value - open_value
= 0.003193788481706411 - 0.0030074999997675204
= 0.00018628848193889044
total_profit_percentage = total_profit / stake_amount
= 0.00018628848193889054 / 0.0009999999999226999
= 0.18628848195329067
total_profit_percentage = total_profit / stake_value
= 0.00018628848193889054 / 0.0010024999999225066
= 0.18582392214792087
"""
trade = Trade(
pair='ETH/BTC',
@ -257,7 +260,7 @@ def test_calc_open_close_trade_price_lev(limit_lev_buy_order, limit_lev_sell_ord
# Profit in BTC
assert round(trade.calc_profit(), 8) == round(0.00018628848193889054, 8)
# Profit in percent
assert round(trade.calc_profit_ratio(), 8) == round(0.18628848195329067, 8)
assert round(trade.calc_profit_ratio(), 8) == round(0.18582392214792087, 8)
@pytest.mark.usefixtures("init_persistence")
@ -280,12 +283,15 @@ def test_trade_close_lev(fee):
close_value: (amount * close_rate) + (amount * close_rate * fee) - interest
= (15 * 0.2) - (15 * 0.2 * 0.0025) - 0.000625
= 2.9918750000000003
stake_value: (amount/lev * open_rate) + (amount/lev * open_rate * fee)
= ((15/3) * 0.1) + ((15/3) * 0.1 * 0.0025)
= 0.50125
total_profit = close_value - open_value
= 2.9918750000000003 - 1.50375
= 1.4881250000000001
total_profit_percentage = total_profit / stake_amount
= 1.4881250000000001 / 0.5
= 2.9762500000000003
total_profit_ratio = total_profit / stake_value
= 1.4881250000000001 / 0.50125
= 2.968827930174564
"""
trade = Trade(
pair='ETH/BTC',
@ -306,7 +312,7 @@ def test_trade_close_lev(fee):
assert trade.is_open is True
trade.close(0.2)
assert trade.is_open is False
assert trade.close_profit == round(2.9762500000000003, 8)
assert trade.close_profit == round(2.968827930174564, 8)
assert trade.close_date is not None
# TODO-mg: Remove these comments probably
@ -391,12 +397,15 @@ def test_update_limit_order_lev(limit_lev_buy_order, limit_lev_sell_order, fee,
close_value: (amount_closed * close_rate) - (amount_closed * close_rate * fee)
= (272.97543219 * 0.00001173) - (272.97543219 * 0.00001173 * 0.0025)
= 0.003193996815039728
stake_value: (amount/lev * open_rate) + (amount/lev * open_rate * fee)
= (272.97543219/3 * 0.00001099) + (272.97543219/3 * 0.00001099 * 0.0025)
= 0.0010024999999225066
total_profit = close_value - open_value - interest
= 0.003193996815039728 - 0.0030074999997675204 - 4.166666666344583e-08
= 0.00018645514860554435
total_profit_percentage = total_profit / stake_amount
= 0.00018645514860554435 / 0.0009999999999226999
= 0.18645514861995735
total_profit_percentage = total_profit / stake_value
= 0.00018645514860554435 / 0.0010024999999225066
= 0.1859901731869899
"""
trade = Trade(
@ -432,7 +441,7 @@ def test_update_limit_order_lev(limit_lev_buy_order, limit_lev_sell_order, fee,
trade.update(limit_lev_sell_order)
# assert trade.open_order_id is None
assert trade.close_rate == 0.00001173
assert trade.close_profit == round(0.18645514861995735, 8)
assert trade.close_profit == round(0.1859901731869899, 8)
assert trade.close_date is not None
assert log_has_re(r"LIMIT_SELL has been fulfilled for Trade\(id=2, "
r"pair=ETH/BTC, amount=272.97543219, open_rate=0.00001099, open_since=.*\).",
@ -460,12 +469,15 @@ def test_update_market_order_lev(market_lev_buy_order, market_lev_sell_order, fe
close_value: (amount_closed * close_rate) - (amount_closed * close_rate * fee)
= (275.97543219 * 0.00004173) - (275.97543219 * 0.00004173 * 0.0025)
= 0.011487663648325479
stake_value: (amount/lev * open_rate) + (amount/lev * open_rate * fee)
= (275.97543219/3 * 0.00004099) + (275.97543219/3 * 0.00004099 * 0.0025)
= 0.0037801711826272568
total_profit = close_value - open_value - interest
= 0.011487663648325479 - 0.01134051354788177 - 3.7707443218227e-06
= 0.0001433793561218866
total_profit_percentage = total_profit / stake_amount
= 0.0001433793561218866 / 0.0037707443218227
= 0.03802415223225211
total_profit_percentage = total_profit / stake_value
= 0.0001433793561218866 / 0.0037801711826272568
= 0.03792932890997717
"""
trade = Trade(
id=1,
@ -500,7 +512,7 @@ def test_update_market_order_lev(market_lev_buy_order, market_lev_sell_order, fe
trade.update(market_lev_sell_order)
assert trade.open_order_id is None
assert trade.close_rate == 0.00004173
assert trade.close_profit == round(0.03802415223225211, 8)
assert trade.close_profit == round(0.03792932890997717, 8)
assert trade.close_date is not None
# TODO: The amount should maybe be the opening amount + the interest
# TODO: Uncomment the next assert and make it work.
@ -560,16 +572,19 @@ def test_calc_profit_lev(market_lev_buy_order, market_lev_sell_order, fee):
= 0.014781713536310649
(275.97543219 * 0.00000437) - (275.97543219 * 0.00000437 * 0.003) - 1.88537216091135e-06
= 0.0012005092285933775
stake_value: (amount/lev * open_rate) + (amount/lev * open_rate * fee)
= (275.97543219/3 * 0.00004099) + (275.97543219/3 * 0.00004099 * 0.0025)
= 0.0037801711826272568
total_profit = close_value - open_value
= 0.01479007168225405 - 0.01134051354788177 = 0.003449558134372281
= 0.001200640891872485 - 0.01134051354788177 = -0.010139872656009285
= 0.014781713536310649 - 0.01134051354788177 = 0.0034411999884288794
= 0.0012005092285933775 - 0.01134051354788177 = -0.010140004319288392
total_profit_percentage = total_profit / stake_amount
0.003449558134372281/0.0037707443218227 = 0.9148215418394732
-0.010139872656009285/0.0037707443218227 = -2.6890904793852157
0.0034411999884288794/0.0037707443218227 = 0.9126049646255184
-0.010140004319288392/0.0037707443218227 = -2.6891253964381554
total_profit_percentage = total_profit / stake_value
0.003449558134372281/0.0037801711826272568 = 0.9125401913610705
-0.010139872656009285/0.0037801711826272568 = -2.682384518089991
0.0034411999884288794/0.0037801711826272568 = 0.9103291417710906
-0.010140004319288392/0.0037801711826272568 = -2.6824193480679854
"""
trade = Trade(
@ -593,33 +608,33 @@ def test_calc_profit_lev(market_lev_buy_order, market_lev_sell_order, fee):
assert trade.calc_profit(rate=0.00005374, interest_rate=0.0005) == round(
0.003449558134372281, 8)
assert trade.calc_profit_ratio(
rate=0.00005374, interest_rate=0.0005) == round(0.9148215418394732, 8)
rate=0.00005374, interest_rate=0.0005) == round(0.9125401913610705, 8)
# Lower than open rate
trade.open_date = datetime.utcnow() - timedelta(hours=5, minutes=0)
assert trade.calc_profit(
rate=0.00000437, interest_rate=0.00025) == round(-0.010139872656009285, 8)
assert trade.calc_profit_ratio(
rate=0.00000437, interest_rate=0.00025) == round(-2.6890904793852157, 8)
rate=0.00000437, interest_rate=0.00025) == round(-2.682384518089991, 8)
# Custom closing rate and custom fee rate
# Higher than open rate
assert trade.calc_profit(rate=0.00005374, fee=0.003,
interest_rate=0.0005) == round(0.0034411999884288794, 8)
assert trade.calc_profit_ratio(rate=0.00005374, fee=0.003,
interest_rate=0.0005) == round(0.9126049646255184, 8)
interest_rate=0.0005) == round(0.9103291417710906, 8)
# Lower than open rate
trade.open_date = datetime.utcnow() - timedelta(hours=0, minutes=10)
assert trade.calc_profit(rate=0.00000437, fee=0.003,
interest_rate=0.00025) == round(-0.010140004319288392, 8)
assert trade.calc_profit_ratio(rate=0.00000437, fee=0.003,
interest_rate=0.00025) == round(-2.6891253964381554, 8)
interest_rate=0.00025) == round(-2.6824193480679854, 8)
# Test when we apply a Sell order. Sell higher than open rate @ 0.00001173
trade.update(market_lev_sell_order)
assert trade.calc_profit() == round(0.0001433793561218866, 8)
assert trade.calc_profit_ratio() == round(0.03802415223225211, 8)
assert trade.calc_profit_ratio() == round(0.03792932890997717, 8)
# Test with a custom fee rate on the close trade
# assert trade.calc_profit(fee=0.003) == 0.00006163