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| 4 |
+
from datetime import datetime, timedelta
|
| 5 |
+
from binance.client import Client
|
| 6 |
+
from sklearn.model_selection import train_test_split
|
| 7 |
+
from sklearn.ensemble import RandomForestClassifier
|
| 8 |
+
from sklearn.metrics import classification_report
|
| 9 |
+
import ta
|
| 10 |
+
|
| 11 |
+
# Connect to Binance
|
| 12 |
+
client = Client()
|
| 13 |
+
|
| 14 |
+
# Settings
|
| 15 |
+
DATA_FILE = "btc_data_4h_full.csv"
|
| 16 |
+
symbol = "BTCUSDT"
|
| 17 |
+
interval = Client.KLINE_INTERVAL_4HOUR
|
| 18 |
+
|
| 19 |
+
# Load or download data
|
| 20 |
+
if os.path.exists(DATA_FILE):
|
| 21 |
+
print("Loading existing data...")
|
| 22 |
+
df = pd.read_csv(DATA_FILE, index_col=0, parse_dates=True)
|
| 23 |
+
last_timestamp = df.index[-1]
|
| 24 |
+
start_time = last_timestamp + timedelta(hours=4)
|
| 25 |
+
start_str = start_time.strftime("%d %B %Y %H:%M:%S")
|
| 26 |
+
|
| 27 |
+
print(f"Downloading new data from {start_str}...")
|
| 28 |
+
new_klines = client.get_historical_klines(symbol, interval, start_str)
|
| 29 |
+
if new_klines:
|
| 30 |
+
new_df = pd.DataFrame(new_klines, columns=['timestamp', 'open', 'high', 'low', 'close', 'volume',
|
| 31 |
+
'close_time', 'quote_av', 'trades', 'tb_base_av', 'tb_quote_av', 'ignore'])
|
| 32 |
+
new_df = new_df[['timestamp', 'open', 'high', 'low', 'close', 'volume']]
|
| 33 |
+
new_df[['open', 'high', 'low', 'close', 'volume']] = new_df[['open', 'high', 'low', 'close', 'volume']].astype(float)
|
| 34 |
+
new_df['timestamp'] = pd.to_datetime(new_df['timestamp'], unit='ms')
|
| 35 |
+
new_df = new_df.set_index('timestamp')
|
| 36 |
+
df = pd.concat([df, new_df])
|
| 37 |
+
df = df[~df.index.duplicated(keep='first')]
|
| 38 |
+
df.to_csv(DATA_FILE)
|
| 39 |
+
else:
|
| 40 |
+
print("Downloading all data from scratch...")
|
| 41 |
+
klinesT = client.get_historical_klines(symbol, interval, "01 December 2021")
|
| 42 |
+
df = pd.DataFrame(klinesT, columns=['timestamp', 'open', 'high', 'low', 'close', 'volume',
|
| 43 |
+
'close_time', 'quote_av', 'trades', 'tb_base_av', 'tb_quote_av', 'ignore'])
|
| 44 |
+
df = df[['timestamp', 'open', 'high', 'low', 'close', 'volume']]
|
| 45 |
+
df[['open', 'high', 'low', 'close', 'volume']] = df[['open', 'high', 'low', 'close', 'volume']].astype(float)
|
| 46 |
+
df['timestamp'] = pd.to_datetime(df['timestamp'], unit='ms')
|
| 47 |
+
df = df.set_index('timestamp')
|
| 48 |
+
df.to_csv(DATA_FILE)
|
| 49 |
+
|
| 50 |
+
# Feature Engineering - Maximum Indicators
|
| 51 |
+
# (RSI, MACD, EMAs, SMAs, BB, ATR, ADX, Stochastic, Williams %R, CCI, Momentum)
|
| 52 |
+
|
| 53 |
+
# RSI (Relative Strength Index)
|
| 54 |
+
df['rsi'] = ta.momentum.RSIIndicator(df['close'], window=14).rsi()
|
| 55 |
+
|
| 56 |
+
# MACD (Moving Average Convergence Divergence)
|
| 57 |
+
df['macd'] = ta.trend.MACD(df['close']).macd()
|
| 58 |
+
|
| 59 |
+
# EMA (Exponential Moving Averages)
|
| 60 |
+
df['ema_10'] = df['close'].ewm(span=10, adjust=False).mean()
|
| 61 |
+
df['ema_20'] = df['close'].ewm(span=20, adjust=False).mean()
|
| 62 |
+
df['ema_50'] = df['close'].ewm(span=50, adjust=False).mean()
|
| 63 |
+
df['ema_100'] = df['close'].ewm(span=100, adjust=False).mean()
|
| 64 |
+
|
| 65 |
+
# SMA (Simple Moving Averages)
|
| 66 |
+
df['sma_10'] = df['close'].rolling(window=10).mean()
|
| 67 |
+
df['sma_20'] = df['close'].rolling(window=20).mean()
|
| 68 |
+
df['sma_50'] = df['close'].rolling(window=50).mean()
|
| 69 |
+
df['sma_100'] = df['close'].rolling(window=100).mean()
|
| 70 |
+
|
| 71 |
+
# Bollinger Bands
|
| 72 |
+
bb_indicator = ta.volatility.BollingerBands(df['close'], window=20, window_dev=2)
|
| 73 |
+
df['bb_bbm'] = bb_indicator.bollinger_mavg()
|
| 74 |
+
df['bb_bbh'] = bb_indicator.bollinger_hband()
|
| 75 |
+
df['bb_bbl'] = bb_indicator.bollinger_lband()
|
| 76 |
+
df['bb_width'] = (df['bb_bbh'] - df['bb_bbl']) / df['bb_bbm']
|
| 77 |
+
|
| 78 |
+
# Average True Range (ATR)
|
| 79 |
+
df['atr'] = ta.volatility.AverageTrueRange(df['high'], df['low'], df['close'], window=14).average_true_range()
|
| 80 |
+
|
| 81 |
+
# ADX - Average Directional Index (Trend strength)
|
| 82 |
+
df['adx'] = ta.trend.ADXIndicator(df['high'], df['low'], df['close'], window=14).adx()
|
| 83 |
+
|
| 84 |
+
# Stochastic Oscillator
|
| 85 |
+
stoch = ta.momentum.StochasticOscillator(df['high'], df['low'], df['close'], window=14)
|
| 86 |
+
df['stoch_k'] = stoch.stoch()
|
| 87 |
+
df['stoch_d'] = stoch.stoch_signal()
|
| 88 |
+
|
| 89 |
+
# Williams %R
|
| 90 |
+
df['williams_r'] = ta.momentum.WilliamsRIndicator(df['high'], df['low'], df['close'], lbp=14).williams_r()
|
| 91 |
+
|
| 92 |
+
# CCI (Commodity Channel Index)
|
| 93 |
+
df['cci'] = ta.trend.CCIIndicator(df['high'], df['low'], df['close'], window=20).cci()
|
| 94 |
+
|
| 95 |
+
# Momentum (manual calculation)
|
| 96 |
+
df['momentum'] = df['close'] - df['close'].shift(10) # Simple momentum calculation
|
| 97 |
+
|
| 98 |
+
# Ichimoku Cloud Indicators
|
| 99 |
+
ichimoku = ta.trend.IchimokuIndicator(df['high'], df['low'], window1=9, window2=26, window3=52)
|
| 100 |
+
df['ichimoku_tenkan_sen'] = ichimoku.ichimoku_conversion_line()
|
| 101 |
+
df['ichimoku_kijun_sen'] = ichimoku.ichimoku_base_line()
|
| 102 |
+
df['ichimoku_senkou_span_a'] = ichimoku.ichimoku_a()
|
| 103 |
+
df['ichimoku_senkou_span_b'] = ichimoku.ichimoku_b()
|
| 104 |
+
df['ichimoku_chikou_span'] = df['close'].shift(-26)
|
| 105 |
+
|
| 106 |
+
# Create Uptrend/Downtrend labels based on cloud (1 = Uptrend, 0 = Downtrend, -1 = Neutral)
|
| 107 |
+
def ichimoku_trend_label(row):
|
| 108 |
+
if row['close'] > row['ichimoku_senkou_span_a'] and row['close'] > row['ichimoku_senkou_span_b']:
|
| 109 |
+
return 1 # Uptrend
|
| 110 |
+
elif row['close'] < row['ichimoku_senkou_span_a'] and row['close'] < row['ichimoku_senkou_span_b']:
|
| 111 |
+
return 0 # Downtrend
|
| 112 |
+
else:
|
| 113 |
+
return -1 # Neutral
|
| 114 |
+
|
| 115 |
+
# Apply function to create 'cloud_trend' labels
|
| 116 |
+
df['cloud_trend'] = df.apply(ichimoku_trend_label, axis=1)
|
| 117 |
+
|
| 118 |
+
# Drop rows with NaN values
|
| 119 |
+
df = df.dropna()
|
| 120 |
+
|
| 121 |
+
# Features and Target
|
| 122 |
+
features = df.drop(columns=['open', 'high', 'low', 'close', 'volume', 'cloud_trend']).columns
|
| 123 |
+
X = df[features]
|
| 124 |
+
y = df['cloud_trend'] # Now predicting cloud trend: up, down, or neutral
|
| 125 |
+
|
| 126 |
+
# Train/Test Split
|
| 127 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, shuffle=False)
|
| 128 |
+
|
| 129 |
+
# Train Random Forest (with class balancing)
|
| 130 |
+
model = RandomForestClassifier(n_estimators=200, class_weight="balanced", random_state=42)
|
| 131 |
+
model.fit(X_train, y_train)
|
| 132 |
+
|
| 133 |
+
# Evaluate
|
| 134 |
+
y_pred = model.predict(X_test)
|
| 135 |
+
print(classification_report(y_test, y_pred))
|
| 136 |
+
|
| 137 |
+
# Predict latest movement
|
| 138 |
+
latest_features = X.iloc[-1].values.reshape(1, -1)
|
| 139 |
+
predicted_trend = model.predict(latest_features)
|
| 140 |
+
trend_label = predicted_trend[0]
|
| 141 |
+
|
| 142 |
+
# Print trend prediction
|
| 143 |
+
if trend_label == 1:
|
| 144 |
+
print("Predicted next trend: Uptrend")
|
| 145 |
+
elif trend_label == 0:
|
| 146 |
+
print("Predicted next trend: Downtrend")
|
| 147 |
+
else:
|
| 148 |
+
print("Predicted next trend: Neutral (inside the cloud)")
|