PINE LIBRARY

PriceFormat

170
Library for automatically converting price values to formatted strings
matching the same format that TradingView uses to display open/high/low/close prices on the chart.




โ–ˆโ€ƒOVERVIEW
This library is intended for Pine Coders who are authors of scripts that display numbers onto a user's charts. Typically, ๐šœ๐š๐š›.๐š๐š˜๐šœ๐š๐š›๐š’๐š—๐š() would be used to convert a number into a string which can be displayed in a label / box / table, but this only works well for values that are formatted as a simple decimal number. The purpose of this library is to provide an easy way to create a formatted string for values which use other types of formats besides the decimal format.

The main functions exported by this library are:
  • ๐š๐š˜๐š›๐š–๐šŠ๐š๐™ฟ๐š›๐š’๐šŒ๐šŽ() - creates a formatted string from a price value
  • ๐š–๐šŽ๐šŠ๐šœ๐šž๐š›๐šŽ๐™ฟ๐š›๐š’๐šŒ๐šŽ๐™ฒ๐š‘๐šŠ๐š—๐š๐šŽ() - creates a formatted string from the distance between two prices
  • ๐š๐š˜๐šœ๐š๐š›๐š’๐š—๐š() - an alternative to the built-in ๐šœ๐š๐š›.๐š๐š˜๐šœ๐š๐š›๐š’๐š—๐š(๐šŸ๐šŠ๐š•๐šž๐šŽ, ๐š๐š˜๐š›๐š–๐šŠ๐š)

This library also exports some auxiliary functions which are used under the hood of the previously mentioned functions, but can also be useful to Pine Coders that need fine-tuned control for customized formatting of numeric values:
  • Functions that determine information about the current chart:
    ๐š’๐šœ๐™ต๐š›๐šŠ๐šŒ๐š๐š’๐š˜๐š—๐šŠ๐š•๐™ต๐š˜๐š›๐š–๐šŠ๐š(), ๐š’๐šœ๐š…๐š˜๐š•๐šž๐š–๐šŽ๐™ต๐š˜๐š›๐š–๐šŠ๐š(), ๐š’๐šœ๐™ฟ๐šŽ๐š›๐šŒ๐šŽ๐š—๐š๐šŠ๐š๐šŽ๐™ต๐š˜๐š›๐š–๐šŠ๐š(), ๐š’๐šœ๐™ณ๐šŽ๐šŒ๐š’๐š–๐šŠ๐š•๐™ต๐š˜๐š›๐š–๐šŠ๐š(), ๐š’๐šœ๐™ฟ๐š’๐š™๐šœ๐™ต๐š˜๐š›๐š–๐šŠ๐š()
  • Functions that convert a ๐š๐š•๐š˜๐šŠ๐š value to a formatted string:
    ๐šŠ๐šœ๐™ณ๐šŽ๐šŒ๐š’๐š–๐šŠ๐š•(), ๐šŠ๐šœ๐™ฟ๐š’๐š™๐šœ(), ๐šŠ๐šœ๐™ต๐š›๐šŠ๐šŒ๐š๐š’๐š˜๐š—๐šŠ๐š•(), ๐šŠ๐šœ๐š…๐š˜๐š•๐šž๐š–๐šŽ()




โ–ˆโ€ƒEXAMPLES

โ€ข Simple Example

This example shows the simplest way to utilize this library.
snapshot
Pine Scriptยฎ
//@version=6 indicator("Simple Example") import n00btraders/PriceFormat/1 var table t = table.new(position.middle_right, 2, 1, bgcolor = color.new(color.blue, 90), force_overlay = true) if barstate.isfirst table.cell(t, 0, 0, "Current Price: ", text_color = color.black, text_size = 40) table.cell(t, 1, 0, text_color = color.blue, text_size = 40) if barstate.islast string lastPrice = close.formatPrice() // Simple, easy way to format price table.cell_set_text(t, 1, 0, lastPrice)


โ€ข Complex Example

