ROIC Chart Genie
November 29, 2022
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.
- GPT-3 reads your prompt and extracts one- or two-stock symbols.
- A mini-crawler pulls 24 key financial metrics from ROIC.ai and saves each company to a CSV.
- GPT writes the Matplotlib code needed to graph exactly what you asked.
- 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.csvget_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 browserTry 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
categories24 scraped metrics; trim to speed things up
max_tokens in GPT callsTrade 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
- Cache scraped CSVs in Firestore to avoid repeat hits.
- Move generation to
gpt-3.5-turbofor cheaper completions. - Add a
GET /image/<id>endpoint for CDN-friendly serving. - Let GPT select chart types: bars for margins, lines for EPS, etc.
Paste a question, get a chart—no finance terminal required.