Library · Pine Script v6 · Library

TSBStyle

by TradingSignalBot

TSBStyle is a helper library designed to provide standardized visual formatting and signal drawing functions across trading strategy indicators.

View on GitHub

GitHub stars19on the repository
Pine versionv6current
Lines of code299
Settings0inputs
LicenceMIT
Last update3 weeks ago2 Nov 2026

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new library, select all of the template text and paste. Press Save and give it a name.
  4. 4Publish or import itA library draws nothing. Publish it from the editor, then import its functions in your own scripts.

Source Code

299 lines of Pine Script v6 from library/TSBStyle.pine, published under MIT. Keep the author's credit and licence lines when you reuse it.

TSBStyle.pine
//@version=6
// This source code is subject to the terms of the MIT License (LICENSE in the source repository).
// © TradingSignalBot

// @description The drawing style the strategy pack shares: the entry, the stop and up to three targets drawn as levels from the signal candle, each labelled with its price and its distance in pips; the signal marked on its candle; a panel with the latest signal; and the signal as a message and as JSON for a webhook. Colours: entry white, stop red, targets green, levels gold, ranges blue, indicator marks violet.
library("TSBStyle", overlay = true)

// ---- TSBStyle:begin ----
// The block between these two markers is the whole library. scripts/sync-style.mjs copies it into
// every indicator (with "export" taken off), so each indicator compiles on its own.

// @function The ink colour: panel backgrounds.
// @returns color
export colInk() => #0B0B0D

// @function The grey colour: secondary text and frames.
// @returns color
export colGrey() => #8A949E

// @function The entry colour.
// @returns color
export colWhite() => #FFFFFF

// @function The stop colour, and sells.
// @returns color
export colRed() => #E5484D

// @function The target colour, and buys.
// @returns color
export colGreen() => #16A34A

// @function The colour of a strategy's levels (the opening range, a swing, the previous day's high or low).
// @returns color
export colGold() => #E8B84A

// @function The colour of a session range (the Asian range) and a second indicator line.
// @returns color
export colBlue() => #5B8DEF

// @function The colour of indicator marks (a trend EMA, a divergence line).
// @returns color
export colViolet() => #B18CFF

// @type The drawings of one signal, deleted together once it is no longer among the newest kept.
// @field lines Its lines.
// @field labels Its labels.
// @field boxes Its boxes.
export type Drawn
    array<line> lines
    array<label> labels
    array<box> boxes

// @function An empty set of drawings.
// @returns Drawn
export newDrawn() =>
    Drawn.new(array.new<line>(), array.new<label>(), array.new<box>())

// @function The pip size of a symbol, by the house convention: gold one dollar, JPY pairs 0.01, other currency pairs 0.0001, anything else ten ticks.
// @param ticker The symbol's ticker (syminfo.ticker).
// @param symType The symbol's type (syminfo.type).
// @param mintick The symbol's tick (syminfo.mintick).
// @param override A pip size to use instead; 0 for automatic.
// @returns float
export pipSize(string ticker, string symType, float mintick, float override) =>
    string fiat = "USD EUR GBP JPY CHF AUD NZD CAD NOK SEK DKK SGD HKD MXN ZAR PLN TRY CNH"
    bool fx = symType == "forex" or (str.length(ticker) == 6 and str.contains(fiat, str.substring(ticker, 0, 3)) and str.contains(fiat, str.substring(ticker, 3, 6)))
    float p = mintick * 10.0
    if override > 0
        p := override
    else if str.contains(ticker, "XAU")
        p := 1.0
    else if fx
        p := str.endswith(ticker, "JPY") ? 0.01 : 0.0001
    p

// @function The distance between two prices in whole pips.
// @param a A price.
// @param b Another price.
// @param pipSz The pip size (pipSize()).
// @returns int
export pipsBetween(float a, float b, float pipSz) =>
    math.round(math.abs(a - b) / pipSz)

// @function A price as the chart shows it.
// @param v The price.
// @returns string
export priceText(float v) =>
    str.tostring(v, format.mintick)

// @function The target multiples as a sentence: "1, 2, 3"; a target of 0 is left out.
// @param r1 TP1 in multiples of the stop distance.
// @param r2 TP2.
// @param r3 TP3, or 0.
// @returns string
export rsText(float r1, float r2, float r3) =>
    string s = str.tostring(r1) + ", " + str.tostring(r2)
    if r3 > 0
        s += ", " + str.tostring(r3)
    s

// @function A target at a multiple of the stop distance, or na when the multiple is 0.
// @param isBuy True for a buy.
// @param entry The entry price.
// @param stop The stop price.
// @param r The multiple.
// @returns float
export targetAt(bool isBuy, float entry, float stop, float r) =>
    float risk = math.abs(entry - stop)
    r > 0 ? (isBuy ? entry + r * risk : entry - r * risk) : na

