Patrick Mauboussin

AI, Healthcare, Business

← Back to home

ROIC Chart Genie

What it does

ROIC Chart Genie lets you type "Compare Apple's ROIC to Microsoft's debt-to-equity over the last ten years" and get back a PNG chart—no tickers, no Excel, zero manual scraping.

  1. GPT-3 reads your prompt and extracts one- or two-stock symbols.
  2. A mini-crawler pulls 24 key financial metrics from ROIC.ai and saves each company to a CSV.
  3. GPT writes the Matplotlib code needed to graph exactly what you asked.
  4. The server executes the code headlessly, saves the plot, encodes it in base-64, and returns JSON (prompt, image, code).

Everything runs behind a single Flask endpoint: POST /prompt.


Pipeline at a glance

flowchart LR
  U(User prompt) -->|GPT symbols| S[get_symbols()]
  S --> CSV[request_stock() → CSV]
  U -->|CSV list + prompt| G[get_graph()]
  G --> C[execute_code()]
  C --> IMG[.png → base64]
  IMG --> R(Return JSON)

Key pieces

Module / file
Job description
get_symbols (GPT)
Pull comma-separated tickers from messy prose
request_stock
Scrape ROIC.ai → clean → DataFrame → TICKER.csv
get_graph (GPT)
Inject file list + prompt into a code-completion template
execute_code
Run the generated Python, swap plt.show() for plt.savefig()
Flask route
Orchestrates flow, catches errors, sends image back to browser

Try it locally

pip install flask flask-cors openai requests beautifulsoup4 lxml pandas matplotlib
export OPEN_AI_KEY="sk-your-key"
python app.py

Then:

curl -X POST http://localhost:5000/prompt \
     -d "plot google vs amazon revenue per share 2010-2022"

The response JSON holds a Base-64 PNG ready for <img src="data:image/png;base64, …">.


Tweak knobs

Variable
Purpose
configs['stocks']
Prompt prefix that tells GPT how to spot tickers
categories
24 scraped metrics; trim to speed things up
max_tokens in GPT calls
Trade creativity for cost / determinism
execute_code()
Swap Matplotlib backend or add seaborn styling

Caveats

  • Scraper fragility – ROIC.ai class names may change without warning.
  • GPT misfires – Irrelevant words like "CAPEX" sometimes sneak in; we strip a few common offenders.
  • Two-stock limit – Code template handles one or two CSVs; extend it for baskets.
  • Security – Generated Python is executed with exec; sandbox before deploying in the wild.

Next steps

  1. Cache scraped CSVs in Firestore to avoid repeat hits.
  2. Move generation to gpt-3.5-turbo for cheaper completions.
  3. Add a GET /image/<id> endpoint for CDN-friendly serving.
  4. Let GPT select chart types: bars for margins, lines for EPS, etc.

Paste a question, get a chart—no finance terminal required.

Thanks for reading!

Written by Patrick Mauboussin on November 29, 2022