This article delves into the “simple” keyword in Pine Script, illustrating its utility with a practical example that highlights the background color of a chart when a non-standard chart type is used.
What are “Simple” Values?
In Pine Script, “simple” values are those that are determined once and remain constant throughout the script’s execution. This consistency is crucial for variables and parameters that rely on information available only at the script’s start. The simple
keyword explicitly defines such variables and parameters, ensuring their values do not change over time.
Key Points:
- Simple Values: Available once at the script’s start and remain constant.
- Keyword Usage:
simple
is used in variable and parameter declarations to ensure consistency. - Compatibility: “Simple” values can also accept “input” or “const” qualified values, making them versatile in scripts.
Practical Example: Highlighting Non-Standard Chart Types
The following Pine Script code demonstrates the use of “simple” values by changing the chart’s background color if a non-standard chart type is detected.
//@version=5 indicator("simple demo", overlay = true) //@variable Is `true` when the current chart is non-standard. Qualified as "simple bool". isNonStandard = not chart.is_standard //@variable Is orange when the the current chart is non-standard. Qualified as "simple color". simple color warningColor = isNonStandard ? color.new(color.orange, 70) : na // Colors the chart's background to warn that it's a non-standard chart type. bgcolor(warningColor, title = "Non-standard chart color")
Code Walkthrough:
- Indicator Declaration: Begins with defining the script as an indicator with overlay capabilities.
- Non-Standard Chart Detection: A simple boolean
isChartNonStandard
is declared to check if the current chart type is standard or not. - Warning Color: The
warningColor
variable, qualified as a “simple color,” changes based on theisChartNonStandard
boolean. It becomes semi-transparent orange for non-standard charts, otherwise not applicable (na
). - Background Coloring: The
bgcolor()
function useswarningColor
to color the chart background, highlighting non-standard charts.
Key Features:
- Function Useability: The example shows how to use simple values in practical scenarios, enhancing script reliability by preventing unintended value changes.
- Syntax and Application: Demonstrates the syntax for declaring “simple” variables and their application in controlling script behavior based on chart conditions.
Takeaways:
- “Simple” values in Pine Script ensure variable consistency throughout script execution.
- The
simple
keyword is essential for parameters and variables that should not change after the script starts. - This example highlights the utility of “simple” values in creating dynamic, condition-based visual indicators in financial chart analysis.