Spaces:
Sleeping

File size: 10,202 Bytes
fef93db
 
775c66a
fef93db
 
 
 
 
34d6698
fef93db
 
 
775c66a
fef93db
 
 
a2fb637
fef93db
775c66a
 
 
 
34d6698
fef93db
 
 
 
 
 
 
 
 
 
 
775c66a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fef93db
775c66a
 
fef93db
775c66a
 
 
 
 
 
 
 
 
 
 
 
 
fef93db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34d6698
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fef93db
775c66a
 
 
 
 
fef93db
 
775c66a
fef93db
 
 
 
 
519f9fc
 
 
 
775c66a
fef93db
 
 
 
0d16c2c
 
 
fef93db
 
 
 
 
 
 
 
 
34d6698
 
 
fef93db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34d6698
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fef93db
 
 
 
c8c0efc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
775c66a
 
 
 
 
 
 
 
c8c0efc
 
 
 
 
 
 
 
775c66a
c8c0efc
 
 
 
 
 
 
 
 
 
 
 
fef93db
 
0d16c2c
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# core/analytics.py
import pandas as pd
import ta
import sqlite3
import json
import sys
import os
from datetime import datetime, timezone, timedelta
import numpy as np

# Add project root to the Python path
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
from config import DB_PATH, TOKENS, SOURCES

# --- CONFIGURATION ---
DATA_LOOKBACK_HOURS = 72
ARBITRAGE_THRESHOLD = 0.001  # 0.1%

# --- Anomaly Detection Configuration ---
ANOMALY_ZSCORE_WINDOW = 24
ANOMALY_ZSCORE_THRESHOLD = 3.0
ANOMALY_PRICE_JUMP_THRESHOLD = 0.05

# --- DATABASE HELPERS ---
def get_db_connection():
    """Establishes a connection to the SQLite database."""
    conn = sqlite3.connect(DB_PATH)
    conn.row_factory = sqlite3.Row
    return conn

def get_token_id(conn, symbol):
    """Fetches the ID for a given token symbol."""
    return conn.execute("SELECT id FROM tokens WHERE symbol = ?", (symbol,)).fetchone()['id']

# --- MODIFIED: This function now handles bulk insertion ---
def insert_indicators(conn, token_id, df_indicators: pd.DataFrame):
    """
    Efficiently bulk-inserts a DataFrame of indicator data into the database.
    This function now expects a DataFrame, not a dictionary.
    """
    if df_indicators.empty:
        return

    # Prepare the DataFrame for SQL insertion
    df_to_insert = df_indicators[['timestamp', 'SMA_10', 'SMA_30', 'EMA_14', 'RSI_14', 'VWAP_24h', 'Realized_Vol_30D']].copy()
    df_to_insert.rename(columns={
        'SMA_10': 'sma10',
        'SMA_30': 'sma30',
        'EMA_14': 'ema',
        'RSI_14': 'rsi14',
        'VWAP_24h': 'vwap24h',
        'Realized_Vol_30D': 'realized_vol'
    }, inplace=True)

    df_to_insert['token_id'] = token_id

    # Drop rows where essential indicators are all NaN
    df_to_insert.dropna(subset=['sma10', 'sma30', 'ema', 'rsi14'], how='all', inplace=True)

    if df_to_insert.empty:
        return

    # Use a temporary table for efficient upserting
    df_to_insert.to_sql('temp_indicators', conn, if_exists='replace', index=False)

    cursor = conn.cursor()

    # Use INSERT OR REPLACE to avoid duplicates and update existing rows
    cursor.execute("""
        INSERT OR REPLACE INTO indicators (token_id, timestamp, sma10, sma30, ema, rsi14, vwap24h, realized_vol)
        SELECT
            t.token_id,
            t.timestamp,
            t.sma10,
            t.sma30,
            t.ema,
            t.rsi14,
            t.vwap24h,
            t.realized_vol
        FROM temp_indicators t;
    """)

    conn.commit()

