enhancing backtesting functionality

This commit is contained in:
moen0
2026-04-23 23:21:03 +02:00
parent 768511e90d
commit e6e0f6f79c
5 changed files with 98 additions and 49 deletions
+5 -1
View File
@@ -10,7 +10,7 @@ from fastapi import FastAPI, HTTPException, Query
from fastapi.responses import StreamingResponse
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel, Field
from indicators.sessions import set_timezone
BACKEND_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
if BACKEND_DIR not in sys.path:
sys.path.insert(0, BACKEND_DIR)
@@ -524,6 +524,10 @@ def get_backtest(
max_consecutive_losses: int = 0,
):
dataset_id = _resolve_dataset(dataset)
if "MT5" in dataset.upper():
set_timezone("mt5")
else:
set_timezone("est")
candles = _get_candles_for_timeframe(dataset_id, timeframe)
strategy = _build_strategy(
-2
View File
@@ -28,5 +28,3 @@ class Trade:
exit_price: float
pnl: float
r_multiple: float = 0.0
partial_tp_taken: bool = False
partial_tp_realized_pnl: float = 0.0
+24 -6
View File
@@ -1,5 +1,6 @@
from datetime import time
# EST sessions (for histdata CSVs)
SESSIONS_EST = {
"asian": (time(19, 0), time(3, 0)),
"london": (time(2, 0), time(5, 0)),
@@ -8,25 +9,42 @@ SESSIONS_EST = {
"london_ny_overlap": (time(8, 0), time(10, 0)),
}
# UTC+2 sessions (for MetaTrader exported CSVs)
SESSIONS_MT5 = {
"asian": (time(2, 0), time(10, 0)),
"london": (time(9, 0), time(12, 0)),
"new_york": (time(14, 0), time(17, 0)),
"london_close": (time(17, 0), time(19, 0)),
"london_ny_overlap": (time(15, 0), time(17, 0)),
}
# Active session map (switch based on data source)
_active_sessions = SESSIONS_EST
def set_timezone(tz="est"):
global _active_sessions
if tz.lower() in ("mt5", "utc+2", "server"):
_active_sessions = SESSIONS_MT5
else:
_active_sessions = SESSIONS_EST
def in_session(candle_time, session_name):
if session_name == "all":
return True
if session_name not in SESSIONS_EST:
if session_name not in _active_sessions:
return True
t = candle_time.time()
start, end = SESSIONS_EST[session_name]
start, end = _active_sessions[session_name]
if start > end:
return t >= start or t < end
return start <= t < end
def get_session(candle_time):
for name in SESSIONS_EST:
if name == "all":
continue
for name in _active_sessions:
if in_session(candle_time, name):
return name
return "off_hours"