Adding templates for leverage/short tests

All previous pytests pass
This commit is contained in:
Sam Germain
2021-06-21 21:26:31 -06:00
parent 10979361c1
commit 000932eed0
6 changed files with 197 additions and 39 deletions

View File

@@ -47,7 +47,7 @@ def migrate_trades_table(decl_base, inspector, engine, table_back_name: str, col
min_rate = get_column_def(cols, 'min_rate', 'null')
sell_reason = get_column_def(cols, 'sell_reason', 'null')
strategy = get_column_def(cols, 'strategy', 'null')
leverage = get_column_def(cols, 'leverage', '0.0')
borrowed = get_column_def(cols, 'borrowed', '0.0')
borrowed_currency = get_column_def(cols, 'borrowed_currency', 'null')
@@ -66,7 +66,8 @@ def migrate_trades_table(decl_base, inspector, engine, table_back_name: str, col
close_profit_abs = get_column_def(
cols, 'close_profit_abs',
f"(amount * close_rate * (1 - {fee_close})) - {open_trade_value}")
close_order_status = get_column_def(cols, 'close_order_status', 'null')
# TODO-mg: update to exit order status
sell_order_status = get_column_def(cols, 'sell_order_status', 'null')
amount_requested = get_column_def(cols, 'amount_requested', 'amount')
# Schema migration necessary
@@ -88,7 +89,7 @@ def migrate_trades_table(decl_base, inspector, engine, table_back_name: str, col
stake_amount, amount, amount_requested, open_date, close_date, open_order_id,
stop_loss, stop_loss_pct, initial_stop_loss, initial_stop_loss_pct,
stoploss_order_id, stoploss_last_update,
max_rate, min_rate, sell_reason, close_order_status, strategy,
max_rate, min_rate, sell_reason, sell_order_status, strategy,
timeframe, open_trade_value, close_profit_abs,
leverage, borrowed, borrowed_currency, collateral_currency, interest_rate, liquidation_price, is_short
)
@@ -111,7 +112,7 @@ def migrate_trades_table(decl_base, inspector, engine, table_back_name: str, col
{initial_stop_loss_pct} initial_stop_loss_pct,
{stoploss_order_id} stoploss_order_id, {stoploss_last_update} stoploss_last_update,
{max_rate} max_rate, {min_rate} min_rate, {sell_reason} sell_reason,
{close_order_status} close_order_status,
{sell_order_status} sell_order_status,
{strategy} strategy, {timeframe} timeframe,
{open_trade_value} open_trade_value, {close_profit_abs} close_profit_abs,
{leverage} leverage, {borrowed} borrowed, {borrowed_currency} borrowed_currency,
@@ -120,7 +121,9 @@ def migrate_trades_table(decl_base, inspector, engine, table_back_name: str, col
from {table_back_name}
"""))
#TODO: Does leverage go in here?
# TODO: Does leverage go in here?
def migrate_open_orders_to_trades(engine):
with engine.begin() as connection:
connection.execute(text("""

View File

@@ -132,7 +132,7 @@ class Order(_DECL_BASE):
order_filled_date = Column(DateTime, nullable=True)
order_update_date = Column(DateTime, nullable=True)
leverage = Column(Float, nullable=True, default=0.0)
leverage = Column(Float, nullable=True, default=1.0)
def __repr__(self):
return (f'Order(id={self.id}, order_id={self.order_id}, trade_id={self.ft_trade_id}, '
@@ -258,12 +258,12 @@ class LocalTrade():
# Lowest price reached
min_rate: float = 0.0
sell_reason: str = ''
close_order_status: str = ''
sell_order_status: str = ''
strategy: str = ''
timeframe: Optional[int] = None
# Margin trading properties
leverage: Optional[float] = 0.0
leverage: Optional[float] = 1.0
borrowed: float = 0.0
borrowed_currency: str = None
collateral_currency: str = None
@@ -287,6 +287,8 @@ class LocalTrade():
for key in kwargs:
setattr(self, key, kwargs[key])
if not self.is_short:
self.is_short = False
self.recalc_open_trade_value()
def __repr__(self):
@@ -348,7 +350,7 @@ class LocalTrade():
'profit_abs': self.close_profit_abs,
'sell_reason': self.sell_reason,
'close_order_status': self.close_order_status,
'sell_order_status': self.sell_order_status,
'stop_loss_abs': self.stop_loss,
'stop_loss_ratio': self.stop_loss_pct if self.stop_loss_pct else None,
'stop_loss_pct': (self.stop_loss_pct * 100) if self.stop_loss_pct else None,
@@ -371,7 +373,7 @@ class LocalTrade():
'collateral_currency': self.collateral_currency,
'interest_rate': self.interest_rate,
'liquidation_price': self.liquidation_price,
'leverage': self.leverage,
'is_short': self.is_short,
'open_order_id': self.open_order_id,
}
@@ -474,12 +476,12 @@ class LocalTrade():
self.recalc_open_trade_value()
if self.is_open:
payment = "SELL" if self.is_short else "BUY"
logger.info(f'{order_type.upper()}_{payment} order has been fulfilled for {self}.')
logger.info(f'{order_type.upper()}_{payment} has been fulfilled for {self}.')
self.open_order_id = None
elif order_type in ('market', 'limit') and self.is_closing_trade(order['side']):
if self.is_open:
payment = "BUY" if self.is_short else "SELL"
logger.info(f'{order_type.upper()}_{payment} order has been fulfilled for {self}.')
logger.info(f'{order_type.upper()}_{payment} has been fulfilled for {self}.')
self.close(safe_value_fallback(order, 'average', 'price')) # TODO: Double check this
elif order_type in ('stop_loss_limit', 'stop-loss', 'stop-loss-limit', 'stop'):
self.stoploss_order_id = None
@@ -502,7 +504,7 @@ class LocalTrade():
self.close_profit = self.calc_profit_ratio()
self.close_profit_abs = self.calc_profit()
self.is_open = False
self.close_order_status = 'closed'
self.sell_order_status = 'closed'
self.open_order_id = None
if show_msg:
logger.info(
@@ -576,8 +578,18 @@ class LocalTrade():
close_trade = Decimal(self.amount) * Decimal(rate or self.close_rate) # type: ignore
fees = close_trade * Decimal(fee or self.fee_close)
#TODO: Interest rate could be hourly instead of daily
interest = ((Decimal(self.interest_rate) * Decimal(self.borrowed)) * Decimal((datetime.utcnow() - self.open_date).days)) or 0 # Interest/day * num of days
# TODO: Need to set other conditions because sometimes self.open_date is not defined, but why would it ever not be set
try:
open = self.open_date.replace(tzinfo=None)
now = datetime.now()
# breakpoint()
interest = ((Decimal(self.interest_rate or 0) * Decimal(self.borrowed or 0)) *
Decimal((now - open).total_seconds())/86400) or 0 # Interest/day * (seconds in trade)/(seconds per day)
except:
interest = 0
if (self.is_short):
return float(close_trade + fees + interest)
else:
@@ -617,12 +629,17 @@ class LocalTrade():
rate=(rate or self.close_rate),
fee=(fee or self.fee_close)
)
if self.open_trade_value == 0.0:
return 0.0
if self.is_short:
profit_ratio = (close_trade_value / self.open_trade_value) - 1
if close_trade_value == 0.0:
return 0.0
else:
profit_ratio = (self.open_trade_value / close_trade_value) - 1
else:
profit_ratio = (self.open_trade_value / close_trade_value) - 1
if self.open_trade_value == 0.0:
return 0.0
else:
profit_ratio = (close_trade_value / self.open_trade_value) - 1
return float(f"{profit_ratio:.8f}")
def select_order(self, order_side: str, is_open: Optional[bool]) -> Optional[Order]:
@@ -672,7 +689,7 @@ class LocalTrade():
sel_trades = [trade for trade in sel_trades if trade.close_date
and trade.close_date > close_date]
return sel_trades
return sel_trades
@staticmethod
def close_bt_trade(trade):
@@ -768,13 +785,13 @@ class Trade(_DECL_BASE, LocalTrade):
max_rate = Column(Float, nullable=True, default=0.0)
# Lowest price reached
min_rate = Column(Float, nullable=True)
sell_reason = Column(String(100), nullable=True) #TODO: Change to close_reason
close_order_status = Column(String(100), nullable=True)
sell_reason = Column(String(100), nullable=True) # TODO: Change to close_reason
sell_order_status = Column(String(100), nullable=True)
strategy = Column(String(100), nullable=True)
timeframe = Column(Integer, nullable=True)
# Margin trading properties
leverage = Column(Float, nullable=True, default=0.0)
leverage = Column(Float, nullable=True, default=1.0)
borrowed = Column(Float, nullable=False, default=0.0)
borrowed_currency = Column(Float, nullable=True)
collateral_currency = Column(String(25), nullable=True)