def insert_arbitrage(conn, token_id, opp):
    """Inserts a detected arbitrage opportunity into the database."""
    cursor = conn.cursor()
    cursor.execute("""
        INSERT INTO arbitrage (token_id, source_a, source_b, price_diff, percent_diff, raw_data)
        VALUES (?, ?, ?, ?, ?, ?)
    """, (
        token_id,
        opp['source_a'],
        opp['source_b'],
        opp['price_diff'],
        opp['percent_diff'],
        json.dumps(opp)
    ))
    conn.commit()

def insert_anomaly(conn, token_id, anomaly):
    """Inserts a detected anomaly into the database."""
    cursor = conn.cursor()
    cursor.execute("""
        INSERT INTO anomalies (token_id, anomaly_type, value, description, raw_data)
        VALUES (?, ?, ?, ?, ?)
    """, (
        token_id,
        anomaly['anomaly_type'],
        anomaly['value'],
        anomaly['description'],
        json.dumps(anomaly)
    ))
    conn.commit()

# --- ANALYTICS FUNCTIONS ---
# --- MODIFIED: This function now returns the full DataFrame ---
def calculate_technical_indicators(df: pd.DataFrame) -> pd.DataFrame:
    """
    Calculates a full set of technical indicators and returns the entire DataFrame.
    """
    if df.empty or len(df) < 30:
        print("Warning: Not enough data points to calculate all indicators.")
        return pd.DataFrame()

    df['SMA_10'] = ta.trend.sma_indicator(df['close'], window=10)
    df['SMA_30'] = ta.trend.sma_indicator(df['close'], window=30)
    df['EMA_14'] = ta.trend.ema_indicator(df['close'], window=14)
    df['RSI_14'] = ta.momentum.rsi(df['close'], window=14)
    df['VWAP_24h'] = (df['close'] * df['volume_24h']).rolling(window=24, min_periods=1).sum() / df['volume_24h'].rolling(window=24, min_periods=1).sum()
    df['log_returns'] = np.log(df['close'] / df['close'].shift(1))
    df['Realized_Vol_30D'] = df['log_returns'].rolling(window=30, min_periods=2).std() * np.sqrt(365)

    return df

def detect_arbitrage(df: pd.DataFrame) -> list:
    """Detects arbitrage opportunities from the latest prices across different sources."""
    opportunities = []
    exchange_names = [name for name, props in SOURCES.items() if props.get('is_exchange', False)]
    df_exchanges = df[df['source'].isin(exchange_names)]
    latest = df_exchanges.sort_values('timestamp').drop_duplicates('source', keep='last')

    if len(latest) < 2:
        return []

    import itertools
    for (idx_a, row_a), (idx_b, row_b) in itertools.combinations(latest.iterrows(), 2):
        price_a = row_a['price_usd']
        price_b = row_b['price_usd']

        if price_a is None or price_b is None or price_a == 0 or price_b == 0:
            continue

        price_diff = abs(price_a - price_b)
        percent_diff = price_diff / min(price_a, price_b)

        if percent_diff >= ARBITRAGE_THRESHOLD:
            if price_a < price_b:
                buy_source, sell_source = row_a['source'], row_b['source']
                buy_price, sell_price = price_a, price_b
            else:
                buy_source, sell_source = row_b['source'], row_a['source']
                buy_price, sell_price = price_b, price_a

            opportunities.append({
                "source_a": f"Buy at {buy_source}",
                "source_b": f"Sell at {sell_source}",
                "price_diff": sell_price - buy_price,
                "percent_diff": percent_diff * 100,
                "buy_price": buy_price,
                "sell_price": sell_price,
            })
    return opportunities

