Create MLCryptoForecasterAllAssets.py
Browse files- MLCryptoForecasterAllAssets.py +107 -0
MLCryptoForecasterAllAssets.py
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
from datetime import 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 |
+
# Initialize Binance client (insert API keys if needed)
|
| 12 |
+
client = Client()
|
| 13 |
+
|
| 14 |
+
# Settings
|
| 15 |
+
interval = Client.KLINE_INTERVAL_4HOUR
|
| 16 |
+
|
| 17 |
+
# Retrieve all trading symbols quoted in USDT
|
| 18 |
+
exchange_info = client.get_exchange_info()
|
| 19 |
+
symbols = [s['symbol'] for s in exchange_info['symbols']
|
| 20 |
+
if s['status'] == 'TRADING' and s['quoteAsset'] == 'USDT']
|
| 21 |
+
|
| 22 |
+
# Function to process a single symbol
|
| 23 |
+
def process_symbol(symbol):
|
| 24 |
+
data_file = f"{symbol}_data_4h_full.csv"
|
| 25 |
+
# Load or download data
|
| 26 |
+
if os.path.exists(data_file):
|
| 27 |
+
df = pd.read_csv(data_file, index_col=0, parse_dates=True)
|
| 28 |
+
last_ts = df.index[-1]
|
| 29 |
+
start_time = last_ts + timedelta(hours=4)
|
| 30 |
+
start_str = start_time.strftime("%d %B %Y %H:%M:%S")
|
| 31 |
+
new_klines = client.get_historical_klines(symbol, interval, start_str)
|
| 32 |
+
if new_klines:
|
| 33 |
+
new_df = pd.DataFrame(new_klines, columns=[
|
| 34 |
+
'timestamp','open','high','low','close','volume',
|
| 35 |
+
'close_time','quote_av','trades','tb_base_av','tb_quote_av','ignore'
|
| 36 |
+
])
|
| 37 |
+
new_df = new_df[['timestamp','open','high','low','close','volume']]
|
| 38 |
+
new_df[['open','high','low','close','volume']] = new_df[['open','high','low','close','volume']].astype(float)
|
| 39 |
+
new_df['timestamp'] = pd.to_datetime(new_df['timestamp'], unit='ms')
|
| 40 |
+
new_df.set_index('timestamp', inplace=True)
|
| 41 |
+
df = pd.concat([df, new_df])
|
| 42 |
+
df = df[~df.index.duplicated(keep='first')]
|
| 43 |
+
df.to_csv(data_file)
|
| 44 |
+
else:
|
| 45 |
+
klines = client.get_historical_klines(symbol, interval, "01 December 2021")
|
| 46 |
+
df = pd.DataFrame(klines, columns=[
|
| 47 |
+
'timestamp','open','high','low','close','volume',
|
| 48 |
+
'close_time','quote_av','trades','tb_base_av','tb_quote_av','ignore'
|
| 49 |
+
])
|
| 50 |
+
df = df[['timestamp','open','high','low','close','volume']]
|
| 51 |
+
df[['open','high','low','close','volume']] = df[['open','high','low','close','volume']].astype(float)
|
| 52 |
+
df['timestamp'] = pd.to_datetime(df['timestamp'], unit='ms')
|
| 53 |
+
df.set_index('timestamp', inplace=True)
|
| 54 |
+
df.to_csv(data_file)
|
| 55 |
+
|
| 56 |
+
# Feature Engineering
|
| 57 |
+
df['rsi'] = ta.momentum.RSIIndicator(df['close'], window=14).rsi()
|
| 58 |
+
df['macd'] = ta.trend.MACD(df['close']).macd()
|
| 59 |
+
for span in [10, 20, 50, 100]:
|
| 60 |
+
df[f'ema_{span}'] = df['close'].ewm(span=span, adjust=False).mean()
|
| 61 |
+
for window in [10, 20, 50, 100]:
|
| 62 |
+
df[f'sma_{window}'] = df['close'].rolling(window=window).mean()
|
| 63 |
+
bb = ta.volatility.BollingerBands(df['close'], window=20, window_dev=2)
|
| 64 |
+
df['bb_width'] = (bb.bollinger_hband() - bb.bollinger_lband()) / bb.bollinger_mavg()
|
| 65 |
+
df['atr'] = ta.volatility.AverageTrueRange(df['high'], df['low'], df['close'], window=14).average_true_range()
|
| 66 |
+
df['adx'] = ta.trend.ADXIndicator(df['high'], df['low'], df['close'], window=14).adx()
|
| 67 |
+
stoch = ta.momentum.StochasticOscillator(df['high'], df['low'], df['close'], window=14)
|
| 68 |
+
df['stoch_k'] = stoch.stoch()
|
| 69 |
+
df['stoch_d'] = stoch.stoch_signal()
|
| 70 |
+
df['williams_r'] = ta.momentum.WilliamsRIndicator(df['high'], df['low'], df['close'], lbp=14).williams_r()
|
| 71 |
+
df['cci'] = ta.trend.CCIIndicator(df['high'], df['low'], df['close'], window=20).cci()
|
| 72 |
+
df['momentum'] = df['close'] - df['close'].shift(10)
|
| 73 |
+
ichi = ta.trend.IchimokuIndicator(df['high'], df['low'], window1=9, window2=26, window3=52)
|
| 74 |
+
df['ichimoku_senkou_span_a'] = ichi.ichimoku_a()
|
| 75 |
+
df['ichimoku_senkou_span_b'] = ichi.ichimoku_b()
|
| 76 |
+
|
| 77 |
+
# Trend Label
|
| 78 |
+
conditions = [
|
| 79 |
+
(df['close'] > df['ichimoku_senkou_span_a']) & (df['close'] > df['ichimoku_senkou_span_b']),
|
| 80 |
+
(df['close'] < df['ichimoku_senkou_span_a']) & (df['close'] < df['ichimoku_senkou_span_b'])
|
| 81 |
+
]
|
| 82 |
+
df['cloud_trend'] = np.select(conditions, [1, 0], default=-1)
|
| 83 |
+
df.dropna(inplace=True)
|
| 84 |
+
|
| 85 |
+
# Model Training
|
| 86 |
+
features = df.drop(columns=['open','high','low','close','volume','cloud_trend']).columns
|
| 87 |
+
X, y = df[features], df['cloud_trend']
|
| 88 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, shuffle=False)
|
| 89 |
+
model = RandomForestClassifier(n_estimators=200, class_weight='balanced', random_state=42)
|
| 90 |
+
model.fit(X_train, y_train)
|
| 91 |
+
y_pred = model.predict(X_test)
|
| 92 |
+
|
| 93 |
+
print(f"\n=== {symbol} ===")
|
| 94 |
+
print(classification_report(y_test, y_pred, zero_division=0))
|
| 95 |
+
|
| 96 |
+
# Latest prediction
|
| 97 |
+
latest_feat = X.iloc[-1].values.reshape(1, -1)
|
| 98 |
+
pred = model.predict(latest_feat)[0]
|
| 99 |
+
labels = {1: 'Uptrend', 0: 'Downtrend', -1: 'Neutral'}
|
| 100 |
+
print(f"Predicted next trend for {symbol}: {labels[pred]}")
|
| 101 |
+
|
| 102 |
+
# Main loop
|
| 103 |
+
for s in symbols:
|
| 104 |
+
try:
|
| 105 |
+
process_symbol(s)
|
| 106 |
+
except Exception as e:
|
| 107 |
+
print(f"Error processing {s}: {e}")
|