Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import MetaTrader5 as mt5
- import pandas as pd
- import mplfinance as mpf
- import matplotlib.pyplot as plt
- # === Inisialisasi koneksi MT5 (tanpa login manual) ===
- if not mt5.initialize():
- print("Gagal konek ke MT5:", mt5.last_error())
- quit()
- symbol = "EURUSD"
- # === Ambil data H1 sebanyak 50 candle ===
- h1_data = pd.DataFrame(mt5.copy_rates_from_pos(symbol, mt5.TIMEFRAME_H1, 0, 50))
- h1_data['time'] = pd.to_datetime(h1_data['time'], unit='s')
- h1_data.set_index('time', inplace=True)
- # === Fungsi deteksi swing high/low lookback 5 (tengah jadi puncak/ dasar) ===
- def is_swing_high(df, i):
- return df['high'][i] > max(df['high'][i-2:i]) and df['high'][i] > max(df['high'][i+1:i+3])
- def is_swing_low(df, i):
- return df['low'][i] < min(df['low'][i-2:i]) and df['low'][i] < min(df['low'][i+1:i+3])
- # === Deteksi sinyal BUY/SELL ===
- sinyal = []
- for i in range(2, len(h1_data)-3):
- prev = h1_data.iloc[i]
- curr = h1_data.iloc[i+1]
- waktu = h1_data.index[i+1]
- # Bullish Engulfing + swing low
- if prev['close'] < prev['open'] and curr['close'] > curr['open'] and \
- curr['close'] > prev['open'] and curr['open'] < prev['close']:
- if is_swing_low(h1_data.reset_index(), i):
- sinyal.append((waktu, 'BUY', curr['close'], curr['low']))
- # Bearish Engulfing + swing high
- elif prev['close'] > prev['open'] and curr['close'] < curr['open'] and \
- curr['close'] < prev['open'] and curr['open'] > prev['close']:
- if is_swing_high(h1_data.reset_index(), i):
- sinyal.append((waktu, 'SELL', curr['close'], curr['high']))
- # === Buat plot tambahan: marker, garis SL/TP ===
- apds = []
- for waktu, arah, entry, sl_level in sinyal:
- if arah == 'BUY':
- sl = sl_level
- tp = entry + (entry - sl) * 3
- color = 'green'
- marker = '^'
- else:
- sl = sl_level
- tp = entry - (sl - entry) * 3
- color = 'red'
- marker = 'v'
- # Tambah marker
- apds.append(mpf.make_addplot(
- [entry if t == waktu else None for t in h1_data.index],
- type='scatter',
- marker=marker,
- markersize=150,
- color=color,
- panel=0
- ))
- # Tambah garis SL & TP
- sl_series = [sl if t == waktu else None for t in h1_data.index]
- tp_series = [tp if t == waktu else None for t in h1_data.index]
- apds.append(mpf.make_addplot(sl_series, color='gray', linestyle='--', width=1.5))
- apds.append(mpf.make_addplot(tp_series, color='blue', linestyle='-.', width=2.5))
- # === Plot dengan label ===
- fig, axes = mpf.plot(
- h1_data,
- type='candle',
- style='yahoo',
- title=f"Sinyal {symbol} H1 - Engulfing + Swing + SL/TP",
- addplot=apds,
- volume=False,
- returnfig=True,
- figsize=(14, 7),
- tight_layout=True
- )
- ax = axes[0]
- # Tambahkan teks label BUY/SELL
- for waktu, arah, entry, sl_level in sinyal:
- y = entry
- label = arah
- color = 'green' if arah == 'BUY' else 'red'
- ax.annotate(
- label,
- xy=(waktu, y),
- xytext=(waktu, y + 0.001 if arah == 'BUY' else y - 0.001),
- fontsize=10,
- color=color,
- weight='bold',
- ha='center',
- arrowprops=dict(arrowstyle="->", color=color)
- )
- plt.show()
- # === Tutup koneksi MT5 ===
- mt5.shutdown()
Advertisement
Add Comment
Please, Sign In to add comment