交易图表生成器

MCP服务器提供程序化访问交易实验室图表生成系统,支持多种技术分析和预测功能。

  • by locupleto
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 locupleto/mcp-chart-generator

使用说明

MCP Chart Generator

MCP (Model Context Protocol) server that exposes the Trading Lab Chart Generator functionality, allowing Claude to generate professional trading charts on demand.

Overview

This MCP server provides programmatic access to the trading-lab chart generation system, enabling:

  • On-demand chart generation during Claude conversations
  • 28+ technical studies (RSI, MACD, Bollinger Bands, etc.)
  • ML predictions from Chronos, TOTO, Prophet, Burg models
  • Signal overlays with buy/sell markers
  • Multiple themes (Dark Casual, Dark Professional, Light, etc.)
  • Flexible timeframes (Daily, Weekly, Monthly, Yearly)

Architecture

Claude Code
    ↓ (MCP Protocol)
MCP Chart Generator Server
    ↓ (Python Import)
trading-lab/src/services/chart_generation/
    ↓
PNG Chart Image

Available Tools

1. render_chart

Generate a trading chart PNG image.

Required Parameters:

  • symbol_code - Stock symbol (e.g., "AAPL", "SPY")
  • file_name - Output filename without extension

Optional Parameters:

  • exchange_code - Exchange (default: "US")
  • symbol_type - Type (default: "Common Stock")
  • study - Study name (default: "ChartOnly")
  • theme - Chart theme (default: "Dark Casual")
  • chart_type - Candlestick/Line/HLC (default: "Candlestick")
  • period - Daily/Weekly/Monthly/Yearly (default: "Daily")
  • history_length - Days of history (default: 365)
  • warmup_period - Extra bars for indicators (default: 200)
  • zoom_level - Number of bars to display (default: 250)
  • width - Image width in pixels (default: 1200)
  • height - Image height (default: auto)
  • prediction_mode - off/view_only/full (default: "view_only")
  • bullish_signal - Bullish signal for overlays
  • bearish_signal - Bearish signal for overlays
  • marker_source - actual/predicted (default: "actual")
  • eod_historical_offset - Days back from today (0-30)

Example:

mcp__chart-generator__render_chart(
    symbol_code="AAPL",
    file_name="aapl_rsi",
    study="RSIStudy",
    zoom_level=120,
    theme="Dark Casual"
)

2. list_studies

List all available chart studies.

Returns: Categorized list of 28+ studies

3. list_signals

List all available trading signals.

Returns: Bullish and bearish signal names

4. list_themes

List all available chart themes.

Returns: Available theme names

5. validate_config

Validate chart configuration before rendering.

Parameters:

  • symbol_code - Stock symbol
  • study - Study name (optional)
  • theme - Theme name (optional)
  • bullish_signal - Bullish signal (optional)
  • bearish_signal - Bearish signal (optional)

Returns: Validation status and errors if any

Installation

Prerequisites

  • Python 3.11+
  • trading-lab project available at expected path
  • MARKET_DATABASE_PATH or MARKETDATA_DB_PATH environment variable set

Setup

  1. Create virtual environment:

    cd /Volumes/Work/development/projects/git/mcp-chart-generator
    python3 -m venv venv
    
  2. Install dependencies:

    venv/bin/pip install -r requirements.txt
    
  3. Configure in .mcp.json (in trading-lab project):

    {
      "mcpServers": {
        "chart-generator": {
          "type": "stdio",
          "command": "/Volumes/Work/development/projects/git/mcp-chart-generator/venv/bin/python3",
          "args": [
            "/Volumes/Work/development/projects/git/mcp-chart-generator/chart_generator_mcp_server.py"
          ],
          "env": {
            "MARKET_DATABASE_PATH": "/Volumes/Work/marketdata/marketdata.db"
          }
        }
      }
    }
    
  4. Test the server:

    cd /path/to/trading-lab
    claude mcp list
    

    You should see chart-generator in the list with ✓ Connected status.

Usage Examples

Example 1: Simple Chart

User: "Generate an AAPL chart"

