feat: фильтр праздников NYSE — бот не торгует когда биржа закрыта
- holidays.py: календарь NYSE holidays 2024–2027 (10 дат/год) - executor: _is_nyse_hours() пропускает праздники - все ORB-бэктесты: исключают holidays из train/test split и run_day() - OOS сравнение: +$18 PnL, +0.8pp WR, Sharpe 0.42→0.47 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
+4
-1
@@ -6,6 +6,7 @@ import sys
|
||||
from zoneinfo import ZoneInfo
|
||||
|
||||
from data import fetch_1m
|
||||
from holidays import is_nyse_holiday
|
||||
from strategy import run_day
|
||||
|
||||
NY = ZoneInfo("America/New_York")
|
||||
@@ -17,7 +18,9 @@ def backtest(symbol: str = "BTC/USDT:USDT", days: int = 60):
|
||||
|
||||
all_trades = []
|
||||
session_count = 0
|
||||
for date, day_df in df.groupby("ny_date"):
|
||||
for dt, day_df in df.groupby("ny_date"):
|
||||
if is_nyse_holiday(dt):
|
||||
continue
|
||||
trades = run_day(day_df.reset_index(drop=True))
|
||||
all_trades.extend(trades)
|
||||
session_count += 1
|
||||
|
||||
@@ -10,6 +10,7 @@ import numpy as np
|
||||
import pandas as pd
|
||||
|
||||
from data import fetch_candles
|
||||
from holidays import is_nyse_holiday
|
||||
from strategy import Params, Trade, run_day
|
||||
|
||||
NY = ZoneInfo("America/New_York")
|
||||
@@ -133,7 +134,9 @@ def compute_metrics(dollar_trades: list[dict], initial_capital: float,
|
||||
|
||||
def _run_on_dates(df: pd.DataFrame, params: Params) -> list[Trade]:
|
||||
all_trades = []
|
||||
for _, day_df in df.groupby("ny_date"):
|
||||
for dt, day_df in df.groupby("ny_date"):
|
||||
if is_nyse_holiday(dt):
|
||||
continue
|
||||
all_trades.extend(run_day(day_df.reset_index(drop=True), params, OR_MINUTES))
|
||||
return all_trades
|
||||
|
||||
@@ -158,7 +161,7 @@ def run_leverage_sweep(symbol: str, days: int = 60):
|
||||
df = fetch_candles(symbol, days, TIMEFRAME)
|
||||
df["ny_date"] = df["timestamp"].dt.tz_convert(NY).dt.date
|
||||
|
||||
dates = sorted(df["ny_date"].unique())
|
||||
dates = sorted(d for d in df["ny_date"].unique() if not is_nyse_holiday(d))
|
||||
split = int(len(dates) * TRAIN_RATIO)
|
||||
train_dates = set(dates[:split])
|
||||
test_dates = set(dates[split:])
|
||||
|
||||
@@ -17,6 +17,7 @@ from backtest_wf import (
|
||||
TIMEFRAME, TRAIN_RATIO, _run_on_dates, compute_metrics, find_best_params,
|
||||
)
|
||||
from data import fetch_candles
|
||||
from holidays import is_nyse_holiday
|
||||
from strategy import Params, Trade
|
||||
|
||||
NY = ZoneInfo("America/New_York")
|
||||
@@ -73,7 +74,7 @@ def run_portfolio(symbols: list[str], days: int = 365):
|
||||
df = fetch_candles(sym, days, TIMEFRAME)
|
||||
df["ny_date"] = df["timestamp"].dt.tz_convert(NY).dt.date
|
||||
|
||||
dates = sorted(df["ny_date"].unique())
|
||||
dates = sorted(d for d in df["ny_date"].unique() if not is_nyse_holiday(d))
|
||||
split = int(len(dates) * TRAIN_RATIO)
|
||||
train_dates = set(dates[:split])
|
||||
test_dates = sorted(dates[split:])
|
||||
|
||||
+5
-2
@@ -17,6 +17,7 @@ import numpy as np
|
||||
import pandas as pd
|
||||
|
||||
from data import fetch_candles
|
||||
from holidays import is_nyse_holiday
|
||||
from strategy import Params, Trade, run_day
|
||||
|
||||
NY = ZoneInfo("America/New_York")
|
||||
@@ -146,7 +147,9 @@ def compute_metrics(dollar_trades: list[dict], initial_capital: float,
|
||||
def _run_on_dates(df: pd.DataFrame, params: Params) -> list[Trade]:
|
||||
"""Run strategy on pre-filtered dataframe, return trades."""
|
||||
all_trades = []
|
||||
for _, day_df in df.groupby("ny_date"):
|
||||
for dt, day_df in df.groupby("ny_date"):
|
||||
if is_nyse_holiday(dt):
|
||||
continue
|
||||
all_trades.extend(run_day(day_df.reset_index(drop=True), params, OR_MINUTES))
|
||||
return all_trades
|
||||
|
||||
@@ -185,7 +188,7 @@ def walk_forward(symbol: str, days: int = 60):
|
||||
df = fetch_candles(symbol, days, TIMEFRAME)
|
||||
df["ny_date"] = df["timestamp"].dt.tz_convert(NY).dt.date
|
||||
|
||||
dates = sorted(df["ny_date"].unique())
|
||||
dates = sorted(d for d in df["ny_date"].unique() if not is_nyse_holiday(d))
|
||||
split = int(len(dates) * TRAIN_RATIO)
|
||||
train_dates = set(dates[:split])
|
||||
test_dates = set(dates[split:])
|
||||
|
||||
+4
-1
@@ -20,6 +20,7 @@ import pandas as pd
|
||||
|
||||
from config import Config, StrategyConfig
|
||||
from exchange import FuturesExecutor
|
||||
from holidays import is_nyse_holiday
|
||||
from notify import notify_entry, notify_exit, notify_or
|
||||
from state import (
|
||||
close_position, get_equity, get_open_positions, get_session,
|
||||
@@ -63,9 +64,11 @@ def _get_session(symbol: str) -> SessionState:
|
||||
|
||||
|
||||
def _is_nyse_hours(now_ny: datetime) -> bool:
|
||||
"""Check if within NYSE trading window (9:25-16:05 NY, weekdays)."""
|
||||
"""Check if within NYSE trading window (9:25-16:05 NY, weekdays, not holidays)."""
|
||||
if now_ny.weekday() >= 5: # Sat, Sun
|
||||
return False
|
||||
if is_nyse_holiday(now_ny.date()):
|
||||
return False
|
||||
t = now_ny.hour * 60 + now_ny.minute
|
||||
return 9 * 60 + 25 <= t <= 16 * 60 + 5
|
||||
|
||||
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
"""NYSE holiday calendar — used to skip non-trading days.
|
||||
|
||||
Covers 2024-2027. Update yearly from:
|
||||
https://www.nyse.com/markets/hours-calendars
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import date
|
||||
|
||||
# NYSE observed holidays (market fully closed)
|
||||
NYSE_HOLIDAYS: set[date] = {
|
||||
# 2024
|
||||
date(2024, 1, 1), # New Year's Day
|
||||
date(2024, 1, 15), # MLK Jr. Day
|
||||
date(2024, 2, 19), # Presidents' Day
|
||||
date(2024, 3, 29), # Good Friday
|
||||
date(2024, 5, 27), # Memorial Day
|
||||
date(2024, 6, 19), # Juneteenth
|
||||
date(2024, 7, 4), # Independence Day
|
||||
date(2024, 9, 2), # Labor Day
|
||||
date(2024, 11, 28), # Thanksgiving
|
||||
date(2024, 12, 25), # Christmas
|
||||
|
||||
# 2025
|
||||
date(2025, 1, 1), # New Year's Day
|
||||
date(2025, 1, 9), # National Day of Mourning (Jimmy Carter)
|
||||
date(2025, 1, 20), # MLK Jr. Day
|
||||
date(2025, 2, 17), # Presidents' Day
|
||||
date(2025, 4, 18), # Good Friday
|
||||
date(2025, 5, 26), # Memorial Day
|
||||
date(2025, 6, 19), # Juneteenth
|
||||
date(2025, 7, 4), # Independence Day
|
||||
date(2025, 9, 1), # Labor Day
|
||||
date(2025, 11, 27), # Thanksgiving
|
||||
date(2025, 12, 25), # Christmas
|
||||
|
||||
# 2026
|
||||
date(2026, 1, 1), # New Year's Day
|
||||
date(2026, 1, 19), # MLK Jr. Day
|
||||
date(2026, 2, 16), # Presidents' Day
|
||||
date(2026, 4, 3), # Good Friday
|
||||
date(2026, 5, 25), # Memorial Day
|
||||
date(2026, 6, 19), # Juneteenth
|
||||
date(2026, 7, 3), # Independence Day (observed, Jul 4 = Sat)
|
||||
date(2026, 9, 7), # Labor Day
|
||||
date(2026, 11, 26), # Thanksgiving
|
||||
date(2026, 12, 25), # Christmas
|
||||
|
||||
# 2027
|
||||
date(2027, 1, 1), # New Year's Day
|
||||
date(2027, 1, 18), # MLK Jr. Day
|
||||
date(2027, 2, 15), # Presidents' Day
|
||||
date(2027, 3, 26), # Good Friday
|
||||
date(2027, 5, 31), # Memorial Day
|
||||
date(2027, 6, 18), # Juneteenth (observed, Jun 19 = Sat)
|
||||
date(2027, 7, 5), # Independence Day (observed, Jul 4 = Sun)
|
||||
date(2027, 9, 6), # Labor Day
|
||||
date(2027, 11, 25), # Thanksgiving
|
||||
date(2027, 12, 24), # Christmas (observed, Dec 25 = Sat)
|
||||
}
|
||||
|
||||
|
||||
def is_nyse_holiday(d: date) -> bool:
|
||||
"""Check if a date is an NYSE holiday (market closed)."""
|
||||
return d in NYSE_HOLIDAYS
|
||||
+2
-1
@@ -13,6 +13,7 @@ from backtest_wf import (
|
||||
_run_on_dates, compute_metrics, find_best_params,
|
||||
)
|
||||
from data import fetch_candles
|
||||
from holidays import is_nyse_holiday
|
||||
|
||||
NY = ZoneInfo("America/New_York")
|
||||
|
||||
@@ -59,7 +60,7 @@ def sweep(symbol: str, days: int = 60):
|
||||
df = fetch_candles(symbol, days, TIMEFRAME)
|
||||
df["ny_date"] = df["timestamp"].dt.tz_convert(NY).dt.date
|
||||
|
||||
dates = sorted(df["ny_date"].unique())
|
||||
dates = sorted(d for d in df["ny_date"].unique() if not is_nyse_holiday(d))
|
||||
split = int(len(dates) * TRAIN_RATIO)
|
||||
train_dates = set(dates[:split])
|
||||
test_dates = set(dates[split:])
|
||||
|
||||
+2
-1
@@ -13,6 +13,7 @@ from backtest_wf import (
|
||||
_run_on_dates, compute_metrics, find_best_params,
|
||||
)
|
||||
from data import fetch_candles
|
||||
from holidays import is_nyse_holiday
|
||||
from strategy import Params
|
||||
|
||||
NY = ZoneInfo("America/New_York")
|
||||
@@ -54,7 +55,7 @@ def sweep(symbol: str, days: int = 60):
|
||||
df = fetch_candles(symbol, days, TIMEFRAME)
|
||||
df["ny_date"] = df["timestamp"].dt.tz_convert(NY).dt.date
|
||||
|
||||
dates = sorted(df["ny_date"].unique())
|
||||
dates = sorted(d for d in df["ny_date"].unique() if not is_nyse_holiday(d))
|
||||
split = int(len(dates) * TRAIN_RATIO)
|
||||
test_dates = set(dates[split:])
|
||||
df_train = df[df["ny_date"].isin(set(dates[:split]))]
|
||||
|
||||
Reference in New Issue
Block a user