Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import MetaTrader5 as mt5
- import pandas as pd
- import numpy as np
- import matplotlib.pyplot as plt
- from datetime import datetime
- # === Inisialisasi MT5 ===
- if not mt5.initialize():
- print("Gagal koneksi ke MT5:", mt5.last_error())
- quit()
- symbol = "BTUSDm"
- timeframe = mt5.TIMEFRAME_M5
- bars = 1000 # banyak candle
- # === Ambil data OHLC dari MT5 ===
- rates = mt5.copy_rates_from_pos(symbol, timeframe, 0, bars)
- if rates is None:
- print("Gagal ambil data:", mt5.last_error())
- mt5.shutdown()
- quit()
- # === Shutdown MT5 setelah ambil data ===
- mt5.shutdown()
- # === Konversi ke DataFrame ===
- df = pd.DataFrame(rates)
- df['time'] = pd.to_datetime(df['time'], unit='s')
- df.set_index('time', inplace=True)
- df.columns = [col.lower() for col in df.columns]
- # === Deteksi Swing High / Low ===
- def is_swing_high(df, i):
- return df['high'][i] > df['high'][i - 1] and df['high'][i] > df['high'][i + 1]
- def is_swing_low(df, i):
- return df['low'][i] < df['low'][i - 1] and df['low'][i] < df['low'][i + 1]
- # === Pola Candlestick ===
- def is_bullish_engulfing(df, i):
- return (df['close'][i] > df['open'][i] and
- df['open'][i] < df['close'][i - 1] and
- df['close'][i] > df['open'][i - 1] and
- df['close'][i - 1] < df['open'][i - 1])
- def is_bearish_engulfing(df, i):
- return (df['close'][i] < df['open'][i] and
- df['open'][i] > df['close'][i - 1] and
- df['close'][i] < df['open'][i - 1] and
- df['close'][i - 1] > df['open'][i - 1])
- def is_hammer(df, i):
- body = abs(df['close'][i] - df['open'][i])
- lower = min(df['open'][i], df['close'][i]) - df['low'][i]
- upper = df['high'][i] - max(df['open'][i], df['close'][i])
- return lower > 2 * body and upper < body
- def is_shooting_star(df, i):
- body = abs(df['close'][i] - df['open'][i])
- upper = df['high'][i] - max(df['open'][i], df['close'][i])
- lower = min(df['open'][i], df['close'][i]) - df['low'][i]
- return upper > 2 * body and lower < body
- # === Deteksi Sinyal Entry ===
- df['signal'] = np.nan
- for i in range(2, len(df) - 2):
- bullish = is_bullish_engulfing(df, i) or is_hammer(df, i)
- bearish = is_bearish_engulfing(df, i) or is_shooting_star(df, i)
- if is_swing_low(df, i) and bullish:
- df.loc[df.index[i], 'signal'] = 'BUY'
- elif is_swing_high(df, i) and bearish:
- df.loc[df.index[i], 'signal'] = 'SELL'
- # === Plot hasil sinyal ===
- plt.figure(figsize=(16, 6))
- plt.plot(df.index, df['close'], label='Close Price', color='black', alpha=0.6)
- buy = df[df['signal'] == 'BUY']
- sell = df[df['signal'] == 'SELL']
- plt.scatter(buy.index, buy['close'], marker='^', color='green', label='Buy Signal', zorder=5)
- plt.scatter(sell.index, sell['close'], marker='v', color='red', label='Sell Signal', zorder=5)
- plt.title(f"Signal Entry {symbol} M5")
- plt.xlabel("Time")
- plt.ylabel("Price")
- plt.grid(True)
- plt.legend()
- plt.tight_layout()
- plt.show()
- # === Print sinyal terakhir ===
- print("\nSinyal Terakhir:")
- print(df[df['signal'].notna()][['close', 'signal']].tail())
Add Comment
Please, Sign In to add comment