training fixes

This commit is contained in:
Dobromir Popov
2025-10-31 01:29:05 +02:00
parent ba91740e4c
commit cefd30d2bd
2 changed files with 68 additions and 23 deletions

View File

@@ -441,12 +441,19 @@ class RealTrainingAdapter:
logger.debug(" No holding period, skipping HOLD samples")
return hold_samples
# Parse entry timestamp
# Parse entry timestamp - handle multiple formats
try:
if 'T' in entry_timestamp:
entry_time = datetime.fromisoformat(entry_timestamp.replace('Z', '+00:00'))
else:
entry_time = datetime.strptime(entry_timestamp, '%Y-%m-%d %H:%M:%S')
# Try with seconds first, then without
try:
entry_time = datetime.strptime(entry_timestamp, '%Y-%m-%d %H:%M:%S')
except ValueError:
# Try without seconds
entry_time = datetime.strptime(entry_timestamp, '%Y-%m-%d %H:%M')
# Make timezone-aware
if pytz:
entry_time = entry_time.replace(tzinfo=pytz.UTC)
else:
@@ -466,7 +473,21 @@ class RealTrainingAdapter:
# Find all candles between entry and exit
for idx, ts_str in enumerate(timestamps):
ts = datetime.fromisoformat(ts_str.replace(' ', 'T'))
# Parse timestamp and ensure it's timezone-aware
try:
if 'T' in ts_str:
ts = datetime.fromisoformat(ts_str.replace('Z', '+00:00'))
else:
ts = datetime.fromisoformat(ts_str.replace(' ', 'T'))
# Make timezone-aware if it's naive
if ts.tzinfo is None:
if pytz:
ts = ts.replace(tzinfo=pytz.UTC)
else:
ts = ts.replace(tzinfo=timezone.utc)
except Exception as e:
logger.debug(f"Could not parse timestamp '{ts_str}': {e}")
continue
# If this candle is between entry and exit (exclusive)
if entry_time < ts < exit_time:
@@ -534,7 +555,14 @@ class RealTrainingAdapter:
if 'T' in signal_timestamp:
signal_time = datetime.fromisoformat(signal_timestamp.replace('Z', '+00:00'))
else:
signal_time = datetime.strptime(signal_timestamp, '%Y-%m-%d %H:%M:%S')
# Try with seconds first, then without
try:
signal_time = datetime.strptime(signal_timestamp, '%Y-%m-%d %H:%M:%S')
except ValueError:
# Try without seconds
signal_time = datetime.strptime(signal_timestamp, '%Y-%m-%d %H:%M')
# Make timezone-aware
if pytz:
signal_time = signal_time.replace(tzinfo=pytz.UTC)
else:
@@ -546,15 +574,22 @@ class RealTrainingAdapter:
signal_index = None
for idx, ts_str in enumerate(timestamps):
try:
# Parse timestamp from market data
# Parse timestamp from market data - handle multiple formats
if 'T' in ts_str:
ts = datetime.fromisoformat(ts_str.replace('Z', '+00:00'))
else:
ts = datetime.strptime(ts_str, '%Y-%m-%d %H:%M:%S')
if pytz:
ts = ts.replace(tzinfo=pytz.UTC)
else:
ts = ts.replace(tzinfo=timezone.utc)
# Try with seconds first, then without
try:
ts = datetime.strptime(ts_str, '%Y-%m-%d %H:%M:%S')
except ValueError:
ts = datetime.strptime(ts_str, '%Y-%m-%d %H:%M')
# Make timezone-aware if naive
if ts.tzinfo is None:
if pytz:
ts = ts.replace(tzinfo=pytz.UTC)
else:
ts = ts.replace(tzinfo=timezone.utc)
# Match within 1 minute
if abs((ts - signal_time).total_seconds()) < 60:
@@ -1166,9 +1201,13 @@ class RealTrainingAdapter:
batch = self._convert_annotation_to_transformer_batch(data)
if batch is not None:
# Repeat based on repetitions parameter
# IMPORTANT: Clone each batch to avoid in-place operation issues when reusing tensors
repetitions = data.get('repetitions', 1)
for _ in range(repetitions):
converted_batches.append(batch)
# Clone all tensors in the batch to ensure independence
cloned_batch = {k: v.clone() if isinstance(v, torch.Tensor) else v
for k, v in batch.items()}
converted_batches.append(cloned_batch)
else:
logger.warning(f" Failed to convert sample {i+1}")