mirror of
https://github.com/freqtrade/freqtrade.git
synced 2025-11-29 08:33:07 +00:00
Further simplify backtest order handling
This commit is contained in:
@@ -567,8 +567,7 @@ class Backtesting:
|
|||||||
pos_trade = self._get_exit_for_signal(trade, row, exit_, amount)
|
pos_trade = self._get_exit_for_signal(trade, row, exit_, amount)
|
||||||
if pos_trade is not None:
|
if pos_trade is not None:
|
||||||
order = pos_trade.orders[-1]
|
order = pos_trade.orders[-1]
|
||||||
if self._get_order_filled(order.ft_price, row):
|
if self._try_close_open_order(order, trade, current_date, row):
|
||||||
self._close_open_order(order, trade, current_date, row)
|
|
||||||
trade.recalc_trade_from_orders()
|
trade.recalc_trade_from_orders()
|
||||||
self.wallets.update()
|
self.wallets.update()
|
||||||
return pos_trade
|
return pos_trade
|
||||||
@@ -579,11 +578,18 @@ class Backtesting:
|
|||||||
""" Rate is within candle, therefore filled"""
|
""" Rate is within candle, therefore filled"""
|
||||||
return row[LOW_IDX] <= rate <= row[HIGH_IDX]
|
return row[LOW_IDX] <= rate <= row[HIGH_IDX]
|
||||||
|
|
||||||
def _close_open_order(
|
def _try_close_open_order(
|
||||||
self, order: Order, trade: LocalTrade, current_date: datetime, row: Tuple) -> None:
|
self, order: Optional[Order], trade: LocalTrade, current_date: datetime,
|
||||||
""" Close an open order, and update trade accordingly"""
|
row: Tuple) -> bool:
|
||||||
order.close_bt_order(current_date, trade)
|
"""
|
||||||
trade.open_order_id = None
|
Check if an order is open and if it should've filled.
|
||||||
|
:return: True if the order filled.
|
||||||
|
"""
|
||||||
|
if order and self._get_order_filled(order.ft_price, row):
|
||||||
|
order.close_bt_order(current_date, trade)
|
||||||
|
trade.open_order_id = None
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
def _get_exit_for_signal(
|
def _get_exit_for_signal(
|
||||||
self, trade: LocalTrade, row: Tuple, exit_: ExitCheckTuple,
|
self, trade: LocalTrade, row: Tuple, exit_: ExitCheckTuple,
|
||||||
@@ -909,9 +915,7 @@ class Backtesting:
|
|||||||
)
|
)
|
||||||
order._trade_bt = trade
|
order._trade_bt = trade
|
||||||
trade.orders.append(order)
|
trade.orders.append(order)
|
||||||
if self._get_order_filled(order.ft_price, row):
|
if not self._try_close_open_order(order, trade, current_time, row):
|
||||||
self._close_open_order(order, trade, current_time, row)
|
|
||||||
else:
|
|
||||||
trade.open_order_id = str(self.order_id_counter)
|
trade.open_order_id = str(self.order_id_counter)
|
||||||
trade.recalc_trade_from_orders()
|
trade.recalc_trade_from_orders()
|
||||||
|
|
||||||
@@ -1127,8 +1131,7 @@ class Backtesting:
|
|||||||
for trade in list(LocalTrade.bt_trades_open_pp[pair]):
|
for trade in list(LocalTrade.bt_trades_open_pp[pair]):
|
||||||
# 3. Process entry orders.
|
# 3. Process entry orders.
|
||||||
order = trade.select_order(trade.entry_side, is_open=True)
|
order = trade.select_order(trade.entry_side, is_open=True)
|
||||||
if order and self._get_order_filled(order.ft_price, row):
|
if self._try_close_open_order(order, trade, current_time, row):
|
||||||
self._close_open_order(order, trade, current_time, row)
|
|
||||||
self.wallets.update()
|
self.wallets.update()
|
||||||
|
|
||||||
# 4. Create exit orders (if any)
|
# 4. Create exit orders (if any)
|
||||||
@@ -1137,8 +1140,7 @@ class Backtesting:
|
|||||||
|
|
||||||
# 5. Process exit orders.
|
# 5. Process exit orders.
|
||||||
order = trade.select_order(trade.exit_side, is_open=True)
|
order = trade.select_order(trade.exit_side, is_open=True)
|
||||||
if order and self._get_order_filled(order.ft_price, row):
|
if self._try_close_open_order(order, trade, current_time, row):
|
||||||
self._close_open_order(order, trade, current_time, row)
|
|
||||||
sub_trade = order.safe_amount_after_fee != trade.amount
|
sub_trade = order.safe_amount_after_fee != trade.amount
|
||||||
if sub_trade:
|
if sub_trade:
|
||||||
trade.recalc_trade_from_orders()
|
trade.recalc_trade_from_orders()
|
||||||
|
|||||||
Reference in New Issue
Block a user