0vergeared commited on
Commit
7614470
Β·
verified Β·
1 Parent(s): 684b72e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -64
app.py CHANGED
@@ -3,96 +3,115 @@ import pandas as pd
3
  import matplotlib.pyplot as plt
4
  import matplotlib.dates as mdates
5
  from io import BytesIO
6
- import gradio as gr
7
- from PIL import Image
8
  from PIL import Image
 
9
 
10
- # Fetch MEXC OHLCV data
11
- def fetch_ohlcv(symbol='BTC/USDT', timeframe='1h', limit=100):
12
  exchange = ccxt.mexc()
13
  ohlcv = exchange.fetch_ohlcv(symbol, timeframe=timeframe, limit=limit)
14
  df = pd.DataFrame(ohlcv, columns=['timestamp', 'open', 'high', 'low', 'close', 'volume'])
15
  df['timestamp'] = pd.to_datetime(df['timestamp'], unit='ms')
16
  return df
17
 
18
- # Calculate indicators
19
- def calculate_rsi(df, period=14):
20
- delta = df['close'].diff()
21
- gain = delta.clip(lower=0)
22
- loss = -delta.clip(upper=0)
23
-
24
- avg_gain = gain.rolling(window=period).mean()
25
- avg_loss = loss.rolling(window=period).mean()
26
-
27
- rs = avg_gain / avg_loss
28
- rsi = 100 - (100 / (1 + rs))
29
- return rsi
30
-
31
- def calculate_macd(df):
32
- ema12 = df['close'].ewm(span=12, adjust=False).mean()
33
- ema26 = df['close'].ewm(span=26, adjust=False).mean()
34
- macd = ema12 - ema26
35
- signal = macd.ewm(span=9, adjust=False).mean()
36
- return macd, signal
37
-
38
- def generate_signal(df):
39
- rsi = calculate_rsi(df)
40
- macd, signal = calculate_macd(df)
41
-
42
- if len(rsi) < 1 or len(macd) < 1 or len(signal) < 1:
43
- return "Not enough data"
44
-
45
- latest_rsi = rsi.iloc[-1]
46
- latest_macd = macd.iloc[-1]
47
- latest_signal = signal.iloc[-1]
48
-
49
- if latest_rsi < 30 and latest_macd > latest_signal:
50
- return "BUY"
51
- elif latest_rsi > 70 and latest_macd < latest_signal:
52
- return "SELL"
 
 
 
 
 
 
 
53
  else:
54
- return "HOLD"
55
 
56
- # Plot candlestick chart
57
- def plot_candlestick(df):
58
- df = df.tail(50).copy()
59
  df['timestamp'] = pd.to_datetime(df['timestamp'])
60
 
61
- fig, ax = plt.subplots(figsize=(8, 4))
 
 
 
 
62
 
 
63
  for _, row in df.iterrows():
64
  color = 'green' if row['close'] >= row['open'] else 'red'
65
- ax.plot([row['timestamp'], row['timestamp']], [row['low'], row['high']], color='black')
66
- ax.plot([row['timestamp'], row['timestamp']], [row['open'], row['close']], color=color, linewidth=5)
67
-
68
- ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))
69
- ax.xaxis.set_major_locator(mdates.AutoDateLocator())
 
 
 
 
 
 
 
 
 
 
 
70
  plt.xticks(rotation=45)
71
- plt.title('Candlestick Chart (MEXC)')
72
  plt.tight_layout()
73
 
74
- # Save chart to image object
75
  buf = BytesIO()
76
  plt.savefig(buf, format='png')
77
  buf.seek(0)
78
  plt.close(fig)
79
  return Image.open(buf)
80
 
81
-
82
- # Main function
83
  def analyze(pair, timeframe):
84
  try:
85
- df = fetch_ohlcv(symbol=pair, timeframe=timeframe)
86
  if df.empty:
87
- return "❌ No data returned from MEXC", None
88
-
89
- signal = generate_signal(df)
90
- chart = plot_candlestick(df)
91
- return f"πŸ“Š Signal: {signal}", chart
92
  except Exception as e:
93
  return f"❌ Error: {str(e)}", None
94
 
95
- # Gradio UI
96
  pairs = ['BTC/USDT', 'ETH/USDT', 'MX/USDT', 'SOL/USDT']
97
  timeframes = ['1m', '5m', '15m', '1h', '4h']
98
 
@@ -103,9 +122,9 @@ gr.Interface(
103
  gr.Dropdown(choices=timeframes, label="Timeframe")
104
  ],
105
  outputs=[
106
- gr.Text(label="Trading Signal"),
107
- gr.Image(label="Candlestick Chart")
108
  ],
109
- title="🧠 Crypto Signal Generator (MEXC)",
110
- description="Live BUY / SELL / HOLD signals based on RSI & MACD indicators using MEXC data."
111
  ).launch()
 
3
  import matplotlib.pyplot as plt
4
  import matplotlib.dates as mdates
5
  from io import BytesIO
 
 
6
  from PIL import Image
7
+ import gradio as gr
8
 
9
+ # πŸ“Š Fetch data from MEXC
10
+ def fetch_ohlcv(symbol='BTC/USDT', timeframe='1h', limit=150):
11
  exchange = ccxt.mexc()
12
  ohlcv = exchange.fetch_ohlcv(symbol, timeframe=timeframe, limit=limit)
13
  df = pd.DataFrame(ohlcv, columns=['timestamp', 'open', 'high', 'low', 'close', 'volume'])
14
  df['timestamp'] = pd.to_datetime(df['timestamp'], unit='ms')
15
  return df
16
 
