From fd694f14c28105624a836611d66521bdb3343e10 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 8 Jan 2023 13:53:08 +0100 Subject: [PATCH] Add new order columns, ft_amount and ft_price --- freqtrade/freqtradebot.py | 7 ++++--- freqtrade/persistence/migrations.py | 13 +++++++++---- freqtrade/persistence/trade_model.py | 15 +++++++++++++-- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 659eb2660..6e87db136 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -720,7 +720,7 @@ class FreqtradeBot(LoggingMixin): time_in_force=time_in_force, leverage=leverage ) - order_obj = Order.parse_from_ccxt_object(order, pair, side) + order_obj = Order.parse_from_ccxt_object(order, pair, side, amount, enter_limit_requested) order_id = order['id'] order_status = order.get('status') logger.info(f"Order #{order_id} was created for {pair} and status is {order_status}.") @@ -1094,7 +1094,8 @@ class FreqtradeBot(LoggingMixin): leverage=trade.leverage ) - order_obj = Order.parse_from_ccxt_object(stoploss_order, trade.pair, 'stoploss') + order_obj = Order.parse_from_ccxt_object(stoploss_order, trade.pair, 'stoploss', + trade.amount, stop_price) trade.orders.append(order_obj) trade.stoploss_order_id = str(stoploss_order['id']) trade.stoploss_last_update = datetime.now(timezone.utc) @@ -1595,7 +1596,7 @@ class FreqtradeBot(LoggingMixin): self.handle_insufficient_funds(trade) return False - order_obj = Order.parse_from_ccxt_object(order, trade.pair, trade.exit_side) + order_obj = Order.parse_from_ccxt_object(order, trade.pair, trade.exit_side, amount, limit) trade.orders.append(order_obj) trade.open_order_id = order['id'] diff --git a/freqtrade/persistence/migrations.py b/freqtrade/persistence/migrations.py index 44a6756d1..87b172846 100644 --- a/freqtrade/persistence/migrations.py +++ b/freqtrade/persistence/migrations.py @@ -214,17 +214,22 @@ def migrate_orders_table(engine, table_back_name: str, cols_order: List): average = get_column_def(cols_order, 'average', 'null') stop_price = get_column_def(cols_order, 'stop_price', 'null') funding_fee = get_column_def(cols_order, 'funding_fee', '0.0') + ft_amount = get_column_def(cols_order, 'ft_amount', 'coalesce(amount, 0.0)') + ft_price = get_column_def(cols_order, 'ft_price', 'coalesce(price, 0.0)') # sqlite does not support literals for booleans 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, - stop_price, order_date, order_filled_date, order_update_date, ft_fee_base, funding_fee) + stop_price, order_date, order_filled_date, order_update_date, ft_fee_base, funding_fee, + ft_amount, ft_price + ) select id, ft_trade_id, ft_order_side, ft_pair, ft_is_open, order_id, status, symbol, order_type, side, price, amount, filled, {average} average, remaining, cost, {stop_price} stop_price, order_date, order_filled_date, - order_update_date, {ft_fee_base} ft_fee_base, {funding_fee} funding_fee + order_update_date, {ft_fee_base} ft_fee_base, {funding_fee} funding_fee, + {ft_amount} ft_amount, {ft_price} ft_price from {table_back_name} """)) @@ -311,8 +316,8 @@ def check_migrate(engine, decl_base, previous_tables) -> None: # if ('orders' not in previous_tables # or not has_column(cols_orders, 'funding_fee')): migrating = False - # if not has_column(cols_orders, 'funding_fee'): - if not has_column(cols_trades, 'max_stake_amount'): + # if not has_column(cols_trades, 'max_stake_amount'): + if not has_column(cols_orders, 'ft_price'): migrating = True logger.info(f"Running database migration for trades - " f"backup: {table_back_name}, {order_table_bak_name}") diff --git a/freqtrade/persistence/trade_model.py b/freqtrade/persistence/trade_model.py index f19b3808f..b6871c04a 100644 --- a/freqtrade/persistence/trade_model.py +++ b/freqtrade/persistence/trade_model.py @@ -49,6 +49,8 @@ class Order(_DECL_BASE): ft_order_side: str = Column(String(25), nullable=False) ft_pair: str = Column(String(25), nullable=False) ft_is_open = Column(Boolean, nullable=False, default=True, index=True) + ft_amount = Column(Float, nullable=False) + ft_price = Column(Float, nullable=False) order_id: str = Column(String(255), nullable=False, index=True) status = Column(String(255), nullable=True) @@ -227,11 +229,20 @@ class Order(_DECL_BASE): logger.warning(f"Did not find order for {order}.") @staticmethod - def parse_from_ccxt_object(order: Dict[str, Any], pair: str, side: str) -> 'Order': + def parse_from_ccxt_object( + order: Dict[str, Any], pair: str, side: str, + amount: Optional[float] = None, price: Optional[float] = None) -> 'Order': """ Parse an order from a ccxt object and return a new order Object. + Optional support for overriding amount and price is only used for test simplification. """ - o = Order(order_id=str(order['id']), ft_order_side=side, ft_pair=pair) + o = Order( + order_id=str(order['id']), + ft_order_side=side, + ft_pair=pair, + ft_amount=amount if amount else order['amount'], + ft_price=price if price else order['price'], + ) o.update_from_ccxt_object(order) return o