Arilnilhaq12

Untitled

Jun 16th, 2025
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. import MetaTrader5 as mt5
  2. import pandas as pd
  3. import numpy as np
  4.  
  5. # --- Init dan ambil data ---
  6. mt5.initialize()
  7. symbol = "BTCUSDm"
  8. rates = mt5.copy_rates_from_pos(symbol, mt5.TIMEFRAME_H1, 0, 100)
  9. df = pd.DataFrame(rates)
  10. df['time'] = pd.to_datetime(df['time'], unit='s')
  11.  
  12. # --- Hitung indikator tambahan ---
  13. def calc_rsi(data, period=14):
  14.     delta = data['close'].diff()
  15.     gain = delta.where(delta > 0, 0)
  16.     loss = -delta.where(delta < 0, 0)
  17.     avg_gain = gain.rolling(window=period).mean()
  18.     avg_loss = loss.rolling(window=period).mean()
  19.     rs = avg_gain / avg_loss
  20.     return 100 - (100 / (1 + rs))
  21.  
  22. def calc_bollinger(data, period=20, dev=2):
  23.     data['ma'] = data['close'].rolling(window=period).mean()
  24.     data['std'] = data['close'].rolling(window=period).std()
  25.     data['upper'] = data['ma'] + dev * data['std']
  26.     data['lower'] = data['ma'] - dev * data['std']
  27.     return data
  28.  
  29. def calc_atr(data, period=14):
  30.     high_low = data['high'] - data['low']
  31.     high_close = np.abs(data['high'] - data['close'].shift())
  32.     low_close = np.abs(data['low'] - data['close'].shift())
  33.     tr = pd.concat([high_low, high_close, low_close], axis=1).max(axis=1)
  34.     return tr.rolling(window=period).mean()
  35.  
  36. df['rsi'] = calc_rsi(df)
  37. df = calc_bollinger(df)
  38. df['atr'] = calc_atr(df)
  39.  
  40. # --- Pattern dan konfirmasi ---
  41. def is_bullish_engulfing(df):
  42.     c = df.iloc[-1]
  43.     p = df.iloc[-2]
  44.     return (p['close'] < p['open']) and (c['close'] > c['open']) and \
  45.            (c['open'] < p['close']) and (c['close'] > p['open'])
  46.  
  47. def is_bearish_engulfing(df):
  48.     c = df.iloc[-1]
  49.     p = df.iloc[-2]
  50.     return (p['close'] > p['open']) and (c['close'] < c['open']) and \
  51.            (c['open'] > p['close']) and (c['close'] < p['open'])
  52.  
  53. def confirm_swing_high(df):
  54.     c = df.iloc[-1]
  55.     return is_bearish_engulfing(df) and \
  56.            (c['rsi'] > 70) and \
  57.            (c['close'] > c['upper'])
  58.  
  59. def confirm_swing_low(df):
  60.     c = df.iloc[-1]
  61.     return is_bullish_engulfing(df) and \
  62.            (c['rsi'] < 30) and \
  63.            (c['close'] < c['lower'])
  64.  
  65. # --- Deteksi ---
  66. if confirm_swing_high(df):
  67.     print("📉 Swing High Dikonfirmasi")
  68. elif confirm_swing_low(df):
  69.     print("📈 Swing Low Dikonfirmasi")
  70. else:
  71.     print("❌ Tidak ada swing valid")
  72.  
Advertisement
Add Comment
Please, Sign In to add comment