17
+ # πŸ“ˆ Calculate indicators
18
+ def calculate_ema(df, span):
19
+ return df['close'].ewm(span=span, adjust=False).mean()
20
+
21
+ def calculate_supertrend(df, period=10, multiplier=3):
22
+ atr = df['high'].rolling(period).max() - df['low'].rolling(period).min()
23
+ atr = atr.rolling(period).mean()
24
+
25
+ hl2 = (df['high'] + df['low']) / 2
26
+ upperband = hl2 + multiplier * atr
27
+ lowerband = hl2 - multiplier * atr
28
+
29
+ supertrend = [True] # True for bullish, False for bearish
30
+ for i in range(1, len(df)):
31
+ if df['close'][i] > upperband[i - 1]:
32
+ supertrend.append(True)
33
+ elif df['close'][i] < lowerband[i - 1]:
34
+ supertrend.append(False)
35
+ else:
36
+ supertrend.append(supertrend[i - 1])
37
+ return supertrend
38
+
39
+ # πŸ” Generate signal logic
40
+ def generate_combined_signal(df):
41
+ df['ema_50'] = calculate_ema(df, 50)
42
+ df['ema_200'] = calculate_ema(df, 200)
43
+ df['supertrend'] = calculate_supertrend(df)
44
+
45
+ ema_signal = None
46
+ if df['ema_50'].iloc[-1] > df['ema_200'].iloc[-1]:
47
+ ema_signal = 'BULLISH'
48
+ elif df['ema_50'].iloc[-1] < df['ema_200'].iloc[-1]:
49
+ ema_signal = 'BEARISH'
50
+
51
+ supertrend_signal = 'BULLISH' if df['supertrend'].iloc[-1] else 'BEARISH'
52
+
53
+ if ema_signal == 'BULLISH' and supertrend_signal == 'BULLISH':
54
+ return "πŸ”₯ STRONG BUY"
55
+ elif ema_signal == 'BEARISH' and supertrend_signal == 'BEARISH':
56
+ return "πŸ’€ STRONG SELL"
57
+ elif ema_signal == supertrend_signal:
58
+ return f"⚠️ {ema_signal}"
59
  else:
60
+ return "πŸ€” NEUTRAL / HOLD"
61
 
62
+ # πŸ“Š Candlestick + indicator chart
63
+ def plot_chart(df):
64
+ df = df.copy().tail(100)
65
  df['timestamp'] = pd.to_datetime(df['timestamp'])
66
 
67
+ df['ema_50'] = calculate_ema(df, 50)
68
+ df['ema_200'] = calculate_ema(df, 200)
69
+ df['supertrend'] = calculate_supertrend(df)
70
+
71
+ fig, ax = plt.subplots(figsize=(10, 5))
72
 
73
+ # Plot candles
74
  for _, row in df.iterrows():
75
  color = 'green' if row['close'] >= row['open'] else 'red'
76
+ ax.plot([row['timestamp'], row['timestamp']], [row['low'], row['high']], color='black', linewidth=1)
77
+ ax.plot([row['timestamp'], row['timestamp']], [row['open'], row['close']], color=color, linewidth=4)
78
+
79
+ # Plot EMAs
80
+ ax.plot(df['timestamp'], df['ema_50'], label='EMA 50', color='orange', linewidth=1.5)
81
+ ax.plot(df['timestamp'], df['ema_200'], label='EMA 200', color='purple', linewidth=1.5)
82
+
83
+ # Plot Supertrend as dots
84
+ for i in range(1, len(df)):
85
+ if df['supertrend'].iloc[i]:
86
+ ax.scatter(df['timestamp'].iloc[i], df['low'].iloc[i] - 0.5, color='green', marker='^', label='Buy' if i == 1 else "", s=60)
87
+ else:
88
+ ax.scatter(df['timestamp'].iloc[i], df['high'].iloc[i] + 0.5, color='red', marker='v', label='Sell' if i == 1 else "", s=60)
89
+
90
+ ax.xaxis.set_major_formatter(mdates.DateFormatter('%m-%d %H:%M'))
91
+ ax.legend()
92
  plt.xticks(rotation=45)
93
+ plt.title("Crypto Chart with LuxAlgo-Inspired Signals")
94
  plt.tight_layout()
95
 
 
96
  buf = BytesIO()
97
  plt.savefig(buf, format='png')
98
  buf.seek(0)
99
  plt.close(fig)
100
  return Image.open(buf)
101
 
102
+ # πŸ” Main analysis
 
103
  def analyze(pair, timeframe):
104
  try:
105
+ df = fetch_ohlcv(pair, timeframe)
106
  if df.empty:
107
+ return "❌ No data available", None
108
+ signal = generate_combined_signal(df)
109
+ chart = plot_chart(df)
110
+ return signal, chart
 
111
  except Exception as e:
112
  return f"❌ Error: {str(e)}", None
113
 
114
+ # 🌐 Gradio UI
115
  pairs = ['BTC/USDT', 'ETH/USDT', 'MX/USDT', 'SOL/USDT']
116
  timeframes = ['1m', '5m', '15m', '1h', '4h']
117
 
 
122
  gr.Dropdown(choices=timeframes, label="Timeframe")
123
  ],
124
  outputs=[
125
+ gr.Text(label="LuxAlgo-Inspired Signal"),
126
+ gr.Image(label="Chart with Indicators")
127
  ],
128
+ title="πŸ’‘ Crypto Signal Generator (Inspired by LuxAlgo)",
129
+ description="Signals based on EMA Cross & SuperTrend, visualized with candlestick chart and arrows."
130
  ).launch()