Arilnilhaq12

Untitled

Jul 1st, 2025 (edited)
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. import MetaTrader5 as mt5
  2. import pandas as pd
  3. import mplfinance as mpf
  4. import matplotlib.pyplot as plt
  5.  
  6. # === Inisialisasi koneksi MT5 (tanpa login manual) ===
  7. if not mt5.initialize():
  8. print("Gagal konek ke MT5:", mt5.last_error())
  9. quit()
  10.  
  11. symbol = "EURUSD"
  12.  
  13. # === Ambil data H1 sebanyak 50 candle ===
  14. h1_data = pd.DataFrame(mt5.copy_rates_from_pos(symbol, mt5.TIMEFRAME_H1, 0, 50))
  15. h1_data['time'] = pd.to_datetime(h1_data['time'], unit='s')
  16. h1_data.set_index('time', inplace=True)
  17.  
  18. # === Fungsi deteksi swing high/low lookback 5 (tengah jadi puncak/ dasar) ===
  19. def is_swing_high(df, i):
  20. return df['high'][i] > max(df['high'][i-2:i]) and df['high'][i] > max(df['high'][i+1:i+3])
  21.  
  22. def is_swing_low(df, i):
  23. return df['low'][i] < min(df['low'][i-2:i]) and df['low'][i] < min(df['low'][i+1:i+3])
  24.  
  25. # === Deteksi sinyal BUY/SELL ===
  26. sinyal = []
  27.  
  28. for i in range(2, len(h1_data)-3):
  29. prev = h1_data.iloc[i]
  30. curr = h1_data.iloc[i+1]
  31. waktu = h1_data.index[i+1]
  32.  
  33. # Bullish Engulfing + swing low
  34. if prev['close'] < prev['open'] and curr['close'] > curr['open'] and \
  35. curr['close'] > prev['open'] and curr['open'] < prev['close']:
  36. if is_swing_low(h1_data.reset_index(), i):
  37. sinyal.append((waktu, 'BUY', curr['close'], curr['low']))
  38.  
  39. # Bearish Engulfing + swing high
  40. elif prev['close'] > prev['open'] and curr['close'] < curr['open'] and \
  41. curr['close'] < prev['open'] and curr['open'] > prev['close']:
  42. if is_swing_high(h1_data.reset_index(), i):
  43. sinyal.append((waktu, 'SELL', curr['close'], curr['high']))
  44.  
  45. # === Buat plot tambahan: marker, garis SL/TP ===
  46. apds = []
  47.  
  48. for waktu, arah, entry, sl_level in sinyal:
  49. if arah == 'BUY':
  50. sl = sl_level
  51. tp = entry + (entry - sl) * 3
  52. color = 'green'
  53. marker = '^'
  54. else:
  55. sl = sl_level
  56. tp = entry - (sl - entry) * 3
  57. color = 'red'
  58. marker = 'v'
  59.  
  60. # Tambah marker
  61. apds.append(mpf.make_addplot(
  62. [entry if t == waktu else None for t in h1_data.index],
  63. type='scatter',
  64. marker=marker,
  65. markersize=150,
  66. color=color,
  67. panel=0
  68. ))
  69.  
  70. # Tambah garis SL & TP
  71. sl_series = [sl if t == waktu else None for t in h1_data.index]
  72. tp_series = [tp if t == waktu else None for t in h1_data.index]
  73.  
  74. apds.append(mpf.make_addplot(sl_series, color='gray', linestyle='--', width=1.5))
  75. apds.append(mpf.make_addplot(tp_series, color='blue', linestyle='-.', width=2.5))
  76.  
  77. # === Plot dengan label ===
  78. fig, axes = mpf.plot(
  79. h1_data,
  80. type='candle',
  81. style='yahoo',
  82. title=f"Sinyal {symbol} H1 - Engulfing + Swing + SL/TP",
  83. addplot=apds,
  84. volume=False,
  85. returnfig=True,
  86. figsize=(14, 7),
  87. tight_layout=True
  88. )
  89.  
  90. ax = axes[0]
  91.  
  92. # Tambahkan teks label BUY/SELL
  93. for waktu, arah, entry, sl_level in sinyal:
  94. y = entry
  95. label = arah
  96. color = 'green' if arah == 'BUY' else 'red'
  97. ax.annotate(
  98. label,
  99. xy=(waktu, y),
  100. xytext=(waktu, y + 0.001 if arah == 'BUY' else y - 0.001),
  101. fontsize=10,
  102. color=color,
  103. weight='bold',
  104. ha='center',
  105. arrowprops=dict(arrowstyle="->", color=color)
  106. )
  107.  
  108. plt.show()
  109.  
  110. # === Tutup koneksi MT5 ===
  111. mt5.shutdown()
  112.  
Advertisement
Add Comment
Please, Sign In to add comment