wip wip wip

This commit is contained in:
Dobromir Popov
2025-10-23 18:57:07 +03:00
parent b0771ff34e
commit 0225f4df58
17 changed files with 2739 additions and 756 deletions

View File

@@ -159,8 +159,19 @@ class AnnotationManager:
return result
def delete_annotation(self, annotation_id: str):
"""Delete annotation"""
def delete_annotation(self, annotation_id: str) -> bool:
"""
Delete annotation and its associated test case file
Args:
annotation_id: ID of annotation to delete
Returns:
bool: True if annotation was deleted, False if not found
Raises:
Exception: If there's an error during deletion
"""
original_count = len(self.annotations_db["annotations"])
self.annotations_db["annotations"] = [
a for a in self.annotations_db["annotations"]
@@ -168,28 +179,89 @@ class AnnotationManager:
]
if len(self.annotations_db["annotations"]) < original_count:
# Annotation was found and removed
self._save_annotations()
# Also delete the associated test case file
test_case_file = self.test_cases_dir / f"annotation_{annotation_id}.json"
if test_case_file.exists():
try:
test_case_file.unlink()
logger.info(f"Deleted test case file: {test_case_file}")
except Exception as e:
logger.error(f"Error deleting test case file {test_case_file}: {e}")
# Don't fail the whole operation if test case deletion fails
logger.info(f"Deleted annotation: {annotation_id}")
return True
else:
logger.warning(f"Annotation not found: {annotation_id}")
return False
def clear_all_annotations(self, symbol: str = None):
"""
Clear all annotations (optionally filtered by symbol)
More efficient than deleting one by one
Args:
symbol: Optional symbol filter. If None, clears all annotations.
Returns:
int: Number of annotations deleted
"""
# Get annotations to delete
if symbol:
annotations_to_delete = [
a for a in self.annotations_db["annotations"]
if a.get('symbol') == symbol
]
# Keep annotations for other symbols
self.annotations_db["annotations"] = [
a for a in self.annotations_db["annotations"]
if a.get('symbol') != symbol
]
else:
annotations_to_delete = self.annotations_db["annotations"].copy()
self.annotations_db["annotations"] = []
deleted_count = len(annotations_to_delete)
if deleted_count > 0:
# Save updated annotations database
self._save_annotations()
# Delete associated test case files
for annotation in annotations_to_delete:
annotation_id = annotation.get('annotation_id')
test_case_file = self.test_cases_dir / f"annotation_{annotation_id}.json"
if test_case_file.exists():
try:
test_case_file.unlink()
logger.debug(f"Deleted test case file: {test_case_file}")
except Exception as e:
logger.error(f"Error deleting test case file {test_case_file}: {e}")
logger.info(f"Cleared {deleted_count} annotations" + (f" for symbol {symbol}" if symbol else ""))
return deleted_count
def generate_test_case(self, annotation: TradeAnnotation, data_provider=None, auto_save: bool = True) -> Dict:
"""
Generate test case from annotation in realtime format
Generate lightweight test case metadata (no OHLCV data stored)
OHLCV data will be fetched dynamically from cache/database during training
Args:
annotation: TradeAnnotation object
data_provider: Optional DataProvider instance to fetch market context
data_provider: Optional DataProvider instance (not used for storage)
Returns:
Test case dictionary in realtime format
Test case metadata dictionary
"""
test_case = {
"test_case_id": f"annotation_{annotation.annotation_id}",
"symbol": annotation.symbol,
"timestamp": annotation.entry['timestamp'],
"action": "BUY" if annotation.direction == "LONG" else "SELL",
"market_state": {},
"expected_outcome": {
"direction": annotation.direction,
"profit_loss_pct": annotation.profit_loss_pct,
@@ -203,53 +275,22 @@ class AnnotationManager:
"notes": annotation.notes,
"created_at": annotation.created_at,
"timeframe": annotation.timeframe
},
"training_config": {
"context_window_minutes": 5, # ±5 minutes around entry/exit
"timeframes": ["1s", "1m", "1h", "1d"],
"data_source": "cache" # Will fetch from cache/database
}
}
# Populate market state with ±5 minutes of data for training
if data_provider:
try:
entry_time = datetime.fromisoformat(annotation.entry['timestamp'].replace('Z', '+00:00'))
exit_time = datetime.fromisoformat(annotation.exit['timestamp'].replace('Z', '+00:00'))
logger.info(f"Fetching market state for {annotation.symbol} at {entry_time} (±5min around entry)")
# Use the new data provider method to get market state at the entry time
market_state = data_provider.get_market_state_at_time(
symbol=annotation.symbol,
timestamp=entry_time,
context_window_minutes=5
)
# Add training labels for each timestamp
# This helps model learn WHERE to signal and WHERE NOT to signal
market_state['training_labels'] = self._generate_training_labels(
market_state,
entry_time,
exit_time,
annotation.direction
)
test_case["market_state"] = market_state
logger.info(f"Populated market state with {len(market_state)-1} timeframes + training labels")
except Exception as e:
logger.error(f"Error fetching market state: {e}")
import traceback
traceback.print_exc()
test_case["market_state"] = {}
else:
logger.warning("No data_provider available, market_state will be empty")
test_case["market_state"] = {}
# Save test case to file if auto_save is True
# Save lightweight test case metadata to file if auto_save is True
if auto_save:
test_case_file = self.test_cases_dir / f"{test_case['test_case_id']}.json"
with open(test_case_file, 'w') as f:
json.dump(test_case, f, indent=2)
logger.info(f"Saved test case to: {test_case_file}")
logger.info(f"Saved test case metadata to: {test_case_file}")
logger.info(f"Generated test case: {test_case['test_case_id']}")
logger.info(f"Generated lightweight test case: {test_case['test_case_id']} (OHLCV data will be fetched dynamically)")
return test_case
def get_all_test_cases(self) -> List[Dict]: