Home » Pinescript Syntax » Introduction to Declaration Statements in Pine Script

Introduction to Declaration Statements in Pine Script

Photo of author
Published on

In this tutorial, we’ll delve into the three types of declaration statements in Pine Script version 5: indicator(), strategy(), and library(). We’ll explore their significance, settings, and how they influence the behavior and execution of scripts.

Declaration Statements: The Starting Point

1. Indicator Function

The indicator() function is employed to declare a script as an indicator. Indicators are tools used to analyze market trends and patterns, helping traders make informed decisions. When you use the indicator() function, you specify key properties of your script, such as its name, the precision of values, and its appearance on the chart. Indicators must produce visual output on the chart, achieved through functions like plot(), plotshape(), or barcolor().


indicator(title="Custom Indicator", shorttitle="CI", overlay=true, precision=2)

2. Strategy Function

The strategy() function declares a script as a strategy. Strategies are used for backtesting trading ideas by simulating how they would have performed based on historical data. This declaration allows you to set parameters relevant to backtesting, such as initial capital, commission, and slippage. Strategies require at least one strategy.*() call, such as strategy.entry(), to define trade entry and exit points.


strategy(title="Trading Strategy", initial_capital=10000, commission_type=strategy.commission.percent, commission_value=0.1)

3. Library Function

The library() function is used to create a script that acts as a library. Libraries in Pine Script are collections of functions or user-defined types that can be reused across different scripts. This declaration is essential for scripts intended to provide shared functionality without directly producing output on a chart or executing trades. Libraries must export at least one function or type to be usable by other scripts.


library(title="Utility Functions")

Understanding the Impact of Declaration Statements

  • Script Type Identification: The declaration statement explicitly identifies the script’s type, influencing its allowed content and execution behavior.
  • Property Settings: Through declaration statements, you can configure various properties of your script, from display settings like name and precision to strategy-specific parameters such as initial capital and commission.
  • Runtime Behavior: Certain properties set in the declaration statement, like the maximum number of drawing objects for indicators, affect the script’s runtime behavior on the chart.

Key Takeaways

  • Declaration statements in Pine Script version 5 are fundamental, determining the script’s type, functionality, and execution behavior.
  • Three types of declaration statements exist: indicator(), strategy(), and library(), each catering to different scripting needs in TradingView.
  • Indicators focus on chart visualization, strategies on backtesting trading ideas, and libraries on providing reusable code blocks.
  • Properly using declaration statements is crucial for script development in Pine Script, ensuring that your scripts function as intended and adhere to best practices.

This tutorial aims to provide a clear understanding of declaration statements in Pine Script version 5, highlighting their importance in developing effective trading tools and strategies. Whether you’re creating indicators, strategies, or libraries, starting with the correct declaration statement sets the foundation for a successful script.

Leave a Comment