-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.py
More file actions
105 lines (81 loc) · 3.56 KB
/
data.py
File metadata and controls
105 lines (81 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import requests
import pandas as pd
import numpy as np
from datetime import datetime
import time
from ta.momentum import RSIIndicator
from ta.trend import MACD, EMAIndicator
from ta.volatility import BollingerBands
# --- KONFIGURASI ---
SYMBOL = 'BTCUSDT'
INTERVAL = '15m'
START_DATE = '01 Jan 2023' # Kita ambil data dari awal 2023 biar model pintar
FILENAME = 'bitcoin_15m_lengkap.csv'
def get_binance_data(symbol, interval, start_str):
print(f"🚀 Mulai menambang data {symbol} timeframe {interval} dari {start_str}...")
api_url = "https://api.binance.com/api/v3/klines"
start_ts = int(datetime.strptime(start_str, '%d %b %Y').timestamp() * 1000)
end_ts = int(time.time() * 1000)
data = []
while start_ts < end_ts:
params = {
'symbol': symbol,
'interval': interval,
'limit': 1000,
'startTime': start_ts
}
try:
response = requests.get(api_url, params=params)
klines = response.json()
if not klines:
break
data.extend(klines)
start_ts = klines[-1][0] + 1
# Progress bar manual biar gak bosen nunggu
last_date = datetime.fromtimestamp(klines[-1][0]/1000).strftime('%Y-%m-%d')
print(f" Sedot data sampai: {last_date} | Total baris: {len(data)}")
time.sleep(0.5) # Biar gak kena banned Binance (sopan dikit)
except Exception as e:
print(f"❌ Error: {e}")
break
# Convert ke DataFrame rapi
df = pd.DataFrame(data, columns=[
'timestamp', 'open', 'high', 'low', 'close', 'volume',
'close_time', 'quote_asset_volume', 'trades',
'taker_buy_base', 'taker_buy_quote', 'ignore'
])
# Ubah tipe data string jadi angka (float)
numeric_cols = ['open', 'high', 'low', 'close', 'volume', 'trades']
df[numeric_cols] = df[numeric_cols].astype(float)
# Rapikan waktu
df['datetime'] = pd.to_datetime(df['timestamp'], unit='ms')
df = df[['datetime', 'open', 'high', 'low', 'close', 'volume', 'trades']]
return df
def add_indicators(df):
print("🛠️ Sedang meracik indikator teknikal...")
# 1. RSI (Momentum)
df['RSI'] = RSIIndicator(close=df['close'], window=14).rsi()
# 2. MACD (Trend)
macd = MACD(close=df['close'])
df['MACD'] = macd.macd()
df['MACD_signal'] = macd.macd_signal()
# 3. EMA (Trend Halus)
df['EMA_50'] = EMAIndicator(close=df['close'], window=50).ema_indicator()
df['EMA_200'] = EMAIndicator(close=df['close'], window=200).ema_indicator()
# 4. Bollinger Bands (Volatilitas)
bb = BollingerBands(close=df['close'], window=20, window_dev=2)
df['BB_Upper'] = bb.bollinger_hband()
df['BB_Lower'] = bb.bollinger_lband()
# 5. Target Label (Yang mau ditebak)
# 1 jika Close candle BERIKUTNYA lebih tinggi dari Close sekarang
df['Target_Next_Close'] = df['close'].shift(-1)
df['Label'] = (df['Target_Next_Close'] > df['close']).astype(int)
# Hapus baris yang ada NaN (kosong karena perhitungan indikator di awal data)
df.dropna(inplace=True)
return df
# --- EKSEKUSI ---
df = get_binance_data(SYMBOL, INTERVAL, START_DATE)
df = add_indicators(df)
print(f"✅ Selesai! Data bersih tersimpan: {len(df)} baris")
df.to_csv(FILENAME, index=False)
print(f"📁 File disimpan sebagai: {FILENAME}")