-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathqt_dashboard.py
210 lines (173 loc) · 7.52 KB
/
qt_dashboard.py
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import sys
import asyncio
from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget, QVBoxLayout,
QHBoxLayout, QLabel, QTableWidget, QTableWidgetItem,
QHeaderView)
from PyQt5.QtCore import QTimer, Qt
from PyQt5.QtGui import QFont, QColor
from trading.engine import TradingEngine
from datetime import datetime
import json
import qasync
import gc
class TradingDashboard(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('MemeHunter Trading Bot')
self.setGeometry(100, 100, 1200, 800) # Smaller window
self.setStyleSheet("background-color: white;")
# Initialize trading engine
self.trading_engine = TradingEngine()
# Create central widget and layout
central_widget = QWidget()
self.setCentralWidget(central_widget)
layout = QVBoxLayout(central_widget)
layout.setSpacing(10) # Reduce spacing
# Create metrics section
metrics_widget = QWidget()
metrics_layout = QHBoxLayout(metrics_widget)
metrics_layout.setSpacing(10) # Reduce spacing
# Style for metric cards (simplified)
metric_style = """
QWidget#metric_card {
background-color: white;
border: 1px solid #ddd;
border-radius: 5px;
padding: 10px;
margin: 5px;
}
QLabel#value_label {
font-size: 24px;
color: #1E88E5;
font-weight: bold;
}
QLabel#label_label {
font-size: 16px;
color: #666;
}
"""
# Create metric cards
self.wallet_balance = self.create_metric_card("Wallet Balance", "$0.00", metric_style)
self.win_rate = self.create_metric_card("Win Rate", "0.0%", metric_style)
self.total_trades = self.create_metric_card("Total Trades", "0", metric_style)
self.moonshots = self.create_metric_card("Moonshots", "0", metric_style)
metrics_layout.addWidget(self.wallet_balance)
metrics_layout.addWidget(self.win_rate)
metrics_layout.addWidget(self.total_trades)
metrics_layout.addWidget(self.moonshots)
layout.addWidget(metrics_widget)
# Create trades table (optimized)
self.trades_table = QTableWidget()
self.trades_table.setColumnCount(6) # Removed 'Status' column
self.trades_table.setHorizontalHeaderLabels([
'Time', 'Token', 'Type', 'Amount', 'Price', 'Profit'
])
# Set table style (simplified)
self.trades_table.setStyleSheet("""
QTableWidget {
background-color: white;
border: 1px solid #ddd;
padding: 5px;
}
QHeaderView::section {
background-color: white;
padding: 5px;
border: none;
font-weight: bold;
}
""")
# Auto-resize columns to content
header = self.trades_table.horizontalHeader()
for i in range(6):
header.setSectionResizeMode(i, QHeaderView.ResizeToContents)
layout.addWidget(self.trades_table)
# Create update timer (reduced frequency)
self.timer = QTimer()
self.timer.timeout.connect(self.update_dashboard)
self.timer.start(5000) # Update every 5 seconds instead of 2
# Memory optimization
self.last_trades = []
gc.collect() # Force garbage collection
def create_metric_card(self, label_text, value_text, style):
"""Create a metric card (optimized)"""
card = QWidget()
card.setObjectName("metric_card")
card.setStyleSheet(style)
layout = QVBoxLayout(card)
layout.setSpacing(5)
value = QLabel(value_text)
value.setObjectName("value_label")
value.setAlignment(Qt.AlignCenter)
label = QLabel(label_text)
label.setObjectName("label_label")
label.setAlignment(Qt.AlignCenter)
layout.addWidget(value)
layout.addWidget(label)
return card
def update_dashboard(self):
"""Update dashboard (optimized)"""
try:
# Get trading data
data = self.trading_engine.get_trading_metrics()
if not data:
return
# Update metrics (only if changed)
balance = f"${data['wallet_balance']:.2f}"
if self.wallet_balance.findChild(QLabel, "value_label").text() != balance:
self.wallet_balance.findChild(QLabel, "value_label").setText(balance)
win_rate = f"{data['win_rate']:.1f}%"
if self.win_rate.findChild(QLabel, "value_label").text() != win_rate:
self.win_rate.findChild(QLabel, "value_label").setText(win_rate)
trades = str(data['total_trades'])
if self.total_trades.findChild(QLabel, "value_label").text() != trades:
self.total_trades.findChild(QLabel, "value_label").setText(trades)
moonshots = str(data['moonshots'])
if self.moonshots.findChild(QLabel, "value_label").text() != moonshots:
self.moonshots.findChild(QLabel, "value_label").setText(moonshots)
# Update trades table (only if new trades)
new_trades = data['recent_trades']
if new_trades != self.last_trades:
self.trades_table.setRowCount(len(new_trades))
for i, trade in enumerate(new_trades):
# Time
self.trades_table.setItem(i, 0, QTableWidgetItem(trade['timestamp']))
# Token
self.trades_table.setItem(i, 1, QTableWidgetItem(trade['token']))
# Type
self.trades_table.setItem(i, 2, QTableWidgetItem(trade['type']))
# Amount
self.trades_table.setItem(i, 3, QTableWidgetItem(f"{trade['amount']:.2f}"))
# Price
self.trades_table.setItem(i, 4, QTableWidgetItem(f"${trade['price']:.4f}"))
# Profit with color
profit_item = QTableWidgetItem(f"{trade['profit']:.2f}%")
if trade['profit'] > 0:
profit_item.setForeground(QColor('#4CAF50'))
else:
profit_item.setForeground(QColor('#f44336'))
self.trades_table.setItem(i, 5, profit_item)
self.last_trades = new_trades
# Force cleanup
gc.collect()
except Exception as e:
print(f"Error updating dashboard: {e}")
async def start_trading_engine(self):
"""Start the trading engine in the background"""
try:
await self.trading_engine.start()
except Exception as e:
print(f"Error starting trading engine: {e}")
if __name__ == '__main__':
app = QApplication(sys.argv)
# Set application-wide font (system default for better performance)
app.setStyle('Fusion') # Use Fusion style for better performance
window = TradingDashboard()
window.show()
# Create event loop
loop = qasync.QEventLoop(app)
asyncio.set_event_loop(loop)
# Start trading engine
loop.create_task(window.start_trading_engine())
# Run event loop
with loop:
loop.run_forever()