Kraken interest test comes really close to passing

Added more trades to conftest_trades
This commit is contained in:
Sam Germain 2021-06-28 10:01:18 -06:00
parent df360fb281
commit 5ac03762f0
3 changed files with 55 additions and 52 deletions

View File

@ -270,7 +270,7 @@ class LocalTrade():
liquidation_price: float = None liquidation_price: float = None
is_short: bool = False is_short: bool = False
_borrowed: float = 0.0 _borrowed: float = 0.0
leverage: float = None # * You probably want to use LocalTrade.leverage instead _leverage: float = None # * You probably want to use LocalTrade.leverage instead
# @property # @property
# def base_currency(self) -> str: # def base_currency(self) -> str:
@ -280,7 +280,7 @@ class LocalTrade():
@property @property
def amount(self) -> float: def amount(self) -> float:
if self.leverage is not None: if self._leverage is not None:
return self._amount * self.leverage return self._amount * self.leverage
else: else:
return self._amount return self._amount
@ -291,20 +291,29 @@ class LocalTrade():
@property @property
def borrowed(self) -> float: def borrowed(self) -> float:
if self.leverage is not None: if self._leverage is not None:
if self.is_short: if self.is_short:
# If shorting the full amount must be borrowed # If shorting the full amount must be borrowed
return self._amount * self.leverage return self._amount * self._leverage
else: else:
# If not shorting, then the trader already owns a bit # If not shorting, then the trader already owns a bit
return self._amount * (self.leverage-1) return self._amount * (self._leverage-1)
else: else:
return self._borrowed return self._borrowed
@borrowed.setter @borrowed.setter
def borrowed(self, value): def borrowed(self, value):
self._borrowed = value self._borrowed = value
self.leverage = None self._leverage = None
@property
def leverage(self) -> float:
return self._leverage
@leverage.setter
def leverage(self, value):
self._leverage = value
self._borrowed = None
# End of margin trading properties # End of margin trading properties
@ -896,7 +905,7 @@ class Trade(_DECL_BASE, LocalTrade):
timeframe = Column(Integer, nullable=True) timeframe = Column(Integer, nullable=True)
# Margin trading properties # Margin trading properties
leverage: float = None # * You probably want to use LocalTrade.leverage instead _leverage: float = None # * You probably want to use LocalTrade.leverage instead
_borrowed = Column(Float, nullable=False, default=0.0) _borrowed = Column(Float, nullable=False, default=0.0)
interest_rate = Column(Float, nullable=False, default=0.0) interest_rate = Column(Float, nullable=False, default=0.0)
liquidation_price = Column(Float, nullable=True) liquidation_price = Column(Float, nullable=True)

View File

