Arilnilhaq12

Untitled

Jun 16th, 2025
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. import MetaTrader5 as mt5
  2. import pandas as pd
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. from datetime import datetime
  6.  
  7. # === Inisialisasi MT5 ===
  8. if not mt5.initialize():
  9. print("Gagal koneksi ke MT5:", mt5.last_error())
  10. quit()
  11.  
  12. symbol = "BTUSDm"
  13. timeframe = mt5.TIMEFRAME_M5
  14. bars = 1000 # banyak candle
  15.  
  16. # === Ambil data OHLC dari MT5 ===
  17. rates = mt5.copy_rates_from_pos(symbol, timeframe, 0, bars)
  18. if rates is None:
  19. print("Gagal ambil data:", mt5.last_error())
  20. mt5.shutdown()
  21. quit()
  22.  
  23. # === Shutdown MT5 setelah ambil data ===
  24. mt5.shutdown()
  25.  
  26. # === Konversi ke DataFrame ===
  27. df = pd.DataFrame(rates)
  28. df['time'] = pd.to_datetime(df['time'], unit='s')
  29. df.set_index('time', inplace=True)
  30. df.columns = [col.lower() for col in df.columns]
  31.  
  32. # === Deteksi Swing High / Low ===
  33. def is_swing_high(df, i):
  34. return df['high'][i] > df['high'][i - 1] and df['high'][i] > df['high'][i + 1]
  35.  
  36. def is_swing_low(df, i):
  37. return df['low'][i] < df['low'][i - 1] and df['low'][i] < df['low'][i + 1]
  38.  
  39. # === Pola Candlestick ===
  40. def is_bullish_engulfing(df, i):
  41. return (df['close'][i] > df['open'][i] and
  42. df['open'][i] < df['close'][i - 1] and
  43. df['close'][i] > df['open'][i - 1] and
  44. df['close'][i - 1] < df['open'][i - 1])
  45.  
  46. def is_bearish_engulfing(df, i):
  47. return (df['close'][i] < df['open'][i] and
  48. df['open'][i] > df['close'][i - 1] and
  49. df['close'][i] < df['open'][i - 1] and
  50. df['close'][i - 1] > df['open'][i - 1])
  51.  
  52. def is_hammer(df, i):
  53. body = abs(df['close'][i] - df['open'][i])
  54. lower = min(df['open'][i], df['close'][i]) - df['low'][i]
  55. upper = df['high'][i] - max(df['open'][i], df['close'][i])
  56. return lower > 2 * body and upper < body
  57.  
  58. def is_shooting_star(df, i):
  59. body = abs(df['close'][i] - df['open'][i])
  60. upper = df['high'][i] - max(df['open'][i], df['close'][i])
  61. lower = min(df['open'][i], df['close'][i]) - df['low'][i]
  62. return upper > 2 * body and lower < body
  63.  
  64. # === Deteksi Sinyal Entry ===
  65. df['signal'] = np.nan
  66. for i in range(2, len(df) - 2):
  67. bullish = is_bullish_engulfing(df, i) or is_hammer(df, i)
  68. bearish = is_bearish_engulfing(df, i) or is_shooting_star(df, i)
  69.  
  70. if is_swing_low(df, i) and bullish:
  71. df.loc[df.index[i], 'signal'] = 'BUY'
  72. elif is_swing_high(df, i) and bearish:
  73. df.loc[df.index[i], 'signal'] = 'SELL'
  74.  
  75. # === Plot hasil sinyal ===
  76. plt.figure(figsize=(16, 6))
  77. plt.plot(df.index, df['close'], label='Close Price', color='black', alpha=0.6)
  78.  
  79. buy = df[df['signal'] == 'BUY']
  80. sell = df[df['signal'] == 'SELL']
  81.  
  82. plt.scatter(buy.index, buy['close'], marker='^', color='green', label='Buy Signal', zorder=5)
  83. plt.scatter(sell.index, sell['close'], marker='v', color='red', label='Sell Signal', zorder=5)
  84.  
  85. plt.title(f"Signal Entry {symbol} M5")
  86. plt.xlabel("Time")
  87. plt.ylabel("Price")
  88. plt.grid(True)
  89. plt.legend()
  90. plt.tight_layout()
  91. plt.show()
  92.  
  93. # === Print sinyal terakhir ===
  94. print("\nSinyal Terakhir:")
  95. print(df[df['signal'].notna()][['close', 'signal']].tail())
  96.  
Add Comment
Please, Sign In to add comment