PERDYPTO commited on
Upload 7 files
Browse files- models.py +30 -12
- reasoning_client.py +56 -27
models.py
CHANGED
|
@@ -1,6 +1,17 @@
|
|
| 1 |
-
from datetime import datetime
|
| 2 |
-
from typing import Optional
|
| 3 |
-
from pydantic import BaseModel, ConfigDict
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
|
| 6 |
class TAAnalysisResult(BaseModel):
|
|
@@ -11,10 +22,15 @@ class TAAnalysisResult(BaseModel):
|
|
| 11 |
trend: str
|
| 12 |
confidence: float
|
| 13 |
analysis_summary: str
|
| 14 |
-
ai_reasoning: str
|
| 15 |
-
rsi_signal: str
|
| 16 |
-
macd_signal_dir: str
|
| 17 |
-
price_vs_bb: str
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
|
| 20 |
class NewsSummary(BaseModel):
|
|
@@ -29,17 +45,19 @@ class NewsSummary(BaseModel):
|
|
| 29 |
summary: str
|
| 30 |
|
| 31 |
|
| 32 |
-
class ReasoningRequest(BaseModel):
|
| 33 |
-
model_config = ConfigDict(from_attributes=True)
|
| 34 |
|
| 35 |
coin_id: str
|
| 36 |
symbol: str
|
| 37 |
name: str
|
| 38 |
current_price: float
|
| 39 |
price_change_24h: float
|
| 40 |
-
volume_24h: float
|
| 41 |
-
ta_result: TAAnalysisResult
|
| 42 |
-
news_summary: NewsSummary
|
|
|
|
|
|
|
| 43 |
|
| 44 |
|
| 45 |
class PredictionOutput(BaseModel):
|
|
|
|
| 1 |
+
from datetime import datetime
|
| 2 |
+
from typing import Optional, List
|
| 3 |
+
from pydantic import BaseModel, ConfigDict, Field
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class CompactCandle(BaseModel):
|
| 7 |
+
model_config = ConfigDict(from_attributes=True)
|
| 8 |
+
|
| 9 |
+
t: str
|
| 10 |
+
o: float
|
| 11 |
+
h: float
|
| 12 |
+
l: float
|
| 13 |
+
c: float
|
| 14 |
+
v: float
|
| 15 |
|
| 16 |
|
| 17 |
class TAAnalysisResult(BaseModel):
|
|
|
|
| 22 |
trend: str
|
| 23 |
confidence: float
|
| 24 |
analysis_summary: str
|
| 25 |
+
ai_reasoning: str
|
| 26 |
+
rsi_signal: str
|
| 27 |
+
macd_signal_dir: str
|
| 28 |
+
price_vs_bb: str
|
| 29 |
+
higher_tf_trend: Optional[str] = None
|
| 30 |
+
higher_tf_confidence: Optional[float] = None
|
| 31 |
+
higher_tf_rsi_signal: Optional[str] = None
|
| 32 |
+
higher_tf_macd_signal_dir: Optional[str] = None
|
| 33 |
+
higher_tf_price_vs_bb: Optional[str] = None
|
| 34 |
|
| 35 |
|
| 36 |
class NewsSummary(BaseModel):
|
|
|
|
| 45 |
summary: str
|
| 46 |
|
| 47 |
|
| 48 |
+
class ReasoningRequest(BaseModel):
|
| 49 |
+
model_config = ConfigDict(from_attributes=True)
|
| 50 |
|
| 51 |
coin_id: str
|
| 52 |
symbol: str
|
| 53 |
name: str
|
| 54 |
current_price: float
|
| 55 |
price_change_24h: float
|
| 56 |
+
volume_24h: float
|
| 57 |
+
ta_result: TAAnalysisResult
|
| 58 |
+
news_summary: NewsSummary
|
| 59 |
+
raw_candles_1m: List[CompactCandle] = Field(default_factory=list)
|
| 60 |
+
raw_candles_1h: List[CompactCandle] = Field(default_factory=list)
|
| 61 |
|
| 62 |
|
| 63 |
class PredictionOutput(BaseModel):
|
reasoning_client.py
CHANGED
|
@@ -45,38 +45,67 @@ def is_rate_limit_error(exc: Exception) -> bool:
|
|
| 45 |
|
| 46 |
|
| 47 |
def _build_user_message(req: ReasoningRequest) -> str:
|
| 48 |
-
ta = req.ta_result
|
| 49 |
-
news = req.news_summary
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
Bearish Articles: {news.bearish_count}
|
| 67 |
Neutral Articles: {news.neutral_count}
|
| 68 |
News Summary: {news.summary}"""
|
| 69 |
|
| 70 |
|
| 71 |
-
def _strip_markdown(text: str) -> str:
|
| 72 |
-
"""Remove markdown code fences from response."""
|
| 73 |
-
text = text.strip()
|
| 74 |
-
text = re.sub(r'^```json\s*', '', text, flags=re.MULTILINE)
|
| 75 |
-
text = re.sub(r'^```\s*', '', text, flags=re.MULTILINE)
|
| 76 |
-
text = re.sub(r'```$', '', text, flags=re.MULTILINE)
|
| 77 |
-
return text.strip()
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
async def get_prediction(request: ReasoningRequest, client: AsyncOpenAI) -> PredictionOutput:
|
| 81 |
"""Call NVIDIA NIM DeepSeek V4 Pro via AsyncOpenAI with streaming for structured price prediction."""
|
| 82 |
if not client.api_key:
|
|
|
|
| 45 |
|
| 46 |
|
| 47 |
def _build_user_message(req: ReasoningRequest) -> str:
|
| 48 |
+
ta = req.ta_result
|
| 49 |
+
news = req.news_summary
|
| 50 |
+
higher_tf_text = ""
|
| 51 |
+
if ta.higher_tf_trend:
|
| 52 |
+
higher_tf_text = f"""
|
| 53 |
+
Higher Timeframe TA (100 x 1h candles, about 4 days):
|
| 54 |
+
1H Trend: {ta.higher_tf_trend}
|
| 55 |
+
1H Confidence: {ta.higher_tf_confidence}
|
| 56 |
+
1H RSI Signal: {ta.higher_tf_rsi_signal}
|
| 57 |
+
1H MACD Direction: {ta.higher_tf_macd_signal_dir}
|
| 58 |
+
1H Bollinger Band Position: {ta.higher_tf_price_vs_bb}
|
| 59 |
+
"""
|
| 60 |
+
raw_candle_text = f"""
|
| 61 |
+
RAW CANDLE CONTEXT:
|
| 62 |
+
Recent 1m candles (last {len(req.raw_candles_1m)}; compact OHLCV):
|
| 63 |
+
{_format_candle_context(req.raw_candles_1m)}
|
| 64 |
+
|
| 65 |
+
Recent 1h candles (last {len(req.raw_candles_1h)}; compact OHLCV):
|
| 66 |
+
{_format_candle_context(req.raw_candles_1h)}
|
| 67 |
+
"""
|
| 68 |
+
return f"""COIN: {req.symbol} ({req.name})
|
| 69 |
+
CURRENT PRICE: {req.current_price}
|
| 70 |
+
24H CHANGE: {req.price_change_24h}%
|
| 71 |
+
24H VOLUME: {req.volume_24h}
|
| 72 |
+
|
| 73 |
+
SHORT-TERM TECHNICAL ANALYSIS (100 x 1m candles, about 100 minutes):
|
| 74 |
+
Trend: {ta.trend}
|
| 75 |
+
TA Confidence: {ta.confidence}
|
| 76 |
+
RSI Signal: {ta.rsi_signal}
|
| 77 |
+
MACD Direction: {ta.macd_signal_dir}
|
| 78 |
+
Bollinger Band Position: {ta.price_vs_bb}
|
| 79 |
+
TA AI Reasoning: {ta.ai_reasoning}
|
| 80 |
+
TA Summary: {ta.analysis_summary}
|
| 81 |
+
{higher_tf_text}
|
| 82 |
+
{raw_candle_text}
|
| 83 |
+
|
| 84 |
+
NEWS SENTIMENT: {news.overall_sentiment}
|
| 85 |
+
Bullish Articles: {news.bullish_count}
|
| 86 |
Bearish Articles: {news.bearish_count}
|
| 87 |
Neutral Articles: {news.neutral_count}
|
| 88 |
News Summary: {news.summary}"""
|
| 89 |
|
| 90 |
|
| 91 |
+
def _strip_markdown(text: str) -> str:
|
| 92 |
+
"""Remove markdown code fences from response."""
|
| 93 |
+
text = text.strip()
|
| 94 |
+
text = re.sub(r'^```json\s*', '', text, flags=re.MULTILINE)
|
| 95 |
+
text = re.sub(r'^```\s*', '', text, flags=re.MULTILINE)
|
| 96 |
+
text = re.sub(r'```$', '', text, flags=re.MULTILINE)
|
| 97 |
+
return text.strip()
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
def _format_candle_context(candles) -> str:
|
| 101 |
+
if not candles:
|
| 102 |
+
return "none"
|
| 103 |
+
return "\n".join(
|
| 104 |
+
f"{c.t}: O={c.o}, H={c.h}, L={c.l}, C={c.c}, V={c.v}"
|
| 105 |
+
for c in candles
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
|
| 109 |
async def get_prediction(request: ReasoningRequest, client: AsyncOpenAI) -> PredictionOutput:
|
| 110 |
"""Call NVIDIA NIM DeepSeek V4 Pro via AsyncOpenAI with streaming for structured price prediction."""
|
| 111 |
if not client.api_key:
|