v1 to v2 Migration
Migrating scripts from Scripting v1 to v2.
Version Directive
All v2 scripts must include the version directive as the first line:
//@version=2Scripts without this directive will be treated as v1.
Entity Keys
In //@version=2, entities (Line, Box, Label, Marker, Arrow) require a key string as the first argument. The key must be unique per entity type per bar — only one entity with a given key exists per bar.
// v1
const ln = Line({ x1: unix(10), y1: 100, x2: unix(0), y2: 120 })
// v2
const ln = Line("trend", { x1: unix(10), y1: 100, x2: unix(0), y2: 120 })Entities without a key will not be plotted.
Arrow APIs
plotArrow() and Arrow() are v2-only APIs.
plotArrow(label, value, options?)requires//@version=2Arrow(key, options?)requires//@version=2and a non-empty key
BOOK .data() Migration
In the current BOOK API, .data() and .dataView() are gone. Calling them now throws a migration error that points you back to the docs.
Use the scalar accessors and helpers directly on the subscription instead:
// old
const book = subscribe(data.BOOK)
const snapshot = book.data()
const bestAsk = snapshot ? snapshot.askPrices[0] : na
const bestBid = snapshot ? snapshot.bidPrices[0] : na// new
const book = subscribe(data.BOOK)
const bestAsk = book.bestAsk()
const bestBid = book.bestBid()Common replacements:
book.data()?.askPrices[i]->book.askPrice(i)book.data()?.bidPrices[i]->book.bidPrice(i)book.data()?.askSizes[i]->book.askSize(i)book.data()?.bidSizes[i]->book.bidSize(i)- manual depth scans ->
book.depth(...)orbook.imbalance(...)
See BOOK Data Source for the full replacement API.
For more advanced migrations:
- use
book.numLevels(),book.numBids(), andbook.numAsks()for availability and side-wise counts - use per-level accessors when you still need custom scan logic
- use
book.depthMany(...)andbook.imbalanceMany(...)when you need multiple depth windows from one reference price
Missing Values
In v2, numeric TA/math helpers use NaN for missing or not-ready values instead of undefined.
Common updates:
x !== undefined->!na(x)x === undefined->na(x)- object presence checks like
if (bb)-> field checks likeif (!na(bb.basis)) subscription.calc(...)object callbacks must return numeric fields; useNaNfor missing fields instead ofundefined
Migration
Most migrations are:
- add
//@version=2as the first line - add stable keys to entity constructors
- use
plotArrow()andArrow()only in v2 scripts - replace BOOK
.data()/.dataView()usage with BOOK accessors and helpers - replace
undefinedchecks on numeric TA/math outputs withna(...)ornz(...)