This example calls all of the main functions and uses their optional arguments.
snapshot
Pine Scriptยฎ
//@version=6 indicator("Complex Example") import n00btraders/PriceFormat/1 // Enum values that can be used as optional arguments precision = input.enum(PriceFormat.Precision.DEFAULT) language = input.enum(PriceFormat.Language.ENGLISH) // Main library functions used to create formatted strings string formattedOpen = open.formatPrice(precision, language, allowPips = true) string rawOpenPrice = PriceFormat.tostring(open, format.price) string formattedClose = close.formatPrice(precision, language, allowPips = true) string rawClosePrice = PriceFormat.tostring(close, format.price) [distance, ticks] = PriceFormat.measurePriceChange(open, close, precision, language, allowPips = true) // Labels to display formatted values on chart string prices = str.format("Open: {0} ({1})\n\nClose: {2} ({3})", formattedOpen, rawOpenPrice, formattedClose, rawClosePrice) string change = str.format("Change (close - open):\n\n{0} / {1}", distance, ticks) label.new(chart.point.now(high), prices, yloc = yloc.abovebar, textalign = text.align_left, force_overlay = true) label.new(chart.point.now(low), change, yloc = yloc.belowbar, style = label.style_label_up, force_overlay = true)




โ–ˆโ€ƒNOTES

โ€ข Function Descriptions

The library source code uses Markdown for the exported functions. Hover over a function/method call in the Pine Editor to display formatted, detailed information about the function/method.
snapshot

โ€ข Precision Settings

The Precision option in the chart settings can change the format of how prices are displayed on the chart. Since the user's selected choice cannot be known through any Pine built-in variable, this library provides a ๐™ฟ๐š›๐šŽ๐šŒ๐š’๐šœ๐š’๐š˜๐š— enum that can be used as an optional script input for the user to specify their selected choice.
snapshot

โ€ข Language Settings

The Language option in the user menu can change the decimal/grouping separators in the prices that are displayed on the chart. Since the user's selected choice cannot be known through any Pine built-in variable, this library provides a ๐™ป๐šŠ๐š—๐š๐šž๐šŠ๐š๐šŽ enum that can be used as an optional script input for the user to specify their selected choice.
snapshot



โ–ˆโ€ƒEXPORTED FUNCTIONS

method formatPrice(price, precision, language, allowPips)
โ€ƒโ€ƒFormats a price value to match how it would be displayed on the user's current chart.
โ€ƒโ€ƒNamespace types: series float, simple float, input float, const float
โ€ƒโ€ƒParameters:
โ€ƒโ€ƒโ€ƒโ€ƒprice (float): The value to format.
โ€ƒโ€ƒโ€ƒโ€ƒprecision (series Precision): A Precision.* enum value.
โ€ƒโ€ƒโ€ƒโ€ƒlanguage (series Language): A Language.* enum value.
โ€ƒโ€ƒโ€ƒโ€ƒallowPips (simple bool): Whether to allow decimal numbers to display as pips.
โ€ƒโ€ƒReturns: Automatically formatted price string.

measurePriceChange(startPrice, endPrice, precision, language, allowPips)
โ€ƒโ€ƒMeasures a change in price in terms of both distance and ticks.
โ€ƒโ€ƒParameters:
โ€ƒโ€ƒโ€ƒโ€ƒstartPrice (float): The starting price.
โ€ƒโ€ƒโ€ƒโ€ƒendPrice (float): The ending price.
โ€ƒโ€ƒโ€ƒโ€ƒprecision (series Precision): A Precision.* enum value.
โ€ƒโ€ƒโ€ƒโ€ƒlanguage (series Language): A Language.* enum value.
โ€ƒโ€ƒโ€ƒโ€ƒallowPips (simple bool): Whether to allow decimal numbers to display as pips.
โ€ƒโ€ƒReturns: A tuple of formatted strings: [string distance, string ticks].

method tostring(value, format)
โ€ƒโ€ƒAlternative to the Pine `str.tostring(value, format)` built-in function.
โ€ƒโ€ƒNamespace types: series float, simple float, input float, const float
โ€ƒโ€ƒParameters:
โ€ƒโ€ƒโ€ƒโ€ƒvalue (float): (series float) The value to format.
โ€ƒโ€ƒโ€ƒโ€ƒformat (string): (series string) The format string.
โ€ƒโ€ƒReturns: String in the specified format.

isFractionalFormat()
โ€ƒโ€ƒDetermines if the default behavior of the chart's price scale is to use a fractional format.
โ€ƒโ€ƒReturns: True if the chart can display prices in fractional format.

isVolumeFormat()
โ€ƒโ€ƒDetermines if the default behavior of the chart's price scale is to display prices as volume.
โ€ƒโ€ƒReturns: True if the chart can display prices as volume.

isPercentageFormat()
โ€ƒโ€ƒDetermines if the default behavior of the chart's price scale is to display percentages.
โ€ƒโ€ƒReturns: True if the chart can display prices as percentages.

