#!/usr/bin/env python3
import json
from pathlib import Path

# Load the graph
graph_file = Path("/home/ubuntu/.hermes/workspace/projects/ORDER_FLOW_GRAPH/data/order_flow_graph_phase1.json")
with open(graph_file, 'r') as f:
    graph = json.load(f)

# Check price level metrics
print("="*80)
print("ORDER FLOW GRAPH ANALYSIS")
print("="*80)

price_levels = [e for e in graph['entities'] if e['type'] == 'PriceLevel']
anomalies = [e for e in graph['entities'] if e['type'] == 'Anomaly']

print(f"\nPrice Levels: {len(price_levels)}")
print(f"Anomalies: {len(anomalies)}")

# Show sample price levels
print("\n" + "="*80)
print("SAMPLE PRICE LEVELS")
print("="*80)

for i, pl in enumerate(price_levels[:10], 1):
    props = pl['properties']
    print(f"\n{i}. Price: ${props['price']}")
    print(f"   Ticks: {props['tick_count']}")
    print(f"   Volume: {props['total_volume']}")
    print(f"   Bid: {props['bid_volume']}, Ask: {props['ask_volume']}")
    print(f"   Delta: {props['delta']}")
    print(f"   Imbalance: {props['imbalance']:.3f}")
    print(f"   Spread: {props['avg_spread']:.2f}")

# Show anomalies
print("\n" + "="*80)
print("ANOMALIES DETECTED")
print("="*80)

for i, anom in enumerate(anomalies[:10], 1):
    props = anom['properties']
    print(f"\n{i}. {props['type']} at {anom['name']}")
    print(f"   Direction: {props['direction']}")
    print(f"   Severity: {props['severity']:.2f}")
    print(f"   Reason: {props['reason']}")

# Check volume distribution
volumes = [pl['properties']['total_volume'] for pl in price_levels]
deltas = [pl['properties']['delta'] for pl in price_levels]

print("\n" + "="*80)
print("VOLUME STATISTICS")
print("="*80)
print(f"Min volume: {min(volumes)}")
print(f"Max volume: {max(volumes)}")
print(f"Avg volume: {sum(volumes)/len(volumes):.1f}")

print(f"\nMin delta: {min(deltas)}")
print(f"Max delta: {max(deltas)}")
print(f"Avg delta: {sum(deltas)/len(deltas):.1f}")

print("\n" + "="*80)
print("ANALYSIS COMPLETE")
print("="*80)
print("\nISSUE: Sierra tick data has very low volume per tick (0-2 contracts)")
print("This makes absorption/squeeze detection difficult.")
print("\nSOLUTION: Need to aggregate more ticks or use different data source")
print("="*80)
