Cuong12354321 commited on
Commit
2a21048
·
verified ·
1 Parent(s): 8f0d43d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -15
app.py CHANGED
@@ -18,22 +18,29 @@ WATCHLIST = {
18
  'BNB/USDT': 'BNB-USD'
19
  }
20
  TIMEFRAME = '1h'
21
- CHECK_INTERVAL = 3600 # Chạy thực tế 1 tiếng/lần để Yahoo tự động mở khóa IP cho BTC
22
  # =========================================================================
23
 
24
- global_status_log = ["🤖 Hệ thống Wyckoff VSA & Smart Fibo v4 đang hoạt động..."]
25
 
26
- def send_telegram_instant(msg):
 
27
  url = f"https://api.telegram.org/bot{TELEGRAM_TOKEN}/sendMessage"
28
  payload = {"chat_id": TELEGRAM_CHAT_ID, "text": msg, "parse_mode": "Markdown"}
29
  try:
30
- requests.post(url, json=payload, timeout=(2, 0.1))
31
  except:
32
  pass
33
 
 
 
 
 
34
  def core_bot_loop():
35
  global global_status_log
36
- send_telegram_instant("⚡ *Bot Wyckoff & Fibo V4 đã vá lỗi dữ liệu 'vol_ma' thành công! Đang trực tuyến...*")
 
 
37
 
38
  while True:
39
  current_time = datetime.now().strftime('%d/%m/%Y %H:%M:%S')
@@ -41,7 +48,6 @@ def core_bot_loop():
41
 
42
  for sàn_symbol, yahoo_symbol in WATCHLIST.items():
43
  try:
44
- # Nới rộng thời gian lấy dữ liệu lên 7 ngày để đảm bảo luôn đủ nến tính toán
45
  df = yf.download(tickers=yahoo_symbol, period="7d", interval="1h", progress=False)
46
 
47
  if df.empty or len(df) < 25:
@@ -52,20 +58,16 @@ def core_bot_loop():
52
  df.columns = df.columns.droplevel(1)
53
  df.columns = [str(col).lower() for col in df.columns]
54
 
55
- # Tính EMA xu hướng
56
  df['ema_20'] = ta.ema(df['close'], length=20)
57
 
58
- # Tính toán Fibonacci
59
  fibo_period = min(50, len(df))
60
  recent_df = df.iloc[-fibo_period:]
61
  highest_p = float(recent_df['high'].max())
62
  lowest_p = float(recent_df['low'].min())
63
  diff = highest_p - lowest_p
64
 
65
- # 🌟 VÁ LỖI WYCKOFF VOLUME: Thêm min_periods=1 để né lỗi thiếu nến ngày cuối tuần
66
  df['vol_ma'] = df['volume'].rolling(window=20, min_periods=1).mean()
67
-
68
- # Làm sạch hoàn toàn dữ liệu rỗng (nếu có) bằng cách điền giá trị gần nhất
69
  df = df.ffill().bfill()
70
 
71
  candle = df.iloc[-2]
@@ -82,7 +84,7 @@ def core_bot_loop():
82
  avg_spread = (df['high'] - df['low']).rolling(window=20, min_periods=1).mean().iloc[-2]
83
  is_wide_spread = spread > (avg_spread * 1.3)
84
 
85
- # Logic chuyển đổi cấu trúc tự động
86
  if close_p >= ema_val:
87
  trend_status = "TĂNG (Pullback Mua)"
88
  fibo_500 = lowest_p + (0.500 * diff)
@@ -128,7 +130,7 @@ def core_bot_loop():
128
  except Exception as e:
129
  cycle_log += f"• Lỗi xử lý mã {sàn_symbol}: {str(e)}\n"
130
 
131
- time.sleep(3.0) # Giãn cách an toàn né chặn anti-spam
132
 
133
  global_status_log.insert(0, cycle_log)
134
  if len(global_status_log) > 5: global_status_log.pop()
@@ -140,9 +142,10 @@ def read_logs_to_web():
140
  bot_thread = threading.Thread(target=core_bot_loop, daemon=True)
141
  bot_thread.start()
142
 
143
- with gr.Blocks(title="Wyckoff Fibo v4") as demo:
144
- gr.Markdown("# 🤖 Bot Định Lượng Đầu Tư Chiến Lược: Phiên Bản Vá Lỗi Dữ Liệu Cuối Tuần")
145
  output_text = gr.Textbox(label="Nhật ký hệ thống từ RAM", value=global_status_log[0], lines=15)
