Skip to main content
This guide provides ready-to-use solutions for common drawing tasks in Indie indicators. Each pattern addresses a specific visualization need with working code examples.

Key Concepts for Drawing in Indie

Before diving into patterns, let’s recall these essential concepts that affect how drawings work in Indie.

Two Execution Phases: History vs Realtime

Indie indicators run in two distinct phases:
  1. History calculation: Each bar is processed once sequentially from left to right. Drawing objects are created and remain stable.
  2. Realtime updates: When new intrabar data arrives, the indicator re-executes for the current bar multiple times.
Code that works perfectly during history calculation can break during realtime updates. Always consider both phases when writing drawing code, and test your indicators on instruments with realtime updates on 1m timeframe for several minutes to ensure runtime stability.

Chart State Rollbacks

During realtime updates, when new intrabar data arrives:
  1. The chart state rolls back to what it was at the beginning of the current bar
  2. Your indicator’s calc() function executes again with updated data
If you created a drawing during an intrabar update, the rollback will remove it. If you erased a drawing during an intrabar update, the rollback will restore it.

Why Series and Var exist

MutSeries[T] and Var[T] do two things for you: they carry a value from one bar to the next, and they roll it back on every intrabar update. The first is why function-based indicators, which have no class fields to keep anything in, reach for a Var at all. The second is the part that is easy to miss: inside one candle your code runs many times, and usually only the final result of that candle should survive — not a value accumulated over every update within it. So before each re-execution both containers restore the value the candle started with. You do not manage that — you read the value with get() and it is already the right one. The difference between the two is history: a MutSeries[T] also keeps the values of previous candles and lets you read them with [1], [2] and so on, while a Var[T] keeps only the latest state. A class field gives you the first half and not the second: it persists, but it keeps whatever the previous intrabar update left in it.

When a drawing belongs in a Var

A drawing is an object like any other, so the same rule applies — with the chart added to the picture, because the chart also rolls its own state back to the start of the candle before each re-execution. Put a drawing in a Var when:
  • The drawing’s next state depends on its current state. Pattern 4 below starts each new polyline segment from the previous segment’s endpoint. The chart rolled that segment back; a plain class field did not, so the two disagree and the polyline breaks.
  • You read values out of the drawing for anything else, and need them to be the values the chart is showing right now.
State you accumulate yourself — a bar counter, a running total — belongs in a Var or a MutSeries whether a drawing displays it or not. In a plain class field it counts intrabar updates instead of candles, because nothing undoes the previous update’s work. Once the number is in a container, the label that displays it can stay in a plain class field.
What a Var does not do is make erasing safe. It cannot reattach a drawing that an intrabar rollback detached from the chart — and it does not need to: self.chart.erase() on an object that is not currently on the chart does nothing at all. You can erase without knowing whether the drawing is there.
Simple guidelines:
  1. One-time drawings (labels on conditions): just create and draw — no special handling
  2. Persistent drawings (info panels): create the object once and reuse it, in a class field or a Var
  3. Drawings you read back (polylines, anything whose next state is built from its current one): keep in a Var and reach it through get()

Reach the drawing through get()

set() hands the value over to the container, which stores a copy of the drawing’s fields. Edits you make to your local object afterwards do not reach it. Keep a single handle — the Var — and go through get() for every draw, edit and erase.
The same applies to later edits: change the drawing through my_var.get() (e.g. my_var.get().text = '...'), not through a local variable you kept on the side.

Common Drawing Patterns

Pattern 1: Draw labels when conditions are met

Problem: You want to display labels on specific bars when certain conditions are satisfied, such as price patterns, indicator signals, or periodic intervals. Solution: Use conditional logic to check your criteria and create new label objects when conditions are met. No special object management is required.
Label breakouts and periodic bars
How it works: Labels are created when conditions are met and automatically persist on the chart. During realtime updates, the chart rolls back and re-executes, creating labels again when conditions are still true. Figure 1. Conditional Labels.

Pattern 2: Display information in fixed screen position

Problem: You want to show live statistics, indicator values, or other information that stays in a fixed position on screen regardless of chart scrolling or zooming. Solution: Create a LabelRel object once and reuse it by updating its text property. Store the label in a class field (for class-based indicators) or use Var (for function-based indicators).
Always reuse the same LabelRel object. Creating new relative-positioned labels on each bar will cause an error “count of relative positioned drawings exceeded the limit 100”.
Figure 2. Info Panel.

Pattern 3: Label that follows the current bar

Problem: You want a label that appears only above the most recent bar and moves with it as new bars arrive, displaying current values or active signals. Solution: Create a LabelAbs object once and update both its position and text properties on each bar to follow the current price action.
Key point: By reusing the same label object and updating its position, you ensure only one label exists at any time, creating a smooth tracking effect. Figure 3. Current Bar Tracker.

Pattern 4: Draw polyline connecting specific points

Problem: You want to create connected lines that pass through specific price points (e.g., highs every 5 bars), forming a polyline that updates as new bars arrive. Solution: Maintain only the current active segment using Var. Update the segment’s endpoint between anchor points, and create new segments at anchor points.
Why Var is essential: this is the read-back case. Each new segment starts from the previous segment’s endpoint, so the value read out of the drawing has to be the one the chart shows — and the chart rolls its state back on every intrabar update. The Var makes the segment roll back with it. The Optional is a separate thing: it only expresses “there is no segment yet”, before the first anchor bar. Figure 4. Polyline Pattern. What happens without Var: If you used a regular class field self.current_segment: Optional[LineSegment] instead:
  1. On anchor bar (e.g., bar 10), you create a new segment starting from the previous segment’s endpoint and save it to self.current_segment
  2. An intrabar update arrives, chart rolls back to the start of bar 10
  3. During recalculation, the code tries to create a new LineSegment, but takes its start point from self.current_segment, which was modified during the previous update and wasn’t rolled back
  4. This creates a disconnected polyline — the new segment starts from an incorrect position

