DEV Community

Cover image for I built an open-source charting app with a backtester, and Claude Code can drive it
Karim Masmoudi
Karim Masmoudi

Posted on

I built an open-source charting app with a backtester, and Claude Code can drive it

Most traders I know pay a monthly subscription for a charting app, and mostly for a few things: more than a handful of indicators on one chart, more than one chart on screen, bar replay and alerts. The open-source side has good chart libraries, but I couldn't find a full app.

So I built one. Kandle is an MIT-licensed charting app that runs in the browser. You can open it here without an account. It has 36 indicators, six chart types including Heikin Ashi, drawing tools, alerts, bar replay, 1, 2 or 4 charts, its own scripting language with strategy backtests, and an MCP server so an AI agent can read the chart and draw on it.

Kandle demo

This post is about three parts that were more interesting to build than I expected.

A scripting language where every value has history

Chart scripts look like normal code, but every variable is a series. close is the close of the bar being calculated, close[1] is the one before, and ta.ema(close, 9) keeps its own state from bar to bar. Kandle Script follows the syntax most chart users already know, so a lot of existing indicator scripts run with few or no changes:

//@version=5
indicator("EMA cross", overlay = true)
fast = ta.ema(close, input.int(9, "Fast"))
slow = ta.ema(close, input.int(21, "Slow"))
plot(fast, "Fast", color.orange, 2)
plot(slow, "Slow", color.blue, 2)
plotshape(ta.crossover(fast, slow), "Up", shape.triangleup, location.belowbar, color.green)
Enter fullscreen mode Exit fullscreen mode

The pipeline is a hand-written lexer and parser (indentation-based blocks, line continuation inside brackets), a compiler that resolves names and gives each call site its own state slot, and a runtime that runs the program once per bar. That last part is the trick: ta.ema inside a function called from two places needs two separate histories, so state is keyed by call site, not by function.

When a new candle arrives, only the last bar is recalculated. A full run over 1,500 bars takes a few milliseconds for typical scripts, so editing feels live.

A backtester that says what it assumes

Change indicator() to strategy() and the script becomes a backtest:

//@version=5
strategy("EMA cross", overlay = true, initial_capital = 10000,
     default_qty_type = strategy.percent_of_equity, default_qty_value = 100,
     commission_type = strategy.commission.percent, commission_value = 0.05)
fast = ta.ema(close, 12)
slow = ta.ema(close, 26)
if ta.crossover(fast, slow)
    strategy.entry("Long", strategy.long)
    strategy.exit("Exit", "Long", stop = close * 0.97, limit = close * 1.06)
if ta.crossunder(fast, slow)
    strategy.close("Long")
Enter fullscreen mode Exit fullscreen mode

The hard part of a bar-based backtester is that you don't know what happened inside a bar. If a bar touched both your stop and your target, which one filled first? Kandle assumes price went from the open to the nearer extreme, then to the other one, then to the close, and the README says so in plain words, along with how gaps, slippage, commission and reversals work. A backtest you can't audit isn't worth much.

The results show up as arrows on the chart with a dashed line from each entry to its exit, and a Strategy tab with the equity curve against buy and hold, drawdown, profit factor, Sharpe and the full trade list.

A Donchian breakout strategy with its trades on the chart

Letting an agent drive the chart, safely

kandle-mcp is a small MCP server. Your agent starts it over stdio, and the Kandle tab connects to it over a WebSocket on 127.0.0.1. From there the agent has 28 tools: read the chart state and candles, change symbol and timeframe, add indicators, draw lines, rectangles and Fibonacci retracements by price and time, run a script and read the backtest, set alerts, take a screenshot.

claude mcp add kandle -- npx -y --package=https://github.com/Kernel-Hunter/kandle/releases/download/v0.11.0/kandle-mcp-0.11.0.tgz kandle-mcp
Enter fullscreen mode Exit fullscreen mode

A local WebSocket server is an easy thing to get wrong, so a few rules:

  • The bridge is off until you turn it on in the app, and it only listens on 127.0.0.1.
  • Every connection has to send a pairing code first (about 59 bits, compared in constant time, locked for a minute after 5 wrong tries).
  • The server checks the Origin header, so a random website can't connect even if it guesses the port, and it checks Host, which blocks DNS rebinding.
  • The agent can only do what the tools allow. It can't place trades, and it can't see anything outside the chart.

Two things surprised me while shipping it. First, npx caches a package by its URL, so a "latest release" tarball link kept running the first version anyone downloaded. The docs now pin a versioned URL, and the server tells the agent when the app is newer than it is. Second, current Chrome asks before a public website may connect to 127.0.0.1. That's a good change, and the app now tells you to allow it when you use the hosted version.

An agent marked up BTC/USDT with the 120-bar high and low, a Fibonacci retracement and a note

Data

Crypto comes straight from the public APIs of Binance, Coinbase, Bybit, OKX and Kraken, with no key. Stocks, ETFs and forex use your own free key from Twelve Data, Alpaca or Polygon. Keys stay in your browser and only go to that provider. Kandle has no server of its own.

What's next

Scripts can now read other timeframes and symbols with request.security, picked from the settings dialog with input.timeframe and input.symbol, and you can import your own data from CSV. If you trade or write indicators, I'd like to hear what's missing. Issues and pull requests are welcome: github.com/Kernel-Hunter/kandle.

Top comments (1)

Collapse
 
alicexixi85 profile image
Dejun (Alice) Chen •

Bar replay plus an MCP server raises one question I'd check: when replay is active, can the agent's read-candles tool see bars past the replay cursor? If it returns the full history, an agent asked to mark up the chart "as of now" during replay will quietly use the future, and its drawings will look smarter than any live read could be.

Clamping every read tool to the cursor would make replay useful for testing an agent's decisions, not just a person's.