Platform Market Analysis Skill
Overview
The Market Analysis System is an AI-powered chat interface that allows users to ask questions about crypto markets, trends, and technical indicators. It uses a "ReAct" style agent approach to reason, fetch data, and formulate answers.
Key Features:
- Natural Language Parsing: Understands "Analyze BTC" or "Is ETH bullish?".
- Real-time Data: Fetches live prices, technical indicators (RSI, MAX), and news.
- Streaming Response: Shows the "thought process" (steps) in real-time before the final answer.
- Session Management: Multi-session chat history with pinning and persistence.
Key Files:
- Frontend:
web/js/chat.js(Chat UI & Stream Handler),web/js/market.js(Data Visualization) - Backend API:
api/routers/analysis.py(Endpoints) - Core Logic:
interfaces/chat_interface.py(Bot & Parser),core/agents.py(Agent Logic)
Architecture
Data Flow
- User Input: Typed in
web/js/chat.js. - API Request:
POST /api/analyze(Stream). - Query Parsing:
CryptoQueryParsercalls LLM to extract intent (Symbol: BTC, Action: Analyze). - Execution:
CryptoAnalysisBotdecides which tools to run (Price, News, Indicators). - Streaming:
- Backend yields "Process Steps" (
[PROCESS] Fetching data...) - Backend yields "Final Answer" (
[RESULT] Bitcoin is currently...)
- Backend yields "Process Steps" (
- Rendering: Frontend parses the stream and updates the UI incrementally.
Components
1. CryptoAnalysisBot (interfaces/chat_interface.py)
the main orchestrator. It:
- Maintains conversation history.
- Manages the
CryptoAgentor executes the "Standard Analysis Mode" (legacy). - Generates the streaming response generator.
2. Streaming Protocol (NDJSON style)
The /api/analyze endpoint returns a stream of JSON objects or specially formatted text lines.
data: {"content": "[PROCESS] Checking limits..."}-> Updates the "Thinking" UI.data: {"content": "[RESULT] **Analysis**: ..."}-> Renders the final markdown response.data: {"done": true}-> Closes the stream.
3. Frontend Chat (web/js/chat.js)
- Session Management: Loads/Creates/Deletes sessions via
/api/chat/sessions. - Stream Reader: Uses
response.body.getReader()to process chunks. - UI Rendering:
renderStoredBotMessage()parses the special tags ([PROCESS_START],[RESULT]) to create the collapsible "Process" section and the Markdown result.
API Endpoints
Analysis
POST /api/analyze
Streamed analysis response. Payload:
{
"message": "Analyze BTC",
"manual_selection": ["rsi", "macd"],
"market_type": "spot",
"session_id": "uuid..."
}
POST /api/analysis/backtest
Runs a quick backtest for a strategy.
Session Management
GET /api/chat/sessions
List user's chat sessions.
POST /api/chat/sessions
Create a new session.
GET /api/chat/history?session_id=...
Get message history for a session.
Frontend Integration (chat.js)
Key Function: sendMessage()
- Checks for API Key.
- Creates/Selects a session.
- Sends
POST /api/analyze. - Reads stream loop:
while (true) { const { value, done } = await reader.read(); // Decode and update UI botMsgDiv.innerHTML = renderStoredBotMessage(fullContent, true); }
UI States:
- Thinking: Shows a spinner and elapsed time.
- Process: Collapsible details tag showing step-by-step actions.
- Result: Final markdown content.
Modification Guidelines
✅ Safe Modifications
Adding New Tools:
- Add tool function in
core/tools.py. - Register tool in
core/agents.py. - The Bot will automatically have access to it (if using Agent mode).
- Add tool function in
Customizing UI:
web/js/chat.js:renderStoredBotMessagecontrols how the message looks. You can change styling here.
⚠️ Risks
LLM Context Window:
- Passing too much history or too large data (news articles) can hit token limits.
- Fix: Use
trim_historyor summarization techniques inCryptoAnalysisBot.
Timeout:
- Complex analysis takes time (>30s).
- Fix: Ensure Nginx/frontend timeout settings allow for long-lived streams.
Related Skills
- platform-db-pattern: For session storage logic.
- pi-auth: For user identification in sessions.
Converted and distributed by TomeVault — claim your Tome and manage your conversions.