This script is a tinny library for creating and displaying formatted values in TradingView scripts. It provides a structured way to present key information like titles, percentages, currency values, decimals, and integers with clear formatting. This allows you to coordinate your strings in advance and hold one item to use for calling your string to a label, box, table.. Made for day to day use of most typical use cases, more advanced techniques should be used for complicated scenarios.
Building Blocks
User Defined Types (UDTs)
The script defines a UDT called `DisplayValue` to encapsulate the components of a display value:
* title: The title or label of the value. * format_string: The string used to format the value (e.g., "{0} - 1,number,percent}"). * value: The actual value to be displayed. * format: An enum value specifying the desired format (percent, currency, etc.).
Enums
The `DisplayFormat` enum provides predefined constants for various formatting options, making the code more readable and less prone to errors.
Functions
* create(): This function creates a new `DisplayValue` instance. It takes the title, value, and desired format as arguments and generates the appropriate format string. * to_string(): This function converts a `DisplayValue` instance into a formatted string ready for display on the chart.
This will display a label on the chart with the text "Profit - 15%".
### Notes
* The library handles the formatting details, making it easier to display values consistently in your scripts. * The use of enums and UDTs improves code organization and readability.
--------
Library "display_value"
create(display_name, display_value, display_format) Gets the appropriate format string based on the display format. Parameters: display_name (string): (string) The name of the display value. Default is na. display_value (float) display_format (series DisplayFormat) Returns: (DisplayValue) A new DisplayValue instance with the formatted value.
to_string(item) Converts the display value to a string with the specified format. Parameters: item (DisplayValue): (DisplayValue) The display value to convert to a string. Returns: (string) The string representation of the display value.
DisplayValue Structure representing a display value. Fields: title (series string): (string) The title of the display value. format_string (series string): (string) The format string to use for display. value (series float): (float) The value to display. format (series DisplayFormat): (DisplayFormat) The format to use.