146
  gr.Timer(value=5, active=True).tick(fn=read_logs_to_web, outputs=output_text)
147
 
148
  demo.launch()
 
 
18
  'BNB/USDT': 'BNB-USD'
19
  }
20
  TIMEFRAME = '1h'
21
+ CHECK_INTERVAL = 3600 # Chạy thực tế ổn định 1 tiếng/lần
22
  # =========================================================================
23
 
24
+ global_status_log = ["🤖 Hệ thống Wyckoff VSA & Smart Fibo v5 đang hoạt động..."]
25
 
26
+ def async_telegram_worker(msg):
27
+ """Luồng phụ chuyên trách kết nối mạng - Đảm bảo tin nhắn đi xuyên tường lửa an toàn"""
28
  url = f"https://api.telegram.org/bot{TELEGRAM_TOKEN}/sendMessage"
29
  payload = {"chat_id": TELEGRAM_CHAT_ID, "text": msg, "parse_mode": "Markdown"}
30
  try:
31
+ requests.post(url, json=payload, timeout=10)
32
  except:
33
  pass
34
 
35
+ def send_telegram_instant(msg):
36
+ """Bọc lệnh gửi vào luồng phụ độc lập - Ép giải phóng luồng chính ngay lập tức"""
37
+ threading.Thread(target=async_telegram_worker, args=(msg,), daemon=True).start()
38
+
39
  def core_bot_loop():
40
  global global_status_log
41
+
42
+ # Kiểm tra cổng mạng ngay giây đầu tiên
43
+ send_telegram_instant("⚡ *Bot Wyckoff & Fibo V5 đã thông suốt đường truyền! Hệ thống chính thức trực tuyến vĩnh viễn.*")
44
 
45
  while True:
46
  current_time = datetime.now().strftime('%d/%m/%Y %H:%M:%S')
 
48
 
49
  for sàn_symbol, yahoo_symbol in WATCHLIST.items():
50
  try:
 
51
  df = yf.download(tickers=yahoo_symbol, period="7d", interval="1h", progress=False)
52
 
53
  if df.empty or len(df) < 25:
 
58
  df.columns = df.columns.droplevel(1)
59
  df.columns = [str(col).lower() for col in df.columns]
60
 
61
+ # Tính toán kỹ thuật
62
  df['ema_20'] = ta.ema(df['close'], length=20)
63
 
 
64
  fibo_period = min(50, len(df))
65
  recent_df = df.iloc[-fibo_period:]
66
  highest_p = float(recent_df['high'].max())
67
  lowest_p = float(recent_df['low'].min())
68
  diff = highest_p - lowest_p
69
 
 
70
  df['vol_ma'] = df['volume'].rolling(window=20, min_periods=1).mean()
 
 
71
  df = df.ffill().bfill()
72
 
73
  candle = df.iloc[-2]
 
84
  avg_spread = (df['high'] - df['low']).rolling(window=20, min_periods=1).mean().iloc[-2]
85
  is_wide_spread = spread > (avg_spread * 1.3)
86
 
87
+ # Điều hướng cấu trúc thông minh
88
  if close_p >= ema_val:
89
  trend_status = "TĂNG (Pullback Mua)"
90
  fibo_500 = lowest_p + (0.500 * diff)
 
130
  except Exception as e:
131
  cycle_log += f"• Lỗi xử lý mã {sàn_symbol}: {str(e)}\n"
132
 
133
+ time.sleep(3.0) # Giãn cách 3 giây chống spam
134
 
135
  global_status_log.insert(0, cycle_log)
136
  if len(global_status_log) > 5: global_status_log.pop()
 
142
  bot_thread = threading.Thread(target=core_bot_loop, daemon=True)
143
  bot_thread.start()
144
 
145
+ with gr.Blocks(title="Wyckoff Fibo v5") as demo:
146
+ gr.Markdown("# 🤖 Bot Định Lượng Đầu Tư Chiến Lược: Sửa Lỗi Đường Truyền Telegram")
147
  output_text = gr.Textbox(label="Nhật ký hệ thống từ RAM", value=global_status_log[0], lines=15)
148
  gr.Timer(value=5, active=True).tick(fn=read_logs_to_web, outputs=output_text)
149
 
150
  demo.launch()
151
+