Scripting v2
Write JavaScript indicators for MMT charts.
MMT scripts are evaluated against historical and realtime market data, then emit plots and entities to the chart.
All v2 scripts must start with the version directive:
//@version=2Quick Example
//@version=2
indicator("SMA Crossover", true)
const fastLen = input.int("Fast Length", 9, { min: 1 })
const slowLen = input.int("Slow Length", 21, { min: 1 })
const candles = subscribe(data.OHLCV)
function onBar(index) {
const fast = ta.sma(candles.close(), fastLen)
const slow = ta.sma(candles.close(), slowLen)
const a = plot("Fast SMA", fast, { color: color.blue, width: 2 })
const b = plot("Slow SMA", slow, { color: color.red, width: 2 })
fill(a, b, { color: color.transp(color.blue, 85) })
}Script Behavior
- Top-level code runs once when the script loads or changes
onBar(index)runs for history warmup and realtime updates- Built-in functions inside
ta.*,math.*,str.*, andcolor.*are available in every script - Higher-timeframe access is done with
subscribe(..., { timeframe }), and higher-timeframe derived values should usesubscription.calc(...) - New plot labels may be blocked after history warmup, so declare labels early
Scripts must define onBar(index). Calls like indicator(), subscribe(), and input.* are top-level only.
API Map
- Execution Model
- Inputs
- Alerts
- Data Sources
- Examples
- Plotting
- Entities
- Globals
- Technical Analysis
- Math
- Strings
- Colors
- Timeframes
- Data
- Drawing
- Context
- v1 to v2 Migration
Key Limits
- Inputs:
256 - Subscriptions:
30 - Plot series labels:
64 - Plot commands per bar:
64 - Fill pairs per bar:
16 - Alert definitions:
32 - Alert fields per definition:
32 - Entities:
2000each forLine,Box,Label,Marker,Arrow - History retention, Free:
10,000bars for non-BOOKsources,5,000forBOOK - History retention, Pro:
20,000bars for non-BOOKsources,10,000forBOOK
For LLMs
Fetch docs.mmt.gg/scripting/llms.txt for a page index, then pull individual pages as needed via /llms.mdx/{slug} (for example /llms.mdx/scripting/inputs).