PINE LIBRARY
Atualizado PineDraw

Extracted from TheStrat Suite, where these components render six timeframes of levels on a single chart without running out of drawing objects. Nothing here knows anything about TheStrat, or about any strategy. It is the layer underneath: put an object at an exact price and time, keep it updated, and keep the chart readable when several of them land on the same price.
The script ends with a small demo that marks the prior bar's high and low, so you can add the library to a chart and see the primitives run before importing anything.
Library "PineDraw"
Object primitives for indicators that draw price levels: update lines,
boxes and labels in place instead of deleting and recreating them every tick, pool
labels so a busy chart stays under the object limit, and merge labels that land on the
same price so they read as one row instead of overlapping.
updateLine(ref, x1, y1, x2, y2, col, w, style)
Update a line in place, or create it if the reference is na.
Parameters:
ref (line): Existing line, or na on the first call.
x1 (int): Left anchor, in bar time (ms).
y1 (float): Left price.
x2 (int): Right anchor, in bar time (ms).
y2 (float): Right price.
col (color): Line color.
w (int): Line width.
style (string): One of line.style_solid, line.style_dashed, line.style_dotted.
Returns: The line reference. Assign it back: `myLine := updateLine(myLine, ...)`.
updateBox(ref, x1, y1, x2, y2, bg, border)
Update a box in place, or create it if the reference is na.
Parameters:
ref (box): Existing box, or na on the first call.
x1 (int): Left anchor in bar time (ms).
y1 (float): Top price.
x2 (int): Right anchor in bar time (ms).
y2 (float): Bottom price.
bg (color): Fill color.
border (color): Border color.
Returns: The box reference.
clearLine(ref)
Delete a line if it exists. Assign the result back to clear your reference.
Parameters:
ref (line): Line to remove, or na.
Returns: na, typed as a line.
clearBox(ref)
Delete a box if it exists. Assign the result back to clear your reference.
Parameters:
ref (box): Box to remove, or na.
Returns: na, typed as a box.
newPool()
Create an empty pool. Assign to a `var` so it survives across bars.
Returns: A new LabelPool.
method reset(this)
Start a render pass. Hides any label left over from the previous pass.
Namespace types: LabelPool
Parameters:
this (LabelPool): The pool.
Returns: The pool, for chaining.
method acquire(this, x, y, txt, useBarIndex, bg, txtCol, sz)
Take the next label from the pool, creating one only if the pool is exhausted.
Namespace types: LabelPool
Parameters:
this (LabelPool): The pool.
x (int): Anchor, bar time in ms when useBarIndex is false, otherwise a bar index.
y (float): Price.
txt (string): Label text.
useBarIndex (bool): Anchor to bar_index instead of bar time. Use for labels that should
float at a fixed offset from the current bar rather than sit at a point in history.
bg (color): Background color.
txtCol (color): Text color.
sz (string): One of size.tiny, size.small, size.normal, size.large.
Returns: The label, already positioned and styled.
newSet()
Create an empty label set for one render pass.
Returns: A new LabelSet.
method add(this, price, txt, joiner, tolerance)
Add a label at a price, merging into an existing row if one is close enough.
Namespace types: LabelSet
Parameters:
this (LabelSet): The set.
price (float): The price to label.
txt (string): The text to add.
joiner (string): Placed between merged entries, for example " + ".
tolerance (float): How far apart two prices can be and still share a row, in price units.
Pass 0 to merge only exact ticks. syminfo.mintick is a sane floor; a fraction of
ta.atr() adapts across instruments.
Returns: The set, for chaining.
method flush(this, pool, x, useBarIndex, bg, txtCol, sz)
Draw a consolidated set through a pool, in one call.
Namespace types: LabelSet
Parameters:
this (LabelSet): The set to draw.
pool (LabelPool): The label pool to draw through.
x (int): Anchor for every label in the set.
useBarIndex (bool): Anchor to bar_index instead of bar time.
bg (color): Background color.
txtCol (color): Text color.
sz (string): Text size.
Returns: The number of labels drawn.
lineStyle(style)
Map a style name to a Pine line style constant.
Parameters:
style (string): "Solid", "Dashed" or "Dotted". Anything else returns dotted.
Returns: A line.style_* constant.
textSize(name)
Map a size name to a Pine size constant.
Parameters:
name (string): "Tiny", "Small", "Normal" or "Large". Anything else returns small.
Returns: A size.* constant.
LabelPool
A reusable set of labels. Hold one in a `var` at the top of your script.
Fields:
items (array<label>): Every label ever allocated by this pool.
used (series int): How many have been handed out in the current render pass.
LabelSet
A set of labels-to-be, grouped by price.
Fields:
prices (array<float>): One entry per distinct price.
texts (array<string>): The merged text for each price.
The script ends with a small demo that marks the prior bar's high and low, so you can add the library to a chart and see the primitives run before importing anything.
Library "PineDraw"
Object primitives for indicators that draw price levels: update lines,
boxes and labels in place instead of deleting and recreating them every tick, pool
labels so a busy chart stays under the object limit, and merge labels that land on the
same price so they read as one row instead of overlapping.
updateLine(ref, x1, y1, x2, y2, col, w, style)
Update a line in place, or create it if the reference is na.
Parameters:
ref (line): Existing line, or na on the first call.
x1 (int): Left anchor, in bar time (ms).
y1 (float): Left price.
x2 (int): Right anchor, in bar time (ms).
y2 (float): Right price.
col (color): Line color.
w (int): Line width.
style (string): One of line.style_solid, line.style_dashed, line.style_dotted.
Returns: The line reference. Assign it back: `myLine := updateLine(myLine, ...)`.
updateBox(ref, x1, y1, x2, y2, bg, border)
Update a box in place, or create it if the reference is na.
Parameters:
ref (box): Existing box, or na on the first call.
x1 (int): Left anchor in bar time (ms).
y1 (float): Top price.
x2 (int): Right anchor in bar time (ms).
y2 (float): Bottom price.
bg (color): Fill color.
border (color): Border color.
Returns: The box reference.
clearLine(ref)
Delete a line if it exists. Assign the result back to clear your reference.
Parameters:
ref (line): Line to remove, or na.
Returns: na, typed as a line.
clearBox(ref)
Delete a box if it exists. Assign the result back to clear your reference.
Parameters:
ref (box): Box to remove, or na.
Returns: na, typed as a box.
newPool()
Create an empty pool. Assign to a `var` so it survives across bars.
Returns: A new LabelPool.
method reset(this)
Start a render pass. Hides any label left over from the previous pass.
Namespace types: LabelPool
Parameters:
this (LabelPool): The pool.
Returns: The pool, for chaining.
method acquire(this, x, y, txt, useBarIndex, bg, txtCol, sz)
Take the next label from the pool, creating one only if the pool is exhausted.
Namespace types: LabelPool
Parameters:
this (LabelPool): The pool.
x (int): Anchor, bar time in ms when useBarIndex is false, otherwise a bar index.
y (float): Price.
txt (string): Label text.
useBarIndex (bool): Anchor to bar_index instead of bar time. Use for labels that should
float at a fixed offset from the current bar rather than sit at a point in history.
bg (color): Background color.
txtCol (color): Text color.
sz (string): One of size.tiny, size.small, size.normal, size.large.
Returns: The label, already positioned and styled.
newSet()
Create an empty label set for one render pass.
Returns: A new LabelSet.
method add(this, price, txt, joiner, tolerance)
Add a label at a price, merging into an existing row if one is close enough.
Namespace types: LabelSet
Parameters:
this (LabelSet): The set.
price (float): The price to label.
txt (string): The text to add.
joiner (string): Placed between merged entries, for example " + ".
tolerance (float): How far apart two prices can be and still share a row, in price units.
Pass 0 to merge only exact ticks. syminfo.mintick is a sane floor; a fraction of
ta.atr() adapts across instruments.
Returns: The set, for chaining.
method flush(this, pool, x, useBarIndex, bg, txtCol, sz)
Draw a consolidated set through a pool, in one call.
Namespace types: LabelSet
Parameters:
this (LabelSet): The set to draw.
pool (LabelPool): The label pool to draw through.
x (int): Anchor for every label in the set.
useBarIndex (bool): Anchor to bar_index instead of bar time.
bg (color): Background color.
txtCol (color): Text color.
sz (string): Text size.
Returns: The number of labels drawn.
lineStyle(style)
Map a style name to a Pine line style constant.
Parameters:
style (string): "Solid", "Dashed" or "Dotted". Anything else returns dotted.
Returns: A line.style_* constant.
textSize(name)
Map a size name to a Pine size constant.
Parameters:
name (string): "Tiny", "Small", "Normal" or "Large". Anything else returns small.
Returns: A size.* constant.
LabelPool
A reusable set of labels. Hold one in a `var` at the top of your script.
Fields:
items (array<label>): Every label ever allocated by this pool.
used (series int): How many have been handed out in the current render pass.
LabelSet
A set of labels-to-be, grouped by price.
Fields:
prices (array<float>): One entry per distinct price.
texts (array<string>): The merged text for each price.
Notas de Lançamento
ColoredLabelSet: price-merged labels where each row keeps its first entry's color and, optionally, its own time anchor, so labels can sit at their own line's end rather than in one column. Later entries at the same price append their text, and an entry whose text is already present on the row is skipped instead of repeated. Two draw styles: flushChips uses each row's color as the label background - the classic level-tag look - and flushText draws transparent colored text.LabelPool.trim(): ends a render pass by deleting every pooled label beyond the ones acquired, so switching a feature off removes its labels instead of leaving blanks against the object cap. reset, acquire, trim is the recommended pass.
tablePos(): maps a position name from an input dropdown to the table position constant.
Biblioteca do Pine
Em verdadeiro espírito TradingView, o autor publicou este código Pine como uma biblioteca de código aberto para que outros programadores Pine da nossa comunidade possam reutilizá-lo. Parabéns ao autor! Você pode usar esta biblioteca de forma privada ou em outras publicações de código aberto, mas a reutilização deste código em publicações é regida pelas Regras da Casa.
Tools for traders and investors smart enough to keep it simple.
TheStrat Suite is now free & open source 😘👌 TheStratSuite.com
Build on it: github.com/natebking/345-strategies
Everything else: 345strategies.com
TheStrat Suite is now free & open source 😘👌 TheStratSuite.com
Build on it: github.com/natebking/345-strategies
Everything else: 345strategies.com
Aviso legal
As informações e publicações não se destinam a ser, e não constituem, conselhos ou recomendações financeiras, de investimento, comerciais ou de outro tipo fornecidos ou endossados pela TradingView. Leia mais nos Termos de Uso.
Biblioteca do Pine
Em verdadeiro espírito TradingView, o autor publicou este código Pine como uma biblioteca de código aberto para que outros programadores Pine da nossa comunidade possam reutilizá-lo. Parabéns ao autor! Você pode usar esta biblioteca de forma privada ou em outras publicações de código aberto, mas a reutilização deste código em publicações é regida pelas Regras da Casa.
Tools for traders and investors smart enough to keep it simple.
TheStrat Suite is now free & open source 😘👌 TheStratSuite.com
Build on it: github.com/natebking/345-strategies
Everything else: 345strategies.com
TheStrat Suite is now free & open source 😘👌 TheStratSuite.com
Build on it: github.com/natebking/345-strategies
Everything else: 345strategies.com
Aviso legal
As informações e publicações não se destinam a ser, e não constituem, conselhos ou recomendações financeiras, de investimento, comerciais ou de outro tipo fornecidos ou endossados pela TradingView. Leia mais nos Termos de Uso.