File size: 6,267 Bytes
da0ce7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166

import os
import sys
import json
import time
from dotenv import load_dotenv

# Load environment variables
load_dotenv()

# Add current directory to path
sys.path.append(os.getcwd())

from binance_client import BinanceClient
from kraken_client import KrakenClient

def debug_binance():
    print("\n" + "="*50)
    print("πŸ” DIAGNOSTIC: BINANCE")
    print("="*50)
    
    api_key = os.getenv("BINANCE_API_KEY")
    api_secret = os.getenv("BINANCE_SECRET_KEY")
    
    if not api_key:
        print("❌ Missing BINANCE_API_KEY")
        return

    # FIXED: No args for init (loads from env)
    client = get_binance_client()
    
    # Check Connectivity
    try:
        status = client.get_system_status()
        print(f"βœ… System Status: {status.get('msg', 'OK')}")
    except Exception as e:
        print(f"❌ Connection Failed: {e}")
        return

    # Check Balances
    try:
        account = client.account()
        balances = [b for b in account.get("balances", []) if float(b.get("free", 0)) > 0 or float(b.get("locked", 0)) > 0]
        
        if not balances:
            print("⚠️ No non-zero balances found.")
        else:
            print(f"πŸ’° Found {len(balances)} assets with balance:")
            for b in balances:
                asset = b['asset']
                amount = float(b['free']) + float(b['locked'])
                print(f"   - {asset}: {amount}")
                
                # Try to find trades for this asset
                quotes = ['USDT', 'USDC', 'GBP', 'EUR', 'BTC', 'ETH', 'FDUSD']
                found_trades = False
                
                for quote in quotes:
                    if asset == quote: continue
                    symbol = f"{asset}{quote}"
                    try:
                        trades = client.get_my_trades(symbol, limit=5)
                        orders = client.get_all_orders(symbol, limit=5, silent=True)
                        
                        if trades:
                            print(f"     βœ… Found {len(trades)} trades for {symbol}")
                            print(f"        Sample Trade: {trades[0]}")
                            found_trades = True
                        
                        if orders:
                            print(f"     βœ… Found {len(orders)} orders for {symbol}")
                            filled_orders = [o for o in orders if o['status'] == 'FILLED']
                            print(f"        Filled: {len(filled_orders)}")
                            if filled_orders:
                                print(f"        Sample Order: {filled_orders[0]}")
                            found_trades = True
                            
                            # Calculate cost basis from Orders if possible
                            if filled_orders:
                                total_qty = 0.0
                                total_cost = 0.0
                                for o in filled_orders:
                                    if o['side'] == 'BUY':
                                        qty = float(o['executedQty'])
                                        cost = float(o['cummulativeQuoteQty'])
                                        total_qty += qty
                                        total_cost += cost
                                if total_qty > 0:
                                    print(f"        Derived Avg Buy Price: {total_cost/total_qty}")

                        if not trades and not orders:
                            # print(f"     . No trades for {symbol}")
                            pass
                    except Exception as e:
                        print(f"     x Error checking {symbol}: {str(e)}")
                        pass
                        
                if not found_trades:
                    print(f"     ⚠️ No trades found for {asset} with standard pairs.")
                    
    except Exception as e:
        print(f"❌ Account/Trade fetch failed: {e}")

def debug_kraken():
    print("\n" + "="*50)
    print("πŸ” DIAGNOSTIC: KRAKEN")
    print("="*50)
    
    api_key = os.getenv("KRAKEN_API_KEY")
    api_secret = os.getenv("KRAKEN_PRIVATE_KEY")
    
    if not api_key:
        print("❌ Missing KRAKEN_API_KEY")
        return

    client = KrakenClient(api_key, api_secret)
    
    # Check Balances
    try:
        balance = client.get_account_balance()
        if not balance:
            print("⚠️ No balances returned.")
        else:
            print(f"πŸ’° Balances: {balance}")
            
        print("\n--- Checking History Sources ---")
        
        # 1. Check Trades History
        print("1️⃣  Checking 'TradesHistory' endpoint...")
        trades = client.get_trades_history()
        if trades:
            count = len(trades)
            print(f"   βœ… Found {count} historical trades.")
            first_key = list(trades.keys())[0]
            print(f"   Sample Key: {first_key}")
            print(f"   Sample Data: {trades[first_key]}")
            
            # Check Pairs in history
            pairs = set(t.get('pair') for t in trades.values())
            print(f"   Pairs found in history: {pairs}")
        else:
            print("   ⚠️ TradesHistory returned empty/None.")

        # 2. Check Ledgers (User suggestion)
        print("\n2️⃣  Checking 'Ledgers' endpoint...")
        ledgers = client.get_ledgers()
        if ledgers:
            count = len(ledgers)
            print(f"   βœ… Found {count} ledger entries.")
            first_key = list(ledgers.keys())[0]
            print(f"   Sample Key: {first_key}")
            print(f"   Sample Data: {ledgers[first_key]}")
            
            # types in ledger
            types = set(l.get('type') for l in ledgers.values())
            print(f"   Ledger Types found: {types}")
        else:
            print("   ⚠️ Ledgers returned empty/None.")
            
    except Exception as e:
        print(f"❌ Kraken Diagnostics failed: {e}")

if __name__ == "__main__":
    debug_binance()
    debug_kraken()