Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import MetaTrader5 as mt5
- import pandas as pd
- import time
- import datetime
- # === Inisialisasi koneksi ke MT5 ===
- if not mt5.initialize():
- print("Gagal konek ke MT5")
- mt5.shutdown()
- quit()
- # === Konfigurasi ===
- symbol = "BTCUSDm" # Ganti sesuai simbol di broker kamu (misal EURUSD, XAUUSD)
- timeframe = mt5.TIMEFRAME_M5
- window = 10 # Jumlah candle untuk analisis
- bar_limit = 100 # Total candle diambil
- refresh_seconds = 60 # Delay loop (deteksi setiap 1 menit, bisa 300 detik = 5 menit)
- # === Fungsi Deteksi Pola dari 10 Candle Terakhir ===
- def detect_trend_strength(df, i, window=10):
- if i < window:
- return None
- highs = df['high'].iloc[i-window:i]
- lows = df['low'].iloc[i-window:i]
- closes = df['close'].iloc[i-window:i]
- # 1. Struktur Naik
- higher_highs = all(highs[j] < highs[j+1] for j in range(len(highs)-1))
- higher_lows = all(lows[j] < lows[j+1] for j in range(len(lows)-1))
- # 2. Struktur Turun
- lower_highs = all(highs[j] > highs[j+1] for j in range(len(highs)-1))
- lower_lows = all(lows[j] > lows[j+1] for j in range(len(lows)-1))
- # 3. Break Level Penting
- last_close = closes.iloc[-1]
- last_high = highs.max()
- last_low = lows.min()
- if higher_highs and higher_lows and last_close > last_high:
- return f"BUY kuat ➜ Naik panjang dari {last_close:.2f}"
- elif lower_highs and lower_lows and last_close < last_low:
- return f"SELL kuat ➜ Turun panjang dari {last_close:.2f}"
- return None
- # === Loop Real-Time / Backtest ===
- while True:
- rates = mt5.copy_rates_from_pos(symbol, timeframe, 0, bar_limit)
- if rates is None or len(rates) == 0:
- print("Gagal ambil data harga")
- time.sleep(5)
- continue
- df = pd.DataFrame(rates)
- df['time'] = pd.to_datetime(df['time'], unit='s')
- # Analisis hanya bar terakhir
- signal = detect_trend_strength(df, len(df), window=window)
- current_time = df['time'].iloc[-1]
- if signal:
- print(f"[{current_time}] {signal}")
- else:
- print(f"[{current_time}] Tidak ada sinyal kuat")
- time.sleep(refresh_seconds)
Advertisement
Add Comment
Please, Sign In to add comment