beef up DQN model, fix training issues

This commit is contained in:
Dobromir Popov
2025-07-27 20:48:44 +03:00
parent 1894d453c9
commit bd986f4534
6 changed files with 414 additions and 55 deletions

View File

@ -28,10 +28,14 @@ from pathlib import Path
from typing import Dict, List, Optional, Tuple, Any, Callable
from dataclasses import dataclass, field
import ta
import warnings
from threading import Thread, Lock
from collections import deque
import math
# Suppress ta library deprecation warnings
warnings.filterwarnings("ignore", category=FutureWarning, module="ta")
from .config import get_config
from .tick_aggregator import RealTimeTickAggregator, RawTick, OHLCVBar
from .cnn_monitor import log_cnn_prediction
@ -1127,17 +1131,20 @@ class DataProvider:
# Convert timestamp to datetime if needed
if isinstance(timestamp, (int, float)):
tick_time = datetime.fromtimestamp(timestamp, tz=pd.Timestamp.now().tz)
# If no timezone info, assume UTC and convert to Europe/Sofia
if tick_time.tzinfo is None:
tick_time = tick_time.replace(tzinfo=pd.Timestamp.now(tz='UTC').tz)
tick_time = tick_time.astimezone(pd.Timestamp.now(tz='Europe/Sofia').tz)
import pytz
utc = pytz.UTC
sofia_tz = pytz.timezone('Europe/Sofia')
tick_time = datetime.fromtimestamp(timestamp, tz=utc)
tick_time = tick_time.astimezone(sofia_tz)
elif isinstance(timestamp, datetime):
import pytz
sofia_tz = pytz.timezone('Europe/Sofia')
tick_time = timestamp
# If no timezone info, assume UTC and convert to Europe/Sofia
if tick_time.tzinfo is None:
tick_time = tick_time.replace(tzinfo=pd.Timestamp.now(tz='UTC').tz)
tick_time = tick_time.astimezone(pd.Timestamp.now(tz='Europe/Sofia').tz)
utc = pytz.UTC
tick_time = utc.localize(tick_time)
tick_time = tick_time.astimezone(sofia_tz)
else:
continue
@ -1177,6 +1184,16 @@ class DataProvider:
# Convert to DataFrame
df = pd.DataFrame(candles)
# Ensure timestamps are timezone-aware (Europe/Sofia)
if not df.empty and 'timestamp' in df.columns:
import pytz
sofia_tz = pytz.timezone('Europe/Sofia')
# If timestamps are not timezone-aware, make them Europe/Sofia
if df['timestamp'].dt.tz is None:
df['timestamp'] = df['timestamp'].dt.tz_localize(sofia_tz)
else:
df['timestamp'] = df['timestamp'].dt.tz_convert(sofia_tz)
df = df.sort_values('timestamp').reset_index(drop=True)
# Limit to requested number
@ -1991,6 +2008,15 @@ class DataProvider:
if cache_file.exists():
try:
df = pd.read_parquet(cache_file)
# Ensure cached monthly data has proper timezone (Europe/Sofia)
if not df.empty and 'timestamp' in df.columns:
if df['timestamp'].dt.tz is None:
# If no timezone info, assume UTC and convert to Europe/Sofia
df['timestamp'] = pd.to_datetime(df['timestamp'], utc=True)
df['timestamp'] = df['timestamp'].dt.tz_convert('Europe/Sofia')
elif str(df['timestamp'].dt.tz) != 'Europe/Sofia':
# Convert to Europe/Sofia if different timezone
df['timestamp'] = df['timestamp'].dt.tz_convert('Europe/Sofia')
logger.info(f"Loaded {len(df)} 1m candles from cache for {symbol}")
return df
except Exception as parquet_e:
@ -2266,6 +2292,15 @@ class DataProvider:
if cache_age < max_age:
try:
df = pd.read_parquet(cache_file)
# Ensure cached data has proper timezone (Europe/Sofia)
if not df.empty and 'timestamp' in df.columns:
if df['timestamp'].dt.tz is None:
# If no timezone info, assume UTC and convert to Europe/Sofia
df['timestamp'] = pd.to_datetime(df['timestamp'], utc=True)
df['timestamp'] = df['timestamp'].dt.tz_convert('Europe/Sofia')
elif str(df['timestamp'].dt.tz) != 'Europe/Sofia':
# Convert to Europe/Sofia if different timezone
df['timestamp'] = df['timestamp'].dt.tz_convert('Europe/Sofia')
logger.debug(f"Loaded {len(df)} rows from cache for {symbol} {timeframe} (age: {cache_age/60:.1f}min)")
return df
except Exception as parquet_e: