Pine Script examples: complete v6 scripts
One page per “how do I do this in Pine Script?” question: the short answer first, then a complete script you can paste into the Pine Editor, what its key lines do, the mistakes that break it, and variations.
20 examples
Markers, drawings and colors
Signals on the chart: shapes, labels, lines, tables, backgrounds, fills and colored averages.
Buy and sell markers (plotshape)
Draw buy and sell markers in Pine Script v6 with plotshape() and plotchar(): a complete script, the fixed-text rule, positions, sizes and alerts.
Labels and lines (label.new, line.new)
Draw labels and lines in Pine Script v6: swing labels with their price, a zigzag, a level that moves instead of being redrawn, and the max_labels_count limit.
Dashboard table (table.new)
Build a dashboard table in Pine Script v6 with table.new() and table.cell(): create it once with var, fill it on the last bar, and color cells by value.
Background, fill and bar colors
Color a TradingView chart from Pine Script v6: a fill between two averages, a background shaded by RSI with color.from_gradient(), candles colored by volume.
Moving average ribbon by slope
A Pine Script v6 moving average ribbon: six EMAs, each colored by its own slope, a fill when the ribbon is stacked in order, and why a loop can't build it.
Signals and price structure
Conditions that fire on the right bar: crosses, pivots, swings, values since an event, closed bars and the daily open.
Crossover and crossunder
How ta.crossover() and ta.crossunder() work in Pine Script v6, the difference with >, crossing a fixed level, a cross within the last N bars, in one script.
Pivot support and resistance
Turn ta.pivothigh() and ta.pivotlow() into support and resistance lines in Pine Script v6: each level extends until a bar closes through it, tracked in arrays.
Value since an event (valuewhen, barssince)
Get a value from the last time a condition was true with ta.valuewhen() and count the bars since with ta.barssince() in Pine Script v6, plus a var alternative.
Higher highs and lower lows
Higher highs, higher lows, lower highs and lower lows in Pine Script v6: bar-by-bar staircases and swing comparisons with ta.valuewhen(), as alert conditions.
barstate.isconfirmed (closed bars)
Use barstate.isconfirmed in Pine Script v6 to act only on closed bars, count the live signals that vanished before the close, and read every barstate value.
Daily open, previous day and time
Draw the daily open and the previous day's high and low in Pine Script v6 from chart data, choose the hour the day starts, and shade a weekday with dayofweek.
Strategies
Order logic for the Strategy Tester: a trailing stop that only moves one way, and four ways to size a position.
ATR trailing stop (strategy)
An ATR trailing stop in a Pine Script v6 strategy: a stop that only moves in the trade's favor, set before the fill and passed to strategy.exit() on every bar.
Position sizing in a strategy
Size Pine Script v6 strategy orders four ways: percent of equity, fixed cash, fixed units, and a fixed risk per trade from the stop distance, with the formulas.
Language features
The parts of Pine Script v6 behind every script: var and varip, arrays, loops, functions, inputs, history buffers and libraries.
var vs varip
var vs varip in Pine Script v6: what each keeps between bars and between live updates, a script that counts both, and when varip is the right choice.
Arrays (push, shift, avg)
Use arrays in Pine Script v6: keep one value per day with push() and shift(), average and rank them, avoid out-of-bounds errors and arrays rebuilt every bar.
for and while loops
for and while loops in Pine Script v6: a lookback search with break, a while loop, counting down, the time limit, and when a built-in or var does it better.
Functions and methods
Write user-defined functions and methods in Pine Script v6: typed parameters, default values, tuples, a method on your own type, and the rules on scope.
Inputs and the settings tab
A Pine Script v6 settings tab done right: input.int, float, source, enum, timeframe, session and color inputs, with groups, inline rows, tooltips and active.
max_bars_back and history
Read history with a variable offset in Pine Script v6: when max_bars_back() is needed, how to size it, and the var accumulator that needs no history buffer.
Libraries (export, import)
Write a Pine Script v6 library with export, publish it, and import it into an indicator with import user/name/version: rules for types, methods and versions.
An error message instead of a chart?
All Pine Script errorsThe pitfalls on these pages link to the matching page of the Pine Script error library, which shows each message with a script that triggers it and the fix. These are the ones the examples point to most often:
Should be called on each calculation for consistency
The function "ta.highest" should be called on each calculation for consistency. It is recommended to extract the call from this scope
Compile errorCannot use plot in local scope
Cannot use "plot" in local scope
Runtime errorMemory limits exceeded
Memory limits exceeded
Compile errorseries int used but simple int expected
Cannot call "ta.ema" with argument "length"="len". An argument of "series int" type was used but a "simple int" is expected.
Compile errorCannot call operator + with a string and a number
Cannot call "operator +" with argument "expr0"="Close: ". An argument of "literal string" type was used but a "series float" is expected.
Compile errorhline(): series float used but input float expected
Cannot call "hline" with argument "price"="call "ta.sma" (series float)". An argument of "series float" type was used but a "input float" is expected.
Every script in this library, including the variations, was checked with Edgoro Pine Check (Pine Script v6 rules) on November 12, 2026. Pine Check is a static checker, not TradingView's compiler: TradingView compiles a script when you save it in the Pine Editor, so paste any message it shows into the free error fixer. The scripts are examples to learn from and adapt, not trading systems.
Need a script that does something else?
Describe the indicator or strategy in plain English and Edgoro writes the Pine Script v6, runs it through Pine Check and explains what each part does.