File size: 3,951 Bytes
fc115d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env python3
"""
Check which storage backend is being used and show current state.
"""

import os
import sys
from pathlib import Path

# Add project root to path
project_root = Path(__file__).parent
sys.path.insert(0, str(project_root))

# Load environment variables
env_file = project_root / ".env"
if env_file.exists():
    with open(env_file) as f:
        for line in f:
            line = line.strip()
            if line and not line.startswith('#') and '=' in line:
                key, value = line.split('=', 1)
                os.environ[key.strip()] = value.strip()

from src.data.storage import get_storage

def check_storage():
    """Check storage backend and current state."""

    print("=" * 60)
    print("STORAGE DIAGNOSTICS")
    print("=" * 60)

    # Get storage instance
    storage = get_storage()
    storage_type = type(storage).__name__

    print(f"\n๐Ÿ“Š Storage Backend: {storage_type}")

    if storage_type == "JsonFileStorage":
        print(f"   - State file: {storage.state_file}")
        print(f"   - Trade file: {storage.trade_file}")
        print(f"   - State exists: {storage.state_file.exists()}")
        print(f"   - Trades exist: {storage.trade_file.exists()}")

        if storage.state_file.exists():
            import json
            with open(storage.state_file) as f:
                state = json.load(f)
            print(f"\n๐Ÿ“‹ Current State (JSON):")
            print(f"   - Keys: {list(state.keys())}")
            if 'positions' in state:
                print(f"   - Positions: {state['positions']}")

    elif storage_type == "MongoStorage":
        env = os.getenv("ENVIRONMENT", "production").lower()
        db_name = "trading_system" if env == "production" else f"trading_system_{env}"
        print(f"   - Database: {db_name}")
        print(f"   - Environment: {env}")

        # Count documents
        state_count = storage.state_collection.count_documents({})
        trades_count = storage.trades_collection.count_documents({})

        print(f"\n๐Ÿ“Š MongoDB State:")
        print(f"   - State documents: {state_count}")
        print(f"   - Trade documents: {trades_count}")

        if state_count > 0:
            state = storage.load_state()
            print(f"\n๐Ÿ“‹ Current State (MongoDB):")
            print(f"   - Keys: {list(state.keys())}")
            if 'positions' in state:
                print(f"   - Positions: {state['positions']}")

    # Load state via interface
    print("\n" + "=" * 60)
    print("CURRENT STATE VIA STORAGE INTERFACE")
    print("=" * 60)

    state = storage.load_state()

    if state:
        print(f"\n๐Ÿ“‹ Loaded state keys: {list(state.keys())}")

        if 'positions' in state:
            positions = state['positions']
            print(f"\n๐Ÿ“Š Open Positions:")
            if positions:
                for asset, pos in positions.items():
                    if pos and isinstance(pos, dict) and pos.get('quantity', 0) != 0:
                        print(f"\n   {asset}:")
                        print(f"      Side: {pos.get('side', 'UNKNOWN')}")
                        print(f"      Quantity: {pos.get('quantity', 0)}")
                        print(f"      Entry Price: ${pos.get('entry_price', 0):.2f}")
                        print(f"      Entry Time: {pos.get('entry_time', 'UNKNOWN')}")
            else:
                print("   No open positions")

        if 'portfolio' in state:
            portfolio = state['portfolio']
            print(f"\n๐Ÿ’ฐ Portfolio:")
            print(f"   - Balance: ${portfolio.get('balance', 0):.2f}")
            print(f"   - Equity: ${portfolio.get('equity', 0):.2f}")
    else:
        print("\nโœ… State is empty - database clean!")

    print("\n" + "=" * 60)

if __name__ == "__main__":
    try:
        check_storage()
    except Exception as e:
        print(f"\nโŒ Error: {e}")
        import traceback
        traceback.print_exc()
        sys.exit(1)