In Pine Script, assessing whether a string is composed entirely of lowercase characters can be invaluable, especially when comparing string values. Although Pine Script lacks a built-in function for this specific check, it’s possible to implement a custom solution. This article explores how to craft such a function and apply it effectively within your scripts.
Custom Function to Check Lowercase
The essence of our approach lies in a custom function, isStrLowercase
, designed to discern if a string is in lowercase. Here’s a detailed breakdown:
// isStrLowercase() determines if all alphabetic characters in 'sourceStr' // are lowercase. It returns 'true' for entirely lowercase strings and 'false' // for strings containing uppercase characters or being empty, 'na', or devoid of letters. isStrLowercase(string sourceStr) => strLower = str.lower(sourceStr) strLower != str.upper(sourceStr) and sourceStr == strLower
The isStrLowercase()
function accepts a single parameter: sourceStr
. It aims to verify if this string parameter is in lowercase.
Within the function, we first transform sourceStr
to its lowercase equivalent using the str.lower()
function, assigning this value to strLower
. This conversion is crucial for the subsequent comparisons.
We then proceed with two critical checks:
- Alphabetic Character Check: By comparing the lowercase version of
sourceStr
(strLower
) against its uppercase counterpart (str.upper(sourceStr)
), we can deduce the presence of alphabetic characters. A string lacking alphabetic characters will have its lowercase and uppercase forms identical, a scenario we aim to exclude. - Lowercase Verification: We confirm that
sourceStr
matches its lowercase version (strLower
). This comparison directly assesses if the original string is in lowercase.
Both conditions must be true for the function to affirm that sourceStr
is indeed in lowercase, achieved by employing the logical and
operator.
Example
To demonstrate the practical use of the isStrLowercase
function, let’s examine an indicator script that evaluates if a trading instrument’s description is entirely in lowercase. The outcome is displayed via a chart label.
//@version=5 indicator(title="Check Lowercase String", overlay=true) isStrLowercase(string sourceStr) => strLower = str.lower(sourceStr) strLower != str.upper(sourceStr) and sourceStr == strLower descIsLower = isStrLowercase(syminfo.description) if barstate.islast label.new(bar_index, high, color=color.purple, textcolor=color.white, text="Instrument description:\n" + syminfo.description + "\nIs entirely in lowercase? " + str.tostring(descIsLower))
Walkthrough of Code
//@version=5
: Specifies the script uses version 5 of Pine Script.indicator(title="Check Lowercase String", overlay=true)
: Defines the script as an indicator with a title, shown over the chart.isStrLowercase(string sourceStr)
: Declares a function to check if a string is in lowercase.strLower = str.lower(sourceStr)
: ConvertssourceStr
to lowercase, storing the result instrLower
.strLower != str.upper(sourceStr) and sourceStr == strLower
: Checks if the string is entirely lowercase.descIsLower = isStrLowercase(syminfo.description)
: Uses the function to check if the instrument description is in lowercase.if barstate.islast
: Executes the following block on the last bar of the chart.label.new(bar_index, high, color=color.purple, textcolor=color.white, ...)
: Creates a label on the chart.text="Instrument description:\n" + syminfo.description + "\nIs entirely in lowercase? " + str.tostring(descIsLower)
: Sets label text displaying the instrument description and whether it’s in lowercase.
Key Features and Takeaways
- Custom Functionality: Pine Script’s flexibility allows for the creation of custom functions like
isStrLowercase
to perform checks not supported natively. - String Evaluation: The function effectively identifies lowercase strings by employing a combination of lowercase conversion and strategic comparisons.
- Practical Application: By integrating such functions into scripts, users can add sophisticated string comparison capabilities to their trading indicators or strategies.
This method exemplifies the adaptability of Pine Script, enabling users to extend its functionality to meet specific analytical needs.