Claude uses:
mcp__chart-generator__render_chart(
    symbol_code="AAPL",
    file_name="aapl_chart"
)

Output: /tmp/aapl_chart.png

Example 2: Chart with RSI Study

User: "Show me SPY with RSI indicator, last 90 days"

Claude uses:
mcp__chart-generator__render_chart(
    symbol_code="SPY",
    file_name="spy_rsi_90d",
    study="RSIStudy",
    zoom_level=90
)

Output: /tmp/spy_rsi_90d.png

Example 3: Chart with Signal Overlays

User: "MSFT chart with buy/sell signals using ADM momentum"

Claude uses:
mcp__chart-generator__render_chart(
    symbol_code="MSFT",
    file_name="msft_signals",
    bullish_signal="adm_momentum_low",
    bearish_signal="adm_momentum_high",
    zoom_level=180
)

Output: /tmp/msft_signals.png with buy/sell markers

Example 4: Discover Available Options

User: "What chart studies are available?"

Claude uses:
mcp__chart-generator__list_studies()

Returns: List of all 28+ studies categorized by type

Viewing Generated Charts

Important: Claude Code currently does not support inline image display (iTerm2 image protocol).

After generating a chart, you have two options to view it:

Option 1: Exit and use imgcat (iTerm2)

# Exit Claude Code session (Ctrl+D or /exit)
imgcat /tmp/your_chart.png

Option 2: Open in default viewer

# From within Claude Code or after exiting
open /tmp/your_chart.png

This will open the chart in Preview (macOS default image viewer).

Why this happens

Claude Code intercepts terminal output and doesn't currently pass through iTerm2's inline image escape sequences. This is tracked in GitHub issue #6389.

Workaround: The MCP server generates charts successfully; viewing them just requires opening them outside the Claude Code session.

Technical Details

Dependencies

The MCP server itself only requires:

  • mcp>=2,<3 - MCP protocol implementation

The actual chart generation is handled by importing from the trading-lab project, which has its own dependencies (pandas, playwright, etc.).

Output Location

Generated charts are saved to /tmp/ directory with the filename specified in the file_name parameter.

Performance

  • Chart generation takes 1-2 seconds per chart
  • The server reuses resources for efficiency
  • Can generate hundreds of charts in batch

Integration Points

Market Data:

  • Requires MARKET_DATABASE_PATH environment variable
  • Uses trading-lab's marketdata.database.Database

Studies:

  • Auto-discovers from trading-lab/src/studies/
  • Supports all 28+ studies

Signals:

  • Uses trading-lab/src/signals/bullish_signals/
  • Uses trading-lab/src/signals/bearish_signals/

Troubleshooting

Server won't connect

  1. Check virtual environment:

    ls -la /Volumes/Work/development/projects/git/mcp-chart-generator/venv/bin/python3
    
  2. Verify dependencies:

    /Volumes/Work/development/projects/git/mcp-chart-generator/venv/bin/pip list | grep mcp
    
  3. Check trading-lab path: Ensure /Volumes/Work/development/projects/git/trading-lab/src exists

  4. Verify environment variable:

    echo $MARKET_DATABASE_PATH
    

Chart generation fails

  1. Check database:

    ls -la $MARKET_DATABASE_PATH
    
  2. Test trading-lab directly:

    cd /Volumes/Work/development/projects/git/trading-lab
    source venv/bin/activate
    python tests/test_chart_generation_poc.py
    
  3. Validate configuration: Use validate_config tool before render_chart

Development

Running Tests

Test the MCP server independently:

cd /Volumes/Work/development/projects/git/mcp-chart-generator
venv/bin/python3 chart_generator_mcp_server.py

This will start the server in stdio mode (waiting for MCP protocol messages).

Adding New Tools

Edit chart_generator_mcp_server.py:

  1. Add tool definition in list_tools()
  2. Add handler in call_tool()
  3. Implement the async function

Debugging

Enable debug logging in the trading-lab:

import logging
logging.basicConfig(level=logging.DEBUG)

Related Projects

License

Same as trading-lab project.

Author

Created as part of the Trading Lab ecosystem for programmatic chart generation via MCP.