Arilnilhaq12

Untitled

Jun 22nd, 2025
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. import MetaTrader5 as mt5
  2. import pandas as pd
  3. import time
  4. import datetime
  5.  
  6. # === Inisialisasi koneksi ke MT5 ===
  7. if not mt5.initialize():
  8. print("Gagal konek ke MT5")
  9. mt5.shutdown()
  10. quit()
  11.  
  12. # === Konfigurasi ===
  13. symbol = "BTCUSDm" # Ganti sesuai simbol di broker kamu (misal EURUSD, XAUUSD)
  14. timeframe = mt5.TIMEFRAME_M5
  15. window = 10 # Jumlah candle untuk analisis
  16. bar_limit = 100 # Total candle diambil
  17. refresh_seconds = 60 # Delay loop (deteksi setiap 1 menit, bisa 300 detik = 5 menit)
  18.  
  19. # === Fungsi Deteksi Pola dari 10 Candle Terakhir ===
  20. def detect_trend_strength(df, i, window=10):
  21. if i < window:
  22. return None
  23.  
  24. highs = df['high'].iloc[i-window:i]
  25. lows = df['low'].iloc[i-window:i]
  26. closes = df['close'].iloc[i-window:i]
  27.  
  28. # 1. Struktur Naik
  29. higher_highs = all(highs[j] < highs[j+1] for j in range(len(highs)-1))
  30. higher_lows = all(lows[j] < lows[j+1] for j in range(len(lows)-1))
  31.  
  32. # 2. Struktur Turun
  33. lower_highs = all(highs[j] > highs[j+1] for j in range(len(highs)-1))
  34. lower_lows = all(lows[j] > lows[j+1] for j in range(len(lows)-1))
  35.  
  36. # 3. Break Level Penting
  37. last_close = closes.iloc[-1]
  38. last_high = highs.max()
  39. last_low = lows.min()
  40.  
  41. if higher_highs and higher_lows and last_close > last_high:
  42. return f"BUY kuat ➜ Naik panjang dari {last_close:.2f}"
  43. elif lower_highs and lower_lows and last_close < last_low:
  44. return f"SELL kuat ➜ Turun panjang dari {last_close:.2f}"
  45.  
  46. return None
  47.  
  48. # === Loop Real-Time / Backtest ===
  49. while True:
  50. rates = mt5.copy_rates_from_pos(symbol, timeframe, 0, bar_limit)
  51. if rates is None or len(rates) == 0:
  52. print("Gagal ambil data harga")
  53. time.sleep(5)
  54. continue
  55.  
  56. df = pd.DataFrame(rates)
  57. df['time'] = pd.to_datetime(df['time'], unit='s')
  58.  
  59. # Analisis hanya bar terakhir
  60. signal = detect_trend_strength(df, len(df), window=window)
  61. current_time = df['time'].iloc[-1]
  62.  
  63. if signal:
  64. print(f"[{current_time}] {signal}")
  65. else:
  66. print(f"[{current_time}] Tidak ada sinyal kuat")
  67.  
  68. time.sleep(refresh_seconds)
  69.  
Advertisement
Add Comment
Please, Sign In to add comment