2020-08-13 06:33:46 +00:00
|
|
|
import logging
|
|
|
|
from typing import List
|
|
|
|
|
2021-04-06 12:53:08 +00:00
|
|
|
from sqlalchemy import inspect, text
|
2020-08-13 06:33:46 +00:00
|
|
|
|
2020-09-28 17:39:41 +00:00
|
|
|
|
2020-08-13 06:33:46 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2020-08-13 12:50:57 +00:00
|
|
|
|
2020-08-13 07:41:12 +00:00
|
|
|
def get_table_names_for_table(inspector, tabletype):
|
|
|
|
return [t for t in inspector.get_table_names() if t.startswith(tabletype)]
|
|
|
|
|
2020-08-13 06:33:46 +00:00
|
|
|
|
|
|
|
def has_column(columns: List, searchname: str) -> bool:
|
|
|
|
return len(list(filter(lambda x: x["name"] == searchname, columns))) == 1
|
|
|
|
|
|
|
|
|
|
|
|
def get_column_def(columns: List, column: str, default: str) -> str:
|
|
|
|
return default if not has_column(columns, column) else column
|
|
|
|
|
|
|
|
|
2020-08-13 12:50:57 +00:00
|
|
|
def get_backup_name(tabs, backup_prefix: str):
|
|
|
|
table_back_name = backup_prefix
|
|
|
|
for i, table_back_name in enumerate(tabs):
|
|
|
|
table_back_name = f'{backup_prefix}{i}'
|
|
|
|
logger.debug(f'trying {table_back_name}')
|
|
|
|
|
|
|
|
return table_back_name
|
|
|
|
|
|
|
|
|
|
|
|
def migrate_trades_table(decl_base, inspector, engine, table_back_name: str, cols: List):
|
|
|
|
fee_open = get_column_def(cols, 'fee_open', 'fee')
|
|
|
|
fee_open_cost = get_column_def(cols, 'fee_open_cost', 'null')
|
|
|
|
fee_open_currency = get_column_def(cols, 'fee_open_currency', 'null')
|
|
|
|
fee_close = get_column_def(cols, 'fee_close', 'fee')
|
|
|
|
fee_close_cost = get_column_def(cols, 'fee_close_cost', 'null')
|
|
|
|
fee_close_currency = get_column_def(cols, 'fee_close_currency', 'null')
|
|
|
|
open_rate_requested = get_column_def(cols, 'open_rate_requested', 'null')
|
|
|
|
close_rate_requested = get_column_def(cols, 'close_rate_requested', 'null')
|
|
|
|
stop_loss = get_column_def(cols, 'stop_loss', '0.0')
|
|
|
|
stop_loss_pct = get_column_def(cols, 'stop_loss_pct', 'null')
|
|
|
|
initial_stop_loss = get_column_def(cols, 'initial_stop_loss', '0.0')
|
|
|
|
initial_stop_loss_pct = get_column_def(cols, 'initial_stop_loss_pct', 'null')
|
|
|
|
stoploss_order_id = get_column_def(cols, 'stoploss_order_id', 'null')
|
|
|
|
stoploss_last_update = get_column_def(cols, 'stoploss_last_update', 'null')
|
|
|
|
max_rate = get_column_def(cols, 'max_rate', '0.0')
|
|
|
|
min_rate = get_column_def(cols, 'min_rate', 'null')
|
2021-06-20 09:01:03 +00:00
|
|
|
sell_reason = get_column_def(cols, 'sell_reason', 'null')
|
2020-08-13 12:50:57 +00:00
|
|
|
strategy = get_column_def(cols, 'strategy', 'null')
|
2021-06-22 03:26:31 +00:00
|
|
|
|
2021-07-05 05:12:07 +00:00
|
|
|
leverage = get_column_def(cols, 'leverage', '1.0')
|
2021-06-20 08:25:22 +00:00
|
|
|
interest_rate = get_column_def(cols, 'interest_rate', '0.0')
|
2021-06-20 09:01:03 +00:00
|
|
|
liquidation_price = get_column_def(cols, 'liquidation_price', 'null')
|
2021-06-20 08:25:22 +00:00
|
|
|
is_short = get_column_def(cols, 'is_short', 'False')
|
2021-07-08 03:04:38 +00:00
|
|
|
interest_mode = get_column_def(cols, 'interest_mode', 'null')
|
2020-08-13 12:50:57 +00:00
|
|
|
# If ticker-interval existed use that, else null.
|
|
|
|
if has_column(cols, 'ticker_interval'):
|
|
|
|
timeframe = get_column_def(cols, 'timeframe', 'ticker_interval')
|
|
|
|
else:
|
|
|
|
timeframe = get_column_def(cols, 'timeframe', 'null')
|
|
|
|
|
2020-12-10 18:36:52 +00:00
|
|
|
open_trade_value = get_column_def(cols, 'open_trade_value',
|
2020-08-13 12:50:57 +00:00
|
|
|
f'amount * open_rate * (1 + {fee_open})')
|
|
|
|
close_profit_abs = get_column_def(
|
|
|
|
cols, 'close_profit_abs',
|
2020-12-10 18:36:52 +00:00
|
|
|
f"(amount * close_rate * (1 - {fee_close})) - {open_trade_value}")
|
2021-06-22 03:26:31 +00:00
|
|
|
# TODO-mg: update to exit order status
|
|
|
|
sell_order_status = get_column_def(cols, 'sell_order_status', 'null')
|
2020-08-13 12:50:57 +00:00
|
|
|
amount_requested = get_column_def(cols, 'amount_requested', 'amount')
|
|
|
|
|
|
|
|
# Schema migration necessary
|
2021-04-06 12:53:08 +00:00
|
|
|
with engine.begin() as connection:
|
|
|
|
connection.execute(text(f"alter table trades rename to {table_back_name}"))
|
|
|
|
# drop indexes on backup table
|
|
|
|
for index in inspector.get_indexes(table_back_name):
|
|
|
|
connection.execute(text(f"drop index {index['name']}"))
|
2020-08-13 12:50:57 +00:00
|
|
|
# let SQLAlchemy create the schema as required
|
|
|
|
decl_base.metadata.create_all(engine)
|
|
|
|
|
|
|
|
# Copy data back - following the correct schema
|
2021-04-06 12:53:08 +00:00
|
|
|
with engine.begin() as connection:
|
|
|
|
connection.execute(text(f"""insert into trades
|
2020-08-13 12:50:57 +00:00
|
|
|
(id, exchange, pair, is_open,
|
|
|
|
fee_open, fee_open_cost, fee_open_currency,
|
|
|
|
fee_close, fee_close_cost, fee_open_currency, open_rate,
|
|
|
|
open_rate_requested, close_rate, close_rate_requested, close_profit,
|
|
|
|
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,
|
2021-06-22 03:26:31 +00:00
|
|
|
max_rate, min_rate, sell_reason, sell_order_status, strategy,
|
2021-06-20 08:25:22 +00:00
|
|
|
timeframe, open_trade_value, close_profit_abs,
|
2021-07-08 03:04:38 +00:00
|
|
|
leverage, interest_rate, liquidation_price, is_short, interest_mode
|
2020-08-13 12:50:57 +00:00
|
|
|
)
|
|
|
|
select id, lower(exchange),
|
|
|
|
case
|
|
|
|
when instr(pair, '_') != 0 then
|
|
|
|
substr(pair, instr(pair, '_') + 1) || '/' ||
|
|
|
|
substr(pair, 1, instr(pair, '_') - 1)
|
|
|
|
else pair
|
|
|
|
end
|
|
|
|
pair,
|
|
|
|
is_open, {fee_open} fee_open, {fee_open_cost} fee_open_cost,
|
|
|
|
{fee_open_currency} fee_open_currency, {fee_close} fee_close,
|
|
|
|
{fee_close_cost} fee_close_cost, {fee_close_currency} fee_close_currency,
|
|
|
|
open_rate, {open_rate_requested} open_rate_requested, close_rate,
|
|
|
|
{close_rate_requested} close_rate_requested, close_profit,
|
|
|
|
stake_amount, amount, {amount_requested}, open_date, close_date, open_order_id,
|
|
|
|
{stop_loss} stop_loss, {stop_loss_pct} stop_loss_pct,
|
|
|
|
{initial_stop_loss} initial_stop_loss,
|
|
|
|
{initial_stop_loss_pct} initial_stop_loss_pct,
|
|
|
|
{stoploss_order_id} stoploss_order_id, {stoploss_last_update} stoploss_last_update,
|
2021-06-20 09:01:03 +00:00
|
|
|
{max_rate} max_rate, {min_rate} min_rate, {sell_reason} sell_reason,
|
2021-06-22 03:26:31 +00:00
|
|
|
{sell_order_status} sell_order_status,
|
2020-08-13 12:50:57 +00:00
|
|
|
{strategy} strategy, {timeframe} timeframe,
|
2021-06-20 08:25:22 +00:00
|
|
|
{open_trade_value} open_trade_value, {close_profit_abs} close_profit_abs,
|
2021-07-04 06:11:59 +00:00
|
|
|
{leverage} leverage, {interest_rate} interest_rate,
|
2021-07-08 03:04:38 +00:00
|
|
|
{liquidation_price} liquidation_price, {is_short} is_short,
|
|
|
|
{interest_mode} interest_mode
|
2020-08-13 12:50:57 +00:00
|
|
|
from {table_back_name}
|
2021-04-06 12:53:08 +00:00
|
|
|
"""))
|
2020-08-13 12:50:57 +00:00
|
|
|
|
2021-06-22 03:26:31 +00:00
|
|
|
|
2020-08-13 12:50:57 +00:00
|
|
|
def migrate_open_orders_to_trades(engine):
|
2021-04-06 12:53:08 +00:00
|
|
|
with engine.begin() as connection:
|
|
|
|
connection.execute(text("""
|
2020-08-13 17:37:41 +00:00
|
|
|
insert into orders (ft_trade_id, ft_pair, order_id, ft_order_side, ft_is_open)
|
|
|
|
select id ft_trade_id, pair ft_pair, open_order_id,
|
|
|
|
case when close_rate_requested is null then 'buy'
|
2020-08-14 09:25:40 +00:00
|
|
|
else 'sell' end ft_order_side, 1 ft_is_open
|
2020-08-13 17:37:41 +00:00
|
|
|
from trades
|
|
|
|
where open_order_id is not null
|
|
|
|
union all
|
|
|
|
select id ft_trade_id, pair ft_pair, stoploss_order_id order_id,
|
2020-08-14 09:25:40 +00:00
|
|
|
'stoploss' ft_order_side, 1 ft_is_open
|
2020-08-13 17:37:41 +00:00
|
|
|
from trades
|
|
|
|
where stoploss_order_id is not null
|
2021-04-06 12:53:08 +00:00
|
|
|
"""))
|
2020-08-13 12:50:57 +00:00
|
|
|
|
|
|
|
|
2021-05-21 18:35:39 +00:00
|
|
|
def migrate_orders_table(decl_base, inspector, engine, table_back_name: str, cols: List):
|
|
|
|
# Schema migration necessary
|
2021-05-23 06:34:58 +00:00
|
|
|
|
|
|
|
with engine.begin() as connection:
|
|
|
|
connection.execute(text(f"alter table orders rename to {table_back_name}"))
|
|
|
|
# drop indexes on backup table
|
|
|
|
for index in inspector.get_indexes(table_back_name):
|
|
|
|
connection.execute(text(f"drop index {index['name']}"))
|
2021-05-21 18:35:39 +00:00
|
|
|
|
|
|
|
# let SQLAlchemy create the schema as required
|
|
|
|
decl_base.metadata.create_all(engine)
|
2021-07-05 05:12:07 +00:00
|
|
|
leverage = get_column_def(cols, 'leverage', '1.0')
|
2021-07-03 15:03:12 +00:00
|
|
|
is_short = get_column_def(cols, 'is_short', 'False')
|
2021-07-08 06:33:40 +00:00
|
|
|
# TODO-mg: Should liquidation price go in here?
|
2021-05-23 06:34:58 +00:00
|
|
|
with engine.begin() as connection:
|
|
|
|
connection.execute(text(f"""
|
|
|
|
insert into orders ( id, ft_trade_id, ft_order_side, ft_pair, ft_is_open, order_id,
|
|
|
|
status, symbol, order_type, side, price, amount, filled, average, remaining, cost,
|
2021-07-03 15:03:12 +00:00
|
|
|
order_date, order_filled_date, order_update_date, leverage, is_short)
|
2021-05-23 06:34:58 +00:00
|
|
|
select id, ft_trade_id, ft_order_side, ft_pair, ft_is_open, order_id,
|
|
|
|
status, symbol, order_type, side, price, amount, filled, null average, remaining, cost,
|
2021-07-03 15:03:12 +00:00
|
|
|
order_date, order_filled_date, order_update_date,
|
2021-07-07 07:30:42 +00:00
|
|
|
{leverage} leverage, {is_short} is_short
|
2021-05-23 06:34:58 +00:00
|
|
|
from {table_back_name}
|
|
|
|
"""))
|
2021-05-21 18:35:39 +00:00
|
|
|
|
|
|
|
|
2020-08-13 12:50:57 +00:00
|
|
|
def check_migrate(engine, decl_base, previous_tables) -> None:
|
2020-08-13 06:33:46 +00:00
|
|
|
"""
|
|
|
|
Checks if migration is necessary and migrates if necessary
|
|
|
|
"""
|
|
|
|
inspector = inspect(engine)
|
|
|
|
|
|
|
|
cols = inspector.get_columns('trades')
|
2020-08-13 07:41:12 +00:00
|
|
|
tabs = get_table_names_for_table(inspector, 'trades')
|
2020-08-13 12:50:57 +00:00
|
|
|
table_back_name = get_backup_name(tabs, 'trades_bak')
|
2020-08-13 06:33:46 +00:00
|
|
|
|
2021-07-05 05:51:58 +00:00
|
|
|
# Check for latest column
|
|
|
|
if not has_column(cols, 'open_trade_value'):
|
2020-08-13 12:50:57 +00:00
|
|
|
logger.info(f'Running database migration for trades - backup: {table_back_name}')
|
|
|
|
migrate_trades_table(decl_base, inspector, engine, table_back_name, cols)
|
2020-08-13 06:33:46 +00:00
|
|
|
# Reread columns - the above recreated the table!
|
|
|
|
inspector = inspect(engine)
|
|
|
|
cols = inspector.get_columns('trades')
|
2020-08-13 12:50:57 +00:00
|
|
|
|
2021-02-09 19:22:33 +00:00
|
|
|
if 'orders' not in previous_tables and 'trades' in previous_tables:
|
2020-08-13 12:50:57 +00:00
|
|
|
logger.info('Moving open orders to Orders table.')
|
|
|
|
migrate_open_orders_to_trades(engine)
|
|
|
|
else:
|
2021-05-21 18:35:39 +00:00
|
|
|
cols_order = inspector.get_columns('orders')
|
|
|
|
|
2021-07-03 15:03:12 +00:00
|
|
|
# Last added column of order table
|
|
|
|
# To determine if migrations need to run
|
|
|
|
if not has_column(cols_order, 'leverage'):
|
2021-05-21 18:35:39 +00:00
|
|
|
tabs = get_table_names_for_table(inspector, 'orders')
|
|
|
|
# Empty for now - as there is only one iteration of the orders table so far.
|
|
|
|
table_back_name = get_backup_name(tabs, 'orders_bak')
|
|
|
|
|
2021-07-03 15:03:12 +00:00
|
|
|
migrate_orders_table(decl_base, inspector, engine, table_back_name, cols_order)
|