-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_rpi5.py
More file actions
677 lines (581 loc) · 24.3 KB
/
main_rpi5.py
File metadata and controls
677 lines (581 loc) · 24.3 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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
#!/usr/bin/env python3
"""
ZEUS AUTONOMOUS TRADING BOT - Raspberry Pi 5 Version
Main entry point - Standalone version without Replit dependencies
Run from project root: python main_rpi5.py
"""
import asyncio
import os
import sys
import threading
import json
from pathlib import Path
from datetime import datetime
# Add project root to path
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, PROJECT_ROOT)
try:
import pytz
LA_TZ = pytz.timezone('America/Los_Angeles')
except ImportError:
LA_TZ = None
print("[WARNING] pytz not installed, using UTC")
from flask import Flask, render_template, jsonify, request
app = Flask(__name__)
app.secret_key = os.environ.get("SECRET_KEY", os.urandom(24).hex())
# Import bot after Flask setup
bot_instance = None
bot_thread = None
bot_lock = threading.Lock()
bot_status = {
"running": False,
"mode": "UNKNOWN",
"error": None,
"balance": 0,
"holdings_count": 0,
"active_trades": 0,
"total_profit": 0,
"candidates_count": 0,
"last_scan": None,
"uptime_start": None
}
def get_la_time():
if LA_TZ:
return datetime.now(LA_TZ)
return datetime.utcnow()
def format_la_time(dt=None):
if dt is None:
dt = get_la_time()
return dt.strftime("%Y-%m-%d %I:%M:%S %p PT")
@app.route("/")
def index():
try:
return render_template("index.html",
user=None,
bot_status=bot_status,
current_time=format_la_time())
except Exception as e:
return f"""
<html><head><title>Zeus Trading Bot</title></head>
<body style="font-family: sans-serif; padding: 20px;">
<h1>Zeus Trading Bot</h1>
<p>Status: {'Running' if bot_status['running'] else 'Stopped'}</p>
<p>Mode: {bot_status['mode']}</p>
<p>Balance: ${bot_status['balance']:.2f}</p>
<p>Active Trades: {bot_status['active_trades']}</p>
<p><a href="/status">JSON Status</a> | <a href="/dashboard">Dashboard</a></p>
</body></html>
"""
@app.route("/health")
def health():
return {"status": "ok", "time": format_la_time()}, 200
@app.route("/status")
def status():
with bot_lock:
status_copy = bot_status.copy()
status_copy["current_time"] = format_la_time()
state_loaded = False
if bot_instance and bot_instance.running:
try:
state = bot_instance.get_status()
stats = state.get("stats", {})
holdings = state.get("holdings", {})
status_copy["balance"] = stats.get("equity", 0)
status_copy["holdings_count"] = len([
k for k, v in holdings.items()
if k not in ("USD", "ZUSD", "USDT", "USDC") and v > 0.0001
])
status_copy["active_trades"] = len(
state.get("active_trades", {}))
status_copy["candidates_count"] = len(
state.get("candidates", []))
status_copy["total_profit"] = stats.get("total_pnl", 0)
status_copy["cycle_count"] = state.get("cycle_count", 0)
status_copy["wins"] = stats.get("wins", 0)
status_copy["losses"] = stats.get("losses", 0)
status_copy["mode"] = stats.get(
"mode", status_copy.get("mode", "UNKNOWN"))
if hasattr(bot_instance, 'alt_data') and bot_instance.alt_data:
fg = bot_instance.alt_data.get_fear_greed()
if fg:
status_copy["fear_greed"] = {
"value": fg.value,
"classification": fg.classification
}
state_loaded = True
except Exception:
pass
if not state_loaded:
try:
state_file = Path("data/bot_state.json")
if state_file.exists():
with open(state_file, 'r') as f:
data = json.load(f)
status_copy["balance"] = round(data.get("equity", 0), 2)
holdings = data.get("holdings", {})
status_copy["holdings_count"] = len([
k for k, v in holdings.items()
if k not in ("USD", "ZUSD", "USDT",
"USDC") and v > 0.0001
])
status_copy["active_trades"] = len(
data.get("active_trades", {}))
status_copy["total_profit"] = round(
data.get("total_pnl", 0), 2)
status_copy["wins"] = data.get("wins", 0)
status_copy["losses"] = data.get("losses", 0)
config = data.get("config", {})
status_copy["mode"] = config.get(
"mode", status_copy.get("mode", "UNKNOWN"))
except Exception:
pass
return jsonify(status_copy), 200
@app.route("/api/trades")
def api_trades():
trades = []
try:
if bot_instance:
state = bot_instance.get_status()
for trade_id, trade in state.get("active_trades", {}).items():
trades.append({
"id": trade_id,
"symbol": trade.get("symbol"),
"side": trade.get("side"),
"entry_price": trade.get("entry_price"),
"size": trade.get("size"),
"stop_loss": trade.get("stop_loss"),
"take_profit": trade.get("take_profit"),
"entry_time": trade.get("entry_time"),
"status": trade.get("status"),
"is_manual": trade.get("is_manual", False),
"protected": trade.get("protected", False)
})
if not trades:
state_file = Path("data/bot_state.json")
if state_file.exists():
with open(state_file, 'r') as f:
data = json.load(f)
for trade_id, trade in data.get("active_trades", {}).items():
trades.append({
"id": trade_id,
"symbol": trade.get("symbol"),
"side": trade.get("side"),
"entry_price": trade.get("entry_price"),
"size": trade.get("size"),
"stop_loss": trade.get("stop_loss"),
"take_profit": trade.get("take_profit"),
"entry_time": trade.get("entry_time"),
"status": trade.get("status"),
"is_manual": trade.get("is_manual", False),
"protected": trade.get("protected", False)
})
return jsonify({"trades": trades, "count": len(trades)}), 200
except Exception as e:
return jsonify({"trades": [], "error": str(e)}), 200
@app.route("/api/holdings")
def api_holdings():
holdings_data = {}
active_trades = {}
try:
if bot_instance:
state = bot_instance.get_status()
holdings_data = state.get("holdings", {})
active_trades = state.get("active_trades", {})
if not holdings_data:
state_file = Path("data/bot_state.json")
if state_file.exists():
with open(state_file, 'r') as f:
data = json.load(f)
holdings_data = data.get("holdings", {})
active_trades = data.get("active_trades", {})
price_cache = {}
if bot_instance and hasattr(bot_instance,
'exchange') and bot_instance.exchange:
price_cache = getattr(bot_instance.exchange, '_ticker_cache', {})
holdings = []
skip_symbols = {"USD", "ZUSD", "EUR", "ZEUR", "USDT", "USDC", "DAI"}
for symbol, amount in holdings_data.items():
if symbol in skip_symbols or amount < 0.0001:
continue
clean_symbol = symbol
for prefix in ("X", "Z"):
if clean_symbol.startswith(prefix) and len(clean_symbol) > 3:
clean_symbol = clean_symbol[1:]
break
price = 0.0
value_usd = 0.0
for pair_suffix in ["USD", "USDT", "USDC"]:
cache_key = f"{symbol}{pair_suffix}"
if cache_key in price_cache:
cached = price_cache[cache_key]
if hasattr(cached, 'last'):
price = cached.last
elif isinstance(cached, dict):
price = cached.get('last', cached.get('price', 0))
if price > 0:
break
if price == 0:
for trade_id, trade in active_trades.items():
trade_symbol = trade.get("symbol", "")
if trade_symbol.startswith(
symbol) or trade_symbol.startswith(clean_symbol):
price = trade.get("entry_price", 0)
if price > 0:
break
value_usd = amount * price if price > 0 else 0
holdings.append({
"symbol": symbol,
"amount": amount,
"price": price,
"value_usd": value_usd,
"pnl_pct": 0
})
holdings.sort(key=lambda x: x.get("value_usd", 0), reverse=True)
return jsonify({
"holdings": holdings[:20],
"count": len(holdings)
}), 200
except Exception as e:
return jsonify({"holdings": [], "error": str(e)}), 200
recent_logs = []
@app.route("/api/logs")
def api_logs():
global recent_logs
try:
log_file = Path("logs/zeus.log")
if log_file.exists():
with open(log_file, 'r') as f:
lines = f.readlines()[-50:]
recent_logs = [l.strip()[:150] for l in lines[-30:]]
except Exception:
pass
return jsonify({"logs": recent_logs}), 200
@app.route("/api/candidates")
def api_candidates():
if not bot_instance:
return jsonify({"candidates": [], "error": "Bot not running"}), 200
try:
state = bot_instance.get_status()
candidates = state.get("candidates", [])[:20]
return jsonify({
"candidates": candidates,
"count": len(candidates)
}), 200
except Exception as e:
return jsonify({"candidates": [], "error": str(e)}), 200
@app.route("/api/bot/toggle", methods=["POST"])
def toggle_bot():
global bot_instance, bot_thread
with bot_lock:
if bot_status["running"]:
if bot_instance:
bot_instance.running = False
bot_status["running"] = False
bot_status["uptime_start"] = None
return jsonify({
"status": "stopped",
"message": "Bot stopped"
}), 200
else:
bot_thread = threading.Thread(target=run_trading_bot, daemon=True)
bot_thread.start()
return jsonify({
"status": "starting",
"message": "Bot starting..."
}), 200
@app.route("/dashboard")
def dashboard():
try:
return render_template("dashboard.html",
user=None,
bot_status=bot_status,
current_time=format_la_time())
except Exception:
return f"""
<html><head><title>Zeus Dashboard</title>
<style>
body {{ font-family: sans-serif; padding: 20px; background: #1a1a2e; color: #eee; }}
.card {{ background: #16213e; padding: 15px; margin: 10px 0; border-radius: 8px; }}
h1 {{ color: #e94560; }}
.stat {{ display: inline-block; margin: 10px 20px; }}
.stat-value {{ font-size: 24px; font-weight: bold; color: #0f3460; }}
.green {{ color: #4ecca3; }}
.red {{ color: #e94560; }}
</style>
</head>
<body>
<h1>Zeus Trading Bot Dashboard</h1>
<div class="card">
<div class="stat"><span class="stat-value {'green' if bot_status['running'] else 'red'}">{'Running' if bot_status['running'] else 'Stopped'}</span><br>Status</div>
<div class="stat"><span class="stat-value">{bot_status['mode']}</span><br>Mode</div>
<div class="stat"><span class="stat-value">${bot_status['balance']:.2f}</span><br>Balance</div>
<div class="stat"><span class="stat-value">{bot_status['active_trades']}</span><br>Active Trades</div>
<div class="stat"><span class="stat-value">{bot_status['holdings_count']}</span><br>Holdings</div>
</div>
<div class="card">
<h3>API Endpoints</h3>
<p><a href="/status" style="color:#4ecca3">/status</a> - Bot status JSON</p>
<p><a href="/api/trades" style="color:#4ecca3">/api/trades</a> - Active trades</p>
<p><a href="/api/holdings" style="color:#4ecca3">/api/holdings</a> - Current holdings</p>
<p><a href="/api/candidates" style="color:#4ecca3">/api/candidates</a> - Trading candidates</p>
<p><a href="/api/logs" style="color:#4ecca3">/api/logs</a> - Recent logs</p>
</div>
<p style="color:#888">Last updated: {format_la_time()}</p>
</body></html>
"""
@app.route("/api/analyze/<symbol>")
def api_analyze_coin(symbol):
if not bot_instance:
return jsonify(
{"error": "Bot not running - please start the bot first"}), 503
symbol = symbol.upper().strip()
if not symbol.endswith("USD"):
symbol = f"{symbol}USD"
async def find_valid_pair(exchange, base_symbol):
markets = await exchange.fetch_markets()
if not markets:
return base_symbol
base = base_symbol.replace("USD", "")
possible_names = [
base_symbol, f"X{base}ZUSD", f"X{base}USD", f"{base}ZUSD"
]
for name in possible_names:
if name in markets:
return name
for pair_name in markets.keys():
if pair_name.upper().endswith("USD"):
pair_base = pair_name.replace("USD",
"").replace("ZUSD",
"").lstrip("X")
if pair_base.upper() == base.upper():
return pair_name
return base_symbol
async def run_analysis():
nonlocal symbol
try:
exchange = bot_instance.exchange
prebreakout = bot_instance.prebreakout
math_kernel = prebreakout.math
valid_symbol = await find_valid_pair(exchange, symbol)
symbol = valid_symbol
timeframes = {"5m": 5, "15m": 15, "1h": 60, "4h": 240, "1d": 1440}
all_analysis = {}
for tf_name, tf_minutes in timeframes.items():
try:
ohlcv = await exchange.fetch_ohlcv(symbol,
tf_name,
limit=500)
if not ohlcv or len(ohlcv) < 50:
continue
high = [c.high for c in ohlcv]
low = [c.low for c in ohlcv]
close = [c.close for c in ohlcv]
volume = [c.volume for c in ohlcv]
analysis = await prebreakout.analyze(
symbol, high, low, close, volume)
rsi = math_kernel.rsi(close)
macd_line, signal_line, macd_hist = math_kernel.macd(close)
bb_upper, bb_mid, bb_lower = math_kernel.bollinger_bands(
close)
atr = math_kernel.atr(high, low, close)
stoch_k, stoch_d = math_kernel.stochastic_rsi(close)
ema9 = math_kernel.ema(close, 9)
ema20 = math_kernel.ema(close, 20)
ema50 = math_kernel.ema(close, 50)
sma200 = math_kernel.sma(close, 200)
adx_val, plus_di, minus_di = math_kernel.adx(
high, low, close)
analysis["technical_indicators"] = {
"rsi":
round(rsi, 2),
"macd":
round(macd_line, 8),
"macd_signal":
round(signal_line, 8),
"macd_histogram":
round(macd_hist, 8),
"bb_upper":
round(bb_upper, 8),
"bb_middle":
round(bb_mid, 8),
"bb_lower":
round(bb_lower, 8),
"bb_position":
round((close[-1] - bb_lower) / (bb_upper - bb_lower) *
100, 2) if (bb_upper - bb_lower) > 0 else 50,
"atr":
round(atr, 8),
"atr_pct":
round(atr / close[-1] *
100, 2) if close[-1] > 0 else 0,
"stoch_k":
round(stoch_k, 2),
"stoch_d":
round(stoch_d, 2),
"ema_9":
round(ema9, 8),
"ema_20":
round(ema20, 8),
"ema_50":
round(ema50, 8),
"sma_200":
round(sma200, 8),
"adx":
round(adx_val, 2),
"plus_di":
round(plus_di, 2),
"minus_di":
round(minus_di, 2)
}
price_change_24h = (
(close[-1] - close[-min(24, len(close))]) /
close[-min(24, len(close))]) * 100 if len(
close) > 24 else 0
high_24h = max(high[-min(24, len(high)):])
low_24h = min(low[-min(24, len(low)):])
avg_volume = sum(volume[-20:]) / 20 if len(
volume) >= 20 else sum(volume) / len(volume)
current_volume = volume[-1] if volume else 0
analysis["market_data"] = {
"current_price":
round(close[-1], 8),
"price_change_24h":
round(price_change_24h, 2),
"high_24h":
round(high_24h, 8),
"low_24h":
round(low_24h, 8),
"current_volume":
round(current_volume, 2),
"avg_volume":
round(avg_volume, 2),
"volume_ratio":
round(current_volume /
avg_volume, 2) if avg_volume > 0 else 0
}
all_analysis[tf_name] = analysis
except Exception as e:
all_analysis[tf_name] = {"error": str(e)}
if not all_analysis:
return {"error": f"Could not fetch data for {symbol}"}
valid_analyses = {
k: v
for k, v in all_analysis.items()
if "error" not in v and v.get("current_price", 0) > 0
}
if not valid_analyses:
return {"error": f"Failed to fetch data for {symbol}"}
primary_tf = valid_analyses.get("15m") or valid_analyses.get(
"1h") or list(valid_analyses.values())[0]
return {
"symbol": symbol,
"timestamp": format_la_time(),
"primary_analysis": primary_tf,
"timeframes": valid_analyses,
"success": True
}
except Exception as e:
return {"error": str(e)}
try:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
result = loop.run_until_complete(run_analysis())
loop.close()
return jsonify(result), 200
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route("/api/pairs")
def api_pairs():
if not bot_instance:
return jsonify({"pairs": []}), 200
try:
pairs = bot_instance._pairs_cache[:100] if bot_instance._pairs_cache else []
return jsonify({"pairs": pairs}), 200
except Exception:
return jsonify({"pairs": []}), 200
def run_trading_bot():
global bot_instance
async def start_bot():
global bot_instance
from src.zeus.core.bot import ZeusBot
print("=" * 60)
print(" ZEUS AUTONOMOUS TRADING BOT")
print(" Raspberry Pi 5 Edition")
print(f" Started: {format_la_time()}")
print("=" * 60)
kraken_key = os.environ.get("KRAKEN_API_KEY", "")
kraken_secret = os.environ.get("KRAKEN_API_SECRET", "")
telegram_token = os.environ.get("TELEGRAM_BOT_TOKEN", "")
telegram_chat_id = os.environ.get("TELEGRAM_CHAT_ID", "")
mode = os.environ.get("TRADING_MODE", "PAPER")
if not kraken_key or not kraken_secret:
print("\n[WARNING] Kraken API credentials not set.")
print("The bot will run in READ-ONLY mode (market scanning only).")
print(f"\n[MODE] Running in {mode} mode")
with bot_lock:
bot_status["mode"] = mode
bot_status["running"] = True
bot_status["uptime_start"] = format_la_time()
bot_status["error"] = None
try:
bot_instance = ZeusBot(kraken_key=kraken_key,
kraken_secret=kraken_secret,
telegram_token=telegram_token,
telegram_chat_id=telegram_chat_id,
mode=mode)
await bot_instance.run_forever()
except Exception as e:
with bot_lock:
bot_status["error"] = str(e)
bot_status["running"] = False
print(f"Bot error: {e}")
import traceback
traceback.print_exc()
asyncio.run(start_bot())
def run_server():
port = int(os.environ.get("PORT", 5000))
host = "0.0.0.0"
print(f"[SERVER] Starting server on {host}:{port}")
try:
from gunicorn.app.base import BaseApplication
class GunicornApp(BaseApplication):
def __init__(self, application, options=None):
self.options = options or {}
self.application = application
super().__init__()
def load_config(self):
for key, value in self.options.items():
if key in self.cfg.settings and value is not None:
self.cfg.set(key.lower(), value)
def load(self):
return self.application
options = {
'bind': f'{host}:{port}',
'workers': 1,
'threads': 4,
'timeout': 120,
'accesslog': '-',
'errorlog': '-',
'loglevel': 'info'
}
print("[SERVER] Using Gunicorn production server")
GunicornApp(app, options).run()
except ImportError:
print(
"[SERVER] Gunicorn not available, using Flask development server")
print(
"[SERVER] Install gunicorn for production use: pip install gunicorn"
)
app.run(host=host, port=port, threaded=True, debug=False)
if __name__ == "__main__":
# Create required directories
Path("data").mkdir(exist_ok=True)
Path("logs").mkdir(exist_ok=True)
print("[ZEUS] Starting Zeus Trading Bot...")
print(f"[ZEUS] Project root: {PROJECT_ROOT}")
# Start trading bot in background thread
bot_thread = threading.Thread(target=run_trading_bot, daemon=True)
bot_thread.start()
print("[BOT] Trading bot started in background thread")
# Start web server
run_server()