// @function A filled tag at the right end of a level, with its text.
// @param x The bar index of the tag.
// @param y Its price.
// @param txt Its text.
// @param bg Its fill.
// @param fg Its text colour.
// @param lblSize A label size ("tiny", "small", "normal", "large").
// @returns label
export levelTag(int x, float y, string txt, color bg, color fg, string lblSize) =>
    label.new(x = x, y = y, text = txt, xloc = xloc.bar_index, yloc = yloc.price, color = bg, textcolor = fg, style = label.style_label_left, size = lblSize)

// @function The entry, the stop and the targets, drawn from the signal candle, and the signal marked on it.
// @param isBuy True for a buy.
// @param entry The entry price (the signal candle's close).
// @param stop The stop price.
// @param tp1 TP1.
// @param tp2 TP2, or na.
// @param tp3 TP3, or na.
// @param x The signal candle's bar index.
// @param length How many candles the levels run to the right.
// @param name The strategy's name, for the mark on the candle.
// @param pipSz The pip size (pipSize()).
// @param lblSize A label size.
// @param anchor Where the mark sits: the candle's low for a buy, its high for a sell.
// @returns Drawn
export drawSignal(bool isBuy, float entry, float stop, float tp1, float tp2, float tp3, int x, int length, string name, float pipSz, string lblSize, float anchor) =>
    Drawn d = newDrawn()
    int x2 = x + length
    string word = isBuy ? "BUY" : "SELL"
    d.lines.push(line.new(x1 = x, y1 = entry, x2 = x2, y2 = entry, xloc = xloc.bar_index, color = colWhite(), width = 2, style = line.style_solid))
    d.lines.push(line.new(x1 = x, y1 = stop, x2 = x2, y2 = stop, xloc = xloc.bar_index, color = colRed(), width = 1, style = line.style_dashed))
    d.labels.push(levelTag(x2, entry, "ENTRY · " + word + " " + priceText(entry), colWhite(), colInk(), lblSize))
    d.labels.push(levelTag(x2, stop, "STOP LOSS · " + str.tostring(pipsBetween(entry, stop, pipSz)) + " PIPS · " + priceText(stop), colRed(), colWhite(), lblSize))
    array<float> tps = array.from(tp1, tp2, tp3)
    for [k, tp] in tps
        if not na(tp)
            d.lines.push(line.new(x1 = x, y1 = tp, x2 = x2, y2 = tp, xloc = xloc.bar_index, color = colGreen(), width = 1, style = line.style_dashed))
            d.labels.push(levelTag(x2, tp, "TP" + str.tostring(k + 1) + " · +" + str.tostring(pipsBetween(entry, tp, pipSz)) + " PIPS · " + priceText(tp), colGreen(), colWhite(), lblSize))
    d.labels.push(label.new(x = x, y = anchor, text = word + " · " + name, xloc = xloc.bar_index, yloc = yloc.price, color = isBuy ? colGreen() : colRed(), textcolor = colWhite(), style = isBuy ? label.style_label_up : label.style_label_down, size = lblSize))
    d

// @function A shaded range (the opening range, the Asian range, a sweep) with its name in the corner.
// @param xLeft The first bar index.
// @param xRight The last bar index.
// @param top The range's high.
// @param bottom The range's low.
// @param col Its colour.
// @param txt Its name.
// @param lblSize A label size.
// @returns box
export rangeBox(int xLeft, int xRight, float top, float bottom, color col, string txt, string lblSize) =>
    box.new(left = xLeft, top = top, right = xRight, bottom = bottom, border_color = col, border_width = 1, border_style = line.style_solid, xloc = xloc.bar_index, bgcolor = color.new(col, 85), text = txt, text_size = lblSize, text_color = col, text_halign = text.align_left, text_valign = text.align_top)

// @function A strategy's level (a swing, the previous day's high or low): a dotted line with its name at the left end.
// @param x1 The bar index it starts at.
// @param x2 The bar index it ends at.
// @param y Its price.
// @param col Its colour.
// @param txt Its name.
// @param below True to write the name under the line.
// @param lblSize A label size.
// @returns [line, label]
export levelLine(int x1, int x2, float y, color col, string txt, bool below, string lblSize) =>
    line ln = line.new(x1 = x1, y1 = y, x2 = x2, y2 = y, xloc = xloc.bar_index, color = col, width = 1, style = line.style_dotted)
    label lb = label.new(x = x1, y = y, text = txt, xloc = xloc.bar_index, yloc = yloc.price, color = color.new(col, 100), textcolor = col, style = below ? label.style_label_up : label.style_label_down, size = lblSize)
    [ln, lb]

