Updated interest and ratio calculations to correct functions

This commit is contained in:
Sam Germain 2021-07-10 20:44:57 -06:00
parent 8e52a3a29c
commit 256160740e
6 changed files with 208 additions and 212 deletions

View File

@ -1,5 +1,6 @@
from decimal import Decimal
from enum import Enum
from math import ceil
from freqtrade.exceptions import OperationalException
@ -21,8 +22,9 @@ class InterestMode(Enum):
borrowed, rate, hours = kwargs["borrowed"], kwargs["rate"], kwargs["hours"]
if self.name == "HOURSPERDAY":
return borrowed * rate * max(hours, one)/twenty_four
return borrowed * rate * ceil(hours)/twenty_four
elif self.name == "HOURSPER4":
return borrowed * rate * (1 + max(0, (hours-four)/four))
# Probably rounded based on https://kraken-fees-calculator.github.io/
return borrowed * rate * (1+ceil(hours/four))
else:
raise OperationalException("Leverage not available on this exchange with freqtrade")

View File

@ -609,14 +609,12 @@ class LocalTrade():
def update_order(self, order: Dict) -> None:
Order.update_orders(self.orders, order)
def _calc_open_trade_value(self, amount: Optional[float] = None) -> float:
def _calc_open_trade_value(self) -> float:
"""
Calculate the open_rate including open_fee.
:return: Price in of the open trade incl. Fees
"""
if amount is None:
amount = self.amount
open_trade = Decimal(amount) * Decimal(self.open_rate)
open_trade = Decimal(self.amount) * Decimal(self.open_rate)
fees = open_trade * Decimal(self.fee_open)
if self.is_short:
return float(open_trade - fees)
@ -653,7 +651,6 @@ 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:
"""
@ -721,30 +718,23 @@ 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.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)
leverage = self.leverage or 1.0
if (short_close_zero or long_close_zero):
return 0.0
else:
if self.is_short:
profit_ratio = ((self.open_trade_value - close_trade_value) / stake_value)
profit_ratio = (1 - (close_trade_value/self.open_trade_value)) * leverage
else:
profit_ratio = ((close_trade_value - self.open_trade_value) / stake_value)
profit_ratio = ((close_trade_value/self.open_trade_value) - 1) * leverage
return float(f"{profit_ratio:.8f}")

View File

@ -434,22 +434,22 @@ def leverage_trade(fee):
stake_amount: 15.129 base
borrowed: 60.516 base
leverage: 5
time-periods: 5 hrs( 5/4 time-period of 4 hours)
interest: borrowed * interest_rate * time-periods
= 60.516 * 0.0005 * 5/4 = 0.0378225 base
hours: 5
interest: borrowed * interest_rate * ceil(1 + hours/4)
= 60.516 * 0.0005 * ceil(1 + 5/4) = 0.090774 base
open_value: (amount * open_rate) + (amount * open_rate * fee)
= (615.0 * 0.123) + (615.0 * 0.123 * 0.0025)
= 75.83411249999999
close_value: (amount_closed * close_rate) - (amount_closed * close_rate * fee) - interest
= (615.0 * 0.128) - (615.0 * 0.128 * 0.0025) - 0.0378225
= 78.4853775
= (615.0 * 0.128) - (615.0 * 0.128 * 0.0025) - 0.090774
= 78.432426
total_profit = close_value - open_value
= 78.4853775 - 75.83411249999999
= 2.6512650000000093
total_profit_percentage = total_profit / stake_amount
= 2.6512650000000093 / 15.129
= 0.17524390243902502
= 78.432426 - 75.83411249999999
= 2.5983135000000175
total_profit_percentage = ((close_value/open_value)-1) * leverage
= ((78.432426/75.83411249999999)-1) * 5
= 0.1713156134055116
"""
trade = Trade(
pair='DOGE/BTC',
@ -461,8 +461,8 @@ def leverage_trade(fee):
fee_close=fee.return_value,
open_rate=0.123,
close_rate=0.128,
close_profit=0.17524390243902502,
close_profit_abs=2.6512650000000093,
close_profit=0.1713156134055116,
close_profit_abs=2.5983135000000175,
exchange='kraken',
is_open=False,
open_order_id='dry_run_leverage_sell_12345',

View File

@ -238,7 +238,6 @@ def test_calc_open_close_trade_price(limit_buy_order, limit_sell_order, fee):
@pytest.mark.usefixtures("init_persistence")
def test_trade_close(limit_buy_order, limit_sell_order, fee):
# TODO: limit_buy_order and limit_sell_order aren't used, remove them probably
trade = Trade(
pair='ETH/BTC',
stake_amount=0.001,

View File

@ -27,11 +27,11 @@ def test_interest_kraken_lev(market_lev_buy_order, fee):
time-periods: 10 minutes(rounds up to 1 time-period of 4hrs)
5 hours = 5/4
interest: borrowed * interest_rate * time-periods
= 0.0075414886436454 * 0.0005 * 1 = 3.7707443218227e-06 base
= 0.0075414886436454 * 0.00025 * 5/4 = 2.3567152011391876e-06 base
= 0.0150829772872908 * 0.0005 * 5/4 = 9.42686080455675e-06 base
= 0.0150829772872908 * 0.00025 * 1 = 3.7707443218227e-06 base
interest: borrowed * interest_rate * ceil(1 + time-periods)
= 0.0075414886436454 * 0.0005 * ceil(2) = 7.5414886436454e-06 base
= 0.0075414886436454 * 0.00025 * ceil(9/4) = 5.65611648273405e-06 base
= 0.0150829772872908 * 0.0005 * ceil(9/4) = 2.26244659309362e-05 base
= 0.0150829772872908 * 0.00025 * ceil(2) = 7.5414886436454e-06 base
"""
trade = Trade(
@ -48,19 +48,17 @@ def test_interest_kraken_lev(market_lev_buy_order, fee):
interest_mode=InterestMode.HOURSPER4
)
# 10 minutes round up to 4 hours evenly on kraken so we can predict the exact value
assert float(trade.calculate_interest()) == 3.7707443218227e-06
trade.open_date = datetime.utcnow() - timedelta(hours=5, minutes=0)
# All trade > 5 hours will vary slightly due to execution time and interest calculated
assert float(trade.calculate_interest()) == 7.5414886436454e-06
trade.open_date = datetime.utcnow() - timedelta(hours=4, minutes=55)
assert float(round(trade.calculate_interest(interest_rate=0.00025), 11)
) == round(2.3567152011391876e-06, 11)
) == round(5.65611648273405e-06, 11)
trade = Trade(
pair='ETH/BTC',
stake_amount=0.0037707443218227,
amount=459.95905365,
open_rate=0.00001099,
open_date=datetime.utcnow() - timedelta(hours=5, minutes=0),
open_date=datetime.utcnow() - timedelta(hours=4, minutes=55),
fee_open=fee.return_value,
fee_close=fee.return_value,
exchange='kraken',
@ -70,9 +68,10 @@ def test_interest_kraken_lev(market_lev_buy_order, fee):
)
assert float(round(trade.calculate_interest(), 11)
) == round(9.42686080455675e-06, 11)
) == round(2.26244659309362e-05, 11)
trade.open_date = datetime.utcnow() - timedelta(hours=0, minutes=10)
assert float(trade.calculate_interest(interest_rate=0.00025)) == 3.7707443218227e-06
trade.interest_rate = 0.00025
assert float(trade.calculate_interest(interest_rate=0.00025)) == 7.5414886436454e-06
@pytest.mark.usefixtures("init_persistence")
@ -116,7 +115,7 @@ def test_interest_binance_lev(market_lev_buy_order, fee):
)
# 10 minutes round up to 4 hours evenly on kraken so we can predict the them more accurately
assert round(float(trade.calculate_interest()), 22) == round(4.166666666344583e-08, 22)
trade.open_date = datetime.utcnow() - timedelta(hours=5, minutes=0)
trade.open_date = datetime.utcnow() - timedelta(hours=4, minutes=55)
# All trade > 5 hours will vary slightly due to execution time and interest calculated
assert float(round(trade.calculate_interest(interest_rate=0.00025), 14)
) == round(1.0416666665861459e-07, 14)
@ -126,7 +125,7 @@ def test_interest_binance_lev(market_lev_buy_order, fee):
stake_amount=0.0009999999999226999,
amount=459.95905365,
open_rate=0.00001099,
open_date=datetime.utcnow() - timedelta(hours=5, minutes=0),
open_date=datetime.utcnow() - timedelta(hours=4, minutes=55),
fee_open=fee.return_value,
fee_close=fee.return_value,
exchange='binance',
@ -178,7 +177,7 @@ def test_calc_open_trade_value_lev(market_lev_buy_order, fee):
borrowed: 0.0075414886436454 base
time-periods: 10 minutes(rounds up to 1 time-period of 4hrs)
interest: borrowed * interest_rate * time-periods
= 0.0075414886436454 * 0.0005 * 1 = 3.7707443218227e-06 crypto
= 0.0075414886436454 * 0.0005 * 1 = 7.5414886436454e-06 crypto
open_value: (amount * open_rate) + (amount * open_rate * fee)
= (275.97543219 * 0.00004099) + (275.97543219 * 0.00004099 * 0.0025)
= 0.01134051354788177
@ -243,7 +242,7 @@ def test_calc_open_close_trade_price_lev(limit_lev_buy_order, limit_lev_sell_ord
stake_amount=0.0009999999999226999,
open_rate=0.01,
amount=5,
open_date=datetime.utcnow() - timedelta(hours=5, minutes=0),
open_date=datetime.utcnow() - timedelta(hours=4, minutes=55),
fee_open=fee.return_value,
fee_close=fee.return_value,
exchange='binance',
@ -275,23 +274,20 @@ def test_trade_close_lev(fee):
stake_amount: 0.5
borrowed: 1 base
time-periods: 5/4 periods of 4hrs
interest: borrowed * interest_rate * time-periods
= 1 * 0.0005 * 5/4 = 0.000625 crypto
interest: borrowed * interest_rate * ceil(1 + time-periods)
= 1 * 0.0005 * ceil(9/4) = 0.0015 crypto
open_value: (amount * open_rate) + (amount * open_rate * fee)
= (15 * 0.1) + (15 * 0.1 * 0.0025)
= 1.50375
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
= (15 * 0.2) - (15 * 0.2 * 0.0025) - 0.0015
= 2.991
total_profit = close_value - open_value
= 2.9918750000000003 - 1.50375
= 1.4881250000000001
total_profit_ratio = total_profit / stake_value
= 1.4881250000000001 / 0.50125
= 2.968827930174564
= 2.991 - 1.50375
= 1.4872500000000002
total_profit_ratio = ((close_value/open_value) - 1) * leverage
= ((2.991/1.50375) - 1) * 3
= 2.96708229426434
"""
trade = Trade(
pair='ETH/BTC',
@ -301,7 +297,7 @@ def test_trade_close_lev(fee):
is_open=True,
fee_open=fee.return_value,
fee_close=fee.return_value,
open_date=datetime.utcnow() - timedelta(hours=5, minutes=0),
open_date=datetime.utcnow() - timedelta(hours=4, minutes=55),
exchange='kraken',
leverage=3.0,
interest_rate=0.0005,
@ -312,7 +308,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.968827930174564, 8)
assert trade.close_profit == round(2.96708229426434, 8)
assert trade.close_date is not None
# TODO-mg: Remove these comments probably
@ -337,19 +333,19 @@ def test_calc_close_trade_price_lev(market_lev_buy_order, market_lev_sell_order,
amount: 91.99181073 * leverage(3) = 275.97543219 crypto
stake_amount: 0.0037707443218227
borrowed: 0.0075414886436454 base
time-periods: 10 minutes(rounds up to 1 time-period of 4hrs)
time-periods: 10 minutes = 2
interest: borrowed * interest_rate * time-periods
= 0.0075414886436454 * 0.0005 * 1 = 3.7707443218227e-06 crypto
= 0.0075414886436454 * 0.0005 * 2 = 7.5414886436454e-06 crypto
open_value: (amount * open_rate) + (amount * open_rate * fee)
= (275.97543219 * 0.00004099) + (275.97543219 * 0.00004099 * 0.0025)
= 0.01134051354788177
close_value: (amount_closed * close_rate) - (amount_closed * close_rate * fee) - interest
= (275.97543219 * 0.00001234) - (275.97543219 * 0.00001234 * 0.0025) - 3.7707443218227e-06
= 0.003393252246819716
= (275.97543219 * 0.00001234) - (275.97543219 * 0.00001234 * 0.003) - 3.7707443218227e-06
= 0.003391549478403104
= (275.97543219 * 0.00004173) - (275.97543219 * 0.00004173 * 0.005) - 3.7707443218227e-06
= 0.011455101767040435
= (275.97543219 * 0.00001234) - (275.97543219 * 0.00001234 * 0.0025) - 7.5414886436454e-06
= 0.0033894815024978933
= (275.97543219 * 0.00001234) - (275.97543219 * 0.00001234 * 0.003) - 7.5414886436454e-06
= 0.003387778734081281
= (275.97543219 * 0.00004173) - (275.97543219 * 0.00004173 * 0.005) - 7.5414886436454e-06
= 0.011451331022718612
"""
trade = Trade(
pair='ETH/BTC',
@ -367,12 +363,12 @@ def test_calc_close_trade_price_lev(market_lev_buy_order, market_lev_sell_order,
trade.open_order_id = 'close_trade'
trade.update(market_lev_buy_order) # Buy @ 0.00001099
# Get the close rate price with a custom close rate and a regular fee rate
assert isclose(trade.calc_close_trade_value(rate=0.00001234), 0.003393252246819716)
assert isclose(trade.calc_close_trade_value(rate=0.00001234), 0.0033894815024978933)
# Get the close rate price with a custom close rate and a custom fee rate
assert isclose(trade.calc_close_trade_value(rate=0.00001234, fee=0.003), 0.003391549478403104)
assert isclose(trade.calc_close_trade_value(rate=0.00001234, fee=0.003), 0.003387778734081281)
# Test when we apply a Sell order, and ask price with a custom fee rate
trade.update(market_lev_sell_order)
assert isclose(trade.calc_close_trade_value(fee=0.005), 0.011455101767040435)
assert isclose(trade.calc_close_trade_value(fee=0.005), 0.011451331022718612)
@pytest.mark.usefixtures("init_persistence")
@ -394,6 +390,9 @@ def test_update_limit_order_lev(limit_lev_buy_order, limit_lev_sell_order, fee,
open_value: (amount * open_rate) + (amount * open_rate * fee)
= (272.97543219 * 0.00001099) + (272.97543219 * 0.00001099 * 0.0025)
= 0.0030074999997675204
stake_value = (amount/lev * open_rate) + (amount/lev * open_rate * fee)
= (272.97543219 * 0.00001099) + (272.97543219 * 0.00001099 * 0.0025)
= 0.0010024999999225066
close_value: (amount_closed * close_rate) - (amount_closed * close_rate * fee)
= (272.97543219 * 0.00001173) - (272.97543219 * 0.00001173 * 0.0025)
= 0.003193996815039728
@ -460,24 +459,23 @@ def test_update_market_order_lev(market_lev_buy_order, market_lev_sell_order, fe
amount: = 275.97543219 crypto
stake_amount: 0.0037707443218227
borrowed: 0.0075414886436454 base
time-periods: 10 minutes(rounds up to 1 time-period of 4hrs)
interest: borrowed * interest_rate * time-periods
= 0.0075414886436454 * 0.0005 * 1 = 3.7707443218227e-06 crypto
interest: borrowed * interest_rate * 1+ceil(hours)
= 0.0075414886436454 * 0.0005 * (1+ceil(1)) = 7.5414886436454e-06 crypto
open_value: (amount * open_rate) + (amount * open_rate * fee)
= (275.97543219 * 0.00004099) + (275.97543219 * 0.00004099 * 0.0025)
= 0.01134051354788177
close_value: (amount_closed * close_rate) - (amount_closed * close_rate * fee)
= (275.97543219 * 0.00004173) - (275.97543219 * 0.00004173 * 0.0025)
= 0.011487663648325479
close_value: (amount_closed * close_rate) - (amount_closed * close_rate * fee) - interest
= (275.97543219 * 0.00004173) - (275.97543219 * 0.00004173 * 0.0025) - 7.5414886436454e-06
= 0.011480122159681833
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_value
= 0.0001433793561218866 / 0.0037801711826272568
= 0.03792932890997717
total_profit = close_value - open_value
= 0.011480122159681833 - 0.01134051354788177
= 0.00013960861180006392
total_profit_percentage = ((close_value/open_value) - 1) * leverage
= ((0.011480122159681833 / 0.01134051354788177)-1) * 3
= 0.036931822675563275
"""
trade = Trade(
id=1,
@ -512,7 +510,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.03792932890997717, 8)
assert trade.close_profit == round(0.036931822675563275, 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.
@ -552,39 +550,38 @@ def test_calc_profit_lev(market_lev_buy_order, market_lev_sell_order, fee):
stake_amount: 0.0037707443218227
amount: 91.99181073 * leverage(3) = 275.97543219 crypto
borrowed: 0.0075414886436454 base
time-periods: 10 minutes(rounds up to 1 time-period of 4hrs)
5 hours = 5/4
hours: 1/6, 5 hours
interest: borrowed * interest_rate * time-periods
= 0.0075414886436454 * 0.0005 * 1 = 3.7707443218227e-06 crypto
= 0.0075414886436454 * 0.00025 * 5/4 = 2.3567152011391876e-06 crypto
= 0.0075414886436454 * 0.0005 * 5/4 = 4.713430402278375e-06 crypto
= 0.0075414886436454 * 0.00025 * 1 = 1.88537216091135e-06 crypto
interest: borrowed * interest_rate * ceil(1+hours/4)
= 0.0075414886436454 * 0.0005 * ceil(1+((1/6)/4)) = 7.5414886436454e-06 crypto
= 0.0075414886436454 * 0.00025 * ceil(1+(5/4)) = 5.65611648273405e-06 crypto
= 0.0075414886436454 * 0.0005 * ceil(1+(5/4)) = 1.13122329654681e-05 crypto
= 0.0075414886436454 * 0.00025 * ceil(1+((1/6)/4)) = 3.7707443218227e-06 crypto
open_value: (amount * open_rate) + (amount * open_rate * fee)
= (275.97543219 * 0.00004099) + (275.97543219 * 0.00004099 * 0.0025)
= 0.01134051354788177
close_value: (amount_closed * close_rate) - (amount_closed * close_rate * fee) - interest
(275.97543219 * 0.00005374) - (275.97543219 * 0.00005374 * 0.0025) - 3.7707443218227e-06
= 0.01479007168225405
(275.97543219 * 0.00000437) - (275.97543219 * 0.00000437 * 0.0025) - 2.3567152011391876e-06
= 0.001200640891872485
(275.97543219 * 0.00005374) - (275.97543219 * 0.00005374 * 0.003) - 4.713430402278375e-06
= 0.014781713536310649
(275.97543219 * 0.00000437) - (275.97543219 * 0.00000437 * 0.003) - 1.88537216091135e-06
= 0.0012005092285933775
(275.97543219 * 0.00005374) - (275.97543219 * 0.00005374 * 0.0025) - 7.5414886436454e-06
= 0.014786300937932227
(275.97543219 * 0.00000437) - (275.97543219 * 0.00000437 * 0.0025) - 5.65611648273405e-06
= 0.0011973414905908902
(275.97543219 * 0.00005374) - (275.97543219 * 0.00005374 * 0.003) - 1.13122329654681e-05
= 0.01477511473374746
(275.97543219 * 0.00000437) - (275.97543219 * 0.00000437 * 0.003) - 3.7707443218227e-06
= 0.0011986238564324662
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_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
= 0.014786300937932227 - 0.01134051354788177 = 0.0034457873900504577
= 0.0011973414905908902 - 0.01134051354788177 = -0.01014317205729088
= 0.01477511473374746 - 0.01134051354788177 = 0.00343460118586569
= 0.0011986238564324662 - 0.01134051354788177 = -0.010141889691449303
total_profit_percentage = ((close_value/open_value) - 1) * leverage
((0.014786300937932227/0.01134051354788177) - 1) * 3 = 0.9115426851266561
((0.0011973414905908902/0.01134051354788177) - 1) * 3 = -2.683257336045103
((0.01477511473374746/0.01134051354788177) - 1) * 3 = 0.908583505860866
((0.0011986238564324662/0.01134051354788177) - 1) * 3 = -2.6829181011851926
"""
trade = Trade(
@ -606,35 +603,35 @@ def test_calc_profit_lev(market_lev_buy_order, market_lev_sell_order, fee):
# Higher than open rate
assert trade.calc_profit(rate=0.00005374, interest_rate=0.0005) == round(
0.003449558134372281, 8)
0.0034457873900504577, 8)
assert trade.calc_profit_ratio(
rate=0.00005374, interest_rate=0.0005) == round(0.9125401913610705, 8)
rate=0.00005374, interest_rate=0.0005) == round(0.9115426851266561, 8)
# Lower than open rate
trade.open_date = datetime.utcnow() - timedelta(hours=5, minutes=0)
trade.open_date = datetime.utcnow() - timedelta(hours=4, minutes=55)
assert trade.calc_profit(
rate=0.00000437, interest_rate=0.00025) == round(-0.010139872656009285, 8)
rate=0.00000437, interest_rate=0.00025) == round(-0.01014317205729088, 8)
assert trade.calc_profit_ratio(
rate=0.00000437, interest_rate=0.00025) == round(-2.682384518089991, 8)
rate=0.00000437, interest_rate=0.00025) == round(-2.683257336045103, 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)
interest_rate=0.0005) == round(0.00343460118586569, 8)
assert trade.calc_profit_ratio(rate=0.00005374, fee=0.003,
interest_rate=0.0005) == round(0.9103291417710906, 8)
interest_rate=0.0005) == round(0.908583505860866, 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)
interest_rate=0.00025) == round(-0.010141889691449303, 8)
assert trade.calc_profit_ratio(rate=0.00000437, fee=0.003,
interest_rate=0.00025) == round(-2.6824193480679854, 8)
interest_rate=0.00025) == round(-2.6829181011851926, 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.03792932890997717, 8)
assert trade.calc_profit() == round(0.00013960861180006392, 8)
assert trade.calc_profit_ratio() == round(0.036931822675563275, 8)
# Test with a custom fee rate on the close trade
# assert trade.calc_profit(fee=0.003) == 0.00006163

View File

@ -26,11 +26,11 @@ def test_interest_kraken_short(market_short_order, fee):
time-periods: 10 minutes(rounds up to 1 time-period of 4hrs)
5 hours = 5/4
interest: borrowed * interest_rate * time-periods
= 275.97543219 * 0.0005 * 1 = 0.137987716095 crypto
= 275.97543219 * 0.00025 * 5/4 = 0.086242322559375 crypto
= 459.95905365 * 0.0005 * 5/4 = 0.28747440853125 crypto
= 459.95905365 * 0.00025 * 1 = 0.1149897634125 crypto
interest: borrowed * interest_rate * ceil(1 + time-periods)
= 275.97543219 * 0.0005 * ceil(1+1) = 0.27597543219 crypto
= 275.97543219 * 0.00025 * ceil(9/4) = 0.20698157414249999 crypto
= 459.95905365 * 0.0005 * ceil(9/4) = 0.689938580475 crypto
= 459.95905365 * 0.00025 * ceil(1+1) = 0.229979526825 crypto
"""
trade = Trade(
@ -48,17 +48,17 @@ def test_interest_kraken_short(market_short_order, fee):
interest_mode=InterestMode.HOURSPER4
)
assert float(round(trade.calculate_interest(), 8)) == round(0.137987716095, 8)
trade.open_date = datetime.utcnow() - timedelta(hours=5, minutes=0)
assert float(round(trade.calculate_interest(), 8)) == round(0.27597543219, 8)
trade.open_date = datetime.utcnow() - timedelta(hours=4, minutes=55)
assert float(round(trade.calculate_interest(interest_rate=0.00025), 8)
) == round(0.086242322559375, 8)
) == round(0.20698157414249999, 8)
trade = Trade(
pair='ETH/BTC',
stake_amount=0.001,
amount=459.95905365,
open_rate=0.00001099,
open_date=datetime.utcnow() - timedelta(hours=5, minutes=0),
open_date=datetime.utcnow() - timedelta(hours=4, minutes=55),
fee_open=fee.return_value,
fee_close=fee.return_value,
exchange='kraken',
@ -68,10 +68,10 @@ def test_interest_kraken_short(market_short_order, fee):
interest_mode=InterestMode.HOURSPER4
)
assert float(round(trade.calculate_interest(), 8)) == round(0.28747440853125, 8)
assert float(round(trade.calculate_interest(), 8)) == round(0.689938580475, 8)
trade.open_date = datetime.utcnow() - timedelta(hours=0, minutes=10)
assert float(round(trade.calculate_interest(interest_rate=0.00025), 8)
) == round(0.1149897634125, 8)
) == round(0.229979526825, 8)
@ pytest.mark.usefixtures("init_persistence")
@ -114,7 +114,7 @@ def test_interest_binance_short(market_short_order, fee):
)
assert float(round(trade.calculate_interest(), 8)) == 0.00574949
trade.open_date = datetime.utcnow() - timedelta(hours=5, minutes=0)
trade.open_date = datetime.utcnow() - timedelta(hours=4, minutes=55)
assert float(round(trade.calculate_interest(interest_rate=0.00025), 8)) == 0.01437372
trade = Trade(
@ -122,7 +122,7 @@ def test_interest_binance_short(market_short_order, fee):
stake_amount=0.001,
amount=459.95905365,
open_rate=0.00001099,
open_date=datetime.utcnow() - timedelta(hours=5, minutes=0),
open_date=datetime.utcnow() - timedelta(hours=4, minutes=55),
fee_open=fee.return_value,
fee_close=fee.return_value,
exchange='binance',
@ -218,13 +218,13 @@ def test_calc_close_trade_price_short(market_short_order, market_exit_short_orde
close_rate: 0.00001234 base
amount: = 275.97543219 crypto
borrowed: 275.97543219 crypto
time-periods: 10 minutes(rounds up to 1 time-period of 4hrs)
interest: borrowed * interest_rate * time-periods
= 275.97543219 * 0.0005 * 1 = 0.137987716095 crypto
amount_closed: amount + interest = 275.97543219 + 0.137987716095 = 276.113419906095
hours: 10 minutes = 1/6
interest: borrowed * interest_rate * ceil(1 + hours/4)
= 275.97543219 * 0.0005 * ceil(1 + ((1/6)/4)) = 0.27597543219 crypto
amount_closed: amount + interest = 275.97543219 + 0.27597543219 = 276.25140762219
close_value: (amount_closed * close_rate) + (amount_closed * close_rate * fee)
= (276.113419906095 * 0.00001234) + (276.113419906095 * 0.00001234 * 0.0025)
= 0.01134618380465571
= (276.25140762219 * 0.00004099) + (276.25140762219 * 0.00004099 * 0.005)
= 0.011380162924425737
"""
trade = Trade(
pair='ETH/BTC',
@ -243,12 +243,12 @@ def test_calc_close_trade_price_short(market_short_order, market_exit_short_orde
trade.open_order_id = 'close_trade'
trade.update(market_short_order) # Buy @ 0.00001099
# Get the close rate price with a custom close rate and a regular fee rate
assert isclose(trade.calc_close_trade_value(rate=0.00001234), 0.003415757700645315)
assert isclose(trade.calc_close_trade_value(rate=0.00001234), 0.0034174647259)
# Get the close rate price with a custom close rate and a custom fee rate
assert isclose(trade.calc_close_trade_value(rate=0.00001234, fee=0.003), 0.0034174613204461354)
assert isclose(trade.calc_close_trade_value(rate=0.00001234, fee=0.003), 0.0034191691971679986)
# Test when we apply a Sell order, and ask price with a custom fee rate
trade.update(market_exit_short_order)
assert isclose(trade.calc_close_trade_value(fee=0.005), 0.011374478527360586)
assert isclose(trade.calc_close_trade_value(fee=0.005), 0.011380162924425737)
@ pytest.mark.usefixtures("init_persistence")
@ -273,19 +273,21 @@ def test_calc_open_close_trade_price_short(limit_short_order, limit_exit_short_o
close_value: (amount_closed * close_rate) + (amount_closed * close_rate * fee)
= (91.0012890436177 * 0.00001099) + (91.0012890436177 * 0.00001099 * 0.0025)
= 0.001002604427005832
stake_value = (amount/lev * open_rate) - (amount/lev * open_rate * fee)
= 0.0010646656050132426
total_profit = open_value - close_value
= 0.0010646656050132426 - 0.001002604427005832
= 0.00006206117800741065
total_profit_percentage = (close_value - open_value) / stake_amount
= (0.0010646656050132426 - 0.0010025208853391716)/0.0010673339398629
= 0.05822425142973869
total_profit_percentage = (close_value - open_value) / stake_value
= (0.0010646656050132426 - 0.001002604427005832)/0.0010646656050132426
= 0.05829170935473088
"""
trade = Trade(
pair='ETH/BTC',
stake_amount=0.0010673339398629,
open_rate=0.01,
amount=5,
open_date=datetime.utcnow() - timedelta(hours=5, minutes=0),
open_date=datetime.utcnow() - timedelta(hours=4, minutes=55),
fee_open=fee.return_value,
fee_close=fee.return_value,
exchange='binance',
@ -302,8 +304,7 @@ def test_calc_open_close_trade_price_short(limit_short_order, limit_exit_short_o
# Profit in BTC
assert round(trade.calc_profit(), 8) == round(0.00006206117800741065, 8)
# Profit in percent
# TODO-mg get this working
# assert round(trade.calc_profit_ratio(), 11) == round(0.05822425142973869, 11)
assert round(trade.calc_profit_ratio(), 8) == round(0.05829170935473088, 8)
@ pytest.mark.usefixtures("init_persistence")
@ -322,20 +323,20 @@ def test_trade_close_short(fee):
time-periods: 5 hours = 5/4
interest: borrowed * interest_rate * time-periods
= 15 * 0.0005 * 5/4 = 0.009375 crypto
= 15 * 0.0005 * ceil(1 + 5/4) = 0.0225 crypto
open_value: (amount * open_rate) - (amount * open_rate * fee)
= (15 * 0.02) - (15 * 0.02 * 0.0025)
= 0.29925
amount_closed: amount + interest = 15 + 0.009375 = 15.009375
amount_closed: amount + interest = 15 + 0.009375 = 15.0225
close_value: (amount_closed * close_rate) + (amount_closed * close_rate * fee)
= (15.009375 * 0.01) + (15.009375 * 0.01 * 0.0025)
= 0.150468984375
= (15.0225 * 0.01) + (15.0225 * 0.01 * 0.0025)
= 0.15060056250000003
total_profit = open_value - close_value
= 0.29925 - 0.150468984375
= 0.148781015625
total_profit_percentage = total_profit / stake_amount
= 0.148781015625 / 0.1
= 1.4878101562500001
= 0.29925 - 0.15060056250000003
= 0.14864943749999998
total_profit_percentage = (1-(close_value/open_value)) * leverage
= (1 - (0.15060056250000003/0.29925)) * 3
= 1.4902199248120298
"""
trade = Trade(
pair='ETH/BTC',
@ -345,7 +346,7 @@ def test_trade_close_short(fee):
is_open=True,
fee_open=fee.return_value,
fee_close=fee.return_value,
open_date=datetime.utcnow() - timedelta(hours=5, minutes=0),
open_date=datetime.utcnow() - timedelta(hours=4, minutes=55),
exchange='kraken',
is_short=True,
leverage=3.0,
@ -357,7 +358,7 @@ def test_trade_close_short(fee):
assert trade.is_open is True
trade.close(0.01)
assert trade.is_open is False
assert trade.close_profit == round(1.4878101562500001, 8)
assert trade.close_profit == round(1.4902199248120298, 8)
assert trade.close_date is not None
# TODO-mg: Remove these comments probably
@ -396,9 +397,9 @@ def test_update_with_binance_short(limit_short_order, limit_exit_short_order, fe
total_profit = open_value - close_value
= 0.0010646656050132426 - 0.0010025208853391716
= 0.00006214471967407108
total_profit_percentage = (close_value - open_value) / stake_amount
= 0.00006214471967407108 / 0.0010673339398629
= 0.05822425142973869
total_profit_percentage = (1 - (close_value/open_value)) * leverage
= (1 - (0.0010025208853391716/0.0010646656050132426)) * 1
= 0.05837017687191848
"""
trade = Trade(
@ -437,7 +438,7 @@ def test_update_with_binance_short(limit_short_order, limit_exit_short_order, fe
trade.update(limit_exit_short_order)
# assert trade.open_order_id is None
assert trade.close_rate == 0.00001099
assert trade.close_profit == 0.05822425
assert trade.close_profit == round(0.05837017687191848, 8)
assert trade.close_date is not None
assert log_has_re(r"LIMIT_BUY has been fulfilled for Trade\(id=2, "
r"pair=ETH/BTC, amount=90.99181073, open_rate=0.00001173, open_since=.*\).",
@ -463,20 +464,21 @@ def test_update_market_order_short(
borrowed: 275.97543219 crypto
time-periods: 10 minutes(rounds up to 1 time-period of 4hrs)
interest: borrowed * interest_rate * time-periods
= 275.97543219 * 0.0005 * 1 = 0.137987716095 crypto
= 275.97543219 * 0.0005 * 2 = 0.27597543219 crypto
open_value: (amount * open_rate) - (amount * open_rate * fee)
= 275.97543219 * 0.00004173 - 275.97543219 * 0.00004173 * 0.0025
= 0.011487663648325479
amount_closed: amount + interest = 275.97543219 + 0.137987716095 = 276.113419906095
amount_closed: amount + interest = 275.97543219 + 0.27597543219 = 276.25140762219
close_value: (amount_closed * close_rate) + (amount_closed * close_rate * fee)
= (276.113419906095 * 0.00004099) + (276.113419906095 * 0.00004099 * 0.0025)
= 0.01134618380465571
= (276.25140762219 * 0.00004099) + (276.25140762219 * 0.00004099 * 0.0025)
= 0.0034174647259
total_profit = open_value - close_value
= 0.011487663648325479 - 0.01134618380465571
= 0.00014147984366976937
= 0.011487663648325479 - 0.0034174647259
= 0.00013580958689582596
total_profit_percentage = total_profit / stake_amount
= 0.00014147984366976937 / 0.0038388182617629
= 0.036855051222142936
= (1 - (close_value/open_value)) * leverage
= (1 - (0.0034174647259/0.011487663648325479)) * 3
= 0.03546663387440563
"""
trade = Trade(
id=1,
@ -511,7 +513,7 @@ def test_update_market_order_short(
trade.update(market_exit_short_order)
assert trade.open_order_id is None
assert trade.close_rate == 0.00004099
assert trade.close_profit == 0.03685505
assert trade.close_profit == round(0.03546663387440563, 8)
assert trade.close_date is not None
# TODO-mg: The amount should maybe be the opening amount + the interest
# TODO-mg: Uncomment the next assert and make it work.
@ -527,7 +529,7 @@ def test_calc_profit_short(market_short_order, market_exit_short_order, fee):
Market trade on Kraken at 3x leverage
Short trade
fee: 0.25% base or 0.3%
interest_rate: 0.05%, 0.25% per 4 hrs
interest_rate: 0.05%, 0.025% per 4 hrs
open_rate: 0.00004173 base
close_rate: 0.00004099 base
stake_amount: 0.0038388182617629
@ -537,38 +539,42 @@ def test_calc_profit_short(market_short_order, market_exit_short_order, fee):
5 hours = 5/4
interest: borrowed * interest_rate * time-periods
= 275.97543219 * 0.0005 * 1 = 0.137987716095 crypto
= 275.97543219 * 0.00025 * 5/4 = 0.086242322559375 crypto
= 275.97543219 * 0.0005 * 5/4 = 0.17248464511875 crypto
= 275.97543219 * 0.00025 * 1 = 0.0689938580475 crypto
= 275.97543219 * 0.0005 * ceil(1+1) = 0.27597543219 crypto
= 275.97543219 * 0.00025 * ceil(1+5/4) = 0.20698157414249999 crypto
= 275.97543219 * 0.0005 * ceil(1+5/4) = 0.41396314828499997 crypto
= 275.97543219 * 0.00025 * ceil(1+1) = 0.27597543219 crypto
= 275.97543219 * 0.00025 * ceil(1+1) = 0.27597543219 crypto
open_value: (amount * open_rate) - (amount * open_rate * fee)
= (275.97543219 * 0.00004173) - (275.97543219 * 0.00004173 * 0.0025)
= 0.011487663648325479
amount_closed: amount + interest
= 275.97543219 + 0.137987716095 = 276.113419906095
= 275.97543219 + 0.086242322559375 = 276.06167451255936
= 275.97543219 + 0.17248464511875 = 276.14791683511874
= 275.97543219 + 0.0689938580475 = 276.0444260480475
= 275.97543219 + 0.27597543219 = 276.25140762219
= 275.97543219 + 0.20698157414249999 = 276.1824137641425
= 275.97543219 + 0.41396314828499997 = 276.389395338285
= 275.97543219 + 0.27597543219 = 276.25140762219
close_value: (amount_closed * close_rate) + (amount_closed * close_rate * fee)
(276.113419906095 * 0.00004374) + (276.113419906095 * 0.00004374 * 0.0025)
= 0.012107393989159325
(276.06167451255936 * 0.00000437) + (276.06167451255936 * 0.00000437 * 0.0025)
= 0.0012094054914139338
(276.14791683511874 * 0.00004374) + (276.14791683511874 * 0.00004374 * 0.003)
= 0.012114946012015198
(276.0444260480475 * 0.00000437) + (276.0444260480475 * 0.00000437 * 0.003)
= 0.0012099330842554573
(276.25140762219 * 0.00004374) + (276.25140762219 * 0.00004374 * 0.0025)
= 0.012113444660818078
(276.1824137641425 * 0.00000437) + (276.1824137641425 * 0.00000437 * 0.0025)
= 0.0012099344410196758
(276.389395338285 * 0.00004374) + (276.389395338285 * 0.00004374 * 0.003)
= 0.012125539968552874
(276.25140762219 * 0.00000437) + (276.25140762219 * 0.00000437 * 0.003)
= 0.0012102354919246037
(276.25140762219 * 0.00004099) + (276.25140762219 * 0.00004099 * 0.0025)
= 0.011351854061429653
total_profit = open_value - close_value
= print(0.011487663648325479 - 0.012107393989159325) = -0.0006197303408338461
= print(0.011487663648325479 - 0.0012094054914139338) = 0.010278258156911545
= print(0.011487663648325479 - 0.012114946012015198) = -0.0006272823636897188
= print(0.011487663648325479 - 0.0012099330842554573) = 0.010277730564070022
total_profit_percentage = (close_value - open_value) / stake_amount
(0.011487663648325479 - 0.012107393989159325)/0.0038388182617629 = -0.16143779115744006
(0.011487663648325479 - 0.0012094054914139338)/0.0038388182617629 = 2.677453699564163
(0.011487663648325479 - 0.012114946012015198)/0.0038388182617629 = -0.16340506919482353
(0.011487663648325479 - 0.0012099330842554573)/0.0038388182617629 = 2.677316263299785
= 0.011487663648325479 - 0.012113444660818078 = -0.0006257810124925996
= 0.011487663648325479 - 0.0012099344410196758 = 0.010277729207305804
= 0.011487663648325479 - 0.012125539968552874 = -0.0006378763202273957
= 0.011487663648325479 - 0.0012102354919246037 = 0.010277428156400875
= 0.011487663648325479 - 0.011351854061429653 = 0.00013580958689582596
total_profit_percentage = (1-(close_value/open_value)) * leverage
(1-(0.012113444660818078 /0.011487663648325479))*3 = -0.16342252828332549
(1-(0.0012099344410196758/0.011487663648325479))*3 = 2.6840259748040123
(1-(0.012125539968552874 /0.011487663648325479))*3 = -0.16658121435868578
(1-(0.0012102354919246037/0.011487663648325479))*3 = 2.68394735544829
(1-(0.011351854061429653/0.011487663648325479))*3 = 0.03546663387440563
"""
trade = Trade(
pair='ETH/BTC',
@ -588,34 +594,36 @@ def test_calc_profit_short(market_short_order, market_exit_short_order, fee):
# Custom closing rate and regular fee rate
# Higher than open rate
assert trade.calc_profit(rate=0.00004374, interest_rate=0.0005) == round(-0.00061973, 8)
assert trade.calc_profit(
rate=0.00004374, interest_rate=0.0005) == round(-0.0006257810124925996, 8)
assert trade.calc_profit_ratio(
rate=0.00004374, interest_rate=0.0005) == round(-0.16143779115744006, 8)
rate=0.00004374, interest_rate=0.0005) == round(-0.16342252828332549, 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.01027826, 8)
trade.open_date = datetime.utcnow() - timedelta(hours=4, minutes=55)
assert trade.calc_profit(rate=0.00000437, interest_rate=0.00025) == round(
0.010277729207305804, 8)
assert trade.calc_profit_ratio(
rate=0.00000437, interest_rate=0.00025) == round(2.677453699564163, 8)
rate=0.00000437, interest_rate=0.00025) == round(2.6840259748040123, 8)
# Custom closing rate and custom fee rate
# Higher than open rate
assert trade.calc_profit(rate=0.00004374, fee=0.003,
interest_rate=0.0005) == round(-0.00062728, 8)
interest_rate=0.0005) == round(-0.0006378763202273957, 8)
assert trade.calc_profit_ratio(rate=0.00004374, fee=0.003,
interest_rate=0.0005) == round(-0.16340506919482353, 8)
interest_rate=0.0005) == round(-0.16658121435868578, 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.01027773, 8)
interest_rate=0.00025) == round(0.010277428156400875, 8)
assert trade.calc_profit_ratio(rate=0.00000437, fee=0.003,
interest_rate=0.00025) == round(2.677316263299785, 8)
interest_rate=0.00025) == round(2.68394735544829, 8)
# Test when we apply a Sell order. Sell higher than open rate @ 0.00001173
# Test when we apply a exit short order.
trade.update(market_exit_short_order)
assert trade.calc_profit() == round(0.00014148, 8)
assert trade.calc_profit_ratio() == round(0.03685505, 8)
assert trade.calc_profit(rate=0.00004099) == round(0.00013580958689582596, 8)
assert trade.calc_profit_ratio() == round(0.03546663387440563, 8)
# Test with a custom fee rate on the close trade
# assert trade.calc_profit(fee=0.003) == 0.00006163
@ -769,4 +777,4 @@ def test_get_best_pair_short(fee):
res = Trade.get_best_pair()
assert len(res) == 2
assert res[0] == 'DOGE/BTC'
assert res[1] == 0.17524390243902502
assert res[1] == 0.1713156134055116