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({