Spaces:
Running
Running
Update app/arbitrage_analyzer.py
Browse files- app/arbitrage_analyzer.py +14 -18
app/arbitrage_analyzer.py
CHANGED
@@ -1,13 +1,13 @@
|
|
1 |
"""
|
2 |
-
The
|
3 |
|
4 |
-
Uses Gemini to provide risk assessment
|
5 |
-
|
6 |
"""
|
7 |
import os
|
8 |
import logging
|
9 |
from typing import Optional, Dict
|
10 |
-
|
11 |
import httpx
|
12 |
|
13 |
logger = logging.getLogger(__name__)
|
@@ -18,8 +18,7 @@ class ArbitrageAnalyzer:
|
|
18 |
def __init__(self, client: httpx.AsyncClient, api_key: Optional[str] = None):
|
19 |
self.client = client
|
20 |
self.api_key = api_key or os.getenv("GEMINI_API_KEY")
|
21 |
-
if not self.api_key:
|
22 |
-
raise ValueError("GEMINI_API_KEY is not set.")
|
23 |
self.params = {"key": self.api_key}
|
24 |
self.headers = {"Content-Type": "application/json"}
|
25 |
|
@@ -28,21 +27,19 @@ class ArbitrageAnalyzer:
|
|
28 |
"contents": [{
|
29 |
"parts": [{
|
30 |
"text": f"""
|
31 |
-
You are a
|
32 |
-
An arbitrage opportunity has been detected for Bitcoin (BTC).
|
33 |
Provide a concise "Alpha Briefing" as a single, minified JSON object with NO markdown formatting.
|
34 |
|
35 |
-
The JSON object must have these exact keys: "risk", "strategy", "
|
36 |
|
37 |
-
- "risk": Assess the execution risk. MUST be one of "LOW", "MEDIUM", "HIGH". Consider
|
38 |
-
- "strategy": A very brief, one-sentence action plan.
|
39 |
-
- "
|
40 |
|
41 |
-
|
42 |
-
-
|
43 |
-
-
|
44 |
-
-
|
45 |
-
- Sell Price: ${opportunity['sell_price']:,.2f}
|
46 |
"""
|
47 |
}]
|
48 |
}]
|
@@ -54,7 +51,6 @@ class ArbitrageAnalyzer:
|
|
54 |
response = await self.client.post(self.API_URL, json=prompt, params=self.params, headers=self.headers, timeout=20)
|
55 |
response.raise_for_status()
|
56 |
content = response.json()["candidates"][0]["content"]["parts"][0]["text"]
|
57 |
-
# A simple trick to remove markdown ```json ... ``` wrappers
|
58 |
if content.startswith("```"):
|
59 |
content = content.strip("```json\n")
|
60 |
return json.loads(content)
|
|
|
1 |
"""
|
2 |
+
The Discrepancy Analyzer Engine.
|
3 |
|
4 |
+
Uses Gemini to provide risk assessment for on-chain vs. off-chain
|
5 |
+
price discrepancies.
|
6 |
"""
|
7 |
import os
|
8 |
import logging
|
9 |
from typing import Optional, Dict
|
10 |
+
import json
|
11 |
import httpx
|
12 |
|
13 |
logger = logging.getLogger(__name__)
|
|
|
18 |
def __init__(self, client: httpx.AsyncClient, api_key: Optional[str] = None):
|
19 |
self.client = client
|
20 |
self.api_key = api_key or os.getenv("GEMINI_API_KEY")
|
21 |
+
if not self.api_key: raise ValueError("GEMINI_API_KEY not set.")
|
|
|
22 |
self.params = {"key": self.api_key}
|
23 |
self.headers = {"Content-Type": "application/json"}
|
24 |
|
|
|
27 |
"contents": [{
|
28 |
"parts": [{
|
29 |
"text": f"""
|
30 |
+
You are a DeFi analyst. A significant price discrepancy for Bitcoin (BTC) has been detected between a decentralized on-chain oracle and a centralized off-chain aggregator.
|
|
|
31 |
Provide a concise "Alpha Briefing" as a single, minified JSON object with NO markdown formatting.
|
32 |
|
33 |
+
The JSON object must have these exact keys: "risk", "strategy", "rationale".
|
34 |
|
35 |
+
- "risk": Assess the execution risk. MUST be one of "LOW", "MEDIUM", "HIGH". Consider oracle latency, network congestion (gas fees), and DEX slippage.
|
36 |
+
- "strategy": A very brief, one-sentence action plan. For example: "Consider buying BTC on a DEX like Uniswap and selling on a CEX." or "Monitor situation, high risk of slippage."
|
37 |
+
- "rationale": A short explanation for the risk assessment.
|
38 |
|
39 |
+
DISCREPANCY DETAILS:
|
40 |
+
- On-Chain Price (Pyth): ${opportunity['on_chain_price']:,.2f}
|
41 |
+
- Off-Chain Agg. Price (CeFi): ${opportunity['off_chain_price']:,.2f}
|
42 |
+
- Discrepancy: {opportunity['spread_pct']:.3f}%
|
|
|
43 |
"""
|
44 |
}]
|
45 |
}]
|
|
|
51 |
response = await self.client.post(self.API_URL, json=prompt, params=self.params, headers=self.headers, timeout=20)
|
52 |
response.raise_for_status()
|
53 |
content = response.json()["candidates"][0]["content"]["parts"][0]["text"]
|
|
|
54 |
if content.startswith("```"):
|
55 |
content = content.strip("```json\n")
|
56 |
return json.loads(content)
|