Home » String Functions » Discovering Uppercase Strings in Pine Script

Discovering Uppercase Strings in Pine Script

Photo of author
Published on

Pine Script, the language designed for creating custom indicators and strategies on TradingView, lacks a built-in function to directly check if a string is in uppercase. However, with a little ingenuity, we can create our own solution to this problem.

Crafting a Custom Function

The IsStringUpper Function

We’ll start by creating a function named IsStringUpper. This function aims to identify whether all characters in a given string are uppercase. It’s important to note that this function returns false for strings containing any lowercase characters, as well as for empty strings, strings equal to ‘na’, or strings without any alphabetical characters.

Here’s how the function looks:

// IsStringUpper() determines if all letters in the string are uppercase.
// Returns 'false' for strings with lowercase letters, empty strings, 'na', or non-letter strings.
IsStringUpper(str source) =>
    strUpper = str.upper(source)
    strUpper != str.lower(source) and source == strUpper

How It Works

  1. Uppercase Conversion: The function begins by converting the source string to uppercase using str.upper(source), storing the result in strUpper.
  2. Letter Detection: It then checks if strUpper is different from the lowercase version of the source string (str.lower(source)). This step ensures the string contains at least one alphabetical character, as a string with only numbers or symbols would remain unchanged through these conversions.
  3. Uppercase Validation: The final step is to verify that the original source string is identical to its uppercase version (strUpper). If so, the string was originally in uppercase.

By combining these checks with an and operator, the function only returns true if both conditions are met: the string contains letters, and all are uppercase.

Implementing the Function in a Script

To illustrate the utility of the IsStringUpper function, consider the following Pine Script indicator. This script checks if the description of the instrument on the chart is entirely in uppercase and displays the result on the chart using a label.

//@version=5
indicator("Check Uppercase String", overlay=true)

// Custom function to check for uppercase strings
IsStringUpper(string source) =>
	strUpper = str.upper(source)
	strUpper != str.lower(source) and source == strUpper

// Check if the instrument's description is in uppercase
isDescUpper = IsStringUpper(syminfo.description)

// Display the result on the chart's last bar
if barstate.islast
    label.new(bar_index, high, color=color.rgb(0, 68, 255), textcolor=color.white,
           text="Instrument description:\n" + syminfo.description + 
              "\n\nEntirely in uppercase? " + str.tostring(isDescUpper))

Walkthrough of Code

  1. Initialization:
    • //@version=5: Specifies the version of Pine Script used, which is version 5 in this case.
    • indicator("Check Uppercase String", overlay=true): Declares a new indicator named “Check Uppercase String” that will overlay directly on the price chart.
  2. Custom Function – IsStringUpper:
    • This function checks if a given string (source) is entirely in uppercase.
    • It first converts the source string to uppercase using str.upper(source) and stores the result in strUpper.
    • The function then checks if strUpper is not equal to the lowercase version of the source string (str.lower(source)) and if source equals strUpper. This ensures the string has at least one letter and that all letters are uppercase.
    • The result is a boolean value (true or false), indicating whether the input string is entirely in uppercase.
  3. Checking Instrument’s Description:
    • The variable isDescUpper stores the result of calling IsStringUpper with syminfo.description as its argument. syminfo.description contains the description of the current chart’s instrument.
    • This step determines whether the instrument’s description is all in uppercase letters.
  4. Displaying Results:
    • The code checks if the current bar is the last bar on the chart using if barstate.islast. This condition ensures that the label is only created once, at the end of the chart.
    • label.new(bar_index, high, color=color.rgb(0, 68, 255), textcolor=color.white, ...): Creates a new label on the chart. The label is positioned at the high price of the last bar and uses a specific blue color (color.rgb(0, 68, 255)) for the background and white for the text color.
    • The label’s text displays the instrument’s description followed by a message indicating whether the description is entirely in uppercase. This is achieved by concatenating strings and converting the boolean result (isDescUpper) into a string using str.tostring(isDescUpper).

Key Features and Takeaways

  • Utility: This script and function are useful for data validation and formatting, especially in custom indicators where string case might convey specific meanings or categories.
  • Customization: The function IsStringUpper can be adapted or extended to meet various criteria, such as checking for lowercase strings or mixed-case strings.
  • Ease of Integration: The straightforward nature of this function allows for easy incorporation into larger scripts or as part of data preprocessing steps in Pine Script.

By understanding and applying such custom functions, Pine Script programmers can enhance the versatility and robustness of their trading indicators and strategies.

Leave a Comment