From 096a361584c84356a4c4e6d0fa2729716de8d67a Mon Sep 17 00:00:00 2001 From: anor Date: Fri, 13 Mar 2026 20:05:03 +1000 Subject: [PATCH] =?UTF-8?q?fix:=20limit=20order=20=D0=BF=D0=BE=20bid/ask,?= =?UTF-8?q?=20partial=20fill,=20maker=20fee=20=D0=B2=20=D0=B1=D1=8D=D0=BA?= =?UTF-8?q?=D1=82=D0=B5=D1=81=D1=82=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - exchange: limit по bid (buy) / ask (sell) вместо last price - exchange: partial fill handling при cancel limit order - backtest_wf: MAKER_FEE + TAKER_FEE вместо TAKER_FEE * 2 - sweep_leverage/sweep_stop: аналогично maker+taker Co-Authored-By: Claude Opus 4.6 --- backtest_wf.py | 7 ++++--- exchange.py | 28 ++++++++++++++++++++++++---- sweep_leverage.py | 4 ++-- sweep_stop.py | 4 ++-- 4 files changed, 32 insertions(+), 11 deletions(-) diff --git a/backtest_wf.py b/backtest_wf.py index 20968d4..3852be2 100644 --- a/backtest_wf.py +++ b/backtest_wf.py @@ -24,7 +24,8 @@ NY = ZoneInfo("America/New_York") # ── Config ─────────────────────────────────────────────────────────── INITIAL_CAPITAL = 1_000.0 RISK_PCT = 0.05 # 5% risk per trade -TAKER_FEE = 0.00055 # Bybit linear futures taker fee (0.055%) +MAKER_FEE = 0.0002 # Bybit maker fee (0.02%) — limit entry +TAKER_FEE = 0.00055 # Bybit taker fee (0.055%) — SL/TP exit LEVERAGE = 10 # max leverage (caps position value) TRAIN_RATIO = 0.5 # first 50% days = in-sample TIMEFRAME = "5m" @@ -67,8 +68,8 @@ def trades_to_pnl(trades: list[Trade], capital: float) -> list[dict]: else: gross = pos_size * (t.entry - t.exit_price) - # Commission: taker on entry + exit - commission = pos_value * TAKER_FEE * 2 + # Commission: maker on entry (limit) + taker on exit (SL/TP) + commission = pos_value * (MAKER_FEE + TAKER_FEE) pnl_net = gross - commission equity += pnl_net diff --git a/exchange.py b/exchange.py index ef805b3..660f575 100644 --- a/exchange.py +++ b/exchange.py @@ -148,8 +148,16 @@ class FuturesExecutor: """Try postOnly limit order. Returns filled order or None.""" import time - # Place at current price — postOnly ensures maker-only - limit_price = float(self.exchange.price_to_precision(symbol, price)) + # Place at bid (buy) or ask (sell) for maker execution + try: + book = self.exchange.fetch_order_book(symbol, limit=1) + if side == "buy": + limit_price = float(book["bids"][0][0]) if book["bids"] else price + else: + limit_price = float(book["asks"][0][0]) if book["asks"] else price + except Exception: + limit_price = price + limit_price = float(self.exchange.price_to_precision(symbol, limit_price)) try: order = self.exchange.create_order( symbol, "limit", side, amount, limit_price, @@ -175,12 +183,24 @@ class FuturesExecutor: except Exception: pass - # Not filled — cancel + # Not filled — cancel and check for partial fill try: self.exchange.cancel_order(order_id, symbol) - log.info(f"LIMIT {side.upper()} cancelled (not filled in {wait_sec}s)") except Exception as e: log.warning(f"Cancel limit order failed: {e}") + + # Check if partially filled + try: + final = self.exchange.fetch_order(order_id, symbol) + filled = float(final.get("filled") or 0) + if filled > 0: + log.info(f"LIMIT {side.upper()} PARTIAL: {filled}/{amount} {symbol} " + f"@ ${limit_price:,.2f} (maker)") + return final + except Exception: + pass + + log.info(f"LIMIT {side.upper()} cancelled (not filled in {wait_sec}s)") return None def close_position(self, symbol: str, side: str, qty: float) -> dict | None: diff --git a/sweep_leverage.py b/sweep_leverage.py index ca89ab4..c8859ac 100644 --- a/sweep_leverage.py +++ b/sweep_leverage.py @@ -9,7 +9,7 @@ import numpy as np import pandas as pd from backtest_wf import ( - INITIAL_CAPITAL, OR_MINUTES, TAKER_FEE, TIMEFRAME, TRAIN_RATIO, + INITIAL_CAPITAL, MAKER_FEE, OR_MINUTES, TAKER_FEE, TIMEFRAME, TRAIN_RATIO, _run_on_dates, compute_metrics, find_best_params, ) from data import fetch_candles @@ -43,7 +43,7 @@ def trades_to_pnl(trades, capital, risk_pct, leverage): else: gross = pos_size * (t.entry - t.exit_price) - commission = pos_value * TAKER_FEE * 2 + commission = pos_value * (MAKER_FEE + TAKER_FEE) pnl_net = gross - commission equity += pnl_net results.append({ diff --git a/sweep_stop.py b/sweep_stop.py index f15db12..0801eb6 100644 --- a/sweep_stop.py +++ b/sweep_stop.py @@ -9,7 +9,7 @@ import numpy as np import pandas as pd from backtest_wf import ( - INITIAL_CAPITAL, OR_MINUTES, TAKER_FEE, TIMEFRAME, TRAIN_RATIO, + INITIAL_CAPITAL, MAKER_FEE, OR_MINUTES, TAKER_FEE, TIMEFRAME, TRAIN_RATIO, _run_on_dates, compute_metrics, find_best_params, ) from data import fetch_candles @@ -40,7 +40,7 @@ def trades_to_pnl(trades, capital, risk_pct, leverage): gross = pos_size * (t.exit_price - t.entry) else: gross = pos_size * (t.entry - t.exit_price) - commission = pos_value * TAKER_FEE * 2 + commission = pos_value * (MAKER_FEE + TAKER_FEE) pnl_net = gross - commission equity += pnl_net results.append({