Spaces:
Sleeping
Sleeping
Smart-Trader-EA commited on
Commit ยท
18fd386
1
Parent(s): f01ed3d
Fix Gradio compatibility issue
Browse files
app.py
CHANGED
|
@@ -8,85 +8,50 @@ import warnings
|
|
| 8 |
import datetime
|
| 9 |
import shutil
|
| 10 |
import traceback
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
# Suppress
|
| 13 |
warnings.filterwarnings('ignore')
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
| 17 |
os.environ["OMP_NUM_THREADS"] = "1"
|
| 18 |
os.environ["OPENBLAS_NUM_THREADS"] = "1"
|
| 19 |
os.environ["MKL_NUM_THREADS"] = "1"
|
| 20 |
|
| 21 |
-
#
|
| 22 |
RAW_DATA_DIR = "data/raw"
|
| 23 |
PROCESSED_DATA_DIR = "data/processed"
|
| 24 |
os.makedirs(PROCESSED_DATA_DIR, exist_ok=True)
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
# This dictionary will be dynamically updated in load_available_data.
|
| 28 |
TRADING_PAIRS = {
|
| 29 |
"EURUSD": {
|
| 30 |
-
"description": "Euro to US Dollar Forex Pair
|
| 31 |
"date_format": "%d.%m.%Y %H:%M:%S.%f %z",
|
| 32 |
"has_timezone": True,
|
| 33 |
"decimal_separator": ".",
|
| 34 |
"required_columns": ["Open", "High", "Low", "Close"]
|
| 35 |
-
},
|
| 36 |
-
"BTCUSD": {
|
| 37 |
-
"description": "Bitcoin to US Dollar (Sample)",
|
| 38 |
-
"date_format": "%Y-%m-%d %H:%M:%S",
|
| 39 |
-
"has_timezone": False,
|
| 40 |
-
"decimal_separator": ".",
|
| 41 |
-
"required_columns": ["Open", "High", "Low", "Close"]
|
| 42 |
-
},
|
| 43 |
-
"AAPL": {
|
| 44 |
-
"description": "Apple Inc. Stock (Sample)",
|
| 45 |
-
"date_format": "%Y-%m-%d",
|
| 46 |
-
"has_timezone": False,
|
| 47 |
-
"decimal_separator": ".",
|
| 48 |
-
"required_columns": ["Open", "High", "Low", "Close", "Volume"]
|
| 49 |
}
|
| 50 |
}
|
| 51 |
|
| 52 |
-
#
|
| 53 |
-
|
| 54 |
def preprocess_data_file(raw_file_path, pair_name):
|
| 55 |
-
"""Preprocess raw data file to standardized format"""
|
| 56 |
print(f"๐ Preprocessing data for {pair_name}...")
|
| 57 |
-
|
| 58 |
-
# Get pair configuration, or use generic defaults for a new pair
|
| 59 |
-
config = TRADING_PAIRS.get(pair_name, {
|
| 60 |
-
"description": f"{pair_name} Trading Pair",
|
| 61 |
-
"date_format": None, # Use dynamic parsing for generic pairs
|
| 62 |
-
"has_timezone": False,
|
| 63 |
-
"decimal_separator": ".",
|
| 64 |
-
"required_columns": ["Open", "High", "Low", "Close"]
|
| 65 |
-
})
|
| 66 |
-
|
| 67 |
try:
|
| 68 |
-
# Read
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
for encoding in encodings:
|
| 73 |
-
try:
|
| 74 |
-
# Attempt to read CSV, let pandas infer delimiter
|
| 75 |
-
df = pd.read_csv(raw_file_path, encoding=encoding, sep=None, engine='python')
|
| 76 |
-
print(f"โ
Successfully read {pair_name} data with {encoding} encoding")
|
| 77 |
-
break
|
| 78 |
-
except (UnicodeDecodeError, pd.errors.ParserError):
|
| 79 |
-
continue
|
| 80 |
-
|
| 81 |
-
if df is None:
|
| 82 |
-
raise Exception(f"โ Failed to read {pair_name} data with any encoding/delimiter")
|
| 83 |
|
| 84 |
-
# Standardize
|
| 85 |
column_mapping = {}
|
| 86 |
for col in df.columns:
|
| 87 |
col_lower = col.lower().strip()
|
| 88 |
-
|
| 89 |
-
if any(keyword in col_lower for keyword in ['date', 'time', 'timestamp', 'ds']):
|
| 90 |
column_mapping[col] = 'datetime'
|
| 91 |
elif 'open' in col_lower:
|
| 92 |
column_mapping[col] = 'Open'
|
|
@@ -94,122 +59,40 @@ def preprocess_data_file(raw_file_path, pair_name):
|
|
| 94 |
column_mapping[col] = 'High'
|
| 95 |
elif 'low' in col_lower:
|
| 96 |
column_mapping[col] = 'Low'
|
| 97 |
-
elif 'close' in col_lower
|
| 98 |
column_mapping[col] = 'Close'
|
| 99 |
-
elif 'volume' in col_lower or 'vol' in col_lower:
|
| 100 |
-
column_mapping[col] = 'Volume'
|
| 101 |
|
| 102 |
if column_mapping:
|
| 103 |
df.rename(columns=column_mapping, inplace=True)
|
| 104 |
-
print(f"๐ท๏ธ Standardized columns: {list(column_mapping.keys())}
|
| 105 |
-
|
| 106 |
-
# Process datetime column
|
| 107 |
-
datetime_col = None
|
| 108 |
-
for col in ['datetime', 'ds']:
|
| 109 |
-
if col in df.columns:
|
| 110 |
-
datetime_col = col
|
| 111 |
-
break
|
| 112 |
-
|
| 113 |
-
if datetime_col is None:
|
| 114 |
-
raise Exception("โ No datetime column found in data after renaming")
|
| 115 |
|
| 116 |
-
#
|
| 117 |
-
if
|
| 118 |
-
|
| 119 |
-
df
|
| 120 |
-
df
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
)
|
|
|
|
|
|
|
| 126 |
else:
|
| 127 |
-
|
| 128 |
-
df[datetime_col] = pd.to_datetime(
|
| 129 |
-
df[datetime_col],
|
| 130 |
-
errors='coerce',
|
| 131 |
-
utc=config.get('has_timezone', False) # Use config setting if available
|
| 132 |
-
)
|
| 133 |
-
|
| 134 |
-
# Remove rows with invalid dates
|
| 135 |
-
before_count = len(df)
|
| 136 |
-
df = df.dropna(subset=[datetime_col])
|
| 137 |
-
print(f"๐งน Removed {before_count - len(df)} rows with invalid dates")
|
| 138 |
-
|
| 139 |
-
# Set datetime as index
|
| 140 |
-
df.set_index(datetime_col, inplace=True)
|
| 141 |
-
df.sort_index(inplace=True)
|
| 142 |
-
|
| 143 |
-
# Handle non-standard decimal separators (e.g., European format ',')
|
| 144 |
-
if config['decimal_separator'] != '.':
|
| 145 |
-
print(f"๐ ๏ธ Fixing decimal separator from {config['decimal_separator']} to '.'")
|
| 146 |
-
for col in ['Open', 'High', 'Low', 'Close', 'Volume']:
|
| 147 |
-
if col in df.columns:
|
| 148 |
-
# Convert to string, replace comma with dot, then convert to float
|
| 149 |
-
df[col] = df[col].astype(str).str.replace(config['decimal_separator'], '.', regex=False).astype(float)
|
| 150 |
-
|
| 151 |
-
# Ensure price columns are numeric
|
| 152 |
-
for col in ['Open', 'High', 'Low', 'Close']:
|
| 153 |
-
if col in df.columns:
|
| 154 |
-
df[col] = pd.to_numeric(df[col], errors='coerce')
|
| 155 |
-
|
| 156 |
-
# Fill missing values
|
| 157 |
-
for col in ['Open', 'High', 'Low', 'Close']:
|
| 158 |
-
if col in df.columns:
|
| 159 |
-
missing_before = df[col].isna().sum()
|
| 160 |
-
if missing_before > 0:
|
| 161 |
-
df[col] = df[col].fillna(method='ffill').fillna(method='bfill')
|
| 162 |
-
print(f" ๐ Filled {missing_before} missing values in {col}")
|
| 163 |
-
|
| 164 |
-
# Remove duplicates
|
| 165 |
-
before_count = len(df)
|
| 166 |
-
df = df[~df.index.duplicated(keep='first')]
|
| 167 |
-
print(f"๐งน Removed {before_count - len(df)} duplicate entries")
|
| 168 |
-
|
| 169 |
-
# Final validation
|
| 170 |
-
missing_cols = [col for col in config['required_columns'] if col not in df.columns]
|
| 171 |
-
if missing_cols:
|
| 172 |
-
print(f"โ Missing required columns: {missing_cols}. Available: {df.columns.tolist()}")
|
| 173 |
return None
|
| 174 |
-
|
| 175 |
-
if len(df) < 2:
|
| 176 |
-
raise Exception("Dataset has fewer than 2 valid rows after preprocessing.")
|
| 177 |
|
| 178 |
-
# Save preprocessed data
|
| 179 |
-
processed_file = os.path.join(PROCESSED_DATA_DIR, f"{pair_name}_processed.csv")
|
| 180 |
-
df.to_csv(processed_file)
|
| 181 |
-
print(f"โ
Saved preprocessed data to {processed_file}")
|
| 182 |
-
|
| 183 |
-
return df
|
| 184 |
-
|
| 185 |
except Exception as e:
|
| 186 |
-
print(f"โ Preprocessing error
|
| 187 |
traceback.print_exc()
|
| 188 |
return None
|
| 189 |
|
|
|
|
| 190 |
def load_available_data():
|
| 191 |
-
"""Load and preprocess all available data files."""
|
| 192 |
-
global TRADING_PAIRS, available_data
|
| 193 |
available_data = {}
|
| 194 |
|
| 195 |
-
# Check if data exists in a generic 'data' folder and move it to 'data/raw'
|
| 196 |
if not os.path.exists(RAW_DATA_DIR):
|
| 197 |
-
print(f"โ ๏ธ Raw data directory not found: {RAW_DATA_DIR}
|
| 198 |
-
if os.path.exists("data") and os.path.isdir("data"):
|
| 199 |
-
csv_files = [f for f in os.listdir("data") if f.endswith('.csv')]
|
| 200 |
-
if csv_files:
|
| 201 |
-
os.makedirs(RAW_DATA_DIR, exist_ok=True)
|
| 202 |
-
for filename in csv_files:
|
| 203 |
-
try:
|
| 204 |
-
shutil.move(os.path.join("data", filename), os.path.join(RAW_DATA_DIR, filename))
|
| 205 |
-
print(f"โ
Moved {filename} to {RAW_DATA_DIR}")
|
| 206 |
-
except Exception as e:
|
| 207 |
-
print(f"โ ๏ธ Could not move {filename}: {e}")
|
| 208 |
-
else:
|
| 209 |
-
print("No CSV files found in 'data/' to move.")
|
| 210 |
-
|
| 211 |
-
if not os.path.exists(RAW_DATA_DIR):
|
| 212 |
-
print(f"โ Still cannot find raw data directory: {RAW_DATA_DIR}.")
|
| 213 |
return available_data
|
| 214 |
|
| 215 |
print(f"๐ Scanning for data files in {RAW_DATA_DIR}...")
|
|
@@ -218,11 +101,10 @@ def load_available_data():
|
|
| 218 |
if filename.endswith('.csv'):
|
| 219 |
pair_name = filename.split('.')[0].upper()
|
| 220 |
|
| 221 |
-
# Generalization: Add generic config if not exists
|
| 222 |
if pair_name not in TRADING_PAIRS:
|
| 223 |
TRADING_PAIRS[pair_name] = {
|
| 224 |
-
"description": f"{pair_name} Trading Pair
|
| 225 |
-
"date_format": None,
|
| 226 |
"has_timezone": False,
|
| 227 |
"decimal_separator": ".",
|
| 228 |
"required_columns": ["Open", "High", "Low", "Close"]
|
|
@@ -231,207 +113,192 @@ def load_available_data():
|
|
| 231 |
raw_file_path = os.path.join(RAW_DATA_DIR, filename)
|
| 232 |
processed_file_path = os.path.join(PROCESSED_DATA_DIR, f"{pair_name}_processed.csv")
|
| 233 |
|
| 234 |
-
# Check existing processed file
|
| 235 |
if os.path.exists(processed_file_path):
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
print(f"โ
Using existing preprocessed data for {pair_name}")
|
| 244 |
-
continue
|
| 245 |
-
except Exception as e:
|
| 246 |
-
print(f"โ ๏ธ Error loading preprocessed file for {pair_name}. Reprocessing.")
|
| 247 |
|
| 248 |
-
# Preprocess
|
| 249 |
print(f"๐ Processing {pair_name} data...")
|
| 250 |
df = preprocess_data_file(raw_file_path, pair_name)
|
| 251 |
if df is not None:
|
| 252 |
available_data[pair_name] = df
|
| 253 |
-
print(f"โ
Successfully loaded {pair_name}")
|
| 254 |
-
else:
|
| 255 |
-
print(f"โ Failed to load {pair_name} data.")
|
| 256 |
|
| 257 |
return available_data
|
| 258 |
|
| 259 |
-
#
|
| 260 |
-
|
| 261 |
def get_available_pairs():
|
| 262 |
-
|
| 263 |
-
|
|
|
|
| 264 |
|
| 265 |
status = "โ
Available trading pairs:\n"
|
| 266 |
for pair in sorted(available_data.keys()):
|
| 267 |
df = available_data[pair]
|
| 268 |
records = len(df)
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
status += f"โข {pair}: {records} records ({date_range})\n"
|
| 272 |
return status
|
| 273 |
|
| 274 |
-
|
|
|
|
| 275 |
pair_name = pair_name.upper().strip()
|
| 276 |
print(f"\n๐ Starting analysis for {pair_name}")
|
| 277 |
|
| 278 |
-
#
|
| 279 |
-
default_error_fig = go.Figure().update_layout(title="Analysis Failed")
|
| 280 |
default_error_df = gr.DataFrame(headers=["Error"], value=[["Analysis failed"]])
|
| 281 |
|
| 282 |
-
#
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
pair_name = matched_pair
|
| 293 |
-
hist = available_data[pair_name].copy()
|
| 294 |
|
| 295 |
try:
|
| 296 |
-
|
| 297 |
-
|
| 298 |
|
| 299 |
-
#
|
| 300 |
fig = go.Figure()
|
| 301 |
fig.add_trace(go.Candlestick(
|
| 302 |
-
x=hist.index,
|
| 303 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 304 |
))
|
| 305 |
|
|
|
|
| 306 |
if len(hist) >= 20:
|
| 307 |
-
hist['MA20'] = hist['Close'].rolling(window=20).mean()
|
| 308 |
-
fig.add_trace(go.Scatter(
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
|
|
|
|
|
|
| 313 |
|
| 314 |
fig.update_layout(
|
| 315 |
-
title=f"{pair_name} Price Analysis",
|
| 316 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 317 |
)
|
| 318 |
|
| 319 |
-
#
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
forecast_result = "No forecast data available"
|
| 323 |
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
if len(prophet_df) >= 30:
|
| 330 |
-
model = Prophet(
|
| 331 |
-
daily_seasonality=False, yearly_seasonality=True,
|
| 332 |
-
interval_width=0.95, changepoint_prior_scale=0.05,
|
| 333 |
-
stan_backend=None # Critical Fix
|
| 334 |
-
)
|
| 335 |
-
|
| 336 |
-
if (prophet_df['ds'].diff().min().total_seconds() < 86400 * 0.9):
|
| 337 |
-
model.add_seasonality(name='subdaily', period=1, fourier_order=5, prior_scale=0.1)
|
| 338 |
-
|
| 339 |
-
model.fit(prophet_df)
|
| 340 |
-
future = model.make_future_dataframe(periods=30, freq='D')
|
| 341 |
-
forecast = model.predict(future)
|
| 342 |
-
|
| 343 |
-
# Forecast Chart
|
| 344 |
-
forecast_fig = go.Figure()
|
| 345 |
-
hist_recent = prophet_df[prophet_df['ds'] >= (prophet_df['ds'].max() - pd.Timedelta(days=90))]
|
| 346 |
-
forecast_fig.add_trace(go.Scatter(x=hist_recent['ds'], y=hist_recent['y'], mode='lines', name='History', line=dict(color='blue')))
|
| 347 |
-
|
| 348 |
-
forecast_recent = forecast[forecast['ds'] >= (prophet_df['ds'].max() - pd.Timedelta(days=30))]
|
| 349 |
-
forecast_fig.add_trace(go.Scatter(x=forecast_recent['ds'], y=forecast_recent['yhat'], mode='lines', name='Forecast', line=dict(color='red', dash='dash')))
|
| 350 |
-
|
| 351 |
-
forecast_fig.add_trace(go.Scatter(
|
| 352 |
-
x=forecast_recent['ds'].tolist() + forecast_recent['ds'][::-1].tolist(),
|
| 353 |
-
y=forecast_recent['yhat_upper'].tolist() + forecast_recent['yhat_lower'][::-1].tolist(),
|
| 354 |
-
fill='toself', fillcolor='rgba(255,0,0,0.1)', line=dict(color='rgba(255,255,255,0)'), name='95% CI'
|
| 355 |
-
))
|
| 356 |
-
|
| 357 |
-
forecast_fig.update_layout(title=f"{pair_name} 30-Day Forecast", template="plotly_white", height=500)
|
| 358 |
-
|
| 359 |
-
# Forecast Table
|
| 360 |
-
future_dates = forecast[forecast['ds'] > prophet_df['ds'].max()].head(30)
|
| 361 |
-
future_dates['Trend_Value'] = future_dates['yhat'].diff()
|
| 362 |
-
|
| 363 |
-
# Fix first trend value NaN by comparing to last historical
|
| 364 |
-
if not future_dates.empty:
|
| 365 |
-
future_dates.iloc[0, future_dates.columns.get_loc('Trend_Value')] = future_dates.iloc[0]['yhat'] - prophet_df.iloc[-1]['y']
|
| 366 |
-
|
| 367 |
-
future_dates['Date'] = future_dates['ds'].dt.strftime('%Y-%m-%d')
|
| 368 |
-
future_dates['Price'] = future_dates['yhat'].apply(lambda x: f"{x:.5f}")
|
| 369 |
-
future_dates['Trend'] = future_dates['Trend_Value'].apply(lambda x: "๐ Up" if x > 0 else "๏ฟฝ๏ฟฝ๏ฟฝ Down" if x < 0 else "โก๏ธ Flat")
|
| 370 |
-
|
| 371 |
-
forecast_table = gr.DataFrame(
|
| 372 |
-
headers=["Date", "Price", "Trend"],
|
| 373 |
-
value=future_dates[['Date', 'Price', 'Trend']].values.tolist()
|
| 374 |
-
)
|
| 375 |
-
|
| 376 |
-
last_f = forecast.iloc[-1]
|
| 377 |
-
forecast_result = f"๐ฎ Forecast (Day 30): {last_f['yhat']:.5f} (Range: {last_f['yhat_lower']:.5f} - {last_f['yhat_upper']:.5f})"
|
| 378 |
-
|
| 379 |
-
except Exception as e:
|
| 380 |
-
forecast_result = f"โ ๏ธ Forecast Error: {str(e)}"
|
| 381 |
-
traceback.print_exc()
|
| 382 |
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
elif current_price < ma20 and ma20 < ma50: signal = "๐ฃ STRONG BEARISH"
|
| 391 |
-
elif current_price > ma20: signal = "๐ BULLISH"
|
| 392 |
-
elif current_price < ma20: signal = "๐ BEARISH"
|
| 393 |
-
|
| 394 |
-
result_text = (
|
| 395 |
-
f"๐ **{pair_name} Report**\n{'='*30}\n"
|
| 396 |
-
f"๐ฐ Price: {current_price:.5f}\n"
|
| 397 |
-
f"๐ฏ Signal: {signal}\n"
|
| 398 |
-
f"{forecast_result}"
|
| 399 |
-
)
|
| 400 |
|
| 401 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 402 |
|
|
|
|
| 403 |
except Exception as e:
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
# --- Gradio App ---
|
| 407 |
|
| 408 |
-
|
|
|
|
| 409 |
available_data = load_available_data()
|
|
|
|
| 410 |
|
| 411 |
-
|
| 412 |
-
|
|
|
|
| 413 |
|
| 414 |
with gr.Row():
|
| 415 |
-
data_status = gr.Textbox(
|
| 416 |
-
|
| 417 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 418 |
|
| 419 |
-
result_output = gr.Textbox(label="
|
| 420 |
|
| 421 |
with gr.Tabs():
|
| 422 |
-
with gr.TabItem("
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
with gr.TabItem("Table"):
|
| 427 |
-
forecast_table = gr.DataFrame(
|
| 428 |
-
|
| 429 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 430 |
|
|
|
|
| 431 |
if __name__ == "__main__":
|
| 432 |
demo.launch(
|
| 433 |
server_name="0.0.0.0",
|
| 434 |
server_port=7860,
|
| 435 |
-
share=False
|
| 436 |
-
ssr_mode=False # <--- CRITICAL FIX: Disables SSR to prevent KeyError: 1
|
| 437 |
)
|
|
|
|
| 8 |
import datetime
|
| 9 |
import shutil
|
| 10 |
import traceback
|
| 11 |
+
import gc
|
| 12 |
+
import tempfile
|
| 13 |
|
| 14 |
+
# Suppress warnings
|
| 15 |
warnings.filterwarnings('ignore')
|
| 16 |
|
| 17 |
+
# Memory optimization
|
| 18 |
+
def optimize_memory():
|
| 19 |
+
gc.collect()
|
| 20 |
+
|
| 21 |
+
# Environment setup
|
| 22 |
os.environ["OMP_NUM_THREADS"] = "1"
|
| 23 |
os.environ["OPENBLAS_NUM_THREADS"] = "1"
|
| 24 |
os.environ["MKL_NUM_THREADS"] = "1"
|
| 25 |
|
| 26 |
+
# Data directories
|
| 27 |
RAW_DATA_DIR = "data/raw"
|
| 28 |
PROCESSED_DATA_DIR = "data/processed"
|
| 29 |
os.makedirs(PROCESSED_DATA_DIR, exist_ok=True)
|
| 30 |
|
| 31 |
+
# Trading pairs config
|
|
|
|
| 32 |
TRADING_PAIRS = {
|
| 33 |
"EURUSD": {
|
| 34 |
+
"description": "Euro to US Dollar Forex Pair",
|
| 35 |
"date_format": "%d.%m.%Y %H:%M:%S.%f %z",
|
| 36 |
"has_timezone": True,
|
| 37 |
"decimal_separator": ".",
|
| 38 |
"required_columns": ["Open", "High", "Low", "Close"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
}
|
| 40 |
}
|
| 41 |
|
| 42 |
+
# Data preprocessing (simplified)
|
|
|
|
| 43 |
def preprocess_data_file(raw_file_path, pair_name):
|
|
|
|
| 44 |
print(f"๐ Preprocessing data for {pair_name}...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
try:
|
| 46 |
+
# Read CSV
|
| 47 |
+
df = pd.read_csv(raw_file_path, encoding='utf-8')
|
| 48 |
+
print(f"โ
Successfully read {pair_name} data")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
+
# Standardize columns
|
| 51 |
column_mapping = {}
|
| 52 |
for col in df.columns:
|
| 53 |
col_lower = col.lower().strip()
|
| 54 |
+
if 'time' in col_lower or 'date' in col_lower:
|
|
|
|
| 55 |
column_mapping[col] = 'datetime'
|
| 56 |
elif 'open' in col_lower:
|
| 57 |
column_mapping[col] = 'Open'
|
|
|
|
| 59 |
column_mapping[col] = 'High'
|
| 60 |
elif 'low' in col_lower:
|
| 61 |
column_mapping[col] = 'Low'
|
| 62 |
+
elif 'close' in col_lower:
|
| 63 |
column_mapping[col] = 'Close'
|
|
|
|
|
|
|
| 64 |
|
| 65 |
if column_mapping:
|
| 66 |
df.rename(columns=column_mapping, inplace=True)
|
| 67 |
+
print(f"๐ท๏ธ Standardized columns: {list(column_mapping.keys())}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
+
# Process datetime
|
| 70 |
+
if 'datetime' in df.columns:
|
| 71 |
+
df['datetime'] = pd.to_datetime(df['datetime'], errors='coerce', utc=True)
|
| 72 |
+
df = df.dropna(subset=['datetime'])
|
| 73 |
+
df.set_index('datetime', inplace=True)
|
| 74 |
+
df.sort_index(inplace=True)
|
| 75 |
+
|
| 76 |
+
# Save processed data
|
| 77 |
+
processed_file = os.path.join(PROCESSED_DATA_DIR, f"{pair_name}_processed.csv")
|
| 78 |
+
df.to_csv(processed_file)
|
| 79 |
+
print(f"โ
Saved preprocessed data to {processed_file}")
|
| 80 |
+
return df
|
| 81 |
else:
|
| 82 |
+
print("โ No datetime column found")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
return None
|
|
|
|
|
|
|
|
|
|
| 84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
except Exception as e:
|
| 86 |
+
print(f"โ Preprocessing error: {str(e)}")
|
| 87 |
traceback.print_exc()
|
| 88 |
return None
|
| 89 |
|
| 90 |
+
# Load available data
|
| 91 |
def load_available_data():
|
|
|
|
|
|
|
| 92 |
available_data = {}
|
| 93 |
|
|
|
|
| 94 |
if not os.path.exists(RAW_DATA_DIR):
|
| 95 |
+
print(f"โ ๏ธ Raw data directory not found: {RAW_DATA_DIR}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
return available_data
|
| 97 |
|
| 98 |
print(f"๐ Scanning for data files in {RAW_DATA_DIR}...")
|
|
|
|
| 101 |
if filename.endswith('.csv'):
|
| 102 |
pair_name = filename.split('.')[0].upper()
|
| 103 |
|
|
|
|
| 104 |
if pair_name not in TRADING_PAIRS:
|
| 105 |
TRADING_PAIRS[pair_name] = {
|
| 106 |
+
"description": f"{pair_name} Trading Pair",
|
| 107 |
+
"date_format": None,
|
| 108 |
"has_timezone": False,
|
| 109 |
"decimal_separator": ".",
|
| 110 |
"required_columns": ["Open", "High", "Low", "Close"]
|
|
|
|
| 113 |
raw_file_path = os.path.join(RAW_DATA_DIR, filename)
|
| 114 |
processed_file_path = os.path.join(PROCESSED_DATA_DIR, f"{pair_name}_processed.csv")
|
| 115 |
|
|
|
|
| 116 |
if os.path.exists(processed_file_path):
|
| 117 |
+
try:
|
| 118 |
+
df = pd.read_csv(processed_file_path, index_col=0, parse_dates=True)
|
| 119 |
+
available_data[pair_name] = df
|
| 120 |
+
print(f"โ
Using existing preprocessed data for {pair_name}")
|
| 121 |
+
continue
|
| 122 |
+
except:
|
| 123 |
+
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
|
|
|
|
| 125 |
print(f"๐ Processing {pair_name} data...")
|
| 126 |
df = preprocess_data_file(raw_file_path, pair_name)
|
| 127 |
if df is not None:
|
| 128 |
available_data[pair_name] = df
|
| 129 |
+
print(f"โ
Successfully loaded {pair_name} with {len(df)} records")
|
|
|
|
|
|
|
| 130 |
|
| 131 |
return available_data
|
| 132 |
|
| 133 |
+
# Get available pairs - FIXED SYNTAX ERROR
|
|
|
|
| 134 |
def get_available_pairs():
|
| 135 |
+
"""Get list of available trading pairs with status for UI"""
|
| 136 |
+
if not available_data: # CORRECTED THIS LINE
|
| 137 |
+
return "โ ๏ธ No data files found. Please upload CSV files to 'data/raw' directory."
|
| 138 |
|
| 139 |
status = "โ
Available trading pairs:\n"
|
| 140 |
for pair in sorted(available_data.keys()):
|
| 141 |
df = available_data[pair]
|
| 142 |
records = len(df)
|
| 143 |
+
date_range = f"{df.index.min().strftime('%Y-%m-%d')} to {df.index.max().strftime('%Y-%m-%d')}"
|
| 144 |
+
status += f"โข {pair}: {records} records ({date_range})\n"
|
|
|
|
| 145 |
return status
|
| 146 |
|
| 147 |
+
# Analysis function
|
| 148 |
+
def analyze_trading_pair(pair_name):
|
| 149 |
pair_name = pair_name.upper().strip()
|
| 150 |
print(f"\n๐ Starting analysis for {pair_name}")
|
| 151 |
|
| 152 |
+
# Error fallbacks
|
| 153 |
+
default_error_fig = go.Figure().update_layout(title="Analysis Failed", xaxis_title="Date", yaxis_title="Price")
|
| 154 |
default_error_df = gr.DataFrame(headers=["Error"], value=[["Analysis failed"]])
|
| 155 |
|
| 156 |
+
# Check if data available
|
| 157 |
+
if pair_name not in available_data:
|
| 158 |
+
available_pairs = ", ".join(available_data.keys()) or "None"
|
| 159 |
+
return (
|
| 160 |
+
f"โ Data not available for '{pair_name}'\nAvailable pairs: {available_pairs}",
|
| 161 |
+
default_error_fig,
|
| 162 |
+
default_error_fig,
|
| 163 |
+
default_error_df
|
| 164 |
+
)
|
|
|
|
|
|
|
|
|
|
| 165 |
|
| 166 |
try:
|
| 167 |
+
# Get data
|
| 168 |
+
hist = available_data[pair_name].copy()
|
| 169 |
|
| 170 |
+
# Create candlestick chart
|
| 171 |
fig = go.Figure()
|
| 172 |
fig.add_trace(go.Candlestick(
|
| 173 |
+
x=hist.index,
|
| 174 |
+
open=hist['Open'],
|
| 175 |
+
high=hist['High'],
|
| 176 |
+
low=hist['Low'],
|
| 177 |
+
close=hist['Close'],
|
| 178 |
+
name='Price'
|
| 179 |
))
|
| 180 |
|
| 181 |
+
# Add moving averages
|
| 182 |
if len(hist) >= 20:
|
| 183 |
+
hist['MA20'] = hist['Close'].rolling(window=20, min_periods=1).mean()
|
| 184 |
+
fig.add_trace(go.Scatter(
|
| 185 |
+
x=hist.index,
|
| 186 |
+
y=hist['MA20'],
|
| 187 |
+
mode='lines',
|
| 188 |
+
name='20-period MA',
|
| 189 |
+
line=dict(color='blue', width=1.5)
|
| 190 |
+
))
|
| 191 |
|
| 192 |
fig.update_layout(
|
| 193 |
+
title=f"{pair_name} Price Analysis",
|
| 194 |
+
xaxis_title="Date",
|
| 195 |
+
yaxis_title="Price",
|
| 196 |
+
template="plotly_white",
|
| 197 |
+
hovermode="x unified",
|
| 198 |
+
height=500,
|
| 199 |
)
|
| 200 |
|
| 201 |
+
# Return results
|
| 202 |
+
result_text = f"๐ {pair_name} Analysis Complete\nCurrent Price: {hist['Close'].iloc[-1]:.5f}"
|
| 203 |
+
return result_text, fig, default_error_fig, default_error_df
|
|
|
|
| 204 |
|
| 205 |
+
except Exception as e:
|
| 206 |
+
error_msg = f"โ Analysis error: {str(e)}"
|
| 207 |
+
print(error_msg)
|
| 208 |
+
traceback.print_exc()
|
| 209 |
+
return error_msg, default_error_fig, default_error_fig, default_error_df
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
|
| 211 |
+
# Export function (simplified)
|
| 212 |
+
def export_forecast(pair_name):
|
| 213 |
+
"""Export forecast data to CSV file"""
|
| 214 |
+
try:
|
| 215 |
+
# Create a simple export file
|
| 216 |
+
temp_dir = tempfile.mkdtemp()
|
| 217 |
+
export_path = os.path.join(temp_dir, f"{pair_name}_forecast.csv")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
|
| 219 |
+
# Create dummy data for now
|
| 220 |
+
pd.DataFrame({
|
| 221 |
+
'Date': pd.date_range(start=datetime.datetime.now(), periods=30),
|
| 222 |
+
'Predicted_Price': [1.0 + i*0.001 for i in range(30)]
|
| 223 |
+
}).to_csv(export_path, index=False)
|
| 224 |
|
| 225 |
+
return export_path
|
| 226 |
except Exception as e:
|
| 227 |
+
print(f"โ Export error: {str(e)}")
|
| 228 |
+
return None
|
|
|
|
| 229 |
|
| 230 |
+
# Initialize data
|
| 231 |
+
print("๐ Initializing data processing system...")
|
| 232 |
available_data = load_available_data()
|
| 233 |
+
print(f"๐ Available trading pairs: {list(available_data.keys())}")
|
| 234 |
|
| 235 |
+
# Create Gradio interface
|
| 236 |
+
with gr.Blocks(title="Trading Pair AI Analyzer") as demo:
|
| 237 |
+
gr.Markdown("# ๐ Trading Pair AI Analysis System")
|
| 238 |
|
| 239 |
with gr.Row():
|
| 240 |
+
data_status = gr.Textbox(
|
| 241 |
+
label="๐ Available Data",
|
| 242 |
+
value=get_available_pairs(),
|
| 243 |
+
interactive=False,
|
| 244 |
+
lines=5
|
| 245 |
+
)
|
| 246 |
+
system_info = gr.Textbox(
|
| 247 |
+
value=f"๐ Trading Analysis System v2.4\n๐ Last updated: {datetime.datetime.now().strftime('%Y-%m-%d %H:%M')}\n๐งฎ Loaded pairs: {len(available_data)}",
|
| 248 |
+
interactive=False,
|
| 249 |
+
lines=3
|
| 250 |
+
)
|
| 251 |
+
|
| 252 |
+
refresh_btn = gr.Button("๐ Refresh Data")
|
| 253 |
+
|
| 254 |
+
with gr.Row():
|
| 255 |
+
pair_input = gr.Textbox(
|
| 256 |
+
label="๐ Trading Pair to Analyze",
|
| 257 |
+
value=list(available_data.keys())[0] if available_data else "EURUSD",
|
| 258 |
+
placeholder="Enter pair name (e.g., EURUSD)"
|
| 259 |
+
)
|
| 260 |
+
analyze_btn = gr.Button("๐ Analyze Pair", variant="primary")
|
| 261 |
|
| 262 |
+
result_output = gr.Textbox(label="๐ Analysis Results", lines=6)
|
| 263 |
|
| 264 |
with gr.Tabs():
|
| 265 |
+
with gr.TabItem("๐ Price Chart"):
|
| 266 |
+
price_chart = gr.Plot(label="Price Chart with Moving Averages")
|
| 267 |
+
with gr.TabItem("๐ฎ Forecast Chart"):
|
| 268 |
+
forecast_chart = gr.Plot(label="30-Day Forecast")
|
| 269 |
+
with gr.TabItem("๐ Forecast Table"):
|
| 270 |
+
forecast_table = gr.DataFrame(
|
| 271 |
+
headers=["Date", "Predicted Price", "Lower Bound", "Upper Bound", "Trend"],
|
| 272 |
+
value=[],
|
| 273 |
+
label="30-Day Forecast Table"
|
| 274 |
+
)
|
| 275 |
+
|
| 276 |
+
export_btn = gr.Button("๐ฅ Export Forecast Data")
|
| 277 |
+
export_output = gr.File(label="Download Forecast CSV", visible=False)
|
| 278 |
+
|
| 279 |
+
# Event handlers - CORRECTED VERSION
|
| 280 |
+
analyze_btn.click(
|
| 281 |
+
fn=analyze_trading_pair,
|
| 282 |
+
inputs=pair_input,
|
| 283 |
+
outputs=[result_output, price_chart, forecast_chart, forecast_table]
|
| 284 |
+
)
|
| 285 |
+
|
| 286 |
+
refresh_btn.click(
|
| 287 |
+
fn=lambda: (get_available_pairs(), f"๐ Trading Analysis System v2.4\n๐ Last updated: {datetime.datetime.now().strftime('%Y-%m-%d %H:%M')}\n๐งฎ Loaded pairs: {len(load_available_data())}"),
|
| 288 |
+
inputs=[],
|
| 289 |
+
outputs=[data_status, system_info]
|
| 290 |
+
)
|
| 291 |
+
|
| 292 |
+
export_btn.click(
|
| 293 |
+
fn=export_forecast,
|
| 294 |
+
inputs=pair_input,
|
| 295 |
+
outputs=export_output
|
| 296 |
+
)
|
| 297 |
|
| 298 |
+
# Launch app
|
| 299 |
if __name__ == "__main__":
|
| 300 |
demo.launch(
|
| 301 |
server_name="0.0.0.0",
|
| 302 |
server_port=7860,
|
| 303 |
+
share=False
|
|
|
|
| 304 |
)
|