-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
98 lines (87 loc) · 2.59 KB
/
database.py
File metadata and controls
98 lines (87 loc) · 2.59 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
88
89
90
91
92
93
94
95
96
97
98
# database.py
import sqlite3
import time
from typing import Optional
DB_FILE = "trading.db"
def get_connection():
conn = sqlite3.connect(DB_FILE, check_same_thread=False)
conn.row_factory = sqlite3.Row
return conn
def init_db():
conn = get_connection()
cursor = conn.cursor()
# 1. POSITIONS (What we own)
cursor.execute('''
CREATE TABLE IF NOT EXISTS positions (
symbol TEXT PRIMARY KEY,
qty REAL,
entry_price REAL,
current_price REAL,
stop_loss REAL,
take_profit REAL,
peak_price REAL,
entry_time INTEGER,
status TEXT
)
''')
# 2. ORDERS (History)
cursor.execute('''
CREATE TABLE IF NOT EXISTS orders (
order_id TEXT PRIMARY KEY,
symbol TEXT,
side TEXT,
qty REAL,
price REAL,
timestamp INTEGER,
reason TEXT
)
''')
# 3. WATCHLIST (The Investigator's Findings) <--- NEW!
cursor.execute('''
CREATE TABLE IF NOT EXISTS watchlist (
symbol TEXT PRIMARY KEY,
timeframe TEXT,
pattern_score REAL,
est_hold_time TEXT,
last_updated INTEGER
)
''')
# 4. LOGS
cursor.execute('''
CREATE TABLE IF NOT EXISTS logs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
timestamp INTEGER,
module TEXT,
level TEXT,
message TEXT
)
''')
conn.commit()
conn.close()
print("✅ Database initialized (trading.db)")
def log(module: str, message: str, level: str = "INFO"):
try:
conn = get_connection()
conn.execute("INSERT INTO logs (timestamp, module, level, message) VALUES (?, ?, ?, ?)",
(int(time.time()), module, level, message))
conn.commit()
conn.close()
print(f"[{module}] {message}")
except Exception as e:
print(f"❌ LOGGING ERROR: {e}")
# --- WATCHLIST MANAGEMENT ---
def update_watchlist(symbol, timeframe, score, hold_time):
conn = get_connection()
conn.execute('''
INSERT INTO watchlist (symbol, timeframe, pattern_score, est_hold_time, last_updated)
VALUES (?, ?, ?, ?, ?)
ON CONFLICT(symbol) DO UPDATE SET
timeframe=excluded.timeframe,
pattern_score=excluded.pattern_score,
est_hold_time=excluded.est_hold_time,
last_updated=excluded.last_updated
''', (symbol, timeframe, score, hold_time, int(time.time())))
conn.commit()
conn.close()
# Initialize immediately
init_db()