Spaces:
Runtime error
Runtime error
Rename App.py to app.py
Browse files
App.py
DELETED
@@ -1,51 +0,0 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
from src.data_fetcher import fetch_crypto_data, fetch_stock_data, fetch_sentiment_data
|
3 |
-
from src.model import train_model, predict_growth
|
4 |
-
from src.visualizer import plot_price_trends, plot_sentiment_trends
|
5 |
-
|
6 |
-
def analyze_asset(asset_type, symbol):
|
7 |
-
"""
|
8 |
-
Analyze a stock or cryptocurrency symbol.
|
9 |
-
Fetches market data, performs sentiment analysis,
|
10 |
-
trains AI model, and provides predictions with visualizations.
|
11 |
-
"""
|
12 |
-
try:
|
13 |
-
if asset_type == "Crypto":
|
14 |
-
market_data = fetch_crypto_data(symbol)
|
15 |
-
elif asset_type == "Stock":
|
16 |
-
market_data = fetch_stock_data(symbol)
|
17 |
-
else:
|
18 |
-
return "Invalid asset type."
|
19 |
-
|
20 |
-
sentiment_score = fetch_sentiment_data(symbol)
|
21 |
-
train_model(market_data)
|
22 |
-
latest_data = market_data.iloc[-1][["close", "volume"]].values.tolist()
|
23 |
-
prediction = predict_growth(latest_data)
|
24 |
-
|
25 |
-
price_plot = plot_price_trends(market_data)
|
26 |
-
sentiment_plot = plot_sentiment_trends(symbol)
|
27 |
-
|
28 |
-
action = "BUY" if prediction == 1 else "SELL"
|
29 |
-
result = {
|
30 |
-
"Symbol": symbol,
|
31 |
-
"Sentiment Score": f"{sentiment_score:.2f}",
|
32 |
-
"Prediction": action,
|
33 |
-
"Visualizations": [price_plot, sentiment_plot],
|
34 |
-
}
|
35 |
-
return result
|
36 |
-
|
37 |
-
except Exception as e:
|
38 |
-
return {"Error": str(e)}
|
39 |
-
|
40 |
-
# Gradio Interface
|
41 |
-
asset_type_input = gr.Radio(["Crypto", "Stock"], label="Asset Type")
|
42 |
-
symbol_input = gr.Textbox(label="Enter Symbol (e.g., BTCUSDT or AAPL)")
|
43 |
-
output_result = gr.JSON(label="Analysis Results")
|
44 |
-
|
45 |
-
gr.Interface(
|
46 |
-
fn=analyze_asset,
|
47 |
-
inputs=[asset_type_input, symbol_input],
|
48 |
-
outputs=output_result,
|
49 |
-
title="Explosive Growth Bot",
|
50 |
-
description="Predicts explosive growth in stocks and cryptocurrencies using AI.",
|
51 |
-
).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.py
ADDED
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
import requests
|
4 |
+
from prophet import Prophet
|
5 |
+
import plotly.graph_objs as go
|
6 |
+
import math
|
7 |
+
import numpy as np
|
8 |
+
|
9 |
+
# Constants for API endpoints
|
10 |
+
OKX_TICKERS_ENDPOINT = "https://www.okx.com/api/v5/market/tickers?instType=SPOT"
|
11 |
+
OKX_CANDLE_ENDPOINT = "https://www.okx.com/api/v5/market/candles"
|
12 |
+
|
13 |
+
TIMEFRAME_MAPPING = {
|
14 |
+
"1m": "1m",
|
15 |
+
"5m": "5m",
|
16 |
+
"15m": "15m",
|
17 |
+
"30m": "30m",
|
18 |
+
"1h": "1H",
|
19 |
+
"2h": "2H",
|
20 |
+
"4h": "4H",
|
21 |
+
"6h": "6H",
|
22 |
+
"12h": "12H",
|
23 |
+
"1d": "1D",
|
24 |
+
"1w": "1W",
|
25 |
+
}
|
26 |
+
|
27 |
+
# Function to calculate technical indicators
|
28 |
+
def calculate_technical_indicators(df):
|
29 |
+
# RSI Calculation
|
30 |
+
delta = df['close'].diff()
|
31 |
+
gain = (delta.where(delta > 0, 0)).rolling(window=14).mean()
|
32 |
+
loss = (-delta.where(delta < 0, 0)).rolling(window=14).mean()
|
33 |
+
rs = gain / loss
|
34 |
+
df['RSI'] = 100 - (100 / (1 + rs))
|
35 |
+
|
36 |
+
# MACD Calculation
|
37 |
+
exp1 = df['close'].ewm(span=12, adjust=False).mean()
|
38 |
+
exp2 = df['close'].ewm(span=26, adjust=False).mean()
|
39 |
+
df['MACD'] = exp1 - exp2
|
40 |
+
df['Signal_Line'] = df['MACD'].ewm(span=9, adjust=False).mean()
|
41 |
+
|
42 |
+
# Bollinger Bands Calculation
|
43 |
+
df['MA20'] = df['close'].rolling(window=20).mean()
|
44 |
+
df['BB_upper'] = df['MA20'] + 2 * df['close'].rolling(window=20).std()
|
45 |
+
df['BB_lower'] = df['MA20'] - 2 * df['close'].rolling(window=20).std()
|
46 |
+
|
47 |
+
return df
|
48 |
+
|
49 |
+
# Function to create technical analysis charts
|
50 |
+
def create_technical_charts(df):
|
51 |
+
# Price and Bollinger Bands Chart
|
52 |
+
fig1 = go.Figure()
|
53 |
+
fig1.add_trace(go.Candlestick(
|
54 |
+
x=df['timestamp'],
|
55 |
+
open=df['open'],
|
56 |
+
high=df['high'],
|
57 |
+
low=df['low'],
|
58 |
+
close=df['close'],
|
59 |
+
name='Price'
|
60 |
+
))
|
61 |
+
fig1.add_trace(go.Scatter(x=df['timestamp'], y=df['BB_upper'], name='Upper BB', line=dict(color='gray', dash='dash')))
|
62 |
+
fig1.add_trace(go.Scatter(x=df['timestamp'], y=df['BB_lower'], name='Lower BB', line=dict(color='gray', dash='dash')))
|
63 |
+
fig1.update_layout(title='Price and Bollinger Bands', xaxis_title='Date', yaxis_title='Price')
|
64 |
+
|
65 |
+
# RSI Chart
|
66 |
+
fig2 = go.Figure()
|
67 |
+
fig2.add_trace(go.Scatter(x=df['timestamp'], y=df['RSI'], name='RSI'))
|
68 |
+
fig2.add_hline(y=70, line_dash="dash", line_color="red")
|
69 |
+
fig2.add_hline(y=30, line_dash="dash", line_color="green")
|
70 |
+
fig2.update_layout(title='RSI Indicator', xaxis_title='Date', yaxis_title='RSI')
|
71 |
+
|
72 |
+
# MACD Chart
|
73 |
+
fig3 = go.Figure()
|
74 |
+
fig3.add_trace(go.Scatter(x=df['timestamp'], y=df['MACD'], name='MACD'))
|
75 |
+
fig3.add_trace(go.Scatter(x=df['timestamp'], y=df['Signal_Line'], name='Signal Line'))
|
76 |
+
fig3.update_layout(title='MACD', xaxis_title='Date', yaxis_title='Value')
|
77 |
+
|
78 |
+
return fig1, fig2, fig3
|
79 |
+
|
80 |
+
# Fetch available symbols from OKX API
|
81 |
+
def fetch_okx_symbols():
|
82 |
+
try:
|
83 |
+
resp = requests.get(OKX_TICKERS_ENDPOINT)
|
84 |
+
data = resp.json().get("data", [])
|
85 |
+
symbols = [item["instId"] for item in data if item.get("instType") == "SPOT"]
|
86 |
+
return ["BTC-USDT"] + symbols if symbols else ["BTC-USDT"]
|
87 |
+
except Exception as e:
|
88 |
+
return ["BTC-USDT"]
|
89 |
+
|
90 |
+
# Fetch historical candle data from OKX API
|
91 |
+
def fetch_okx_candles(symbol, timeframe="1H", total=2000):
|
92 |
+
calls_needed = math.ceil(total / 300)
|
93 |
+
all_data = []
|
94 |
+
|
95 |
+
for _ in range(calls_needed):
|
96 |
+
params = {"instId": symbol, "bar": timeframe, "limit": 300}
|
97 |
+
resp = requests.get(OKX_CANDLE_ENDPOINT, params=params)
|
98 |
+
data = resp.json().get("data", [])
|
99 |
+
|
100 |
+
if not data:
|
101 |
+
break
|
102 |
+
|
103 |
+
columns = ["ts", "o", "h", "l", "c"]
|
104 |
+
df_chunk = pd.DataFrame(data, columns=columns)
|
105 |
+
df_chunk.rename(columns={"ts": "timestamp", "o": "open",
|
106 |
+
"h": "high", "l": "low",
|
107 |
+
"c": "close"}, inplace=True)
|
108 |
+
all_data.append(df_chunk)
|
109 |
+
|
110 |
+
if len(data) < 300:
|
111 |
+
break
|
112 |
+
|
113 |
+
if not all_data:
|
114 |
+
return pd.DataFrame()
|
115 |
+
|
116 |
+
df_all = pd.concat(all_data)
|
117 |
+
|
118 |
+
# Convert timestamps to datetime and calculate indicators
|
119 |
+
...
|