Lightbgm Train
Lightbgm Train
import joblib
import pandas as pd
import lightgbm as lgb
from sklearn.model_selection import train_test_split
import ccxt
def get_symbols(self):
info = self.exchange.fetch_markets()
return [m['symbol'] for m in info if m['quote'] == 'USDT']
@staticmethod
def rsi(df, period=14):
delta = df['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))
@staticmethod
def macd(df, fast=12, slow=26, signal=9):
macd = df['close'].ewm(span=fast, adjust=False).mean() -
df['close'].ewm(span=slow, adjust=False).mean()
macd_signal = macd.ewm(span=signal, adjust=False).mean()
macd_hist = macd - macd_signal
return macd, macd_signal, macd_hist
@staticmethod
def stochastic_oscillator(df, k_period=14, d_period=3):
low_min = df['low'].rolling(window=k_period).min()
high_max = df['high'].rolling(window=k_period).max()
stoch_k = 100 * ((df['close'] - low_min) / (high_max - low_min))
stoch_d = stoch_k.rolling(window=d_period).mean()
return stoch_k, stoch_d
@staticmethod
def atr(df, period=14):
tr = pd.concat([df['high'] - df['low'],
(df['high'] - df['close'].shift()).abs(),
(df['low'] - df['close'].shift()).abs()], axis=1)
atr = tr.max(axis=1).rolling(window=period).mean()
return atr
# Train-test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
random_state=42)
for symbol in symbols[:750]: # You can limit the number of symbols for testing
purposes (replace 10 with 750 if needed)
for timeframe in timeframes:
print(f"Training LightGBM model for {symbol} at {timeframe}
timeframe...")
train_lightgbm_model(symbol, timeframe, fetcher)
print(f"LightGBM model saved for {symbol} at {timeframe} timeframe.")