isDecimalFormat()
โ€ƒโ€ƒDetermines if the default behavior of the chart's price scale is to use a decimal format.
โ€ƒโ€ƒReturns: True if the chart can display prices in decimal format.

isPipsFormat()
โ€ƒโ€ƒDetermines if the current symbol's prices can be displayed as pips.
โ€ƒโ€ƒReturns: True if the chart can display prices as pips.

method asDecimal(value, precision, minTick, decimalSeparator, groupingSeparator, eNotation)
โ€ƒโ€ƒConverts a number to a string in decimal format.
โ€ƒโ€ƒNamespace types: series float, simple float, input float, const float
โ€ƒโ€ƒParameters:
โ€ƒโ€ƒโ€ƒโ€ƒvalue (float): The value to format.
โ€ƒโ€ƒโ€ƒโ€ƒprecision (int): Number of decimal places.
โ€ƒโ€ƒโ€ƒโ€ƒminTick (float): Minimum tick size.
โ€ƒโ€ƒโ€ƒโ€ƒdecimalSeparator (string): The decimal separator.
โ€ƒโ€ƒโ€ƒโ€ƒgroupingSeparator (string): The thousands separator, aka digit group separator.
โ€ƒโ€ƒโ€ƒโ€ƒeNotation (bool): Whether the result should use E notation.
โ€ƒโ€ƒReturns: String in decimal format.

method asPips(value, priceScale, minMove, minMove2, decimalSeparator, groupingSeparator)
โ€ƒโ€ƒConverts a number to a string in decimal format with the last digit replaced by a superscript.
โ€ƒโ€ƒNamespace types: series float, simple float, input float, const float
โ€ƒโ€ƒParameters:
โ€ƒโ€ƒโ€ƒโ€ƒvalue (float): The value to format.
โ€ƒโ€ƒโ€ƒโ€ƒpriceScale (int): Price scale.
โ€ƒโ€ƒโ€ƒโ€ƒminMove (int): Min move.
โ€ƒโ€ƒโ€ƒโ€ƒminMove2 (int): Min move 2.
โ€ƒโ€ƒโ€ƒโ€ƒdecimalSeparator (string): The decimal separator.
โ€ƒโ€ƒโ€ƒโ€ƒgroupingSeparator (string): The thousands separator, aka digit group separator.
โ€ƒโ€ƒReturns: String in decimal format with an emphasis on the pip value.

method asFractional(value, priceScale, minMove, minMove2, fractionalSeparator1, fractionalSeparator2)
โ€ƒโ€ƒConverts a number to a string in fractional format.
โ€ƒโ€ƒNamespace types: series float, simple float, input float, const float
โ€ƒโ€ƒParameters:
โ€ƒโ€ƒโ€ƒโ€ƒvalue (float): The value to format.
โ€ƒโ€ƒโ€ƒโ€ƒpriceScale (int): Price scale.
โ€ƒโ€ƒโ€ƒโ€ƒminMove (int): Min move.
โ€ƒโ€ƒโ€ƒโ€ƒminMove2 (int): Min move 2.
โ€ƒโ€ƒโ€ƒโ€ƒfractionalSeparator1 (string): The primary fractional separator.
โ€ƒโ€ƒโ€ƒโ€ƒfractionalSeparator2 (string): The secondary fractional separator.
โ€ƒโ€ƒReturns: String in fractional format.

method asVolume(value, precision, minTick, decimalSeparator, groupingSeparator, spacing)
โ€ƒโ€ƒConverts a number to a string in volume format.
โ€ƒโ€ƒNamespace types: series float, simple float, input float, const float
โ€ƒโ€ƒParameters:
โ€ƒโ€ƒโ€ƒโ€ƒvalue (float): The value to format.
โ€ƒโ€ƒโ€ƒโ€ƒprecision (int): Maximum number of decimal places.
โ€ƒโ€ƒโ€ƒโ€ƒminTick (float): Minimum tick size.
โ€ƒโ€ƒโ€ƒโ€ƒdecimalSeparator (string): The decimal separator.
โ€ƒโ€ƒโ€ƒโ€ƒgroupingSeparator (string): The thousands separator, aka digit group separator.
โ€ƒโ€ƒโ€ƒโ€ƒspacing (string): The whitespace separator.
โ€ƒโ€ƒReturns: String in volume format.

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.