Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import yfinance as yf | |
| import pandas as pd | |
| import plotly.graph_objects as go | |
| from datetime import datetime | |
| # --- ALGORITHM STATE --- | |
| portfolio = {"balance": 100.0, "trades": []} | |
| def get_market_data(): | |
| df = yf.download("EURUSD=X", period="5d", interval="1h") | |
| # Flatten multi-index if necessary | |
| if isinstance(df.columns, pd.MultiIndex): | |
| df.columns = df.columns.get_level_values(0) | |
| return df.dropna() | |
| def run_trading_logic(): | |
| df = get_market_data() | |
| # 1. CALCULATE INDICATORS (The "Brain") | |
| df['SMA_Fast'] = df['Close'].rolling(window=10).mean() | |
| df['SMA_Slow'] = df['Close'].rolling(window=30).mean() | |
| current_price = df['Close'].iloc[-1] | |
| fast_ma = df['SMA_Fast'].iloc[-1] | |
| slow_ma = df['SMA_Slow'].iloc[-1] | |
| # 2. DECISION LOGIC (The "Algorithm") | |
| signal = "HOLD" | |
| # Check if we already traded today | |
| today = datetime.now().strftime("%Y-%m-%d") | |
| already_traded = any(t['Date'] == today for t in portfolio['trades']) | |
| if not already_traded: | |
| if fast_ma > slow_ma: | |
| signal = "BUY" | |
| elif fast_ma < slow_ma: | |
| signal = "SELL" | |
| # 3. EXECUTE TRADE (1:3 Risk Management) | |
| if signal != "HOLD": | |
| risk = 0.0020 # 20 Pips Stop Loss | |
| reward = 0.0060 # 60 Pips Take Profit | |
| trade = { | |
| "Date": today, | |
| "Action": signal, | |
| "Entry": round(current_price, 5), | |
| "SL": round(current_price - risk if signal == "BUY" else current_price + risk, 5), | |
| "TP": round(current_price + reward if signal == "BUY" else current_price - reward, 5), | |
| "Result": "PENDING" | |
| } | |
| portfolio['trades'].append(trade) | |
| # 4. VISUALS | |
| fig = go.Figure(data=[go.Candlestick(x=df.index, open=df['Open'], high=df['High'], low=df['Low'], close=df['Close'])]) | |
| fig.add_trace(go.Scatter(x=df.index, y=df['SMA_Fast'], name='Fast MA', line=dict(color='orange'))) | |
| fig.add_trace(go.Scatter(x=df.index, y=df['SMA_Slow'], name='Slow MA', line=dict(color='blue'))) | |
| fig.update_layout(template="plotly_dark", title="EUR/USD Algorithm Analysis") | |
| return fig, pd.DataFrame(portfolio['trades']), f"Current Balance: ${portfolio['balance']:.2f}" | |
| # --- GRADIO INTERFACE --- | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# ๐ EUR/USD Pure Algorithm (No-ML)") | |
| status = gr.Label(value="Analyzing Market...") | |
| with gr.Row(): | |
| chart = gr.Plot() | |
| history = gr.Dataframe(headers=["Date", "Action", "Entry", "SL", "TP", "Result"]) | |
| btn = gr.Button("Manual Refresh / Scan Market") | |
| btn.click(run_trading_logic, outputs=[chart, history, status]) | |
| # Optional Auto-refresh | |
| timer = gr.Timer(60) | |
| timer.tick(run_trading_logic, outputs=[chart, history, status]) | |
| demo.launch() |