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
+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"