// @function A small mark on a candle (the break, the sweep, an RSI value).
// @param x The bar index.
// @param y The price.
// @param txt Its text.
// @param col Its colour.
// @param above True to put it over the price, false under it.
// @param tip The tooltip.
// @param lblSize A label size.
// @returns label
export markLabel(int x, float y, string txt, color col, bool above, string tip, string lblSize) =>
    label.new(x = x, y = y, text = txt, xloc = xloc.bar_index, yloc = yloc.price, color = color.new(col, 100), textcolor = col, style = above ? label.style_label_down : label.style_label_up, size = lblSize, tooltip = tip)

// @function Deletes a signal's drawings.
// @param d The drawings.
// @returns bool
export deleteDrawn(Drawn d) =>
    for ln in d.lines
        line.delete(ln)
    for lb in d.labels
        label.delete(lb)
    for bx in d.boxes
        box.delete(bx)
    true

// @function Keeps a signal's drawings, and deletes the oldest beyond the newest maxKept.
// @param list The drawings kept so far.
// @param d The new signal's drawings.
// @param maxKept How many signals to keep on the chart.
// @returns int
export keepDrawn(array<Drawn> list, Drawn d, int maxKept) =>
    list.push(d)
    while list.size() > math.max(maxKept, 1)
        deleteDrawn(list.shift())
    list.size()

// @function The signal as the bot's message: side, symbol and strategy; entry, stop and targets in pips; the strategy's line; the disclaimer.
// @param isBuy True for a buy.
// @param symbol The symbol.
// @param name The strategy's name.
// @param entry The entry.
// @param stop The stop.
// @param tp1 TP1.
// @param tp2 TP2, or na.
// @param tp3 TP3, or na.
// @param pipSz The pip size.
// @param why The strategy's sentence on what happened and where the stop and targets go.
// @returns string
export signalText(bool isBuy, string symbol, string name, float entry, float stop, float tp1, float tp2, float tp3, float pipSz, string why) =>
    string s = (isBuy ? "🟢 BUY " : "🔴 SELL ") + symbol + " · " + name + "\n\n"
    s += "Entry: " + priceText(entry) + " (market)\n"
    s += "Stop: " + priceText(stop) + " (" + str.tostring(pipsBetween(entry, stop, pipSz)) + " pips)\n"
    array<float> tps = array.from(tp1, tp2, tp3)
    for [k, tp] in tps
        if not na(tp)
            s += "TP" + str.tostring(k + 1) + ": " + priceText(tp) + " (+" + str.tostring(pipsBetween(entry, tp, pipSz)) + " pips)\n"
    s += "\n" + why + "\n\nNot financial advice. Trade at your own risk."
    s

// @function A string made safe to put between quotes in JSON.
// @param s The string.
// @returns string
export jsonText(string s) =>
    str.replace_all(str.replace_all(str.replace_all(s, "\\", "\\\\"), "\"", "\\\""), "\n", "\\n")

// @function The signal as JSON for a webhook (the format docs/alerts.md describes).
// @param id The strategy's id ("london-breakout").
// @param isBuy True for a buy.
// @param symbol The symbol (syminfo.ticker).
// @param exchange The exchange (syminfo.prefix).
// @param tf The chart's timeframe (timeframe.period).
// @param entry The entry.
// @param stop The stop.
// @param tp1 TP1.
// @param tp2 TP2, or na.
// @param tp3 TP3, or na.
// @param barTime The signal candle's close time, in milliseconds (time_close).
// @param msg The message (signalText()).
// @param secretKey A shared secret for the webhook, or "".
// @returns string
export alertJson(string id, bool isBuy, string symbol, string exchange, string tf, float entry, float stop, float tp1, float tp2, float tp3, int barTime, string msg, string secretKey) =>
    string tps = priceText(tp1)
    if not na(tp2)
        tps += "," + priceText(tp2)
    if not na(tp3)
        tps += "," + priceText(tp3)
    string j = "{\"source\":\"tv-strategies\",\"strategy\":\"" + id + "\",\"symbol\":\"" + jsonText(symbol) + "\",\"exchange\":\"" + jsonText(exchange) + "\",\"timeframe\":\"" + tf + "\""
    j += ",\"side\":\"" + (isBuy ? "buy" : "sell") + "\",\"entry\":" + priceText(entry) + ",\"stop\":" + priceText(stop) + ",\"targets\":[" + tps + "]"
    j += ",\"time\":" + str.tostring(barTime) + ",\"text\":\"" + jsonText(msg) + "\""
    if secretKey != ""
        j += ",\"secret\":\"" + jsonText(secretKey) + "\""
    j + "}"

