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for testing
|
| 4 |
+
"""
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import numpy as np
|
| 7 |
+
import pandas as pd
|
| 8 |
+
from sklearn.preprocessing import RobustScaler
|
| 9 |
+
from sklearn.ensemble import GradientBoostingClassifier
|
| 10 |
+
from sklearn.metrics import accuracy_score, classification_report
|
| 11 |
+
import warnings
|
| 12 |
+
warnings.filterwarnings('ignore')
|
| 13 |
+
|
| 14 |
+
def generate_data(n_samples=100000):
|
| 15 |
+
"""Generate synthetic XAUUSD-like data"""
|
| 16 |
+
np.random.seed(42)
|
| 17 |
+
|
| 18 |
+
# Price-like data
|
| 19 |
+
prices = 1800 + np.cumsum(np.random.randn(n_samples) * 10)
|
| 20 |
+
|
| 21 |
+
data = pd.DataFrame({
|
| 22 |
+
'close': prices,
|
| 23 |
+
'open': prices + np.random.randn(n_samples) * 5,
|
| 24 |
+
'high': prices + np.abs(np.random.randn(n_samples) * 8),
|
| 25 |
+
'low': prices - np.abs(np.random.randn(n_samples) * 8),
|
| 26 |
+
'volume': np.random.randint(1000, 100000, n_samples)
|
| 27 |
+
})
|
| 28 |
+
return data
|
| 29 |
+
|
| 30 |
+
def create_features(df):
|
| 31 |
+
"""Create technical indicators"""
|
| 32 |
+
features = pd.DataFrame()
|
| 33 |
+
features['close'] = df['close']
|
| 34 |
+
features['open'] = df['open']
|
| 35 |
+
features['high'] = df['high']
|
| 36 |
+
features['low'] = df['low']
|
| 37 |
+
features['volume'] = df['volume']
|
| 38 |
+
|
| 39 |
+
# Returns
|
| 40 |
+
features['returns'] = df['close'].pct_change()
|
| 41 |
+
|
| 42 |
+
# Simple Moving Averages
|
| 43 |
+
features['sma20'] = df['close'].rolling(20).mean()
|
| 44 |
+
features['sma50'] = df['close'].rolling(50).mean()
|
| 45 |
+
|
| 46 |
+
# MACD
|
| 47 |
+
ema12 = df['close'].ewm(span=12).mean()
|
| 48 |
+
ema26 = df['close'].ewm(span=26).mean()
|
| 49 |
+
macd = ema12 - ema26
|
| 50 |
+
features['macd'] = macd
|
| 51 |
+
features['macd_signal'] = macd.ewm(span=9).mean()
|
| 52 |
+
features['histogram'] = features['macd'] - features['macd_signal']
|
| 53 |
+
|
| 54 |
+
# RSI
|
| 55 |
+
delta = df['close'].diff()
|
| 56 |
+
gain = delta.where(delta > 0, 0).rolling(14).mean()
|
| 57 |
+
loss = (-delta.where(delta < 0, 0)).rolling(14).mean()
|
| 58 |
+
rs = gain / loss
|
| 59 |
+
features['rsi'] = 100 - (100 / (1 + rs))
|
| 60 |
+
|
| 61 |
+
# Bollinger Bands
|
| 62 |
+
sma20 = df['close'].rolling(20).mean()
|
| 63 |
+
std20 = df['close'].rolling(20).std()
|
| 64 |
+
features['bb_upper'] = sma20 + (std20 * 2)
|
| 65 |
+
features['bb_lower'] = sma20 - (std20 * 2)
|
| 66 |
+
|
| 67 |
+
# Future returns for labels
|
| 68 |
+
future_returns = df['close'].shift(-5) / df['close'] - 1
|
| 69 |
+
labels = pd.Series(0, index=df.index)
|
| 70 |
+
labels[future_returns > 0.005] = 1 # BUY
|
| 71 |
+
labels[future_returns < -0.005] = -1 # SELL
|
| 72 |
+
# HOLD = 0
|
| 73 |
+
|
| 74 |
+
features['label'] = labels
|
| 75 |
+
return features.dropna()
|
| 76 |
+
|
| 77 |
+
def train_model(n_estimators, max_depth, learning_rate):
|
| 78 |
+
import time
|
| 79 |
+
start = time.time()
|
| 80 |
+
|
| 81 |
+
yield "π Loading data..."
|
| 82 |
+
df = generate_data(100000)
|
| 83 |
+
|
| 84 |
+
yield "π§ Creating features..."
|
| 85 |
+
data = create_features(df)
|
| 86 |
+
|
| 87 |
+
yield "βοΈ Preparing..."
|
| 88 |
+
X = data.drop('label', axis=1).values
|
| 89 |
+
y = data['label'].values
|
| 90 |
+
|
| 91 |
+
split_idx = int(len(X) * 0.8)
|
| 92 |
+
X_train, X_test = X[:split_idx], X[split_idx:]
|
| 93 |
+
y_train, y_test = y[:split_idx], y[split_idx:]
|
| 94 |
+
|
| 95 |
+
scaler = RobustScaler()
|
| 96 |
+
X_train_scaled = scaler.fit_transform(X_train)
|
| 97 |
+
X_test_scaled = scaler.transform(X_test)
|
| 98 |
+
|
| 99 |
+
yield f"π Training (n={n_estimators}, depth={max_depth})..."
|
| 100 |
+
model = GradientBoostingClassifier(
|
| 101 |
+
n_estimators=int(n_estimators),
|
| 102 |
+
max_depth=int(max_depth),
|
| 103 |
+
learning_rate=learning_rate,
|
| 104 |
+
subsample=0.8,
|
| 105 |
+
random_state=42
|
| 106 |
+
)
|
| 107 |
+
model.fit(X_train_scaled, y_train)
|
| 108 |
+
|
| 109 |
+
yield "π Evaluating..."
|
| 110 |
+
y_pred = model.predict(X_test_scaled)
|
| 111 |
+
accuracy = accuracy_score(y_test, y_pred)
|
| 112 |
+
|
| 113 |
+
elapsed = time.time() - start
|
| 114 |
+
|
| 115 |
+
yield {
|
| 116 |
+
"accuracy": f"{accuracy:.2%}",
|
| 117 |
+
"time": f"{elapsed:.1f}s",
|
| 118 |
+
"train_samples": f"{len(X_train):,}",
|
| 119 |
+
"test_samples": f"{len(X_test):,}",
|
| 120 |
+
"report": classification_report(y_test, y_pred, target_names=['SELL', 'HOLD', 'BUY'])
|
| 121 |
+
}
|
| 122 |
+
|
| 123 |
+
# Gradio UI
|
| 124 |
+
with gr.Blocks() as demo:
|
| 125 |
+
gr.Markdown("# π€ XAUUSD Trading Model Training")
|
| 126 |
+
gr.Markdown("**TESTING VERSION** - Uses synthetic data")
|
| 127 |
+
|
| 128 |
+
with gr.Row():
|
| 129 |
+
with gr.Column():
|
| 130 |
+
n_estimators = gr.Slider(50, 200, value=100, step=10, label="n_estimators")
|
| 131 |
+
max_depth = gr.Slider(3, 8, value=5, step=1, label="max_depth")
|
| 132 |
+
learning_rate = gr.Slider(0.01, 0.3, value=0.1, step=0.01, label="learning_rate")
|
| 133 |
+
train_btn = gr.Button("π Train Model", variant="primary")
|
| 134 |
+
|
| 135 |
+
with gr.Column():
|
| 136 |
+
output = gr.JSON(label="Results")
|
| 137 |
+
report = gr.Textbox(label="Report", lines=8)
|
| 138 |
+
|
| 139 |
+
train_btn.click(
|
| 140 |
+
fn=train_model,
|
| 141 |
+
inputs=[n_estimators, max_depth, learning_rate],
|
| 142 |
+
outputs=[output, report]
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
gr.Markdown("""
|
| 146 |
+
## Test Mode
|
| 147 |
+
- Synthetic 100k samples
|
| 148 |
+
- 14 features (price + MACD + RSI + BB)
|
| 149 |
+
- No external dependencies needed
|
| 150 |
+
""")
|
| 151 |
+
|
| 152 |
+
demo.launch()
|