Pattern 5: Erase drawings created on previous executions

Problem: You need to erase drawings that were created during previous bar updates or previous intrabar executions. This is common for temporary overlays, alerts, or any drawing that should appear and disappear based on conditions. Solution: Let’s explore this through a concrete example — a blinking line indicator. The instructive part is the first attempt, because its failure is silent. First attempt — a new object in a class field:
Where it breaks: Consider this scenario:
  1. On the last update of bar N, the line is erased (is_visible was False)
  2. Bar N+1 starts with no line on the chart
  3. First execution: is_visible becomes True, a new line object is created and drawn
  4. Intrabar update arrives, chart rolls back to start of bar N+1 (no line)
  5. Second execution: is_visible becomes False, so the script erases the object the class field still points at — an object the rollback has already detached from the chart
  6. The erase does nothing: the object the field points at is not the object the chart is showing
What you end up seeing depends on the phasing. Shift the scenario by one update — the candle begins with the line already on the chart — and the erase in step 5 does land, the rollback then puts the line back, and the next execution draws a second line. Over a run the two cases mix, so the chart accumulates lines the script is certain it removed.
Nothing is reported when this happens — the erase is simply skipped. And it only happens during realtime, so there is nothing to point at the cause.
The working solution — one object, stated explicitly on every execution:
Why this works: the problem in the first attempt was never the class field — it was the new object on every execution. One object cannot get out of sync with the chart, because every execution ends by stating what that one object should be: on the chart, or off it. Whatever a rollback did before that, the candle ends in the state the last execution asked for. And nothing has to be tracked: erase on a line that is not on the chart does nothing, and draw on a line that is already there updates it. Note that there is no Var here, and no Optional standing in for “not drawn”. Neither is needed: nothing is ever read back out of the line, and both of its endpoints are overwritten from current bar data each time, so there is no state to roll back. Compare Pattern 4, where each segment is built from the previous one’s endpoint — that is what makes the Var load-bearing there. Result: The line blinks with each execution — appearing when is_visible is true and disappearing when false. During realtime updates, each intrabar update toggles the visibility, creating a visual heartbeat of market activity. The toggle lives in a plain class field on purpose: it is meant to flip on every execution, intrabar updates included, which is exactly what the “heartbeat” is.
Reusing one object also keeps the number of chart objects flat. Over the same data, this indicator creates exactly one, while a version that builds a fresh LineSegment on each toggle creates one per toggle — a count that keeps growing for as long as the indicator runs.
When you cannot reuse a single object: some indicators draw a number of objects that varies — a set of zones, a pool of levels. Give them a fixed set of reused objects and erase the ones that should disappear; you do not need to track which are currently drawn, because erasing one that is not on the chart is a no-op.

Pattern 6: Skip redundant draws during history with is_last_bar

Problem: You have a drawing that gets updated every bar (e.g., an info panel, a “current price” label) but only the final state matters to the user. During history calculation the runtime tracks every chart.draw() call internally, so redrawing the same object on each of thousands of history bars wastes memory and slows down calculation — even though only the last bar’s state will ever be visible. Solution: Guard the drawing call with self.is_last_bar so that the object is only drawn once — on the most recent bar. During realtime updates is_last_bar is always True, so the drawing updates normally.
When to use this pattern: Apply it to any drawing that is redrawn on every bar with only the latest state being meaningful — dashboards, info panels, “current value” trackers, trailing stop lines, etc. Do not use it for drawings that should remain on the chart at their historical positions (e.g., signal labels, trend lines between pivot points).

Pattern 7: Prefer @level over a constant plot line

Problem: You need a fixed horizontal line at a known value — an overbought threshold, a zero line, a target price. It is tempting to reach for @plot.line and return the constant on every bar, but that is the wrong tool for a static value. Solution: Use the @level decorator. It is a purely static, compile-time decorator: it takes no return value and occupies no slot in the return tuple. The line is described once, at compile time, from a single constant.
Why @level is faster and leaner: A value returned from @plot.line is treated as a full time series. The runtime evaluates it, stores it, and streams it to the chart on every one of the thousands of history bars — even though the number never changes. @level carries a single constant: nothing is computed or stored per bar. Same visual result, a fraction of the memory and compute.

Pattern 8: Prefer @band over two lines with a fill

Problem: You want a shaded zone between two fixed values — a neutral RSI zone, a value area, a no-trade corridor. The manual approach is two @plot.line decorators plus a @plot.fill, returning both constants and a plot.Fill() every bar. Solution: Use the @band decorator. Like @level, it is static and compile-time: two constant edges and a fill described once, with no return value and no output slots to feed.
Why @band is faster and leaner: The manual version occupies three extra output slots — two constant lines and a fill — each tracked as its own per-bar series across the entire history. @band replaces all three with a single static declaration that carries two constants. Fewer output slots, no per-bar evaluation, less data streamed to the chart.
Rule of thumb: whenever a horizontal line or zone sits at a value that is known at compile time and never changes during the run, use @level or @band. Reach for a constant @plot.line (and @plot.fill) only when the value is genuinely dynamic — computed at runtime or changing bar-to-bar (e.g., a volatility-adjusted threshold). In that case the value really is a series, and a plot is the right tool.