File size: 1,926 Bytes
909c182
462c221
 
909c182
462c221
 
 
 
 
 
 
 
 
 
 
 
909c182
 
462c221
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
909c182
462c221
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# app.py
import pandas as pd
import yfinance as yf
import pandas_ta as ta
import gradio as gr
import logging

logging.basicConfig(level=logging.INFO)

class SimpleTrader:
    def fetch_data(self, symbol, timeframe="1h", limit=100):
        try:
            df = yf.download(symbol, period="1mo", interval=timeframe)
            df = df[['Open', 'High', 'Low', 'Close', 'Volume']].rename(columns={
                'Open': 'open', 'High': 'high', 'Low': 'low', 'Close': 'close', 'Volume': 'tick_volume'
            })
            df['rsi'] = ta.rsi(df['close'], length=14)  # محاسبه RSI با pandas-ta
            df['ma20'] = ta.sma(df['close'], length=20)  # محاسبه میانگین متحرک با pandas-ta
            return df.tail(limit)
        except Exception as e:
            logging.error(f"Error fetching data: {e}")
            return pd.DataFrame()

    def predict(self, symbol):
        df = self.fetch_data(symbol)
        if df.empty:
            return "No data", 0, df
        last_price = df['close'].iloc[-1]
        rsi = df['rsi'].iloc[-1]
        ma20 = df['ma20'].iloc[-1]
        signal = "BUY" if rsi < 30 and last_price > ma20 else "SELL" if rsi > 70 else "HOLD"
        return signal, last_price, df

def run_trader(symbol):
    trader = SimpleTrader()
    signal, price, df = trader.predict(symbol)
    if df.empty:
        return "Error: Could not fetch data", None
    fig = df['close'].plot(title=f"{symbol} Price (Signal: {signal})").figure
    return f"Price: {price:.2f}\nSignal: {signal}", fig

interface = gr.Interface(
    fn=run_trader,
    inputs=gr.Textbox(label="Symbol (e.g., XAUUSD=X)", value="XAUUSD=X"),  # تغییر default به value
    outputs=[gr.Textbox(label="Prediction"), gr.Plot(label="Chart")],
    title="OmniTrader v11.0 - Simplified (No Pretrained Model)",
    description="A simple trading tool using RSI and MA20 for predictions."
)
interface.launch()