-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_positions.py
More file actions
87 lines (76 loc) · 2.6 KB
/
debug_positions.py
File metadata and controls
87 lines (76 loc) · 2.6 KB
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
#!/usr/bin/env python
"""Debug: trace open positions through the entire flow."""
import sys
from datetime import datetime, timezone
from config import settings
from oms import OMS
from strategies.base_strategy import Signal
from system_state import publish_from_engine, snapshot
print("=== Step 1: Create OMS and open a position ===")
oms = OMS(config=settings)
# Create a signal
sig = Signal(
direction="LONG",
reason="OBI_LONG",
price=70000.0,
timestamp=datetime.now(timezone.utc),
)
# Validate and open
allowed, reason = oms.validate_entry(sig)
print(f"Entry allowed: {allowed}, reason: {reason}")
if allowed:
pos = oms.open_position(sig)
print(f"Position opened: {pos.id}")
print(f" Direction: {pos.direction}")
print(f" Entry price: {pos.entry_price}")
print(f" Entry reason: {pos.entry_reason}")
else:
print("ERROR: Entry not allowed!")
sys.exit(1)
print("\n=== Step 2: Get open positions from OMS ===")
open_positions = oms.get_open_positions()
print(f"Open positions count: {len(open_positions)}")
for p in open_positions:
print(f" - {p.id}: {p.direction} @ {p.entry_price}")
print("\n=== Step 3: Build position dicts like engine does ===")
pos_dicts = []
px = 70100.0 # Current price
for p in open_positions:
pnl_pct = ((px - float(p.entry_price)) / float(p.entry_price) * 100.0) if p.entry_price else 0.0
pos_dict = {
"id": p.id,
"symbol": p.symbol,
"direction": p.direction,
"entry_price": float(p.entry_price),
"current_price": px,
"unrealized_pnl_usdt": float(p.pnl_usdt),
"pnl_pct": pnl_pct,
"stop_loss_price": float(p.stop_loss_price),
"entry_reason": p.entry_reason or "",
}
pos_dicts.append(pos_dict)
print(f"Position dict: {pos_dict}")
print("\n=== Step 4: Publish to system_state ===")
publish_from_engine(
price=px,
tick_utc=datetime.now(timezone.utc),
rsi=None,
trix=None,
candle=None,
positions=pos_dicts,
daily_trade_count=1,
unrealized_total=1.0,
paper_equity=10001.0,
)
print("Published to system_state")
print("\n=== Step 5: Get snapshot like dashboard does ===")
snap = snapshot()
print(f"Positions in snapshot: {len(snap['positions'])}")
for p in snap['positions']:
print(f" - {p['id']}: {p['direction']} @ {p['entry_price']}")
print(f" Entry reason: {p.get('entry_reason')}")
print(f" uPnL: {p.get('unrealized_pnl_usdt')} USDT ({p.get('pnl_pct'):.2f}%)")
if not snap['positions']:
print("\nERROR: No positions in snapshot!")
sys.exit(1)
print("\n✅ SUCCESS: Positions flow correctly through entire system!")