@ -347,13 +347,13 @@ def short_trade(fee):
trade = Trade( trade = Trade(
pair='ETC/BTC', pair='ETC/BTC',
stake_amount=0.001, stake_amount=0.001,
amount=123.0, # TODO-mg: In BTC? amount=123.0,
amount_requested=123.0, amount_requested=123.0,
fee_open=fee.return_value, fee_open=fee.return_value,
fee_close=fee.return_value, fee_close=fee.return_value,
open_rate=0.123, open_rate=0.123,
close_rate=0.128, close_rate=0.128,
close_profit=0.005, # TODO-mg: Would this be -0.005 or -0.025 close_profit=0.025,
close_profit_abs=0.000584127, close_profit_abs=0.000584127,
exchange='binance', exchange='binance',
is_open=False, is_open=False,

View File

@ -502,14 +502,14 @@ def test_interest_kraken(market_short_order, ten_minutes_ago, five_hours_ago, fe
interest: borrowed * interest_rate * time-periods interest: borrowed * interest_rate * time-periods
= 275.97543219 * 0.0005 * 1 = 0.137987716095 crypto = 275.97543219 * 0.0005 * 1 = 0.137987716095 crypto
= 275.97543219 * 0.00025 * 5/4 = 0.086242322559375 crypto = 275.97543219 * 0.00025 * 5/4 = 0.086242322559375 crypto
= 459.95905365 * 0.0005 * 5/4 = 0.17248464511875 crypto = 459.95905365 * 0.0005 * 5/4 = 0.28747440853125 crypto
= 459.95905365 * 0.00025 * 1 = 0.0689938580475 crypto = 459.95905365 * 0.00025 * 1 = 0.1149897634125 crypto
""" """
trade = Trade( trade = Trade(
pair='ETH/BTC', pair='ETH/BTC',
stake_amount=0.001, stake_amount=0.001,
amount=5, amount=91.99181073,
open_rate=0.00001099, open_rate=0.00001099,
open_date=ten_minutes_ago, open_date=ten_minutes_ago,
fee_open=fee.return_value, fee_open=fee.return_value,
@ -519,19 +519,18 @@ def test_interest_kraken(market_short_order, ten_minutes_ago, five_hours_ago, fe
leverage=3.0, leverage=3.0,
interest_rate=0.0005 interest_rate=0.0005
) )
trade.update(market_short_order) # Buy @ 0.00001099
assert isclose(float("{:.15f}".format(trade.calculate_interest())), 0.137987716095) assert float(round(trade.calculate_interest(), 8)) == 0.13798772
trade.open_date = five_hours_ago trade.open_date = five_hours_ago
assert isclose(float("{:.15f}".format( assert float(round(trade.calculate_interest(interest_rate=0.00025), 8)
trade.calculate_interest(interest_rate=0.00025))), 0.086242322559375) ) == 0.08624232 # TODO: Fails with 0.08624233
trade = Trade( trade = Trade(
pair='ETH/BTC', pair='ETH/BTC',
stake_amount=0.001, stake_amount=0.001,
amount=5, amount=91.99181073,
open_rate=0.00001099, open_rate=0.00001099,
open_date=ten_minutes_ago, open_date=five_hours_ago,
fee_open=fee.return_value, fee_open=fee.return_value,
fee_close=fee.return_value, fee_close=fee.return_value,
exchange='kraken', exchange='kraken',
@ -539,12 +538,10 @@ def test_interest_kraken(market_short_order, ten_minutes_ago, five_hours_ago, fe
leverage=5.0, leverage=5.0,
interest_rate=0.0005 interest_rate=0.0005
) )
trade.update(market_short_order) # Buy @ 0.00001099
assert isclose(float("{:.15f}".format(trade.calculate_interest())), 0.17248464511875) assert float(round(trade.calculate_interest(), 8)) == 0.28747441 # TODO: Fails with 0.28747445
trade.open_date = ten_minutes_ago trade.open_date = ten_minutes_ago
assert isclose(float("{:.15f}".format( assert float(round(trade.calculate_interest(interest_rate=0.00025), 8)) == 0.11498976
trade.calculate_interest(interest_rate=0.00025))), 0.0689938580475)
@pytest.mark.usefixtures("init_persistence") @pytest.mark.usefixtures("init_persistence")
@ -565,50 +562,47 @@ def test_interest_binance(market_short_order, ten_minutes_ago, five_hours_ago, f
5 hours = 5/24 5 hours = 5/24
interest: borrowed * interest_rate * time-periods interest: borrowed * interest_rate * time-periods
= print(275.97543219 * 0.0005 * 1/24) = 0.005749488170625 crypto = 275.97543219 * 0.0005 * 1/24 = 0.005749488170625 crypto
= print(275.97543219 * 0.00025 * 5/24) = 0.0143737204265625 crypto = 275.97543219 * 0.00025 * 5/24 = 0.0143737204265625 crypto
= print(459.95905365 * 0.0005 * 5/24) = 0.047912401421875 crypto = 459.95905365 * 0.0005 * 5/24 = 0.047912401421875 crypto
= print(459.95905365 * 0.00025 * 1/24) = 0.0047912401421875 crypto = 459.95905365 * 0.00025 * 1/24 = 0.0047912401421875 crypto
""" """
trade = Trade( trade = Trade(
pair='ETH/BTC', pair='ETH/BTC',
stake_amount=0.001, stake_amount=0.001,
amount=5, amount=275.97543219,
open_rate=0.00001099, open_rate=0.00001099,
open_date=ten_minutes_ago, open_date=ten_minutes_ago,
fee_open=fee.return_value, fee_open=fee.return_value,
fee_close=fee.return_value, fee_close=fee.return_value,
exchange='binance', exchange='binance',
is_short=True, is_short=True,
borrowed=275.97543219,
interest_rate=0.0005 interest_rate=0.0005
) )
trade.update(market_short_order) # Buy @ 0.00001099
assert isclose(float("{:.15f}".format(trade.calculate_interest())), 0.005749488170625) assert float(round(trade.calculate_interest(), 8)) == 0.00574949
trade.open_date = five_hours_ago trade.open_date = five_hours_ago
assert isclose(float("{:.15f}".format( assert float(round(trade.calculate_interest(interest_rate=0.00025), 8)) == 0.01437372
trade.calculate_interest(interest_rate=0.00025))), 0.0143737204265625)
trade = Trade( trade = Trade(
pair='ETH/BTC', pair='ETH/BTC',
stake_amount=0.001, stake_amount=0.001,
amount=5, amount=459.95905365,
open_rate=0.00001099, open_rate=0.00001099,
open_date=ten_minutes_ago, open_date=five_hours_ago,
fee_open=fee.return_value, fee_open=fee.return_value,
fee_close=fee.return_value, fee_close=fee.return_value,
exchange='binance', exchange='binance',
is_short=True, is_short=True,
leverage=5.0, borrowed=459.95905365,
interest_rate=0.0005 interest_rate=0.0005
) )
trade.update(market_short_order) # Buy @ 0.00001099
assert isclose(float("{:.15f}".format(trade.calculate_interest())), 0.047912401421875) assert float(round(trade.calculate_interest(), 8)) == 0.04791240
trade.open_date = ten_minutes_ago trade.open_date = ten_minutes_ago
assert isclose(float("{:.15f}".format( assert float(round(trade.calculate_interest(interest_rate=0.00025), 8)) == 0.00479124
trade.calculate_interest(interest_rate=0.00025))), 0.0047912401421875)
def test_adjust_stop_loss(fee): def test_adjust_stop_loss(fee):