margin test_trade_close
This commit is contained in:
parent
9a53aeec10
commit
c98b9ab768
@ -610,33 +610,39 @@ class LocalTrade():
|
|||||||
|
|
||||||
def calculate_interest(self) -> float:
|
def calculate_interest(self) -> float:
|
||||||
# TODO-mg: Need to set other conditions because sometimes self.open_date is not defined, but why would it ever not be set
|
# TODO-mg: Need to set other conditions because sometimes self.open_date is not defined, but why would it ever not be set
|
||||||
|
zero = Decimal(0.0)
|
||||||
if not self.interest_rate or not (self.borrowed):
|
if not self.interest_rate or not (self.borrowed):
|
||||||
return 0.0
|
return zero
|
||||||
|
|
||||||
open_date = self.open_date.replace(tzinfo=None)
|
open_date = self.open_date.replace(tzinfo=None)
|
||||||
now = datetime.utcnow()
|
now = datetime.utcnow()
|
||||||
secPerDay = 86400
|
# sec_per_day = Decimal(86400)
|
||||||
days = ((now - open_date).total_seconds())/secPerDay or 0.0
|
sec_per_hour = Decimal(3600)
|
||||||
hours = days * 24
|
total_seconds = Decimal((now - open_date).total_seconds())
|
||||||
|
#days = total_seconds/sec_per_day or zero
|
||||||
|
hours = total_seconds/sec_per_hour or zero
|
||||||
|
|
||||||
rate = self.interest_rate
|
rate = Decimal(self.interest_rate)
|
||||||
borrowed = self.borrowed
|
borrowed = Decimal(self.borrowed)
|
||||||
|
one = Decimal(1.0)
|
||||||
|
twenty_four = Decimal(24.0)
|
||||||
|
four = Decimal(4.0)
|
||||||
|
|
||||||
if self.exchange == 'binance':
|
if self.exchange == 'binance':
|
||||||
# Rate is per day but accrued hourly or something
|
# Rate is per day but accrued hourly or something
|
||||||
# binance: https://www.binance.com/en-AU/support/faq/360030157812
|
# binance: https://www.binance.com/en-AU/support/faq/360030157812
|
||||||
return borrowed * rate * max(hours, 1)/24 # TODO-mg: Is hours rounded?
|
return borrowed * rate * max(hours, one)/twenty_four # TODO-mg: Is hours rounded?
|
||||||
elif self.exchange == 'kraken':
|
elif self.exchange == 'kraken':
|
||||||
# https://support.kraken.com/hc/en-us/articles/206161568-What-are-the-fees-for-margin-trading-
|
# https://support.kraken.com/hc/en-us/articles/206161568-What-are-the-fees-for-margin-trading-
|
||||||
opening_fee = borrowed * rate
|
opening_fee = borrowed * rate
|
||||||
roll_over_fee = borrowed * rate * max(0, (hours-4)/4)
|
roll_over_fee = borrowed * rate * max(0, (hours-four)/four)
|
||||||
return opening_fee + roll_over_fee
|
return opening_fee + roll_over_fee
|
||||||
elif self.exchange == 'binance_usdm_futures':
|
elif self.exchange == 'binance_usdm_futures':
|
||||||
# ! TODO-mg: This is incorrect, I didn't look it up
|
# ! TODO-mg: This is incorrect, I didn't look it up
|
||||||
return borrowed * (rate/24) * max(hours, 1)
|
return borrowed * (rate/twenty_four) * max(hours, one)
|
||||||
elif self.exchange == 'binance_coinm_futures':
|
elif self.exchange == 'binance_coinm_futures':
|
||||||
# ! TODO-mg: This is incorrect, I didn't look it up
|
# ! TODO-mg: This is incorrect, I didn't look it up
|
||||||
return borrowed * (rate/24) * max(hours, 1)
|
return borrowed * (rate/twenty_four) * max(hours, one)
|
||||||
else:
|
else:
|
||||||
# TODO-mg: make sure this breaks and can't be squelched
|
# TODO-mg: make sure this breaks and can't be squelched
|
||||||
raise OperationalException("Leverage not available on this exchange")
|
raise OperationalException("Leverage not available on this exchange")
|
||||||
@ -731,7 +737,7 @@ class LocalTrade():
|
|||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@ staticmethod
|
@staticmethod
|
||||||
def get_trades_proxy(*, pair: str = None, is_open: bool = None,
|
def get_trades_proxy(*, pair: str = None, is_open: bool = None,
|
||||||
open_date: datetime = None, close_date: datetime = None,
|
open_date: datetime = None, close_date: datetime = None,
|
||||||
) -> List['LocalTrade']:
|
) -> List['LocalTrade']:
|
||||||
@ -765,27 +771,27 @@ class LocalTrade():
|
|||||||
|
|
||||||
return sel_trades
|
return sel_trades
|
||||||
|
|
||||||
@ staticmethod
|
@staticmethod
|
||||||
def close_bt_trade(trade):
|
def close_bt_trade(trade):
|
||||||
LocalTrade.trades_open.remove(trade)
|
LocalTrade.trades_open.remove(trade)
|
||||||
LocalTrade.trades.append(trade)
|
LocalTrade.trades.append(trade)
|
||||||
LocalTrade.total_profit += trade.close_profit_abs
|
LocalTrade.total_profit += trade.close_profit_abs
|
||||||
|
|
||||||
@ staticmethod
|
@staticmethod
|
||||||
def add_bt_trade(trade):
|
def add_bt_trade(trade):
|
||||||
if trade.is_open:
|
if trade.is_open:
|
||||||
LocalTrade.trades_open.append(trade)
|
LocalTrade.trades_open.append(trade)
|
||||||
else:
|
else:
|
||||||
LocalTrade.trades.append(trade)
|
LocalTrade.trades.append(trade)
|
||||||
|
|
||||||
@ staticmethod
|
@staticmethod
|
||||||
def get_open_trades() -> List[Any]:
|
def get_open_trades() -> List[Any]:
|
||||||
"""
|
"""
|
||||||
Query trades from persistence layer
|
Query trades from persistence layer
|
||||||
"""
|
"""
|
||||||
return Trade.get_trades_proxy(is_open=True)
|
return Trade.get_trades_proxy(is_open=True)
|
||||||
|
|
||||||
@ staticmethod
|
@staticmethod
|
||||||
def stoploss_reinitialization(desired_stoploss):
|
def stoploss_reinitialization(desired_stoploss):
|
||||||
"""
|
"""
|
||||||
Adjust initial Stoploss to desired stoploss for all open trades.
|
Adjust initial Stoploss to desired stoploss for all open trades.
|
||||||
@ -887,11 +893,11 @@ class Trade(_DECL_BASE, LocalTrade):
|
|||||||
Trade.query.session.delete(self)
|
Trade.query.session.delete(self)
|
||||||
Trade.commit()
|
Trade.commit()
|
||||||
|
|
||||||
@ staticmethod
|
@staticmethod
|
||||||
def commit():
|
def commit():
|
||||||
Trade.query.session.commit()
|
Trade.query.session.commit()
|
||||||
|
|
||||||
@ staticmethod
|
@staticmethod
|
||||||
def get_trades_proxy(*, pair: str = None, is_open: bool = None,
|
def get_trades_proxy(*, pair: str = None, is_open: bool = None,
|
||||||
open_date: datetime = None, close_date: datetime = None,
|
open_date: datetime = None, close_date: datetime = None,
|
||||||
) -> List['LocalTrade']:
|
) -> List['LocalTrade']:
|
||||||
@ -921,7 +927,7 @@ class Trade(_DECL_BASE, LocalTrade):
|
|||||||
close_date=close_date
|
close_date=close_date
|
||||||
)
|
)
|
||||||
|
|
||||||
@ staticmethod
|
@staticmethod
|
||||||
def get_trades(trade_filter=None) -> Query:
|
def get_trades(trade_filter=None) -> Query:
|
||||||
"""
|
"""
|
||||||
Helper function to query Trades using filters.
|
Helper function to query Trades using filters.
|
||||||
@ -941,7 +947,7 @@ class Trade(_DECL_BASE, LocalTrade):
|
|||||||
else:
|
else:
|
||||||
return Trade.query
|
return Trade.query
|
||||||
|
|
||||||
@ staticmethod
|
@staticmethod
|
||||||
def get_open_order_trades():
|
def get_open_order_trades():
|
||||||
"""
|
"""
|
||||||
Returns all open trades
|
Returns all open trades
|
||||||
@ -949,7 +955,7 @@ class Trade(_DECL_BASE, LocalTrade):
|
|||||||
"""
|
"""
|
||||||
return Trade.get_trades(Trade.open_order_id.isnot(None)).all()
|
return Trade.get_trades(Trade.open_order_id.isnot(None)).all()
|
||||||
|
|
||||||
@ staticmethod
|
@staticmethod
|
||||||
def get_open_trades_without_assigned_fees():
|
def get_open_trades_without_assigned_fees():
|
||||||
"""
|
"""
|
||||||
Returns all open trades which don't have open fees set correctly
|
Returns all open trades which don't have open fees set correctly
|
||||||
@ -960,7 +966,7 @@ class Trade(_DECL_BASE, LocalTrade):
|
|||||||
Trade.is_open.is_(True),
|
Trade.is_open.is_(True),
|
||||||
]).all()
|
]).all()
|
||||||
|
|
||||||
@ staticmethod
|
@staticmethod
|
||||||
def get_closed_trades_without_assigned_fees():
|
def get_closed_trades_without_assigned_fees():
|
||||||
"""
|
"""
|
||||||
Returns all closed trades which don't have fees set correctly
|
Returns all closed trades which don't have fees set correctly
|
||||||
@ -971,7 +977,7 @@ class Trade(_DECL_BASE, LocalTrade):
|
|||||||
Trade.is_open.is_(False),
|
Trade.is_open.is_(False),
|
||||||
]).all()
|
]).all()
|
||||||
|
|
||||||
@ staticmethod
|
@staticmethod
|
||||||
def total_open_trades_stakes() -> float:
|
def total_open_trades_stakes() -> float:
|
||||||
"""
|
"""
|
||||||
Calculates total invested amount in open trades
|
Calculates total invested amount in open trades
|
||||||
@ -985,7 +991,7 @@ class Trade(_DECL_BASE, LocalTrade):
|
|||||||
t.stake_amount for t in LocalTrade.get_trades_proxy(is_open=True))
|
t.stake_amount for t in LocalTrade.get_trades_proxy(is_open=True))
|
||||||
return total_open_stake_amount or 0
|
return total_open_stake_amount or 0
|
||||||
|
|
||||||
@ staticmethod
|
@staticmethod
|
||||||
def get_overall_performance() -> List[Dict[str, Any]]:
|
def get_overall_performance() -> List[Dict[str, Any]]:
|
||||||
"""
|
"""
|
||||||
Returns List of dicts containing all Trades, including profit and trade count
|
Returns List of dicts containing all Trades, including profit and trade count
|
||||||
@ -1010,7 +1016,7 @@ class Trade(_DECL_BASE, LocalTrade):
|
|||||||
for pair, profit, profit_abs, count in pair_rates
|
for pair, profit, profit_abs, count in pair_rates
|
||||||
]
|
]
|
||||||
|
|
||||||
@ staticmethod
|
@staticmethod
|
||||||
def get_best_pair():
|
def get_best_pair():
|
||||||
"""
|
"""
|
||||||
Get best pair with closed trade.
|
Get best pair with closed trade.
|
||||||
@ -1048,7 +1054,7 @@ class PairLock(_DECL_BASE):
|
|||||||
return (f'PairLock(id={self.id}, pair={self.pair}, lock_time={lock_time}, '
|
return (f'PairLock(id={self.id}, pair={self.pair}, lock_time={lock_time}, '
|
||||||
f'lock_end_time={lock_end_time})')
|
f'lock_end_time={lock_end_time})')
|
||||||
|
|
||||||
@ staticmethod
|
@staticmethod
|
||||||
def query_pair_locks(pair: Optional[str], now: datetime) -> Query:
|
def query_pair_locks(pair: Optional[str], now: datetime) -> Query:
|
||||||
"""
|
"""
|
||||||
Get all currently active locks for this pair
|
Get all currently active locks for this pair
|
||||||
|
@ -57,9 +57,9 @@ def log_has_re(line, logs):
|
|||||||
|
|
||||||
def get_args(args):
|
def get_args(args):
|
||||||
return Arguments(args).get_parsed_arg()
|
return Arguments(args).get_parsed_arg()
|
||||||
|
|
||||||
|
|
||||||
# Source: https://stackoverflow.com/questions/29881236/how-to-mock-asyncio-coroutines
|
# Source: https://stackoverflow.com/questions/29881236/how-to-mock-asyncio-coroutines
|
||||||
|
|
||||||
|
|
||||||
def get_mock_coro(return_value):
|
def get_mock_coro(return_value):
|
||||||
async def mock_coro(*args, **kwargs):
|
async def mock_coro(*args, **kwargs):
|
||||||
return return_value
|
return return_value
|
||||||
|
@ -196,6 +196,7 @@ def test_calc_open_close_trade_price(limit_buy_order, limit_sell_order, fee):
|
|||||||
|
|
||||||
@pytest.mark.usefixtures("init_persistence")
|
@pytest.mark.usefixtures("init_persistence")
|
||||||
def test_trade_close(limit_buy_order, limit_sell_order, fee):
|
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(
|
trade = Trade(
|
||||||
pair='ETH/BTC',
|
pair='ETH/BTC',
|
||||||
stake_amount=0.001,
|
stake_amount=0.001,
|
||||||
|
@ -211,33 +211,67 @@ def test_calc_open_close_trade_price(limit_short_order, limit_exit_short_order,
|
|||||||
assert isclose(trade.calc_profit_ratio(), 0.06189996)
|
assert isclose(trade.calc_profit_ratio(), 0.06189996)
|
||||||
|
|
||||||
|
|
||||||
# @pytest.mark.usefixtures("init_persistence")
|
@pytest.mark.usefixtures("init_persistence")
|
||||||
# def test_trade_close(limit_buy_order, limit_sell_order, fee):
|
def test_trade_close(fee, five_hours_ago):
|
||||||
# trade = Trade(
|
"""
|
||||||
# pair='ETH/BTC',
|
This trade lasts for five hours, but the one above lasted for 10 minutes
|
||||||
# stake_amount=0.001,
|
Short trade
|
||||||
# open_rate=0.01,
|
Exchange: Kraken
|
||||||
# amount=5,
|
fee: 0.25% base
|
||||||
# is_open=True,
|
interest_rate: 0.05% per 4 hours
|
||||||
# fee_open=fee.return_value,
|
open_rate: 0.02 base
|
||||||
# fee_close=fee.return_value,
|
close_rate: 0.01 base
|
||||||
# open_date=arrow.Arrow(2020, 2, 1, 15, 5, 1).datetime,
|
leverage: 3.0
|
||||||
# exchange='binance',
|
amount: 5 * 3 = 15 crypto
|
||||||
# )
|
borrowed: 15 crypto
|
||||||
# assert trade.close_profit is None
|
time-periods: 5 hours = 5/4
|
||||||
# assert trade.close_date is None
|
|
||||||
# assert trade.is_open is True
|
interest: borrowed * interest_rate * time-periods
|
||||||
# trade.close(0.02)
|
= 15 * 0.0005 * 5/4 = 0.009375 crypto
|
||||||
# assert trade.is_open is False
|
open_value: (amount * open_rate) - (amount * open_rate * fee)
|
||||||
# assert trade.close_profit == 0.99002494
|
= (15 * 0.02) - (15 * 0.02 * 0.0025)
|
||||||
# assert trade.close_date is not None
|
= 0.29925
|
||||||
# new_date = arrow.Arrow(2020, 2, 2, 15, 6, 1).datetime,
|
amount_closed: amount + interest = 15 + 0.009375 = 15.009375
|
||||||
# assert trade.close_date != new_date
|
close_value: (amount_closed * close_rate) + (amount_closed * close_rate * fee)
|
||||||
# # Close should NOT update close_date if the trade has been closed already
|
= (15.009375 * 0.01) + (15.009375 * 0.01 * 0.0025)
|
||||||
# assert trade.is_open is False
|
= 0.150468984375
|
||||||
# trade.close_date = new_date
|
total_profit = open_value - close_value
|
||||||
# trade.close(0.02)
|
= 0.29925 - 0.150468984375
|
||||||
# assert trade.close_date == new_date
|
= 0.148781015625
|
||||||
|
total_profit_percentage = (open_value/close_value) - 1
|
||||||
|
= (0.29925/0.150468984375)-1
|
||||||
|
= 0.9887819489377738
|
||||||
|
"""
|
||||||
|
trade = Trade(
|
||||||
|
pair='ETH/BTC',
|
||||||
|
stake_amount=0.001,
|
||||||
|
open_rate=0.02,
|
||||||
|
amount=5,
|
||||||
|
is_open=True,
|
||||||
|
fee_open=fee.return_value,
|
||||||
|
fee_close=fee.return_value,
|
||||||
|
open_date=five_hours_ago,
|
||||||
|
exchange='kraken',
|
||||||
|
is_short=True,
|
||||||
|
leverage=3.0,
|
||||||
|
interest_rate=0.0005
|
||||||
|
)
|
||||||
|
assert trade.close_profit is None
|
||||||
|
assert trade.close_date is None
|
||||||
|
assert trade.is_open is True
|
||||||
|
trade.close(0.01)
|
||||||
|
assert trade.is_open is False
|
||||||
|
assert trade.close_profit == 0.98878195
|
||||||
|
assert trade.close_date is not None
|
||||||
|
|
||||||
|
# TODO-mg: Remove these comments probably
|
||||||
|
#new_date = arrow.Arrow(2020, 2, 2, 15, 6, 1).datetime,
|
||||||
|
# assert trade.close_date != new_date
|
||||||
|
# # Close should NOT update close_date if the trade has been closed already
|
||||||
|
# assert trade.is_open is False
|
||||||
|
# trade.close_date = new_date
|
||||||
|
# trade.close(0.02)
|
||||||
|
# assert trade.close_date == new_date
|
||||||
|
|
||||||
|
|
||||||
# @pytest.mark.usefixtures("init_persistence")
|
# @pytest.mark.usefixtures("init_persistence")
|
||||||
|
Loading…
Reference in New Issue
Block a user