def detect_anomalies(df: pd.DataFrame) -> list:
    """Detects volume spikes and price jumps from time-series data."""
    anomalies = []
    df = df.sort_values('timestamp').reset_index(drop=True)

    if df.empty:
        return []

    # Volume Spike Detection (Z-score)
    if 'volume_24h' in df.columns and len(df) >= ANOMALY_ZSCORE_WINDOW:
        rolling_window = df['volume_24h'].iloc[-ANOMALY_ZSCORE_WINDOW:]
        mean = rolling_window.mean()
        std = rolling_window.std()

        if std > 0:
            latest_volume = rolling_window.iloc[-1]
            z_score = (latest_volume - mean) / std
            if abs(z_score) >= ANOMALY_ZSCORE_THRESHOLD:
                anomalies.append({
                    "anomaly_type": "Volume Spike",
                    "value": z_score,
                    "description": f"Volume Z-score of {z_score:.2f} exceeds threshold ({ANOMALY_ZSCORE_THRESHOLD}). Current Volume: {latest_volume:,.2f}",
                })

    # Price Jump Detection (% change)
    if 'close' in df.columns and len(df) >= 2:
        df['price_pct_change'] = df['close'].pct_change()
        latest_change = df['price_pct_change'].iloc[-1]

        if pd.notna(latest_change) and abs(latest_change) >= ANOMALY_PRICE_JUMP_THRESHOLD:
            anomalies.append({
                "anomaly_type": "Price Jump",
                "value": latest_change * 100,
                "description": f"Price changed by {latest_change:.2%}, exceeding threshold of {ANOMALY_PRICE_JUMP_THRESHOLD:.2%}.",
            })

    return anomalies

# --- MAIN RUNNER ---
def run_analytics():
    """Main function to run all analytics for all configured tokens."""
    print("Starting analytics engine...")
    conn = None
    try:
        conn = get_db_connection()
        for token_config in TOKENS:
            symbol = token_config['symbol']
            print(f"--- Processing analytics for {symbol} ---")
            try:
                token_id = get_token_id(conn, symbol)
                lookback_time = (datetime.now(timezone.utc) - timedelta(hours=DATA_LOOKBACK_HOURS)).isoformat()
                query = "SELECT * FROM prices WHERE token_id = ? AND timestamp >= ? ORDER BY timestamp ASC"
                df = pd.read_sql_query(query, conn, params=(token_id, lookback_time))

                if df.empty:
                    print(f"No recent price data found for {symbol}. Skipping.")
                    continue

                df_original = df.copy()
                df.rename(columns={'price_usd': 'close'}, inplace=True)

                # --- MODIFIED: Technical Indicators Workflow ---
                df_with_indicators = calculate_technical_indicators(df.copy())
                if not df_with_indicators.empty:
                    # Clear old indicators and bulk insert the full new set
                    conn.execute("DELETE FROM indicators WHERE token_id = ?", (token_id,))
                    insert_indicators(conn, token_id, df_with_indicators)
                    print(f"Successfully calculated and stored {len(df_with_indicators)} indicator rows for {symbol}.")
                # --- End of Modified Section ---

                # Arbitrage (use the original dataframe with 'price_usd')
                arbitrage_opps = detect_arbitrage(df_original)
                if arbitrage_opps:
                    for opp in arbitrage_opps:
                        insert_arbitrage(conn, token_id, opp)
                    print(f"Found and stored {len(arbitrage_opps)} arbitrage opportunities for {symbol}.")

                # Anomalies (use the dataframe with 'close')
                anomalies_found = detect_anomalies(df.copy())
                if anomalies_found:
                    for anomaly in anomalies_found:
                        insert_anomaly(conn, token_id, anomaly)
                    print(f"Found and stored {len(anomalies_found)} anomalies for {symbol}.")

            except Exception as e:
                print(f"An error occurred while processing {symbol}: {e}")
    finally:
        if conn:
            conn.close()
        print("\nAnalytics engine run complete.")

if __name__ == "__main__":
    run_analytics()