Home » Compiler Directives » Library Function in Pine Script

Library Function in Pine Script

Photo of author
Published on

Programming languages are often equipped with several pre-defined sets of code, which perform specific tasks and can be reused across multiple programs. Pine Script, a domain-specific programming language created by TradingView, isn’t an exception to this rule. It allows us to create libraries using the library function, further simplifying the scripting process. In this article, we’ll delve deep into the library function, its syntax, and a unique use case.

What is the library Function?

Before we jump into the syntax and use case, let’s understand the library function. In Pine Script, library is a declaration statement that identifies a script as a library. In other words, it is a custom-made set of reusable functions that you can use in various scripts.

Syntax of library Function

The library function has a simple syntax:

library(title, overlay) → void


The function accepts two arguments:

  1. title (const string): This is the title of the library and its identifier. It cannot contain spaces, special characters, or begin with a digit. This title serves as the default publication title and is used to uniquely identify the library in the import statement, and as the script’s name on the chart.
  2. overlay (const bool): This boolean argument determines whether the library will be added over the chart (if true), or added in a separate pane (if false). This argument is optional, and defaults to false.

Example of library Function

Let’s take a look at an example to further understand how the library function works in Pine Script. Here we are defining a library named “num_methods” with an exported sinh function.

// @description Math library
library("num_methods", overlay = true)

// Calculate "sinh()" from the float parameter `x`
export sinh(float x) =>
    (math.exp(x) - math.exp(-x)) / 2.0

In this script:

  1. The library function is first declared with the title “num_methods” and overlay set to true. This means that the library will be added over the chart.
  2. The export keyword is used to make the sinh function available outside the library. This function takes a float parameter x and calculates the hyperbolic sine (sinh) of x using the mathematical exponential function math.exp().
  3. The plot() function is then used to draw the value of sinh(0) on the chart.

Key Takeaway

The library function in Pine Script is a powerful feature that enables users to create reusable sets of functions. This reduces code duplication, increases modularity, and makes maintenance easier. Understanding how to create, export, and use functions in a library is a critical skill for anyone interested in creating complex scripts in Pine Script.


Library functions are an essential part of Pine Script, making it more efficient and powerful. With the ability to create your own libraries and use pre-existing ones, you can create more complex strategies and indicators with less code. Always remember that a well-organized script with the proper use of library functions not only simplifies the coding process but also improves the readability and maintainability of your code. Happy scripting!

Leave a Comment