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
- # --- Init dan ambil data ---
- mt5.initialize()
- symbol = "BTCUSDm"
- rates = mt5.copy_rates_from_pos(symbol, mt5.TIMEFRAME_H1, 0, 100)
- df = pd.DataFrame(rates)
- df['time'] = pd.to_datetime(df['time'], unit='s')
- # --- Hitung indikator tambahan ---
- def calc_rsi(data, period=14):
- delta = data['close'].diff()
- gain = delta.where(delta > 0, 0)
- loss = -delta.where(delta < 0, 0)
- avg_gain = gain.rolling(window=period).mean()
- avg_loss = loss.rolling(window=period).mean()
- rs = avg_gain / avg_loss
- return 100 - (100 / (1 + rs))
- def calc_bollinger(data, period=20, dev=2):
- data['ma'] = data['close'].rolling(window=period).mean()
- data['std'] = data['close'].rolling(window=period).std()
- data['upper'] = data['ma'] + dev * data['std']
- data['lower'] = data['ma'] - dev * data['std']
- return data
- def calc_atr(data, period=14):
- high_low = data['high'] - data['low']
- high_close = np.abs(data['high'] - data['close'].shift())
- low_close = np.abs(data['low'] - data['close'].shift())
- tr = pd.concat([high_low, high_close, low_close], axis=1).max(axis=1)
- return tr.rolling(window=period).mean()
- df['rsi'] = calc_rsi(df)
- df = calc_bollinger(df)
- df['atr'] = calc_atr(df)
- # --- Pattern dan konfirmasi ---
- def is_bullish_engulfing(df):
- c = df.iloc[-1]
- p = df.iloc[-2]
- return (p['close'] < p['open']) and (c['close'] > c['open']) and \
- (c['open'] < p['close']) and (c['close'] > p['open'])
- def is_bearish_engulfing(df):
- c = df.iloc[-1]
- p = df.iloc[-2]
- return (p['close'] > p['open']) and (c['close'] < c['open']) and \
- (c['open'] > p['close']) and (c['close'] < p['open'])
- def confirm_swing_high(df):
- c = df.iloc[-1]
- return is_bearish_engulfing(df) and \
- (c['rsi'] > 70) and \
- (c['close'] > c['upper'])
- def confirm_swing_low(df):
- c = df.iloc[-1]
- return is_bullish_engulfing(df) and \
- (c['rsi'] < 30) and \
- (c['close'] < c['lower'])
- # --- Deteksi ---
- if confirm_swing_high(df):
- print("📉 Swing High Dikonfirmasi")
- elif confirm_swing_low(df):
- print("📈 Swing Low Dikonfirmasi")
- else:
- print("❌ Tidak ada swing valid")
Advertisement
Add Comment
Please, Sign In to add comment