// @function Fills the latest-signal panel: the strategy, the side and symbol, and the levels.
// @param t A table of one column and three rows.
// @param isBuy True for a buy.
// @param name The strategy's name.
// @param symbol The symbol.
// @param detail The levels in one line.
// @returns bool
export heroPanel(table t, bool isBuy, string name, string symbol, string detail) =>
    table.cell(t, 0, 0, "New signal · " + name, text_color = colGrey(), text_halign = text.align_right, text_size = size.small)
    table.cell(t, 0, 1, (isBuy ? "▲ BUY  " : "▼ SELL  ") + symbol, text_color = isBuy ? colGreen() : colRed(), text_halign = text.align_right, text_size = size.large)
    table.cell(t, 0, 2, detail, text_color = colWhite(), text_halign = text.align_right, text_size = size.small)
    true

// @function Writes a notice (a timeframe the strategy cannot run on) in a one-cell table.
// @param t A table of one cell.
// @param msg The notice.
// @returns bool
export notice(table t, string msg) =>
    table.cell(t, 0, 0, msg, text_color = colGold(), text_halign = text.align_left, text_size = size.small)
    true
// ---- TSBStyle:end ----

// A demonstration when the library itself is added to a chart: a buy at the last close, the stop
// one ATR under it, targets at 1, 2 and 3 times the stop.
var array<Drawn> demo = array.new<Drawn>()
float demoAtr = ta.atr(14)
if barstate.islastconfirmedhistory and not na(demoAtr)
    float pipDemo = pipSize(syminfo.ticker, syminfo.type, syminfo.mintick, 0.0)
    float stopDemo = close - demoAtr
    keepDrawn(demo, drawSignal(true, close, stopDemo, targetAt(true, close, stopDemo, 1.0), targetAt(true, close, stopDemo, 2.0), targetAt(true, close, stopDemo, 3.0), bar_index, 15, "Style demo", pipDemo, size.small, low), 1)

Overview

TSBStyle provides unified color schemes for trade elements such as entries, stop losses, take profits, session ranges, and indicator lines. The library includes automatic pip size calculation for various asset classes, including Forex pairs and Gold, along with price formatting and pip distance helper functions. It also defines custom object types to manage grouped chart drawings, including lines, labels, and boxes directly on the price chart.

Details

Published by

TradingSignalBot on GitHub

Draws

Library (no plots)

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Topics

forexindicatorlondon-breakoutpine-scriptsupertrendtrading-strategytradingview

Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

FunctionLinearRegression

by ricardosantos79
261 stars

This Pine Script v5 library provides functions to calculate and plot linear regression channels using sample point arrays. It evaluates paired X and Y arrays to calculate the regression slope, intercept, fitted values, maximum deviation, minimum deviation, and

Library v5 Trend
Updated 5 years ago · None stated GitHub repository of FunctionLinearRegression

PositionSizing

by iamrichardD
57 stars

This Pine Script v6 library calculates trade position sizes and provides risk management functions for external scripts. It computes position sizes from account equity, percentage risk, and stop-loss distance, and offers dynamic sizing adjustments based on vol

Library v6
Updated 1 year ago · Apache-2.0 GitHub repository of PositionSizing

PineDraw

by 345-strategies
30 stars

PineDraw is a Pine Script v6 library that provides object primitives for drawing and updating price levels on the chart. It enables indicators and strategies to maintain lines, boxes, and labels in place by updating existing objects instead of deleting and rec

Library v6 Support & resistance
Updated 2 weeks ago · MPL-2.0 GitHub repository of PineDraw

Chart Patterns

by deepentropy
13 stars

Chart Patterns is a Pine Script v6 library designed to scan for and render technical chart patterns directly on the price chart. It defines custom user-defined types to configure scanning parameters, such as Zigzag pivot offsets, error ratios, flat ratios, and

Library v6 Repaints
Updated 6 months ago · MIT GitHub repository of Chart Patterns

AuthLib

by alboogycOdR
6 stars

AuthLib is a Pine Script utility library providing access control and state management structures for technical indicators. It checks callers against a hardcoded array of authorized TradingView usernames before executing operations. The library exports user-de

Library v5 SMC / ICT / price actionSessions & time
Updated 8 months ago · None stated GitHub repository of AuthLib

WebhookMessageLibrary

by TradersPost
6 stars

WebhookMessageLibrary is a helper library designed to construct structured JSON payload messages for automated trading integrations like TradersPost. It defines custom exported types and constants for trade actions, market sentiments, quantities, limit prices,

Library v5
Updated 1 year ago · None stated GitHub repository of WebhookMessageLibrary

More libraries

Related on ForexR

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers