A fundamental aspect of writing scripts in Pine Script is the indicator()
function. This function is crucial as it designates your script as an indicator and sets various properties related to it. Let’s dive deep into the syntax, arguments, and practical usage of the indicator()
function in Pine Script.
Syntax of indicator()
The general syntax of the indicator()
function is as follows:
indicator(title, shorttitle, overlay, format, precision, scale, max_bars_back, timeframe, timeframe_gaps, explicit_plot_zorder, max_lines_count, max_labels_count, max_boxes_count, max_polylines_count) → void
Breaking Down the Arguments
- title (const string): This is the main title of your script. It appears on the chart and becomes the default title when publishing the script.
- shorttitle (const string): Acts as the display name on charts. If specified, it replaces the
title
in chart-related windows. It’s optional and defaults to thetitle
. - overlay (const bool): Determines the placement of the indicator. If
true
, the indicator overlays the chart; iffalse
, it appears in a separate pane. Default isfalse
. - format (const string): Defines the formatting of displayed values. Options include
format.inherit
,format.price
,format.volume
, andformat.percent
. It’s optional, withformat.inherit
as default. - precision (const int): Specifies the number of decimal places for values. It’s a non-negative integer up to 16. The default is inherited from the chart’s symbol precision.
- scale (const scale_type): Sets the price scale. Options are
scale.right
,scale.left
, andscale.none
.scale.none
is valid only withoverlay = true
. It defaults to the chart’s scale. - max_bars_back (const int): Defines the historical buffer length for variables and functions. Default is 0, but Pine Script® automatically detects the necessary buffer size.
- timeframe (const string): Adds multi-timeframe capabilities. An empty string or
timeframe.period
represents the chart’s timeframe. It’s optional, defaulting totimeframe.period
. - timeframe_gaps (const bool): Manages indicator values on higher timeframe bars.
true
causes gaps withna
values, whilefalse
fills gaps with the latest known value. Default istrue
. - explicit_plot_zorder (const bool): Controls the rendering order of plots, fills, and hlines. When
true
, newer plots overlay older ones. Default isfalse
. - max_lines_count (const int): Sets the maximum displayed line drawings, with a range of 1-500. This count is approximate. Default is 50.
- max_labels_count (const int): Similar to
max_lines_count
, it sets the maximum label drawings displayed. Default is 50. - max_boxes_count (const int): Determines the maximum box drawings displayed, ranging from 1-500. Default is 50.
- max_polylines_count (const int): Sets the maximum polyline drawings, ranging from 1-100. Default is 50.
Example Usage
Here’s a simple example to illustrate the use of indicator()
:
//@version=5 indicator("My script", shorttitle="Script") plot(close)
In this example, the script is set as an indicator named “My script” with a short title “Script”. It plots the closing prices of the trading instrument.
Key Features and Takeaways
- Functionality: The
indicator()
function is essential for defining the basic properties of your Pine Script. - Customization: It offers extensive customization options for how your script interacts with the chart.
- Flexibility: You have control over aspects like overlay, scale, precision, and multi-timeframe functionality.
- Enhanced Visuals: Through parameters like
max_lines_count
,max_labels_count
, etc., you can manage the visual elements of your script effectively.
Conclusion
The indicator()
function is a cornerstone in Pine Script programming, offering vast customization to tailor your scripts precisely to your trading needs. Understanding and effectively using this function is key to developing robust and efficient indicators in Pine Script.